-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameScene.js
285 lines (239 loc) · 8.2 KB
/
GameScene.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import * as GC from "./Constants.js";
import LEVEL from "./Level.js";
class GameScene extends Phaser.Scene {
constructor() {
super({ key: 'GameScene' });
this.markus = null;
this.obstacles = [];
this.mood = 0;
this.infected = 1000;
this.vaccinated = 0;
this.masked = 0;
this.opened = 0;
this.hidden = 0;
this.background1 = null;
this.background1Swap = null;
this.background2 = null;
this.background2Swap = null;
this.person = null;
this.lastPosition = 0;
this.runPosition = 0;
this.levelIterator = 0;
this.levelIterationX = 0;
this.vaccinationText = null;
this.openText = null;
}
preload() {
this.load.spritesheet('markus-w', 'img/markus-w-suite.png', {
frameWidth: 96,
frameHeight: 128
});
this.load.image('backgroundGame', 'img/background.png');
this.load.image('background-wald1', 'img/bregenzerwald-background.png');
this.load.image('background-wald2', 'img/bregenzerwald-trees-background.png');
this.load.image('background-bregenz', 'img/bregenz-festspielhaus-background.png');
this.load.image('dgs', 'img/DGS.png');
this.load.image('floor', 'img/floor.png');
this.load.spritesheet('gang', 'img/gang.png', {
frameWidth: 56,
frameHeight: 128
});
}
create() {
this.background1 = this.add.image(0, 0, 'backgroundGame');
this.background2 = this.add.image(GC.WIDTH, 0, 'backgroundGame');
this.background1.setOrigin(0);
this.background2.setOrigin(0);
this.physics.add.existing(this.background1);
this.background1.body.setVelocityX(GC.BACKGROUND_SCROLL_SPEED);
this.physics.add.existing(this.background2);
this.background2.body.setVelocityX(GC.BACKGROUND_SCROLL_SPEED);
this.add.image(GC.WIDTH/2, GC.HEIGHT - 32 / 2, 'floor');
this.markus = this.add.sprite(128, GC.HEIGHT - 34 - 128 / 2, 'markus-w', 0);
this.markus.depth = 100;
this.anims.create({
key: 'walk',
frames: this.anims.generateFrameNumbers('markus-w', { frames: [ 0, 1, 2, 3, 4, 5, 6, 7 ] }),
frameRate: 8,
repeat: -1
});
this.markus.play('walk');
/*
this.input.once('pointerdown', function () {
console.log('switching to EndScene');
this.scene.start('EndScene');
}, this);
*/
//this.fillObjects();
// key to open doors
this.one = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ONE);
// syringe to vaccinate people
this.two = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.TWO);
// masks to give it to people
this.three = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.THREE);
// disguise with sunglasses
this.four = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.FOUR);
this.vaccinationText = this.add.text(4, GC.HEIGHT/2, GC.VACCINATION_PREFIX + this.vaccinated);
this.openText = this.add.text(200, GC.HEIGHT/2, GC.HOUSE_PREFIX + this.opened);
// TODO: remove this text
this.add.text(400, GC.HEIGHT/2, 'press 1: open house, 2: vaccinate human');
this.fillObjects();
}
update() {
this.runPosition += this.lastPosition - this.background1.x;
if(this.background1.x < -GC.WIDTH) {
this.background1.x += 2 * GC.WIDTH;
if(this.background1Swap) {
let obgX = this.background1.x;
this.background1.destroy();
this.background1 = this.add.image(obgX, 0, 'background-' + this.background1Swap);
this.background1.setOrigin(0);
this.physics.add.existing(this.background1);
this.background1.body.setVelocityX(GC.BACKGROUND_SCROLL_SPEED);
this.background1.depth = 0;
this.background1Swap = null;
}
}
if(this.background2.x < -GC.WIDTH) {
this.background2.x += 2 * GC.WIDTH;
if(this.background2Swap) {
let obgX = this.background2.x;
this.background2.destroy();
this.background2 = this.add.image(obgX, 0, 'background-' + this.background2Swap);
this.background2.setOrigin(0);
this.physics.add.existing(this.background2);
this.background2.body.setVelocityX(GC.BACKGROUND_SCROLL_SPEED);
this.background1.depth = 0;
this.background2Swap = null;
}
}
this.lastPosition = this.background1.x;
this.fillObjects();
this.checkHitAndCount();
}
checkHitAndCount() {
// find objects that are within the HIT_RANGE of markus
//console.log(this.markus.x, this.obstacles.map(obj => obj.x));
let hitObjs = this.obstacles.filter(
obj => obj.x <= this.markus.x + GC.HIT_REGION && obj.x >= this.markus.x - GC.HIT_REGION && obj.data.get('reacted') === false
);
// find objects that are out of the game to remove them from the obstacles array
let notReactedObjs = this.obstacles.filter(
obj => obj.x <= -GC.WIDTH/2
);
// TODO: remove not reacted objs from this.obstacles
// open doors
if (this.one.isDown) {
// console.log('OPEN doors');
let houses = hitObjs.filter( obj => obj.data.get('type') === GC.TYPE_HOUSE);
// do open doors
houses.forEach( house => {
// store on the house that it has been opened - we use the same attribute
// for all game objects so that we can identify those that have not been handled
house.data.set('reacted', true);
} );
// score the opening
this.opened += houses.length;
this.openText.setText(GC.HOUSE_PREFIX + this.opened);
}
// vaccinate people
if (this.two.isDown) {
// console.log('VACCINATE people');
let humans = hitObjs.filter( obj => obj.data.get('type') === GC.TYPE_PERSON);
// console.log(this.markus.x - GC.HIT_REGION, this.obstacles[0].x, this.markus.x + GC.HIT_REGION);
// console.log(hitObjs.length);
// console.log(humans.length);
// do vaccinate humans
humans.forEach( human => {
// store on the human that they have been vaccinated
human.data.set('reacted', true);
} );
// score the vaccination
this.vaccinated += humans.length;
this.vaccinationText.setText(GC.VACCINATION_PREFIX + this.vaccinated);
}
// mask people
if (this.three.isDown) {
// console.log('MASK people');
let humans = hitObjs.filter( obj => obj.data.get('type') === GC.TYPE_PERSON);
// do mask humans
humans.forEach( human => {
// store on the human that they have been masked
human.data.set('reacted', true);
} );
// score the masking
this.masked += humans.length;
}
// disguise with sunglasses
if (this.four.isDown) {
// console.log('DISGUISE');
let demonstrations = hitObjs.filter( obj => obj.data.get('type') === GC.TYPE_DEMONSTRATION);
// do mask humans
demonstrations.forEach( human => {
// store on the demonstrations that markus has disguised
demonstrations.data.set('reacted', true);
} );
// score the disguise
this.hidden += demonstrations.length;
}
}
fillObjects() {
while(LEVEL[this.levelIterator]
&& LEVEL[this.levelIterator].deltaX + this.levelIterationX
<= this.runPosition + GC.WIDTH
) {
switch(LEVEL[this.levelIterator].type) {
case 'person':
this.createPerson(LEVEL[this.levelIterator]);
break;
case 'house':
this.createHouse(LEVEL[this.levelIterator]);
break;
case 'background':
this.swapBackground(LEVEL[this.levelIterator]);
break;
case 'end':
this.end();
break;
}
this.levelIterationX += LEVEL[this.levelIterator].deltaX;
this.levelIterator++;
}
}
end() {
this.scene.start('EndScene');
}
createPerson(obj) {
let person = this.add.sprite(
obj.deltaX + this.levelIterationX - this.runPosition,
GC.HEIGHT - GC.FLOOR_HEIGHT,
'gang', 3
);
person.setOrigin(0, 1);
this.physics.add.existing(person);
person.body.setVelocityX(GC.PERSON_SCROLL_SPEED);
person.setDataEnabled();
person.data.set('type', GC.TYPE_PERSON);
person.data.set('reacted', false);
this.obstacles.push(person);
}
createHouse(obj) {
let house = this.add.image(
obj.deltaX + this.levelIterationX - this.runPosition,
GC.HEIGHT - GC.FLOOR_HEIGHT,
'dgs'
);
house.setOrigin(0, 1);
this.physics.add.existing(house);
house.body.setVelocityX(GC.HOUSE_SCROLL_SPEED);
house.setDataEnabled();
house.data.set('type', GC.TYPE_HOUSE);
house.data.set('reacted', false);
this.obstacles.push(house);
}
swapBackground(obj) {
if(obj.key1) this.background1Swap = obj.key1;
if(obj.key2) this.background2Swap = obj.key2;
}
}
export default GameScene;