-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.ts
84 lines (66 loc) · 2.3 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
71
72
73
74
75
76
77
78
79
80
81
82
83
abstract class Sprite {
public rotation: number = 0
public renderPivot: Point = { x:0, y:0 }
public isLoaded: boolean;
public flipped: boolean = false;
public alpha: number = 1.0
public drawOrder: number;
public image: CanvasImageSource;
public abstract draw(ctx: CanvasRenderingContext2D): void
}
class SimpleSprite extends Sprite {
public constructor(src: ImageResource) {
super()
this.image = new Image()
this.image.src = src
this.image.onload = () => {
this.isLoaded = true
}
this.image.onerror = () => {
console.error("Could not load image " + src)
}
}
public draw(ctx: CanvasRenderingContext2D) {
ctx.drawImage(this.image, 0 , 0 )
}
}
class AnimatedSprite extends Sprite {
private framesize: Point;
private currentFrame = 0;
private frames: Point[] = new Array();
public constructor(src: ImageResource, framesize:Point) {
super()
this.framesize = framesize
this.image = new Image()
this.image.src = src
this.image.onload = () => {
this.isLoaded = true
// generate the frames
let x = 0
let y = 0
while( x + framesize.x <= this.image.width &&
y + framesize.y <= this.image.height ) {
this.frames.push( { x: x, y: y } )
x += framesize.x
if (x + framesize.x > this.image.width) {
x = 0
y += framesize.y
}
}
}
this.image.onerror = () => {
console.error("Could not load image " + src)
}
}
public setProgress(time: number) {
this.currentFrame = Math.floor(this.frames.length * time)
}
public draw(ctx: CanvasRenderingContext2D) {
if (this.currentFrame < 0 ) this.currentFrame = 0
if (this.currentFrame >= this.frames.length ) this.currentFrame = this.frames.length - 1
let frame = this.frames[this.currentFrame];
ctx.drawImage( this.image,
frame.x, frame.y, this.framesize.x, this.framesize.y, // srcrect
0, 0, this.framesize.x, this.framesize.y ) // dstrect
}
}