-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.js
224 lines (194 loc) · 5.78 KB
/
entity.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
import { toRad, toDeg, Vector, getRandomInt } from "./util.js";
export const DEBUG = false; // Show debug info/bodies
// Monster constants
const TYPE_CHASER = "chaser";
const TYPE_SHOOTER = "shooter";
const TYPE_CHARGER = "charger";
export class Entity {
/**
* Construct a new Entity.
* @param game Reference to the Phaser Game object.
*/
constructor(game) {
this.game = game;
this.sprite = null; // Children will deal with this
this.health = 1;
this.speed = 60;
this.damage = 1;
this.componentList = [];
}
update() {
this.componentList.forEach((comp) => comp.update());
}
render() {
if (DEBUG && this.sprite) {
// Enable body debug
this.game.debug.body(this.sprite);
}
}
// Add a component
addComponent(comp) {
this.componentList.push(comp);
}
}
/**
* Monster that chases the hero.
*/
export class Monster extends Entity {
/**
* Construct a Monster.
* @param game
* @param x
* @param y
* @param scale Entity render scale.
* @param imageName Name of cached image for monster sprite.
* @param hero Reference to Hero object.
*/
constructor(game, x, y, scale, imageName, hero) {
super(game);
this.sprite = this.game.add.sprite(x, y, imageName);
this.sprite.anchor.setTo(.5, .5);
this.sprite.smoothed = false; // Keep sharp even when scaled
this.sprite.scale.setTo(scale, scale);
this.health = 1; // default
this.speed = 45; // default
this.damage = 1; // default
this.hero = hero;
this.bounceBack = false; // Bouncing back?
this.bounceTicks = 0; // Ticks for bounceBack
this.type = "chaser"; // Chases hero?
game.physics.arcade.enable(this.sprite);
}
update() {
super.update();
if (this.isAlive()) {
if (this.type == "chaser") {
this.chaseUpdate();
}
this.bounceUpdate(this.sprite.body);
} else {
this.sprite.kill();
}
}
render() {
super.render();
}
// Update monster to bounce backwards (if bounceBack = true)
bounceUpdate(sprite) {
if (this.bounceBack) {
this.bounceTicks += 1;
// "bounce" in the opposite direction;
sprite.velocity.x = -sprite.velocity.x;
sprite.velocity.y = -sprite.velocity.y;
if (this.bounceTicks >= 20) {
this.bounceBack = false;
}
} else {
this.bounceTicks = 0;
}
}
// Trigger a bounce back maneuver
bounceBack() {
this.bounceBack = true;
}
// Update monster to chase hero
chaseUpdate() {
this.sprite.angle = toDeg(this.game.physics.arcade.angleBetween(
this.sprite,
this.hero.sprite
));
let sprite = this.sprite.body;
let speed = this.speed;
sprite.velocity.x = Math.cos(toRad(this.sprite.angle)) * speed;
sprite.velocity.y = Math.sin(toRad(this.sprite.angle)) * speed;
// Fix facing angle after calculating velocity to simplify things
this.sprite.angle += 90;
}
isAlive() {
return this.health > 0;
}
}
// Ugly eye monster, weakest enemy
export class EyeMonster extends Monster {
constructor(game, x, y, scale, hero) {
super(game, x, y, scale, "eye", hero);
this.health = 2;
}
update() {
super.update();
}
render() {
super.render();
}
}
// Demon monster, stronger than eye
export class DemonMonster extends Monster {
constructor(game, x, y, scale, hero) {
super(game, x, y, scale, "demon", hero);
this.speed = 69;
this.health = 4;
this.damage = 2;
}
update() {
super.update();
}
render() {
super.render();
}
}
export class Weapon extends Entity {
/**
* @param {*} game Phaser Game object.
* @param {*} hero Hero object.
* @param {*} scale Sprite scale.
* @param {*} weaponInfo Weapon info object (see info.js).
*/
constructor(game, hero, scale, weaponInfo, critsEnabled) {
super(game);
this.weaponInfo = weaponInfo;
this.sprite = this.game.add.sprite(0, 0, weaponInfo.resource);
this.sprite.visible = false;
game.physics.arcade.enable(this.sprite);
this.hero = hero;
this.sprite.smoothed = false;
this.sprite.anchor.setTo(.5, .5);
this.sprite.scale.setTo(scale, scale);
this.attack = false; // Currently attacking?
this.tick = 0; // Visible ticks
this.critsEnabled = critsEnabled; // Crits enabled flag
this.criticalHit = false; // Attack was crit?
}
update() {
super.update();
// Updates the tick if in attack state
if (this.attack) {
// Update position
let front = this.getFrontOfHero();
this.sprite.x = front.x;
this.sprite.y = front.y;
this.sprite.rotation = this.hero.sprite.rotation;
if (this.tick >= 20) {
this.tick = 0;
this.attack = false;
this.sprite.visible = false;
} else {
this.tick += 1;
this.sprite.visible = true;
}
}
}
/**
* Get the center Vector in front of the hero.
* @return Vector.
*/
getFrontOfHero() {
let hero = this.hero.sprite;
let b = (hero.width + this.sprite.width) / 2;
let x = hero.x + (-b * Math.cos(hero.rotation + Math.PI / 2));
let y = hero.y + (-b * Math.sin(hero.rotation + Math.PI / 2));
return new Vector(x, y);
}
render() {
super.render();
}
}