-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
306 lines (265 loc) · 10.3 KB
/
script.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* The game's global configuration.
*/
var Smc = {
// The Phaser event handlers. Keys are event names, and values are arrays of functions that take no arguments.
phaserEventHandlers: {
preload: [],
create: [],
update: []
},
playerTypes: {}
};
var game = new Phaser.Game(640,480, Phaser.AUTO, 'world', {
preload: buildPhaserEventHandler("preload"),
create: buildPhaserEventHandler("create"),
update: buildPhaserEventHandler("update")
});
/**
* A game player.
*
* @param {string} name
* @param {Phaser.Sprite} phaserObject
* @constructor
*/
Smc.playerTypes.Player = function (name, phaserObject) {
this._defense = 1;
this._name = name;
this._isMovingVertically = false;
this._phaserObject = phaserObject;
game.physics.arcade.enable(this._phaserObject);
this._phaserObject.animations.add('front', ['front1', 'front2', 'front3', 'front4', 'front5', 'front6', 'front7', 'front8', 'front9']);
this._phaserObject.animations.add('left', ['left1', 'left2', 'left3', 'left4', 'left5', 'left6', 'left7', 'left8', 'left9']);
this._phaserObject.animations.add('right', ['right1', 'right2','right3', 'right4','right5', 'right6','right7', 'right8', 'right9']);
this._phaserObject.body.gravity.set(0, 180);
this._phaserObject.body.collideWorldBounds = true;
this._phaserObject.anchor.setTo(0.5, 0.5);
this._weaponMountPhaserObject = game.add.sprite( 600,480, 'pixel');
game.physics.arcade.enable(this._weaponMountPhaserObject);
this._weaponMountPhaserObject.body.enable = true;
this._weaponMountPhaserObject.body.allowRotation = true;
// Creates 30 bullets, using the 'bullet' graphic
this._weaponPhaserObject = game.add.weapon(30, 'bullet');
game.physics.arcade.enable(this._weaponPhaserObject);
// The bullet will be automatically killed when it leaves the world bounds
this._weaponPhaserObject.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
// The speed at which the bullet is fired
this._weaponPhaserObject.bulletSpeed = 600;
// Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms
this._weaponPhaserObject.fireRate = 100;
// Tell the Weapon to track the player
// With no offsets from the position
// But the 'true' argument tells the weapon to track sprite rotation
this._weaponPhaserObject.trackSprite(this._weaponMountPhaserObject, 0, 0, true);
this._id = 1;
this._phaserObject.maxHealth = 100;
this._phaserObject.health= 100;
this._phaserObject.hud = Phaser.Plugin.HUDManager.create(this._phaserObject.game, this._phaserObject, 'smc.player.hud.' + this._id);
this._healthHud =this._phaserObject.hud.addBar(0, -20, 32, 2, this._phaserObject.maxHealth, 'health', this._phaserObject, Phaser.Plugin.HUDManager.HEALTHBAR, false);
this._healthHud.bar.anchor.setTo(0.5, 0.5);
this._phaserObject.addChild(this._healthHud.bar);
game.physics.arcade.enable(this._phaserObject)
// Set up the trail blaze.
this._trailBlazeEmitter = game.add.emitter(game.world.centerX, game.world.centerY, 400);
this._trailBlazeEmitter.makeParticles( [ 'fire1', 'fire2', 'fire3', 'smoke' ] );
this._trailBlazeEmitter.gravity = 800;
this._trailBlazeEmitter.setAlpha(1, 0, 3000);
// Ensure the character itself is never hidden by the other sprites that belong to it.
this._phaserObject.bringToTop();
}
/**
* Hits the player.
*/
Smc.playerTypes.Player.prototype.hit = function() {
this._phaserObject.health = this._phaserObject.health - (10 / this._defense);
if (this._phaserObject.health<=0){
this._kill();
}
}
/**
* Kills the player.
*/
Smc.playerTypes.Player.prototype._kill = function() {
this._phaserObject.health = 0;
this._phaserObject.kill();
}
/**
* Moves the player to the left.
*/
Smc.playerTypes.Player.prototype.moveLeft = function() {
if (this._isMovingVertically) {
this._phaserObject.x = this._phaserObject.x - 10;
} else {
this._phaserObject.x = this._phaserObject.x - 5;
}
this._phaserObject.animations.play('left', 2, true);
this._weaponMountPhaserObject.angle = 180;
this._onMove();
}
/**
* Moves the player to the right.
*/
Smc.playerTypes.Player.prototype.moveRight = function() {
if (this._isMovingVertically) {
this._phaserObject.x = this._phaserObject.x + 10;
} else {
this._phaserObject.x = this._phaserObject.x + 5;
}
this._phaserObject.animations.play('right', 2, true);
this._weaponMountPhaserObject.x = x;
this._weaponMountPhaserObject.angle = 0;
this._onMove();
}
/**
* Moves the player upwards.
*/
Smc.playerTypes.Player.prototype.moveUp = function() {
this._isMovingVertically = true;
this._phaserObject.y = this._phaserObject.y - 10;
this._phaserObject.animations.play('front', 2, true);
this._weaponMountPhaserObject.angle = 270;
this._onMove();
this._isMovingVertically = false;
}
/**
* Moves the player downwards.
*/
Smc.playerTypes.Player.prototype.moveDown = function() {
this._isMovingVertically = true;
this._phaserObject.y = this._phaserObject.y + 10;
this._phaserObject.animations.play('front', 2, true);
this._weaponMountPhaserObject.angle = 90;
this._onMove();
this._isMovingVertically = false;
}
/**
* Responds to player movement.
*/
Smc.playerTypes.Player.prototype._onMove = function() {
// Show the trail blaze.
var trailBlazeVelocityX = this._phaserObject.body.velocity.x * -1;
var trailBlazeVelocityY = this._phaserObject.body.velocity.y * -1;
this._trailBlazeEmitter.minParticleSpeed.set(trailBlazeVelocityX, trailBlazeVelocityY);
this._trailBlazeEmitter.maxParticleSpeed.set(trailBlazeVelocityX, trailBlazeVelocityY);
this._trailBlazeEmitter.emitX = this._phaserObject.x;
this._trailBlazeEmitter.emitY = this._phaserObject.y;
this._trailBlazeEmitter.setScale(0.1,0, 0.1,0, 3000);
this._trailBlazeEmitter.start(true, 100, null, 5);
}
/**
* Fires the player's weapon.
*/
Smc.playerTypes.Player.prototype.fireWeapon = function() {
// It appears to be impossible to reposition the weapon as soon as the character is moved through game forces such
// as gravity, so do we do it here, where it actually matters.
this._weaponMountPhaserObject.y = this._phaserObject.y;
this._weaponMountPhaserObject.x = this._phaserObject.x;
this._weaponPhaserObject.fire();
}
Smc.playerTypes.Student = (function() {
return function() {
Smc.playerTypes.Player.call(this, "student", game.add.sprite(600,480, 'student'));
};
})();
Smc.playerTypes.Student.prototype = Smc.playerTypes.Player.prototype;
Smc.playerTypes.Mexican = (function() {
return function() {
Smc.playerTypes.Player.call(this, "mexican", game.add.sprite( mexicanX, mexicanY, 'mexican'));
this._phaserObject.body.immovable = true;
};
})();
Smc.playerTypes.Mexican.prototype = Smc.playerTypes.Player.prototype;
/**
* Builds a Phaser event handler for a specific event.
*
* @param {string} eventName
* The name of the event to create the handler for. Must exist as a key in Smc.eventHandlers.
*
* @returns {Function}
* The event handler, which takes no arguments.
*/
function buildPhaserEventHandler(eventName) {
// Phaser callbacks are functions that take no arguments. We create them dynamically using the event name that was
// passed on to this builder function.
return function() {
Smc.phaserEventHandlers[eventName].forEach(function (handler) {
handler();
});
}
}
var cursors;
var mexicanX = 200;
var mexicanY= 100;
var boxX = 200;
var boxY = 250;
var liftX = 400;
var liftY = 250;
var lift ;
var mexican;
var armX = 46;
var armY= 93;
var pumpX = 62;
var pumpY= 168;
var weightX = 0;
var weightY= 345;
var x = game.width/2;
var y = game.height/2;
Smc.phaserEventHandlers.preload.push(function() {
game.load.image('arm', 'assets/arm.png');
game.load.image('pump', 'assets/pump.png');
game.load.image('weight', 'assets/weight.png');
game.load.image('fire1', 'assets/fire1.png');
game.load.image('fire2', 'assets/fire2.png');
game.load.image('fire3', 'assets/fire3.png');
game.load.image('smoke', 'assets/smoke-puff.png');
game.load.image('pixel', 'assets/trans-pixel.png');
game.load.image('bullet', 'assets/bullet.png');
game.load.image('box', 'assets/box.png');
game.load.image('lift', 'assets/lift.png');
game.load.image('background', 'assets/header.jpg');
game.load.atlasJSONHash('student', 'assets/student.png','assets/student.json');
game.load.atlasJSONHash('mexican', 'assets/mexican.png', 'assets/mexican.json');
game.load.image('pixel', 'assets/trans-pixel.png');
game.load.script('HudManager', 'plugins/HUDManager.js');
});
Smc.phaserEventHandlers.create.push(function() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#333';
game.add.tileSprite(-400,-400, 2000, 1600, 'background');
student = new Smc.playerTypes.Student();
mexican = new Smc.playerTypes.Mexican();
box = game.add.sprite( boxX, boxY, 'box');
lift = game.add.sprite( liftX, liftY, 'lift');
pump = game.add.sprite( pumpX, pumpY, 'pump');
arm = game.add.sprite( armX, armY, 'arm');
weight = game.add.sprite( weightX, weightY, 'weight');
game.physics.arcade.enable(box);
game.physics.arcade.enable(lift);
lift.body.collideWorldBounds = true;
box.body.collideWorldBounds = true;
cursors = game.input.keyboard.createCursorKeys();
});
Smc.phaserEventHandlers.update.push(function() {
game.physics.arcade.collide(mexican._phaserObject, box);
game.physics.arcade.collide(student._phaserObject, box);
game.physics.arcade.collide(student._phaserObject, lift);
game.physics.arcade.collide(mexican._phaserObject, student._weaponPhaserObject.bullets, function(mexicanPhaserObject, bulletPhaserObject) {
mexican.hit();
bulletPhaserObject.kill();
}, null, this);
if (cursors.up.isDown) {
student.moveUp();
}
if (cursors.down.isDown) {
student.moveDown();
}
if (cursors.left.isDown) {
student.moveLeft();
}
if (cursors.right.isDown) {
student.moveRight();
}
if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) {
student.fireWeapon();
}
});