-
Notifications
You must be signed in to change notification settings - Fork 7
/
enemy.js
177 lines (142 loc) · 3.97 KB
/
enemy.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
/* Defines the enemies. */
var Enemy = me.ObjectEntity.extend({
init: function( x, y, settings ){
this.parent( x, y, settings );
// Pick the starting direction
this.changeDirection( settings.direction || 0 );
// Starting velocity is arbitrary.
this.setVelocity( 1, 0 );
this.setMaxVelocity( 1, 8 );
// Gravity is arbitrary.
this.gravity = 0.6;
//this.updateColRect( 8, 32, -1 );
this.collidable = true;
this.type = me.game.ENEMY_OBJECT;
this.addAnimation( "idle", [0] );
this.addAnimation( "die", [3] );
this.addAnimation( "run", [1, 0, 2, 0] );
this.setCurrentAnimation( "run" );
this.updateColRect( 0, 96, 20, 67 );
},
changeDirection: function( d ) {
this.direction = d;
this.flipX( d );
},
die: function() {
me.audio.play( "enemydeath" );
this.alive = false;
this.collidable = false;
this.setCurrentAnimation( "die" );
this.setVelocity( 0, 0 );
this.flicker( 90, function() {
me.game.remove( this );
});
},
onCollision: function( res, obj ) {
this.collide( res, obj );
},
collide: function( res, obj ) {
if ( obj == me.game.player ) {
if ( res.y > 0 && obj.buttStomped) {
this.die();
}
else {
obj.hit("enemy");
this.die();
}
}
},
update: function() {
if ( !this.visible ) {
return false;
}
this.parent();
this.doWalk( this.direction );
var collision = this.updateMovement();
if( collision.x ) {
this.changeDirection( ! this.direction );
}
return false;
}
});
var Saw = me.ObjectEntity.extend({
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.toprange = y;
this.bottomrange = y + settings.height;
this.changeDirection( false );
this.flipX( settings.direction || false );
this.animationspeed = 2;
this.setVelocity( 0, 1.5 );
this.gravity = 0;
this.collidable = true;
this.type = me.game.ENEMY_OBJECT;
this.addAnimation( "idle", [0, 1, 2] );
this.setCurrentAnimation( "idle" );
this.soundCounter = 0;
this.soundName = settings.underwater ? "watersaw" : "saw";
this.soundCounter = 50;
this.soundCounterMax = this.soundCounter;
},
changeDirection: function( d ) {
this.direction = d;
},
onCollision: function( res, obj ) {
if ( obj == me.game.player ) {
obj.hit("saw");
}
},
update: function() {
if ( !this.visible ) {
this.soundCounter = this.soundCounterMax - 1;
return false;
}
// small hack - for some reason newly init'd
// objs are visible for first frame so don't play immediately
if ( this.soundCounter == this.soundCounterMax - 1 )
{
me.audio.play( this.soundName );
}
else if ( this.soundCounter == 0 )
{
this.soundCounter = this.soundCounterMax;
}
this.soundCounter--;
this.parent();
// arbitrary math
this.vel.y = ( this.direction ? -1 : 1 ) * 1.5 * me.timer.tick;
this.pos.add( this.vel );
// Turn around at extents.
if( this.bottom > this.bottomrange || this.top < this.toprange ) {
this.changeDirection( ! this.direction );
}
return false;
}
});
var Bomb = me.ObjectEntity.extend({
init: function( x, y, settings )
{
this.parent( x, y, settings );
this.gravity = 0.0;
this.collidable = true;
this.type = "bomb";
this.updateColRect( -48, 144, -48, 144 );
},
die: function()
{
me.game.remove( this );
spawnParticle( this.pos.x - 48, this.pos.y - 48,
"explode", 144, [ 0, 1, 2, 3, 4, 5, 6, 7 ], 3, this.z );
me.audio.play( "explosion" );
},
checkCollision: function( obj )
{
// remove bomb on hit
var retVal = this.parent( obj );
if ( retVal )
{
this.die();
}
return retVal;
}
});