-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestRigidBodyAcceleration.html
More file actions
107 lines (88 loc) · 3.84 KB
/
testRigidBodyAcceleration.html
File metadata and controls
107 lines (88 loc) · 3.84 KB
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
<!-- Please carefully review the rules about academic integrity found in the academicIntegrity.md file found at the root of this project. -->
<!doctype html>
<html>
<head>
<title>Test Rigid Body Acceleration</title>
<style>
/* Engine-Specific */
* {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="canv"></canvas>
<script src="./engine/Engine.js"></script>
<script src="./engine/Scene.js"></script>
<script src="./engine/GameObject.js"></script>
<script src="./engine/Component.js"></script>
<script src="./engine/Input.js"></script>
<script src="./engine/Vector2.js"></script>
<script src="./engine/Time.js"></script>
<script src="./engine/Collisions.js"></script>
<script src="./engine/components/Transform.js"></script>
<script src="./engine/components/TextLabel.js"></script>
<script src="./engine/components/Polygon.js"></script>
<script src="./engine/components/Collider.js"></script>
<script src="./engine/components/RigidBody.js"></script>
<script>
class MainScene extends Scene {
constructor() {
super()
this.instantiate(new GameObject("Gravity Game Object"), new Vector2(100, 100)).addComponent(new TextLabel(), { text: "Constant Gravity" })
this.instantiate(new GameObject("TerminalVelocity Game Object"), new Vector2(300, 100)).addComponent(new TextLabel(), { text: "Terminal Velocity" })
this.instantiate(new GameObject("Impulse Game Object"), new Vector2(500, 100)).addComponent(new TextLabel(), { text: "Impulse Acceleration" })
this.instantiate(new GameObject("Nothing Game Object"), new Vector2(700, 100)).addComponent(new TextLabel(), { text: "No Velocity" })
this.instantiate(new GameObject("Push Space Game Object"), new Vector2(300, 50)).addComponent(new TextLabel(), { text: "Push Space to give all objects an upward impulse acceleration"})
this.instantiate(new MovingGameObject(), new Vector2(150, 200)).addComponent(new Controller1())
this.instantiate(new MovingGameObject(), new Vector2(350, 200)).addComponent(new Controller2())
this.instantiate(new MovingGameObject(), new Vector2(550, 200)).addComponent(new Controller3())
this.instantiate(new MovingGameObject(), new Vector2(750, 200)).addComponent(new Controller4())
}
}
class MovingGameObject extends GameObject {
constructor() {
super("Moving Game Object")
this.addComponent(new Polygon(), { points: [new Vector2(-50, -50), new Vector2(-50, 50), new Vector2(50, 50), new Vector2(50, -50)] })
this.addComponent(new RigidBody())
}
}
class Controller1 extends Component {
start() {
this.gameObject.getComponent(RigidBody).gravity = new Vector2(0, 32)
}
}
class Controller2 extends Component {
start() {
this.gameObject.getComponent(RigidBody).gravity = new Vector2(0, 32)
}
update(){
const rigidBody = this.gameObject.getComponent(RigidBody)
//Enforce a terminal velocity
if(rigidBody.velocity.y > 10)
rigidBody.velocity.y = 10
}
}
class Controller3 extends Component {
start() {
this.gameObject.getComponent(RigidBody).acceleration = new Vector2(0, 1000 )
}
}
class Controller4 extends Component {
start() {
}
update(){
if(Input.keysUpThisFrame.includes("Space")){
const movingGameObjects = Engine.currentScene.gameObjects.filter(go=>go.getComponent(RigidBody))
for(const movingGameObject of movingGameObjects){
movingGameObject.getComponent(RigidBody).acceleration = new Vector2(0, -2000)
}
}
}
}
Engine.currentScene = new MainScene()
Engine.start()
</script>
</body>
</html>