-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScreenObject.dart
36 lines (28 loc) · 970 Bytes
/
ScreenObject.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
/**
*
* Superclass describing a screen object holding convenience methods for drawing and clearing itself.
*
*/
class ScreenObject {
CanvasRenderingContext2D context;
int x, y, size;
ImageElement image;
ScreenObject(this.context, this.x, this.y, this.size, this.image);
/* Make the rocket appear in the middle (x-axis) of the object */
int get rocketCenterX() => x + (size/2).toInt() - (Rocket.SIZE/2).toInt();
/* Make the rocket appear at the top of the object */
int get rocketTop() => y - Rocket.SIZE;
/* Make the rocket appear at the bottom of the object */
int get rocketBottom() => y + size + Rocket.SIZE;
/* Check collision between a rocket and the object */
bool checkCollision(Rocket r) => ((r.x >= x && r.x < x + size)
&& (r.y >= y && r.y < y + size));
void draw() {
context.drawImage(image, x, y, size, size);
}
void updatePosition(int dx, int dy) {
x += dx;
y += dy;
draw();
}
}