Created
August 2, 2010 21:18
-
-
Save lorennorman/505331 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's specify some Behaviors, first | |
// Gotta have x,y coords! | |
var Positionable = | |
{ x: 0 | |
, y: 0 // phear my inverted c0mmaZ! | |
}; | |
// Mmmm, delicious physics | |
var Physical = | |
{ speedX: 0 | |
, speedY: 0 | |
, tick: function() | |
{ | |
this.x += this.speedX; | |
this.y += this.speedY; | |
} | |
}; | |
// Annnnnnnd let's draw this thing, too | |
var Renderable = | |
{ render: function(gc) | |
{ | |
gc.fillStyle = 'rgb(255,0,0)'; | |
gc.fillRect( this.x-5, this.y-5, 10, 10 ); | |
} | |
} | |
// Build a GameObject using these behaviors | |
var gameObject = GameObject.create(); | |
gameObject.actsLikeA(Positionable); | |
gameObject.x = Math.random()*800; // Magic numbers ftl, but whatev | |
gameObject.y = Math.random()*600; | |
gameObject.actsLikeA(Physical); | |
gameObject.speedX = Math.random()*10 - 5; | |
gameObject.speedY = Math.random()*10 - 5; | |
gameObject.actsLikeA(Renderable); | |
// Now we can be like: | |
gameObject.tick(); | |
gameObject.render(graphicsContext); | |
// ... in the game loop. Simple! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment