-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnemyProjectile.js
119 lines (96 loc) · 3.01 KB
/
EnemyProjectile.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
// ARTG/CMPM 120 Final Project
// Tomb of the Ancients
// EnemyProjectile.js
// Enemy projectile prefab
// enemy projectile class
function EnemyProjectile(posX, posY, playerX, playerY, type){
// type of enemy
this.type = type;
// checks enemy type and sets speed, damage, size, etc.
if (type == "scorpion"){
this.speed = 200;
this.damage = 1;
Phaser.Sprite.call(this, game, posX, posY, 'enemyproj', 'sprite1');
this.anchor.set(0.5);
var scale = 1.5;
this.scale.x = scale;
this.scale.y = scale;
game.physics.enable(this);
this.body.collideWorldBounds = false; // projectile persists offscreen.
var angle = game.math.angleBetween(posX, posY, playerX, playerY); // angle between the player and the scorpion
this.rotation = angle;
this.animations.add('anim', Phaser.Animation.generateFrameNames('sprite', 1, 2), 8, true);
this.animations.play('anim');
}
if (type == "snake"){
this.speed = 250;
this.damage = 1;
Phaser.Sprite.call(this, game, posX, posY, 'enemyproj', 'sprite1');
this.anchor.set(0.5);
var scale = 1;
this.scale.x = scale;
this.scale.y = scale;
game.physics.enable(this);
this.body.collideWorldBounds = false;
var angle = game.math.angleBetween(posX, posY, playerX, playerY);
this.rotation = angle;
this.animations.add('anim', Phaser.Animation.generateFrameNames('sprite', 1, 2), 8, true);
this.animations.play('anim');
}
game.add.existing(this);
}
EnemyProjectile.prototype = Object.create(Phaser.Sprite.prototype);
EnemyProjectile.prototype.constructor = EnemyProjectile;
EnemyProjectile.prototype.update = function() {
var bulletHitWall = game.physics.arcade.collide(this, walls);
var bulletHitCurrentWall = game.physics.arcade.collide(this, currentwalls);
// delete the bullet if it hits a wall
if (bulletHitWall == true || bulletHitCurrentWall == true){
this.kill();
this.destroy();
}
var slash = playerslash;
// destroys a projectile if it comes into contact with a slash.
if (slash != null){
for (var k = 0; k < slash.hitboxes.length; k++){
var box = slash.hitboxes[k];
var bulletHitSlash = game.physics.arcade.collide(this, box);
if (bulletHitSlash == true){
this.kill();
this.destroy();
}
}
}
var bulletHitPlayer = game.physics.arcade.collide(this, player);
// delete the bullet if it hits an enemy and damage the enemy
if (bulletHitPlayer == true){
this.kill();
this.destroy();
// player is damaged
if (bulletHitPlayer == true && iframes <= 0){
if (PLAYER_PROPERTIES.WEAPON_1 == "Bone Dagger" || PLAYER_PROPERTIES.WEAPON_2 == "Bone Dagger"){
PLAYER_PROPERTIES.HEALTH -= 2*(this.damage);
} else {
PLAYER_PROPERTIES.HEALTH -= this.damage;
}
iframes = IFRAMES_MAX;
}
// plays a random sound effect each time
var rand = game.rnd.integerInRange(1, 5);
if (rand == 1){
gruntfx1.play();
}
if (rand == 2){
gruntfx2.play();
}
if (rand == 3){
gruntfx3.play();
}
if (rand == 4){
gruntfx4.play();
}
if (rand == 5){
gruntfx5.play();
}
}
}