xonecas.io
211
Ze

zen / files

zen / js / script.js

8.8 KB
1
2/* Author: Sean Caetano Martin
3      xonecas 
4*/
5
6(function (window, undefined) {
7
8   var document = window.document,
9      LEFT = 37, RIGHT = 39, SPACE = 32,
10      IMGSIZE = 38, PADSIZE = 20,
11      BG = '#444',
12      BULL_W = 5, BULL_H = 10, BULL_COLOR = '#FFF';
13   
14   function get (id) {
15      return document.getElementById(id);
16   }
17   
18   // oo approach
19   // http://ejohn.org/blog/simple-javascript-inheritance/
20
21   var Shape = Class.extend({
22      x: 0,
23      y: 0,
24      width: 0,
25      height: 0,
26      ctx: undefined,
27      fill: undefined,
28
29      // constructor
30      init: function (w, h, ctx, fill) {
31         this.x = 0;
32         this.y = 0;
33         this.width = w;
34         this.height = h;
35         this.ctx = ctx;
36         this.fill = fill;
37      },
38
39      // draw the shape
40      draw: function (x, y) {
41         this.x = x;
42         this.y = y;
43         var c =  this.ctx;
44         c.fillStyle = this.fill;
45         c.fillRect(x, y, this.width, this.height);
46      },
47
48      // erase the shape
49      clear: function () {
50         var ctx = this.ctx;
51         ctx.fillStyle = BG;
52         ctx.clearRect(this.x, this.y, this.width, this.height);
53      }
54   });
55
56   var Image = Shape.extend({
57      // call the Shape constructor
58      init: function (w, h, ctx, fill) {
59         this._super(w, h, ctx, fill);
60      },
61      
62      // override
63      // draw the image instead
64      draw: function (x, y) {
65         this.x = x;
66         this.y = y;
67         this.ctx.drawImage(this.fill, x, y, this.width, this.height);
68      }
69   });
70
71   var Bullet = Shape.extend({
72      // call up the chain of constructors
73      init: function (owner, ctx) {
74         this._super(BULL_W, BULL_H, ctx, BULL_COLOR);
75         this.owner = owner;
76      },
77
78      // trigger method
79      fire: function (x, y, targets, limit) {
80         // go up or down?
81         var direction =  (this.owner instanceof Cannon)? -5: 5,
82            that = this;
83
84         (function fly () {
85
86            that.clear();
87            that.draw(x, (y += direction));
88
89            if ((!that.isHit(targets) || y <= 0 || y >= limit) &&
90               !that.isBlown)
91               setTimeout(fly, 50);
92
93         }) ();
94            
95      },
96
97      // did the bullet hit anything?
98      isHit: function (targets) {
99         var i = targets.length;
100
101         while (--i > -1) {
102            var target = targets[i];
103            if (this.y >= target.y && this.y <= (target.y +IMGSIZE)) {
104               if (this.x >= target.x && this.x <= (target.x +IMGSIZE)) {
105                  if (!target.isDead) {
106                     target.isDead = true;
107                     target.clear();
108                     this.explode();
109                     targets.splice(i,1);
110                     return true;
111                  }
112               }
113            }
114         }
115         return false;
116      },
117   
118      // be done with this bullet
119      explode: function () {
120         this.clear();
121         this.isBlown = true;
122      }
123   });
124
125   var Cannon = Image.extend({
126      // use the image constructor
127      init: function (size, ctx, src) {
128         this._super(size, size, ctx, src);
129      },
130      
131      // fire missile!!!
132      shoot: function (targets, limit) {
133         if (!this.isDead) {
134            var bullet = new Bullet(this, this.ctx);
135            bullet.fire(Math.floor((this.x + this.width /2)), this.y, targets, limit);
136         }
137      },
138
139      // movement controls 
140      move: function (ev) {
141         if (!this.keepMoving) {
142
143            var that = this;
144            this.keepMoving = true;
145            this.direction = ev.which;
146
147            (function move () {
148               if (that.keepMoving && !that.isDead) {
149                  that.clear();
150                  that.x += (that.direction === LEFT)? -5: 5;
151                  that.draw(that.x, that.y);
152                  setTimeout(move, 50);
153               }
154            }) ();
155         }
156         else
157            this.direction = ev.which;
158      },
159
160      stop: function (ev) {
161         if (this.keepMoving && this.direction === ev.which)
162            this.keepMoving = false;
163      }
164
165   });
166
167   var Invader = Image.extend({
168      face: '1',
169      direction: 1,
170
171      init: function (size, ctx, type) {
172         this._super(size, size, ctx, get(type+this.face));
173         this.direction = -1;
174      },
175
176      // the aliens strike back!
177      shoot: function (targets, limit) {
178
179         var bullet = new Bullet(this, this.ctx);
180         bullet.fire(Math.floor((this.x + this.width /2)), this.y, targets, limit);
181
182      },
183
184      // give some dancing abilities to the invaders
185/*
186      live: function (x, y) {
187         this.x = x;
188         this.y = y;
189
190         var that = this;
191         (function dance () {
192               
193            that.fill = get('skully'+ that.face);
194            that.face = (that.face === '1')? '2': '1';            
195
196            if (!that.isDead) {
197               if (!that.moving) 
198                  that.draw(that.x, that.y);
199               setTimeout(dance, 500);
200            }
201         
202         }) ();
203
204      },
205*/
206
207      move: function (direction) {
208         this.fill = get('skully'+ this.face);
209         this.face = (this.face === '1')? '2': '1';            
210         this.moving = true;
211         this.clear();
212         this.x += IMGSIZE * direction;
213         if (!this.isDead) 
214            this.draw(this.x, this.y);
215
216         this.moving = false;
217      }
218   });
219
220   SpaceInvaders = Class.extend({
221      started: false,
222      paused: false,
223      lives: 3,
224      canvas: {},
225      ctx: false,
226      invaders: [],
227      cannon: {},
228      
229      // constructor
230      init: function (canvas) {
231
232         this.canvas = canvas;
233         this.width = canvas.width = $(window).width();
234         this.height = canvas.height = $(window).height();
235         this.ctx = canvas.getContext('2d');
236         this.started = true;
237         this.cannon = new Cannon(IMGSIZE, this.ctx, get('cannon'));
238         this.cannon.draw(this.width /2, this.height - (IMGSIZE + PADSIZE));
239
240         var rows = 2, cols = 11, index = 0, center = this.width/2,
241            x = Math.floor(center - (((IMGSIZE + PADSIZE) * 11) - 20) /2), y = PADSIZE;
242
243         this.formation = {
244            width: (IMGSIZE+PADSIZE)*11-20,
245            height: (IMGSIZE+PADSIZE)*2-20,
246            'x': x, 'y': y,
247            direction: 1
248         };
249         
250
251         while (rows--) {
252            while (cols--) {
253               index = this.invaders.push(new Invader(IMGSIZE, this.ctx, 'skully')) -1;
254               var invader = this.invaders[index];
255               invader.draw(x, y);
256               x += IMGSIZE + PADSIZE;
257            }
258            y += IMGSIZE + PADSIZE;
259            x = Math.floor(center - (((IMGSIZE + PADSIZE) * 11) - 20) /2);
260            cols = 11;
261         }
262
263      },
264
265      // Death to HUMANS!
266      invade: function () {
267         var that = this;
268
269         // open fire!
270         (function attack () {
271            if (that.invaders.length > 0) {
272            var random = Math.floor(Math.random() * that.invaders.length);
273
274            that.invaders[random].shoot([ that.cannon ], that.height);
275
276            if (that.started && !that.paused)
277               setTimeout(attack, 3000);
278            }
279         }) ();
280      },
281
282      moveFormation: function () {
283         var invaders = this.invaders,
284            formation = this.formation,
285            block = PADSIZE+IMGSIZE,
286            that = this;
287
288         (function attackangle () {
289            var x_max = formation.x + formation.width;
290            if (x_max >= that.width - block || formation.x <= PADSIZE )
291               formation.direction = (formation.direction === -1)? 1: -1;
292
293            for (var z=0; z < invaders.length; z++) {
294
295               var invader = invaders[z];
296               invader.move(formation.direction);
297            }
298            formation.x += (block*formation.direction);
299
300                        
301            setTimeout(attackangle, 1000);
302         }) ();
303      },
304
305      keydown: function (ev) {
306         switch(ev.which) {
307            case LEFT:
308            case RIGHT:
309               ev.preventDefault();
310               this.cannon.move(ev);
311            break;
312            
313            case SPACE:
314               ev.preventDefault();
315               this.cannon.shoot(this.invaders, 0);
316            break;
317         }
318      },
319      keyup: function (ev) {
320         switch(ev.which) {
321            case LEFT:
322            case RIGHT:
323               ev.preventDefault();
324               this.cannon.stop(ev);
325            break;
326         }
327      }
328      
329
330   });
331
332   window.onload = function () {
333
334      var canvas = document.createElement('canvas');
335      $('#fontLoader').remove();
336      $('body').append(canvas);
337      var game = new SpaceInvaders(canvas);
338
339      document.onkeydown = function (ev) {
340         game.keydown.call(game, ev);
341      }
342      document.onkeyup = function (ev) {
343         game.keyup.call(game, ev);
344      }
345
346      game.moveFormation();
347      game.invade();
348   }
349
350}) (window);
351