Skip to content

Commit 1c23a2f

Browse files
committed
Added basic canvas game files
1 parent ab6d93a commit 1c23a2f

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

canvasGame/Entity.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function Entity(game)
2+
{
3+
this.game = game;
4+
this.x = 0;
5+
this.y = 0;
6+
}
7+
8+
Entity.prototype.draw = function(ctx)
9+
{
10+
};
11+
12+
Entity.prototype.update = function(elapsed)
13+
{
14+
};
15+
16+
Entity.prototype.drawCentered = function(ctx, sprite, x, y)
17+
{
18+
x -= sprite.width/2;
19+
y -= sprite.height/2;
20+
ctx.drawImage(sprite, x, y);
21+
};

canvasGame/Game.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function Game()
2+
{
3+
}
4+
5+
Game.prototype.loop = function()
6+
{
7+
this.draw(this.ctx);
8+
this.update();
9+
};
10+
11+
Game.prototype.init = function(canvas, imagePrefix)
12+
{
13+
this.imageManager = new ImageManager(imagePrefix);
14+
this.ctx = canvas.getContext('2d');
15+
};
16+
17+
Game.prototype.draw = function(ctx)
18+
{
19+
};
20+
21+
Game.prototype.update = function(elapsed)
22+
{
23+
this.imageManager.update();
24+
};
25+
26+
Game.prototype.start = function()
27+
{
28+
var that = this;
29+
(function gameLoop()
30+
{
31+
that.loop();
32+
requestAnimFrame(gameLoop, that.ctx.canvas);
33+
})();
34+
};

canvasGame/ImageManager.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function ImageManager(prefix)
2+
{
3+
this.prefix = prefix;
4+
this.successCount = 0;
5+
this.errorCount = 0;
6+
this.cache = {};
7+
this.downloadQueue = [];
8+
}
9+
10+
ImageManager.prototype.queueDownload = function(path)
11+
{
12+
this.downloadQueue.push(path);
13+
this.cache[path] = new Image();
14+
};
15+
16+
ImageManager.prototype.isDone = function()
17+
{
18+
return (this.downloadQueue.length == this.successCount + this.errorCount);
19+
};
20+
21+
ImageManager.prototype.update = function()
22+
{
23+
if (this.downloadQueue.length > 0)
24+
{
25+
var path = this.downloadQueue.pop();
26+
this.cache[path].src = this.prefix+path;
27+
}
28+
};
29+
30+
ImageManager.prototype.get = function(path)
31+
{
32+
if (!this.cache[path])
33+
{
34+
this.queueDownload(path);
35+
}
36+
return this.cache[path];
37+
};

0 commit comments

Comments
 (0)