forked from JacobDanielsF/FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
333 lines (272 loc) · 10.6 KB
/
Player.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// ARTG/CMPM 120 Final Project
// Tomb of the Ancients
// Player.js
// Player prefab
// player class
function Player(game, posX, posY, sprite, frame){
Phaser.Sprite.call(this, game, posX, posY, sprite, frame);
this.anchor.set(0.5);
var scale = 0.5;
this.scale.x = scale;
this.scale.y = scale;
game.physics.enable(this);
this.body.collideWorldBounds = false;
this.body.setSize(54, 74, 37, 33); // sets the bounding box.
// lock the camera to the player
game.camera.follow(this);
this.animations.add('walkup', Phaser.Animation.generateFrameNames('frameup', 1, 4), 8, true);
this.animations.add('walkright', Phaser.Animation.generateFrameNames('frameright', 1, 6), 12, true);
this.animations.add('walkleft', Phaser.Animation.generateFrameNames('frameleft', 1, 6), 12, true);
this.animations.add('walkdown', Phaser.Animation.generateFrameNames('framedown', 1, 4), 8, true);
this.bulletfire = 0;
this.walkspeed = WALK_SPEED;
this.direction = "down";
this.ornateuse = false;
game.add.existing(this);
}
Player.prototype = Object.create(Phaser.Sprite.prototype);
Player.prototype.constructor = Player;
Player.prototype.update = function() {
// the two icons at the bottom right of the screen.
weaponicon.body.x = player.body.x + 340;
weaponicon.body.y = player.body.y + 240;
weaponicon2.body.x = player.body.x + 360;
weaponicon2.body.y = player.body.y + 200;
// Cross hair follows mouse. Mouse cursor gets hidden elsewhere.
crossHair.x = game.input.mousePointer.x+game.camera.x;
crossHair.y = game.input.mousePointer.y+game.camera.y;
var angle = game.physics.arcade.angleToPointer(player);
this.body.velocity.x = 0;
this.body.velocity.y = 0;
// player movement handling
var velocity = this.walkspeed;
var pressed = false;
// check for WASD inputs
if (game.input.keyboard.isDown(Phaser.Keyboard.A)) {
// Move left
player.body.velocity.x = -velocity;
if (pressed == false){
player.animations.play('walkleft');
player.direction = "left";
}
pressed = true;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.D)) {
// Move right
player.body.velocity.x = velocity;
if (pressed == false){
player.animations.play('walkright');
player.direction = "right";
}
pressed = true;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.W)) {
// Move up
player.body.velocity.y = -velocity;
if (pressed == false){
player.animations.play('walkup');
player.direction = "up";
}
pressed = true;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.S)) {
// Move down
player.body.velocity.y = velocity;
if (pressed == false){
player.animations.play('walkdown');
player.direction = "down";
}
pressed = true;
}
// player idle frames
if (pressed == false) {
if (player.direction == "down"){
player.frameName = 'idledown';
} else if (player.direction == "up"){
player.frameName = 'idleup';
} else if (player.direction == "left"){
player.frameName = 'idleleft';
} else if (player.direction == "right"){
player.frameName = 'idleright';
}
if (stepfx.isPlaying == true){
stepfx.stop();
}
} else {
if (stepfx.isPlaying == false){
stepfx.play();
}
}
// weapon switching
if (weaponswitch > 0) { weaponswitch--; }
if (game.input.keyboard.isDown(Phaser.Keyboard.Q) && weaponswitch < 1) {
// switch icons
if (PLAYER_PROPERTIES.CURRENT_WEAPON == PLAYER_PROPERTIES.WEAPON_1) {
PLAYER_PROPERTIES.CURRENT_WEAPON = PLAYER_PROPERTIES.WEAPON_2;
weaponicon.loadTexture(GetWeaponSprite(PLAYER_PROPERTIES.CURRENT_WEAPON));
weaponicon2.loadTexture(GetWeaponSprite(PLAYER_PROPERTIES.WEAPON_1));
} else {
PLAYER_PROPERTIES.CURRENT_WEAPON = PLAYER_PROPERTIES.WEAPON_1;
weaponicon.loadTexture(GetWeaponSprite(PLAYER_PROPERTIES.CURRENT_WEAPON));
weaponicon2.loadTexture(GetWeaponSprite(PLAYER_PROPERTIES.WEAPON_2));
}
// retrieve weapon properties
SetWeaponSprite();
SetFireRate();
weaponswitch = 10;
}
// weapon animations
var range = 32;
if (slashframe < 0 || PLAYER_PROPERTIES.CURRENT_WEAPON == "Knife Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Scorpion Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Iron Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Ornate Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Bone Dagger"){
// if the weapon is idle, it must follow the player's mouse
if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Bronze Sword" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Stone Sword"){
var offset = -(Math.PI/4);
weapon.body.x = player.body.x + (Math.cos(angle + offset)*range);
weapon.body.y = weaponoffset + player.body.y + (Math.sin(angle + offset)*range);
weapon.rotation = angle + offset + (Math.PI/4);
} else {
weapon.body.x = player.body.x + (Math.cos(angle)*range);
weapon.body.y = weaponoffset + player.body.y + (Math.sin(angle)*range);
weapon.rotation = angle + (Math.PI/4);
}
} else if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Wooden Crossbow" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Short Bow" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Composite Bow" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Revolver Gun" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Energy Staff" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Serpentine Staff") {
// ranged weapon shooting animation
range -= slashframe*50;
weapon.body.x = player.body.x + (Math.cos(angle)*range);
weapon.body.y = weaponoffset + player.body.y + (Math.sin(angle)*range);
weapon.rotation = angle + (Math.PI/4);
slashframe = slashframe - 0.03;
if (slashframe < 0){
weapon.loadTexture(GetWeaponSprite(PLAYER_PROPERTIES.CURRENT_WEAPON));
}
} else if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Bronze Sword" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Stone Sword") {
if (slashframe > 0.2){
angle = angle + (Math.PI/4);
weapon.body.x = player.body.x + (Math.cos(angle)*range);
weapon.body.y = weaponoffset + player.body.y + (Math.sin(angle)*range);
weapon.rotation = angle + (Math.PI/4);
slashframe = slashframe - 0.02;
} else {
// sword slash animation
angle += -1 + (slashframe*10);
weapon.body.x = player.body.x + (Math.cos(angle)*range);
weapon.body.y = weaponoffset + player.body.y + (Math.sin(angle)*range);
weapon.rotation = angle + (Math.PI/4);
slashframe = slashframe - 0.02;
}
}
if (daggerreturn == false && daggercooldown < 0){
daggerreturn = true;
weapon.loadTexture(GetWeaponSprite(PLAYER_PROPERTIES.CURRENT_WEAPON));
weaponshadow.loadTexture(GetWeaponSprite(PLAYER_PROPERTIES.CURRENT_WEAPON));
}
if (daggercooldown >= 0){
daggercooldown--;
}
// flip gun sprite so that it is not upside-down
if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Revolver Gun"){
if(weapon.body.x<player.body.x){
weapon.scale.x=-1;
weaponshadow.scale.x=-1
weapon.rotation = angle + (Math.PI/2) + (Math.PI/4);
} else {
weapon.scale.x=1;
weaponshadow.scale.x=1;
}
} else {
weapon.scale.x=1;
weaponshadow.scale.x=1;
}
function MakePlayerSlash(posX, posY, time, type){ // for melee weapons.
slash = new PlayerSlash(posX, posY + weaponoffset, type);
if (playerslash != null){
for (var k = 0; k < playerslash.hitboxes.length; k++){
var box = playerslash.hitboxes[k];
box.kill();
box.destroy();
}
playerslash.mainslash.kill();
playerslash.mainslash.destroy();
playerslash = null;
}
playerslash = slash;
}
function MakePlayerBullet(posX, posY, time, type, angleoffset){ // for ranged weapons.
bullet = new PlayerProjectile(posX, posY + weaponoffset, type, angleoffset);
playerbulletgroup.add(bullet);
}
// fire weapon on left mouse click
if (game.input.activePointer.leftButton.isDown){
var time = (game.time.now)/1000;
// check if you can fire the weapon (based on fire rate)
if (time > nextFire) {
// check weapon type, play sfx, and makes the correct bullet for it
if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Knife Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Scorpion Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Ornate Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Bone Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Iron Dagger" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Wooden Crossbow" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Short Bow") {
if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Wooden Crossbow") {
weapon.loadTexture('wooden_crossbow_shot');
crossbowfx.play();
} else if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Short Bow") {
weapon.loadTexture('short_bow_shot');
bowfx.play();
} else {
daggerreturn = false;
daggercooldown = 10;
weapon.loadTexture('blank');
weaponshadow.loadTexture('blank');
}
// special property for ornate daggers
if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Ornate Dagger") { ornateuse = true; }
// shoot a single bullet
MakePlayerBullet(player.body.x + 16, player.body.y + 16, time, PLAYER_PROPERTIES.CURRENT_WEAPON, 0);
} else if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Energy Staff" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Serpentine Staff"){
// shoot 3 bullets
for (var i = 0; i < 3; i++){
MakePlayerBullet(player.body.x + 16, player.body.y + 16, time, PLAYER_PROPERTIES.CURRENT_WEAPON, (i - 1)*0.2);
}
magicfx.play();
} else if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Revolver Gun"){
// sets up a timer for semi-automatic shots
this.bulletfire = 3;
} else if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Composite Bow"){
weapon.loadTexture('composite_bow_shot');
bowfx.play();
// shoot 2 arrows
MakePlayerBullet(player.body.x + 16, player.body.y + 16, time, PLAYER_PROPERTIES.CURRENT_WEAPON, -0.1);
MakePlayerBullet(player.body.x + 16, player.body.y + 16, time, PLAYER_PROPERTIES.CURRENT_WEAPON, 0.1);
} else if (PLAYER_PROPERTIES.CURRENT_WEAPON == "Bronze Sword" || PLAYER_PROPERTIES.CURRENT_WEAPON == "Stone Sword") {
// make a slash
MakePlayerSlash(player.body.x + 8, player.body.y + 8, time, PLAYER_PROPERTIES.CURRENT_WEAPON);
whooshfx.play();
}
nextFire = time + PLAYER_PROPERTIES.FIRE_RATE;
isslashing = true;
slashframe = 0.4;
} else {
isslashing = false;
}
}
// for semi-auto bullets
if (this.bulletfire > 0){
if ((this.bulletfire)%1 == 0.75){
gunshotfx.play();
MakePlayerBullet(player.body.x + 16, player.body.y + 16, time, "Revolver Gun", 0);
}
this.bulletfire -= 0.25;
} else {
this.bulletfire = 0;
}
// place shadow behind weapon and rotate it
weaponshadow.body.x = weapon.body.x;
weaponshadow.body.y = weapon.body.y+4;
weaponshadow.rotation = weapon.rotation;
// sprite layering
if (angle < 0){
weaponshadow.bringToTop();
weapon.bringToTop();
player.bringToTop();
} else {
weaponshadow.bringToTop();
player.bringToTop();
weapon.bringToTop();
}
}