Skip to content

Commit

Permalink
adding more examples, changing _proto to prototype to make it more ob…
Browse files Browse the repository at this point in the history
…vious
  • Loading branch information
straker committed May 18, 2015
1 parent fd64fdb commit a56ba92
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 57 deletions.
101 changes: 101 additions & 0 deletions examples/extendingSprite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<!DOCTYPE html>
<html>
<head>
<title>Extend Sprite</title>
<style>
canvas {
background: black;
}
</style>
<script type="text/javascript" src="../../kontra.js"></script>
</head>
<body>
<canvas width="600" height="300"></canvas>
<script>
kontra.init();

// traditional constructor function inheritance
function ConstructorParent() {
this.one = 1;
}
ConstructorParent.prototype.myFunc = function() {
console.log(this.one);
};

function ConstructorChild() {
this.two = 2;
}
ConstructorChild.prototype = new ConstructorParent();

ConstructorChild.prototype.someFunc = function() {
console.log(this.two);
};

var constructorObj = new ConstructorChild();





// kontra object inheritance using constructor function
function spriteConstructorChild() {
this.two = 2;
}
spriteConstructorChild.prototype = Object.create(kontra.sprite.prototype);

spriteConstructorChild.prototype.one = 1;
spriteConstructorChild.prototype.someFunc = function() {
console.log(this.two);
};

var spriteConstructorObj = new spriteConstructorChild();





// kontra object inheritance using factory function
function createExtendedSprite() {
var extendedSprite = Object.create(createExtendedSprite.prototype);
extendedSprite.two = 2;

return extendedSprite;
}

createExtendedSprite.prototype = Object.create(kontra.sprite.prototype);

createExtendedSprite.prototype.one = 1;
createExtendedSprite.prototype.someFunc = function() {
console.log(this.two);
};

var extendedSprite = createExtendedSprite();





// overriding sprite set function
function createSetSprite() {
var extendedSprite = Object.create(createSetSprite.prototype);
extendedSprite.two = 2;

return extendedSprite;
}

createSetSprite.prototype = Object.create(kontra.sprite.prototype);

createSetSprite.prototype.set = function(properties) {
this.one = 1;
this.someFunc = function() {
console.log(this.two);
};

// call kontra.sprite set function
kontra.sprite._proto.set.call(this, properties);
};

var setSprite = createSetSprite();
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/galaxian/js/galaxian.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kontra.loadAssets(
/*
* Audio pools
*/
// create an audio sprite for use in an audio pool, mimicing a normal image sprite
// create an audio sprite for use in an audio pool, mimicking a normal image sprite
var audioSprite = {
isAlive: function() {
return !this.audio.ended;
Expand Down
110 changes: 110 additions & 0 deletions examples/particleEngine.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!DOCTYPE html>
<html>
<head>
<title>Particle Engine</title>
<style>
canvas {
background: black;
}
</style>
<script type="text/javascript" src="../../kontra.js"></script>
</head>
<body>
<canvas width="600" height="600"></canvas>
<script>
kontra.init();

var midX = kontra.canvas.width / 2;
var midY = kontra.canvas.height / 2;

// you can animate about 5000 particles in 60PFS
// each field means you have to reduce the particle count about 1000 to remain under 60FPS
var fields = [
kontra.sprite({
x: midX - 75,
y: midY + 20,
width: 3,
height: 3,
color: 'green',
properties: {
mass: 900,
size: 3
}
}),
kontra.sprite({
x: midX + 25,
y: midY + 10,
width: 3,
height: 3,
color: 'red',
properties: {
mass: -50,
size: 3
}
})
];

var pool = kontra.pool({
create: kontra.sprite,
maxSize: 4000,
fill: true
});

kontra.context.fillStyle = 'blue';

// redefine sprite update to account for fields
kontra.sprite.prototype.update = function() {
// apply field
var totalAccelerationX = 0
var totalAccelerationY = 0;

for (var i = 0, field; field = fields[i]; i++) {

var vectorX = field.position.x - this.position.x;
var vectorY = field.position.y - this.position.y;

var force = field.mass / Math.pow(vectorX * vectorX + vectorY * vectorY, 1.5);

totalAccelerationX += vectorX * force;
totalAccelerationY += vectorY * force;
}

this.acceleration.x = totalAccelerationX
this.acceleration.y = totalAccelerationY

this.advance();
};

// redefine sprite render to not change context fillStyle every time
kontra.sprite.prototype.render = function() {
kontra.context.fillRect(this.position.x, this.position.y, 3, 3);
};

var loop = kontra.gameLoop({
update: function() {
for (var i = 0; i < 4; i++) {
pool.get({
x: midX + 75,
y: midY,
dx: 2 - Math.random() * 4,
dy: 2 - Math.random() * 4,
color: 'blue',
width: 1,
height: 1,
timeToLive: Infinity
});
}

pool.update();
},
render: function() {
kontra.context.clearRect(0, 0, kontra.game.width, kontra.game.height);

pool.render();
}
});

loop.start();
</script>
</body>
</html>
55 changes: 28 additions & 27 deletions kontra.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,16 +976,16 @@ var kontra = (function(kontra, window) {
* Game loop that updates and renders the game every frame.
* @memberof kontra
*
* @see kontra.gameLoop._proto.set for list of parameters.
* @see kontra.gameLoop.prototype.set for list of parameters.
*/
kontra.gameLoop = function(properties) {
var gameLoop = Object.create(kontra.gameLoop._proto);
var gameLoop = Object.create(kontra.gameLoop.prototype);
gameLoop.set(properties);

return gameLoop;
};

kontra.gameLoop._proto = {
kontra.gameLoop.prototype = {
/**
* Set properties on the game loop.
* @memberof kontra.gameLoop
Expand Down Expand Up @@ -1382,16 +1382,16 @@ var kontra = (function(kontra) {
* Unused items are at the front of the pool and in use items are at the of the pool.
* @memberof kontra
*
* @see kontra.pool._proto.set for list of parameters.
* @see kontra.pool.prototype.set for list of parameters.
*/
kontra.pool = function(properties) {
var pool = Object.create(kontra.pool._proto);
var pool = Object.create(kontra.pool.prototype);
pool.set(properties);

return pool;
};

kontra.pool._proto = {
kontra.pool.prototype = {
/**
* Set properties on the pool.
*
Expand Down Expand Up @@ -1577,7 +1577,7 @@ var kontra = (function(kontra, undefined) {
* collapses to avoid garbage collection.
* @memberof kontra
*
* @see kontra.quadtree._proto.set for list of parameters.
* @see kontra.quadtree.prototype.set for list of parameters.
*L
* The quadrant indices are numbered as follows (following a z-order curve):
* |
Expand All @@ -1587,13 +1587,13 @@ var kontra = (function(kontra, undefined) {
* |
*/
kontra.quadtree = function(properties) {
var quadtree = Object.create(kontra.quadtree._proto);
var quadtree = Object.create(kontra.quadtree.prototype);
quadtree.set(properties);

return quadtree;
};

kontra.quadtree._proto = {
kontra.quadtree.prototype = {
/**
* Set properties on the quadtree.
* @memberof kontra.quadtree
Expand Down Expand Up @@ -1845,26 +1845,30 @@ var kontra = (function(kontra, Math, undefined) {
* A vector for 2D space.
* @memberof kontra
*
* @see kontra.vector._proto.set for list of parameters.
* @see kontra.vector.prototype.set for list of parameters.
*/
kontra.vector = function(x, y) {
var vector = Object.create(kontra.vector._proto);
var vector = Object.create(kontra.vector.prototype);
vector.set(x, y);

return vector;
};

kontra.vector._proto = {
kontra.vector.prototype = {
/**
* Set the vector's x and y position.
* @memberof kontra.vector
*
* @param {number} x=0 - Center x coordinate.
* @param {number} y=0 - Center y coordinate.
*
* @returns {vector}
*/
set: function set(x, y) {
this.x = x || 0;
this.y = y || 0;

return this;
},

/**
Expand Down Expand Up @@ -1914,16 +1918,13 @@ var kontra = (function(kontra, Math, undefined) {
* @see kontra.sprite._prot.set for list of parameters.
*/
kontra.sprite = function(properties) {
var sprite = Object.create(kontra.sprite._proto);
sprite.position = kontra.vector();
sprite.velocity = kontra.vector();
sprite.acceleration = kontra.vector();
var sprite = Object.create(kontra.sprite.prototype);
sprite.set(properties);

return sprite;
};

kontra.sprite._proto = {
kontra.sprite.prototype = {
/**
* Move the sprite by its velocity.
* @memberof kontra.sprite
Expand Down Expand Up @@ -2034,11 +2035,11 @@ var kontra = (function(kontra, Math, undefined) {

var _this = this;

_this.position.set(properties.x, properties.y);
_this.velocity.set(properties.dx, properties.dy);
_this.acceleration.set(properties.ddx, properties.ddy);
_this.timeToLive = properties.timeToLive || 0;
_this.position = (_this.position || kontra.vector()).set(properties.x, properties.y);
_this.velocity = (_this.velocity || kontra.vector()).set(properties.dx, properties.dy);
_this.acceleration = (_this.acceleration || kontra.vector()).set(properties.ddx, properties.ddy);

_this.timeToLive = properties.timeToLive || 0;
_this.context = properties.context || kontra.context;

// image sprite
Expand Down Expand Up @@ -2172,16 +2173,16 @@ var kontra = (function(kontra, undefined) {
* Single animation from a sprite sheet.
* @memberof kontra
*
* @see kontra.pool._proto.set for list of parameters.
* @see kontra.pool.prototype.set for list of parameters.
*/
kontra.animation = function(properties) {
var animation = Object.create(kontra.animation._proto);
var animation = Object.create(kontra.animation.prototype);
animation.set(properties);

return animation;
};

kontra.animation._proto = {
kontra.animation.prototype = {
/**
* Set properties on the animation.
* @memberof kontra.animation
Expand Down Expand Up @@ -2298,16 +2299,16 @@ var kontra = (function(kontra, undefined) {
* Create a sprite sheet from an image.
* @memberof kontra
*
* @see kontra.spriteSheet._proto.set for list of parameters.
* @see kontra.spriteSheet.prototype.set for list of parameters.
*/
kontra.spriteSheet = function(properties) {
var spriteSheet = Object.create(kontra.spriteSheet._proto);
var spriteSheet = Object.create(kontra.spriteSheet.prototype);
spriteSheet.set(properties);

return spriteSheet;
};

kontra.spriteSheet._proto = {
kontra.spriteSheet.prototype = {
/**
* Set properties on the spriteSheet.
* @memberof kontra
Expand Down
2 changes: 1 addition & 1 deletion kontra.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion kontra.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit a56ba92

Please sign in to comment.