From 58e5e72564bce298fcdc73728b91d981feda9451 Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Thu, 4 Aug 2011 09:16:49 +0300 Subject: Almost converted from Jaws -> EaselJS --- catgag.js | 152 ++++++++++++++------------------------------------------------ 1 file changed, 34 insertions(+), 118 deletions(-) (limited to 'catgag.js') diff --git a/catgag.js b/catgag.js index 968020c..b7bbf96 100644 --- a/catgag.js +++ b/catgag.js @@ -1,126 +1,42 @@ -function CatGAGGame() { - //! Returns true if the item is outside the canvas. - function outsideCanvas(item) { - return (item.x < 0 || item.y < 0 || item.x > jaws.width || item.y > jaws.height); - } +(function(window) { + +var Game = function() { }; + +Game.init = function() { + this.canvas = document.getElementById("canvas"); + this.stage = new Stage(this.canvas); + + this.stage.preventDefaultKeys(["space", "up", "down", "left", "right"]); - //! Our hero, the Cat! - var Cat = function() { - this.barrelRolling = false; - this.anchor("center"); - this.setImage("cat.jpg"); - - this._rollState = 0; - this._shooting = false; - - this.gags = new jaws.SpriteList(); - - this.update = function() { - if (this.barrelRolling) { - this.rotate(360.0 / 45.0); - - this._rollState++; - - if (this._rollState >= 45) { - this._rollState = 0; - this.barrelRolling = false; - } - } - - for (i = 0; this.gags[i]; i++) { - if (this.gags[i].escaped && this.rect().collideRect(this.gags[i].rect())) { - this.gags.splice(i, 1); - } - } - - this.gags.update(); - this.gags.deleteIf(outsideCanvas); - } - - this.barrelRoll = function() { - this.barrelRolling = true; - } - - this.shoot = function() { - if (!this._shooting) { - this._shooting = true; - this.gags.push(new CatGag(this, this.x, this.y, this.angle)); - window.setTimeout(function(cat) { cat._shooting = false; }, 250, this); - } - } - - this._old_draw = this.draw; - this.draw = function() { - this._old_draw(); - this.gags.draw(); - } - }; - Cat.prototype = new jaws.Sprite({}); - - var CatGag = function(cat, x, y, dir) { - this.x = x; - this.y = y; - this.dir = dir; - this.cat = cat; - this.escaped = false; //! False if the gag hasn't escaped the cat yet - - this.update = function() { - if (!this.escaped && !this.rect().collideRect(this.cat.rect())) { - this.escaped = true; - } - - this.x += Math.cos(this.dir * Math.PI / 180.0) * 2; - this.y += Math.sin(this.dir * Math.PI / 180.0) * 2; - } - }; - CatGag.prototype = new jaws.Sprite({image: "catgag.png"}); + this.cat = new Cat(); + this.cat.x = this.canvas.width / 2; + this.cat.y = this.canvas.height / 2; - var cat; + this.stage.addChild(this.cat); - this.setup = function() { - jaws.preventDefaultKeys(["up", "down", "left", "right", "space"]) + Ticker.addListener(this); +}; + +Game.tick = function() { + this.handleKeys(); + this.stage.update(); +}; - cat = new Cat(); - cat.x = 100; cat.y = 100; - }; +Game.handleKeys = function() { + var stage = this.stage; - this.update = function() { - if (jaws.pressed("left")) { - cat.x -= 5; - } - - if (jaws.pressed("right")) { - cat.x += 5; - } - - if (jaws.pressed("up")) { - cat.y -= 5; - } - - if (jaws.pressed("down")) { - cat.y += 5; - } - - if (jaws.pressed("space") && !cat.barrelRolling) { - cat.barrelRoll(); - } - - if (jaws.pressed("z")) { - cat.shoot(); - } - - cat.update(); - }; + if (stage.isKeyPressed("space")) { this.cat.gag(); } - this.draw = function() { - jaws.clear(); - cat.draw(); - }; + if (stage.isKeyPressed("left")) { this.cat.x -= 5; } + if (stage.isKeyPressed("right")) { this.cat.x += 5; } + if (stage.isKeyPressed("up")) { this.cat.y -= 5; } + if (stage.isKeyPressed("down")) { this.cat.y += 5; } + + if (stage.isKeyPressed("a")) { this.cat.barrelRoll(); } } -window.onload = function() { - jaws.assets.add("cat.jpg"); - jaws.assets.add("catgag.png"); - - jaws.start(CatGAGGame, {fps: 30}); -}; +window.Game = Game; +}(window)); + +window.onload = function() { Ticker.setFPS(30); Game.init(); } + -- cgit v1.2.3