xonecas.io
211
Ze

zen / commit d9b37c6

Lots of changes, now using oo approach

xonecas2011-02-04 d9b37c66b9486bf03af850a8b1a71d61f2ee1061 parent a5b8cee

6 files changed

404.html+0−22
@@ -1,22 +0,0 @@
1-<!doctype html>
2-<title>not found</title>
3-
4-<style>
5-body { text-align: center;}
6-h1 { font-size: 50px; }
7-body { font: 20px Constantia, 'Hoefler Text', "Adobe Caslon Pro", Baskerville, Georgia, Times, serif; color: #999; text-shadow: 2px 2px 2px rgba(200, 200, 200, 0.5); }
8-::-moz-selection{ background:#FF5E99; color:#fff; }
9-::selection { background:#FF5E99; color:#fff; }
10-details { display:block; }
11-a { color: rgb(36, 109, 56); text-decoration:none; }
12-a:hover { color: rgb(96, 73, 141) ; text-shadow: 2px 2px 2px rgba(36, 109, 56, 0.5); }
13-span[frown] { transform: rotate(90deg); display:inline-block; color: #bbb; }
14-</style>
15-
16-
17-
18-
19-<details>
20- <summary><h1>Not found</h1></summary>
21- <p><span frown>:(</span></p>
22-</details>
images/alien.png+0−0
Binary file, not shown.
images/laser.png+0−0
Binary file, not shown.
index.html+3−26
@@ -35,22 +35,11 @@
3535 </head>
3636
3737 <body lang="en">
38-
39- <div id="container">
40- <header>
41- <div id="fontLoader">LOADING zxc</div>
42- </header>
43-
44- <section id="main" role="main">
45-
46- </section>
47-
38+ <div id="fontLoader">LOADING zxc</div>
4839 <footer id="imageLoader">
49- <img src="images/alien.png" id="alien" />
50- <img src="images/laser.png" id="laser" />
40+ <img src="images/skully1.png" id="skully" />
41+ <img src="images/cannon.png" id="cannon" />
5142 </footer>
52- </div> <!--! end of #container -->
53-
5443
5544 <!-- Javascript at the bottom for fast page loading -->
5645
@@ -60,13 +49,11 @@
6049 !window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.4.js"%3E%3C/script%3E'))
6150 </script>
6251
63-
6452 <!-- scripts concatenated and minified via ant build script-->
6553 <script src="js/plugins.js"></script>
6654 <script src="js/script.js"></script>
6755 <!-- end concatenated and minified scripts-->
6856
69-
7057 <!--[if lt IE 7 ]>
7158 <script src="js/libs/dd_belatedpng.js"></script>
7259 <script>
@@ -74,15 +61,5 @@
7461 </script>
7562 <![endif]-->
7663
77-
78- <!-- mathiasbynens.be/notes/async-analytics-snippet Change UA-XXXXX-X to be your site's ID
79- I don't haz one...
80- <script>
81- var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
82- (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;
83- g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
84- s.parentNode.insertBefore(g,s)}(document,'script'));
85- </script> -->
86-
8764 </body>
8865 </html>
js/plugins.js+63−0
@@ -7,7 +7,70 @@ window.log = function(){
77 if(this.console) console.log( Array.prototype.slice.call(arguments) );
88 };
99
10+/* Simple JavaScript Inheritance
11+ * By John Resig http://ejohn.org/
12+ * MIT Licensed.
13+ */
1014
15+// Inspired by base2 and Prototype
16+(function(){
17+ var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
18+ // The base Class implementation (does nothing)
19+ this.Class = function(){};
20+
21+ // Create a new Class that inherits from this class
22+ Class.extend = function(prop) {
23+ var _super = this.prototype;
24+
25+ // Instantiate a base class (but only create the instance,
26+ // don't run the init constructor)
27+ initializing = true;
28+ var prototype = new this();
29+ initializing = false;
30+
31+ // Copy the properties over onto the new prototype
32+ for (var name in prop) {
33+ // Check if we're overwriting an existing function
34+ prototype[name] = typeof prop[name] == "function" &&
35+ typeof _super[name] == "function" && fnTest.test(prop[name]) ?
36+ (function(name, fn){
37+ return function() {
38+ var tmp = this._super;
39+
40+ // Add a new ._super() method that is the same method
41+ // but on the super-class
42+ this._super = _super[name];
43+
44+ // The method only need to be bound temporarily, so we
45+ // remove it when we're done executing
46+ var ret = fn.apply(this, arguments);
47+ this._super = tmp;
48+
49+ return ret;
50+ };
51+ })(name, prop[name]) :
52+ prop[name];
53+ }
54+
55+ // The dummy class constructor
56+ function Class() {
57+ // All construction is actually done in the init method
58+ if ( !initializing && this.init )
59+ this.init.apply(this, arguments);
60+ }
61+
62+ // Populate our constructed prototype object
63+ Class.prototype = prototype;
64+
65+ // Enforce the constructor to be what we expect
66+ Class.constructor = Class;
67+
68+ // And make this class extendable
69+ Class.extend = arguments.callee;
70+
71+ return Class;
72+ };
73+})();
1174
1275 // place any jQuery/helper plugins in here, instead of separate, slower script files.
1376
js/script.js+223−111
@@ -1,137 +1,249 @@
1-/* Author: xonecas
2-
3- this file needs to be cleaned up and organized
41
2+/* Author: xonecas
53 */
64
75 (function (window, undefined) {
86
9- var document = window.document;
10-
11- $(window).load(function () {
12- $('#fontLoader').remove();
13-
14- if (Modernizr.canvas) {
15-
16- var canvas = document.createElement('canvas'),
17- canvasWidth = $(window).width() - 38,
18- canvasHeight = $(window).height(),
19- center = Math.ceil(canvasWidth / 2),
20- currentX = center + 10,
21- currentY = canvasHeight - 58,
22- aliens = [];
7+ var document = window.document,
8+ LEFT = 37, RIGHT = 39, SPACE = 32,
9+ IMGSIZE = 38, PADSIZE = 20,
10+ BG = '#444',
11+ BULL_W = 5, BULL_H = 10, BULL_COLOR = '#FFF';
12+
13+ function get (id) {
14+ return document.getElementById(id);
15+ }
16+
17+ // oo approach
18+ // http://ejohn.org/blog/simple-javascript-inheritance/
19+
20+ var Shape = Class.extend({
21+ x: 0,
22+ y: 0,
23+ width: 0,
24+ height: 0,
25+ ctx: undefined,
26+ fill: undefined,
27+
28+ // constructor
29+ init: function (w, h, ctx, fill) {
30+ this.x = 0;
31+ this.y = 0;
32+ this.width = w;
33+ this.height = h;
34+ this.ctx = ctx;
35+ this.fill = fill;
36+ },
37+
38+ // draw the shape
39+ draw: function (x, y) {
40+ this.x = x;
41+ this.y = y;
42+ var c = this.ctx;
43+ c.fillStyle = this.fill;
44+ c.fillRect(x, y, this.width, this.height);
45+ },
46+
47+ // erase the shape
48+ clear: function () {
49+ var ctx = this.ctx;
50+ ctx.fillStyle = BG;
51+ ctx.fillRect(this.x, this.y, this.width, this.height);
52+ }
53+ });
2354
55+ var Image = Shape.extend({
56+ // call the Shape constructor
57+ init: function (w, h, ctx, fill) {
58+ this._super(w, h, ctx, fill);
59+ },
60+
61+ // override
62+ // draw the image instead
63+ draw: function (x, y) {
64+ this.x = x;
65+ this.y = y;
66+ this.ctx.drawImage(this.fill, x, y, this.width, this.height);
67+ }
68+ });
2469
25- $(canvas).attr({
26- 'height': canvasHeight,
27- 'width': canvasWidth
28- });
70+ var Bullet = Shape.extend({
71+ // call up the chain of constructors
72+ init: function (owner, ctx) {
73+ this._super(BULL_W, BULL_H, ctx, BULL_COLOR);
74+ this.owner = owner;
75+ },
76+
77+ // trigger method
78+ fire: function (x, y, targets, limit) {
79+ // go up or down?
80+ var direction = (this.owner instanceof Cannon)? -2: 2,
81+ that = this;
82+
83+ (function fly () {
84+
85+ that.clear();
86+ that.draw(x, (y += direction));
87+
88+ if (!that.isHit(targets) || y <= 0 || y >= limit)
89+ setTimeout(fly, 10);
90+
91+ }) ();
92+
93+ },
94+
95+ // did the bullet hit anything?
96+ isHit: function (targets) {
97+ var i = targets.length;
98+
99+ while (--i > -1) {
100+ var target = targets[i];
101+ if (this.y >= target.y && this.y <= (target.y +IMGSIZE)) {
102+ if (this.x >= target.x && this.x <= (target.x +IMGSIZE)) {
103+ if (!target.isDead) {
104+ target.isDead = true;
105+ target.clear();
106+ this.clear();
107+ return true;
108+ }
109+ }
110+ }
111+ }
112+ return false;
113+ }
114+ });
29115
30- $('#main').html(canvas);
116+ var Cannon = Image.extend({
117+ // use the image constructor
118+ init: function (size, ctx, src) {
119+ this._super(size, size, ctx, src);
120+ },
121+
122+ // fire missile!!!
123+ 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+ },
127+
128+ // movement controls
129+ move: function (ev) {
130+ if (!this.keepMoving) {
131+
132+ var that = this;
133+ this.keepMoving = true;
134+ this.direction = ev.which;
135+
136+ (function move () {
137+ if (that.keepMoving) {
138+ that.clear();
139+ that.x += (that.direction === LEFT)? -2: 2;
140+ that.draw(that.x, that.y);
141+ setTimeout(move, 10);
142+ }
143+ }) ();
144+ }
145+ else
146+ this.direction = ev.which;
147+ },
31148
32- var ctx = canvas.getContext('2d');
33- var alien = $("#alien").get(0),
34- laser = $('#laser').get(0);
149+ stop: function (ev) {
150+ if (this.keepMoving && this.direction === ev.which)
151+ this.keepMoving = false;
152+ }
35153
36- ctx.fillStyle = '#fff';
154+ });
37155
38- ctx.drawImage(laser, currentX, currentY, 38, 38);
156+ var Invader = Image.extend({
157+ init: function (size, ctx, type) {
158+ this._super(size, size, ctx, get(type));
159+ }
160+ });
39161
40- var x = 5, z = 11, startx = center - 279, starty = 20;
41- while (x--) {
42- while (z--) {
43- ctx.drawImage(alien, startx, starty, 38, 38);
44- aliens.push({ x: startx, y: starty });
45- startx += 58;
162+ SpaceInvaders = Class.extend({
163+ started: false,
164+ paused: false,
165+ lives: 3,
166+ canvas: {},
167+ ctx: false,
168+ invaders: [],
169+ cannon: {},
170+
171+ // constructor
172+ init: function (canvas) {
173+
174+ this.canvas = canvas;
175+ this.width = canvas.width = $(window).width();
176+ this.height = canvas.height = $(window).height();
177+ this.ctx = canvas.getContext('2d');
178+ this.started = true;
179+ this.cannon = new Cannon(IMGSIZE, this.ctx, get('cannon'));
180+ this.cannon.draw(this.width /2, this.height - (IMGSIZE + PADSIZE));
181+
182+ var rows = 2, cols = 11, index = 0, center = this.width/2,
183+ x = Math.floor(center - (((IMGSIZE + PADSIZE) * 11) - 20) /2), y = PADSIZE;
184+ while (rows--) {
185+ while (cols--) {
186+ index = this.invaders.push(new Invader(IMGSIZE, this.ctx, 'skully')) -1;
187+ this.invaders[index].draw(x, y);
188+ x += IMGSIZE + PADSIZE;
46189 }
47- z = 11;
48- startx = center -279;
49- starty += 58;
190+ y += IMGSIZE + PADSIZE;
191+ x = Math.floor(center - (((IMGSIZE + PADSIZE) * 11) - 20) /2);
192+ cols = 11;
50193 }
51194
52- function isHit (aliens, missile) {
53- var hit = false,
54- i = aliens.length;
55-
56- while (!hit && --i > -1) {
57- var alien = aliens[i];
58- if (missile.y >= alien.y && missile.y <= (alien.y +38)) {
59- if (missile.x >= alien.x && missile.x <= (alien.x +38)) {
60- if (!alien.isDead) {
61- hit = { x: alien.x, y: alien.y };
62- alien.isDead = true;
63- }
64- }
65- }
66- }
67-
68- if (hit) return hit;
195+ },
196+
197+ keydown: function (ev) {
198+ switch(ev.which) {
199+ case LEFT:
200+ case RIGHT:
201+ ev.preventDefault();
202+ this.cannon.move(ev);
203+ break;
204+
205+ case SPACE:
206+ ev.preventDefault();
207+ this.cannon.shoot(this.invaders);
208+ break;
209+ }
210+ },
211+ keyup: function (ev) {
212+ switch(ev.which) {
213+ case LEFT:
214+ case RIGHT:
215+ ev.preventDefault();
216+ this.cannon.stop(ev);
217+ break;
69218 }
219+ }
220+
70221
71- $(document).keydown(function (ev) {
72- switch(ev.which) {
73- case 37: // left arrow
74- case 39: // right arrow
75- var keepMoving = 10;
76- (function animate () {
77- if (currentX <= 38 ||
78- currentX >= (canvasWidth -38))
79- return currentX = (currentX <= 38)? 40: canvasWidth -40;
80-
81- ctx.fillStyle = '#444';
82- ctx.fillRect(currentX -2, currentY , 42, 38);
83-
84- currentX += (ev.which === 37)? -2: 2;
85-
86- ctx.fillStyle = '#ffffff';
87- ctx.drawImage(laser, currentX, currentY, 38, 38);
88- if (--keepMoving)
89- setTimeout(animate, 20);
90- }) ();
91- break;
92-
93- case 32: // space bar
94- var originX = currentX +18,
95- originY = currentY;
96-
97- (function fly () {
98- ctx.fillStyle = '#444';
99- ctx.fillRect(originX, originY, 2, 10);
100-
101- originY -= 22;
102- ctx.fillStyle = '#fff';
103- ctx.fillRect(originX, originY, 2, 10);
104-
105- var hit = isHit(aliens, { x: originX, y: originY });
106- if (originY >= -10 && !hit)
107- setTimeout(fly, 30);
108- else if (hit) {
109- ctx.fillStyle = '#444';
110- ctx.fillRect(hit.x, hit.y, 38, 38);
111- ctx.fillRect(originX, originY, 2, 10);
112- }
113- }) ();
114- break;
115- }
222+ });
116223
117- //log(ev.which);
118- });
119- }
224+ window.onload = function () {
120225
121- else { // IE or old browser
122- var canvas_ = $('#main'),
123- testString = 'TEST: Hello world!',
124- fillText = document.createElement('div');
226+ var canvas = document.createElement('canvas');
227+ $('#fontLoader').remove();
228+ $('body').append(canvas);
229+ var game = new SpaceInvaders(canvas);
125230
126- $(fillText).html(testString).css({
127- 'top': '20px',
128- 'left': '50px'
129- });
231+/*
232+ var $doc = $(document);
233+ $doc.keydown(game.keydown);
234+ $doc.keyup(game.keyup);
235+*/
130236
131- canvas_.html(fillText);
237+ // this works, but I don't fully understand
238+ // why I had to call it like that...
239+ document.onkeydown = function (ev) {
240+ game.keydown.call(game, ev);
241+ }
242+ document.onkeyup = function (ev) {
243+ game.keyup.call(game, ev);
132244 }
133- });
134245
246+ }
135247
136248 }) (window);