xonecas.io
211
Ze

zen / commit 8d7c8e6

improved invaders movement

xonecas2011-02-04 8d7c8e6ae58bbc56bb1c20089a038527b5bf8586 parent d9b37c6

2 files changed

index.html+2−1
@@ -37,7 +37,8 @@
3737 <body lang="en">
3838 <div id="fontLoader">LOADING zxc</div>
3939 <footer id="imageLoader">
40- <img src="images/skully1.png" id="skully" />
40+ <img src="images/skully1.png" id="skully1" />
41+ <img src="images/skully2.png" id="skully2" />
4142 <img src="images/cannon.png" id="cannon" />
4243 </footer>
4344
js/script.js+89−39
@@ -1,5 +1,6 @@
11
2-/* Author: xonecas
2+/* Author: Sean Caetano Martin
3+ xonecas
34 */
45
56 (function (window, undefined) {
@@ -48,7 +49,7 @@
4849 clear: function () {
4950 var ctx = this.ctx;
5051 ctx.fillStyle = BG;
51- ctx.fillRect(this.x, this.y, this.width, this.height);
52+ ctx.clearRect(this.x, this.y, this.width, this.height);
5253 }
5354 });
5455
@@ -77,7 +78,7 @@
7778 // trigger method
7879 fire: function (x, y, targets, limit) {
7980 // go up or down?
80- var direction = (this.owner instanceof Cannon)? -2: 2,
81+ var direction = (this.owner instanceof Cannon)? -5: 5,
8182 that = this;
8283
8384 (function fly () {
@@ -86,7 +87,7 @@
8687 that.draw(x, (y += direction));
8788
8889 if (!that.isHit(targets) || y <= 0 || y >= limit)
89- setTimeout(fly, 10);
90+ setTimeout(fly, 50);
9091
9192 }) ();
9293
@@ -104,6 +105,7 @@
104105 target.isDead = true;
105106 target.clear();
106107 this.clear();
108+ targets.splice(i,1);
107109 return true;
108110 }
109111 }
@@ -121,8 +123,10 @@
121123
122124 // fire missile!!!
123125 shoot: function (targets, limit) {
124- var bullet = new Bullet(this, this.ctx);
125- bullet.fire(Math.floor((this.x + this.width /2)), this.y, targets, limit);
126+ if (!this.isDead) {
127+ var bullet = new Bullet(this, this.ctx);
128+ bullet.fire(Math.floor((this.x + this.width /2)), this.y, targets, limit);
129+ }
126130 },
127131
128132 // movement controls
@@ -134,11 +138,11 @@
134138 this.direction = ev.which;
135139
136140 (function move () {
137- if (that.keepMoving) {
141+ if (that.keepMoving && !that.isDead) {
138142 that.clear();
139- that.x += (that.direction === LEFT)? -2: 2;
143+ that.x += (that.direction === LEFT)? -5: 5;
140144 that.draw(that.x, that.y);
141- setTimeout(move, 10);
145+ setTimeout(move, 50);
142146 }
143147 }) ();
144148 }
@@ -154,8 +158,60 @@
154158 });
155159
156160 var Invader = Image.extend({
161+ isDrawn: false,
162+ face: '1',
163+ direction: 1,
164+
157165 init: function (size, ctx, type) {
158166 this._super(size, size, ctx, get(type));
167+ this.direction = -1;
168+ },
169+
170+ // the aliens strike back!
171+ shoot: function (targets, limit) {
172+
173+ var bullet = new Bullet(this, this.ctx);
174+ bullet.fire(Math.floor((this.x + this.width /2)), this.y, targets, limit);
175+
176+ },
177+
178+ // give some dancing abilities to the invaders
179+ live: function (x, y) {
180+ this.x = x;
181+ this.y = y;
182+
183+ var that = this;
184+ (function dance () {
185+
186+ that.fill = get('skully'+ that.face);
187+ that.face = (that.face === '1')? '2': '1';
188+
189+ if (!that.isDead) {
190+ if (!that.moving)
191+ that.draw(that.x, that.y);
192+ setTimeout(dance, 500);
193+ }
194+
195+ }) ();
196+
197+ },
198+
199+ move: function (width, others) {
200+ var that = this;
201+
202+ (function trust () {
203+ that.moving = true;
204+ that.clear();
205+ that.x += IMGSIZE * that.direction;
206+ if (!that.isDead) {
207+ that.draw(that.x, that.y);
208+ setTimeout(trust, 1000);
209+ }
210+ that.moving = false;
211+
212+ if ((others[others.length -1].x + IMGSIZE) >= width || others[0].x <= (PADSIZE + IMGSIZE))
213+ that.direction = (that.direction === 1)? -1: 1;
214+ }) ();
159215 }
160216 });
161217
@@ -184,7 +240,7 @@
184240 while (rows--) {
185241 while (cols--) {
186242 index = this.invaders.push(new Invader(IMGSIZE, this.ctx, 'skully')) -1;
187- this.invaders[index].draw(x, y);
243+ this.invaders[index].live(x, y);
188244 x += IMGSIZE + PADSIZE;
189245 }
190246 y += IMGSIZE + PADSIZE;
@@ -192,6 +248,27 @@
192248 cols = 11;
193249 }
194250
251+
252+ },
253+
254+ // Death to HUMANS!
255+ invade: function () {
256+ var that = this;
257+
258+ // open fire!
259+ (function attack () {
260+ if (that.invaders.length > 0) {
261+ var random = Math.floor(Math.random() * that.invaders.length);
262+
263+ that.invaders[random].shoot([ that.cannon ], that.height);
264+
265+ if (that.started && !that.paused)
266+ setTimeout(attack, 3000);
267+ }
268+ }) ();
269+
270+ for (var ship = 0; ship < this.invaders.length; ship++)
271+ this.invaders[ship].move(this.width, this.invaders);
195272 },
196273
197274 keydown: function (ev) {
@@ -204,7 +281,7 @@
204281
205282 case SPACE:
206283 ev.preventDefault();
207- this.cannon.shoot(this.invaders);
284+ this.cannon.shoot(this.invaders, 0);
208285 break;
209286 }
210287 },
@@ -228,12 +305,6 @@
228305 $('body').append(canvas);
229306 var game = new SpaceInvaders(canvas);
230307
231-/*
232- var $doc = $(document);
233- $doc.keydown(game.keydown);
234- $doc.keyup(game.keyup);
235-*/
236-
237308 // this works, but I don't fully understand
238309 // why I had to call it like that...
239310 document.onkeydown = function (ev) {
@@ -243,29 +314,8 @@
243314 game.keyup.call(game, ev);
244315 }
245316
317+ game.invade();
246318 }
247319
248320 }) (window);
249321
250-
251-
252-
253-
254-
255-
256-
257-
258-
259-
260-
261-
262-
263-
264-
265-
266-
267-
268-
269-
270-
271-