-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhero.js
208 lines (141 loc) · 5 KB
/
hero.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
function Hero(x) {
var self = this,
sprite = SPRITES.HERO;
self.x = x;
self.y = 200;
self.w = sprite.w * PIXEL_SIZE;
self.h = sprite.h * PIXEL_SIZE;
self.ax = 0;
self.ay = -9;
self.speed = self.defaultSpeed = 10;
self.dir = 1;
self.damageFactor = 1;
off();
on('ArrowRight', self.run.bind(self), self.stop.bind(self));
on('ArrowLeft', self.run.bind(self, true), self.stop.bind(self, true));
on('ArrowUp', self.jump.bind(self, 24)); // 24 is the jump force.
// Arguments are attack type and cost.
on('KeyZ', self.attack.bind(self, 'shot', 1));
on('KeyX', self.attack.bind(self, 'beam', 15));
self.animation = new Animation(sprite);
self.timers = {};
self.glitchAmount = self.maxGlitchAmount = 25; // GLITCH is Hero's hit points.
self.oneThird = self.maxGlitchAmount / 3; // To calculate GLITCH intensity.
self.glitch = new Glitch(self.x, self.y, self.w, self.h, self.getGlitchIntensity());
self.glitchUp();
self.glitchTrail();
}
extend(Hero, Unit);
Hero.prototype.tick = function () {
var self = this;
// Gap is 0 because the hero cannot move beyond the borders.
self.applyBorders(0);
self.applyFriction();
self.applyGravity();
self.getLandingState();
};
Hero.prototype.ownTick = function () {
var self = this;
level.iterateUnits(self.id, true, function (obj) {
if (haveCollision(self, obj)) {
self.hit(obj.gp); // Hit hero with enemy's Glitch Points.
obj.bump(self, self.ax, -12); // Direct collision bumps the enemy, then kills it.
}
});
};
Hero.prototype.glitchUp = function (n) {
var self = this;
// To avoid several concurrent timers.
gameTimers.clearTimeout(self.timers.glitch);
// Hits, powerups, or constant grow of GLITCH.
self.glitchAmount += (n || 1);
// GLITCH bounds.
if (self.glitchAmount > self.maxGlitchAmount) {
self.glitchAmount = self.maxGlitchAmount;
} else if (self.glitchAmount < 0) {
self.glitchAmount = 0;
}
if (self.glitchAmount == 0) {
self.kill();
} else {
self.timers.glitch = gameTimers.setTimeout(self.glitchUp.bind(self), 800); // New GLITCH point every 800 ms.
self.pushedWithForce = n; // Here, `n` is most often `undefined`, so `pushedWithForce` animation isn't triggered.
}
};
// That is visual part of Hero's GLITCH.
Hero.prototype.glitchTrail = function () {
var self = this,
delay = 70;
// Glitch is updated once in `delay` ms which leaves a kind of trail while running or jumping.
self.glitch.update(self.x, self.y, self.w, self.h, self.getGlitchIntensity());
self.timers.trail = gameTimers.setTimeout(self.glitchTrail.bind(self), delay);
};
Hero.prototype.getGlitchIntensity = function () {
return Math.ceil(this.glitchAmount / this.oneThird);
};
Hero.prototype.run = function (left, rec) {
var self = this,
dir = left ? -1 : 1;
if (rec || !self.isRunning || self.dir != dir) {
gameTimers.clearTimeout(self.timers.run);
self.isRunning = true;
self.dir = dir;
self.ax = left ? -self.speed : self.speed;
self.timers.run = gameTimers.setTimeout(self.run.bind(self, left, true), 50);
}
};
Hero.prototype.stop = function (left) {
var self = this,
dir = left ? -1 : 1;
if (self.dir == dir) {
gameTimers.clearTimeout(this.timers.run);
this.isRunning = false;
}
};
Hero.prototype.attack = function (type, cost) {
var self = this,
directedX = self.x + (self.dir < 0 ? 0 : self.w);
if (self.glitchAmount <= cost) return;
self.glitchAmount -= cost;
if (type == 'shot') {
new Shot(self, 'SHOT', directedX, self.y, self.dir, null, 1000, 10 * self.damageFactor);
} else if (type == 'beam') {
new Beam(5 * self.damageFactor);
}
};
Hero.prototype.hit = function (n) {
this.glitchUp(-n); // It must be named glitchDown().
};
Hero.prototype.bump = function (bumper, ax, ay) {
var self = this;
// A little hack to interrupt running.
gameTimers.clearTimeout(self.timers.run);
gameTimers.setTimeout(self.stop.bind(self), 300);
self.ax = ax || -self.ax;
self.ay = ay || -5;
};
Hero.prototype.kill = function () {
var self = this;
self.dead = true;
self.ax = 0;
self.glitch.remove();
self.clearTimers();
off(); // Switch off controls.
level.lose();
};
Hero.prototype.getAnimationFrame = function () {
var self = this,
reversed = this.dir < 0;
if (self.dead) {
return self.animation.getFrame('dead');
} else if (self.pushedWithForce) {
self.pushedWithForce--;
return self.animation.getFrame('punched', reversed);
} else if (!self.isLanded) {
return self.animation.getFrame('jump', reversed);
} else if (self.isRunning) {
return self.animation.getFrame(isOdd ? 'stepLeft' : 'stepRight', reversed);
} else {
return self.animation.getFrame('still', reversed);
}
};