-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.js
More file actions
43 lines (37 loc) · 1.34 KB
/
entity.js
File metadata and controls
43 lines (37 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function Entity(game, x, y) {
this.game = game;
this.x = x;
this.y = y;
this.removeFromWorld = false;
}
Entity.prototype.update = function () {
//console.log(this.angle);
};
// Anything defined here is inherited by all entities (the outline for debugging).
Entity.prototype.draw = function (ctx) {
ctx.drawImage(this.rotateAndCache(this.image, this.velocity.angle()), this.x, this.y, 20, 20);
// Show debug circles only for entities that have a radius
if (this.game.enableDebug && this.radius) {
ctx.beginPath();
ctx.strokeStyle = "green";
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.stroke();
ctx.closePath();
}
};
Entity.prototype.rotateAndCache = function (image, angle) {
var offscreenCanvas = document.createElement('canvas');
var size = Math.max(image.width, image.height);
offscreenCanvas.width = size;
offscreenCanvas.height = size;
var offscreenCtx = offscreenCanvas.getContext('2d');
offscreenCtx.save();
offscreenCtx.translate(size / 2, size / 2);
offscreenCtx.rotate(angle);
offscreenCtx.translate(0, 0);
offscreenCtx.drawImage(image, -(image.width / 2), -(image.height / 2));
offscreenCtx.restore();
//offscreenCtx.strokeStyle = "red";
//offscreenCtx.strokeRect(0,0,size,size);
return offscreenCanvas;
};