-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathenemy-patrols-on-platform.html
141 lines (116 loc) Β· 4.47 KB
/
enemy-patrols-on-platform.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Enemy Patrols on Platform</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.1.1"></script>
<script src="/lib/phaser.min.js?ver=3.52.0"></script>
<script src="/lib/enable3d/enable3d.phaserExtension.0.25.4.min.js"></script>
</head>
<body>
<script>
const { enable3d, Scene3D, Canvas, ExtendedObject3D, THREE } = ENABLE3D
class MainScene extends Scene3D {
constructor() {
super({ key: 'MainScene' })
}
init() {
this.accessThirdDimension()
}
async create() {
const { ground } = await this.third.warpSpeed()
// These assets are from mixamo.com
// The the Idle.fbx contains the skin and the idle animation.
// this.third.physics.debug.enable()
this.third.camera.position.set(10, 10, 20)
this.third.camera.lookAt(0, 0, 0)
this.robot = new ExtendedObject3D()
const pos = { x: 3, y: 2, z: -7 }
this.third.physics.debug.enable()
const sensor = this.third.physics.add.box(
{
x: pos.x - 4,
y: pos.y - 0.5,
z: pos.z - 2,
width: 0.5,
height: 3,
depth: 0.5,
collisionFlags: 1, // set the flag to static
mass: 0.001
},
{ lambert: { color: 0xff00ff, transparent: true, opacity: 0.2 } }
)
sensor.castShadow = sensor.receiveShadow = false
this.third.load.fbx('/assets/fbx/Idle.fbx').then(object => {
// set the flag to ghost
sensor.body.setCollisionFlags(4)
this.robot.add(object)
this.third.animationMixers.add(this.robot.animation.mixer)
this.robot.animation.add('Idle', object.animations[0])
this.robot.animation.play('Idle')
this.robot.traverse(child => {
if (child.isMesh) child.castShadow = child.receiveShadow = true
})
this.robot.scale.set(0.02, 0.02, 0.02)
this.robot.position.set(pos.x, pos.y, pos.z)
this.robot.rotation.set(0, -Math.PI / 2, 0)
this.third.add.existing(this.robot)
this.third.physics.add.existing(this.robot, {
shape: 'box',
ignoreScale: true,
width: 1,
depth: 1,
offset: { y: -0.5 }
})
this.third.physics.add.constraints.lock(this.robot.body, sensor.body)
this.third.physics.add.collider(sensor, ground, event => {
// console.log(event)
if (event === 'end') this.robot.body.setAngularVelocityY(5)
else this.robot.body.setAngularVelocityY(0)
})
// load Walking animations
this.third.load.fbx(`/assets/fbx/Walking.fbx`).then(object => {
console.log('loaded')
this.robot.animation.add('Walking', object.animations[0])
this.robot.animation.play('Walking')
})
})
}
update() {
if (this.robot && this.robot.body) {
const speed = 4
const rotation = this.robot.getWorldDirection(
new THREE.Vector3()?.setFromEuler?.(this.robot.rotation) || this.robot.rotation.toVector3()
)
const theta = Math.atan2(rotation.x, rotation.z)
const x = Math.sin(theta) * speed,
y = this.robot.body.velocity.y,
z = Math.cos(theta) * speed
this.robot.body.setVelocity(x, y, z)
}
}
}
const config = {
type: Phaser.WEBGL,
transparent: true,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: window.innerWidth * Math.max(1, window.devicePixelRatio / 2),
height: window.innerHeight * Math.max(1, window.devicePixelRatio / 2)
},
scene: [MainScene],
...Canvas()
}
window.addEventListener('load', () => {
enable3d(() => new Phaser.Game(config)).withPhysics('/lib/ammo/kripken')
})
</script>
</body>
</html>