/
GameEntity.dart
50 lines (43 loc) · 1.31 KB
/
GameEntity.dart
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
44
45
46
47
48
49
50
class GameEntity {
Game game;
num x;
num y;
bool removeFromWorld = false;
var sprite = null;
num radius;
GameEntity(Game this.game);
GameEntity.withPosition(Game this.game, num this.x, num this.y);
abstract void update();
void draw(ctx) {
if (game.showOutlines) {
ctx.beginPath();
ctx.strokeStyle = "green";
ctx.arc(x, y, radius, 0, Math.PI*2, false);
ctx.stroke();
ctx.closePath();
}
}
void drawSpriteCentered(ctx) {
num x = x - sprite.width/2;
num y = y - sprite.height/2;
ctx.drawImage(sprite, x, y);
}
bool outsideScreen() {
return (x > game.halfSurfaceWidth || x < -(game.halfSurfaceWidth) ||
y > game.halfSurfaceHeight || y < -(game.halfSurfaceHeight));
}
void rotateAndCache(offscreenCanvas, image, angle) {
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 + Math.PI/2);
offscreenCtx.translate(0,0);
offscreenCtx.drawImage(image, -(image.width/2), -(image.height/2));
offscreenCtx.restore();
//offscreenCtx.strokeStyle = "red";
//offscreenCtx.strokeRect(0,0,size,size);
}
}