-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.ts
70 lines (60 loc) · 1.98 KB
/
Sprite.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Screen from './Screen';
import SceneObject from './SceneObject';
import Image from './Image';
import Animation from './Animation';
import Rect from './Rect';
export default class Sprite extends SceneObject {
private image: Image;
private animations: Animation[] = [];
private curAnimation: number = 0;
private rect: Rect = {x: 0, y: 0, width: 0, height: 0};
constructor(image: Image) {
super(0, 0);
this.image = image;
}
public onUpdate = (dt: number): boolean => {
if (this.animations.length > 0) {
return this.animations[this.curAnimation].onUpdate(dt);
}
return false;
}
public onDraw = (screen: Screen): void => {
if (this.animations.length > 0) {
const currentFrame = this.animations[this.curAnimation].getCurrentFrame();
this.setWidth(currentFrame.getWidth());
this.setHeight(currentFrame.getHeight());
screen.drawImage(
this,
this.image,
currentFrame.getX(), currentFrame.getY(),
currentFrame.getWidth(), currentFrame.getHeight(),
0, 0);
} else {
screen.drawImage(
this,
this.image,
this.rect.x, this.rect.y,
this.rect.width, this.rect.height,
0, 0);
}
}
public setRect(x: number, y: number, width: number, height: number): void {
this.rect.x = x;
this.rect.y = y;
this.rect.width = width;
this.rect.height = height;
}
public addAnimation(anim: Animation): void {
this.animations.push(anim);
}
public setAnimation(anim: number | string): void {
if (typeof anim === 'number') {
this.curAnimation = anim;
} else if (typeof anim === 'string') {
this.curAnimation = 0;
}
}
public getCurrentAnimation(): number {
return this.curAnimation;
}
}