-
Notifications
You must be signed in to change notification settings - Fork 3
/
Scene.js
133 lines (119 loc) · 3.92 KB
/
Scene.js
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
class Scene {
constructor() {
this.objects = {
borders: [],
discs: [],
};
this.metaObjects = {
boxes: [],
goals: [],
balls: [],
centers: [],
players: [],
};
this.collisions = [];
}
addObject(obj) {
if (obj instanceof Disc) {
this.objects.discs.push(obj);
}
if (obj instanceof Player) {
this.objects.discs.push(obj.kicker);
this.metaObjects.players.push(obj);
} else if (obj instanceof Ball) {
this.metaObjects.balls.push(obj);
} else if (obj instanceof Border) {
this.objects.borders.push(obj);
} else if (obj instanceof Box) {
this.metaObjects.boxes.push(obj);
for (let borderKey in obj.borders) {
this.objects.borders.push(obj.borders[borderKey]);
}
} else if (obj instanceof Goal) {
this.metaObjects.goals.push(obj);
this.objects.borders.push(obj.goalLine);
this.objects.borders.push(obj.netTop);
this.objects.borders.push(obj.netBottom);
this.objects.borders.push(obj.netBack);
this.objects.discs.push(obj.topPost);
this.objects.discs.push(obj.bottomPost);
if (this.metaObjects.goals.length == 2) {
let goal1Center = this.metaObjects.goals[0].center;
let goal2Center = this.metaObjects.goals[1].center;
this.metaObjects.centers.push(Vector.add(goal1Center, goal2Center).div(2));
}
}
obj.scene = this;
}
getCollisions() {
// Disc-Border collisions
for (let disc of this.objects.discs) {
for (let border of this.objects.borders) {
let cls = Collision.getCollision(disc, border);
if (cls) {
this.collisions.push(cls);
}
}
}
//Disc-Disc collisions
for (let i = 0; i < this.objects.discs.length; i++) {
for (let j = i + 1; j < this.objects.discs.length; j++) {
let disc1 = this.objects.discs[i];
let disc2 = this.objects.discs[j];
let cls = Collision.getCollision(disc1, disc2);
if (cls) {
this.collisions.push(cls);
}
}
}
}
resolveCollisions() {
while (this.collisions.length > 0) {
let collision = this.collisions.pop();
collision.resolve();
}
}
checkGoals() {
for (let goal of this.metaObjects.goals) {
for (let ball of this.objects.discs) {
if (goal.checkGoal(ball)) {
return true;
}
}
}
return false;
}
update() {
for (let objectKey in this.objects) {
let objectList = this.objects[objectKey];
for (let object of objectList) {
object.update();
}
}
//Iterate 20 times for collisions
for (let i = 0; i < 20; i++) {
this.getCollisions();
this.resolveCollisions();
}
}
draw() {
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, cWidth, cHeight);
for (let objectKey in this.objects) {
let objectList = this.objects[objectKey];
for (let object of objectList) {
object.draw();
}
}
}
reset() {
this.redStartX = 100;
this.blueStartX = 700;
this.metaObjects.players[0].resetPosition();
this.metaObjects.players[0].velocity.mult(0);
this.metaObjects.players[1].resetPosition();
this.metaObjects.players[1].velocity.mult(0);
this.metaObjects.balls[0].resetPosition();
this.metaObjects.balls[0].velocity.mult(0);
}
}