forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.ts
95 lines (86 loc) · 2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
namespace microcode {
export class Sprite extends Component implements IPlaceable, ISizable {
private xfrm_: Affine
private image_: ImageG
private invisible_: boolean
//% blockCombine block="xfrm" callInDebugger
public get xfrm() {
return this.xfrm_
}
//% blockCombine block="width" callInDebugger
public get width() {
return this.image_.width
}
//% blockCombine block="height" callInDebugger
public get height() {
return this.image_.height
}
//% blockCombine block="image" callInDebugger
public get image(): ImageG {
return this.image_
}
public set image(img: ImageG) {
this.setImage(img)
}
//% blockCombine block="invisible" callInDebugger
public get invisible() {
return this.invisible_
}
public set invisible(b: boolean) {
this.invisible_ = b
}
//% blockCombine block="hitbox" callInDebugger
public get hitbox() {
return Bounds.FromSprite(this)
}
constructor(opts: { parent?: IPlaceable; img: ImageG }) {
super("sprite")
this.xfrm_ = new Affine()
this.xfrm_.parent = opts.parent && opts.parent.xfrm
this.image = opts.img
}
/* override */ destroy() {
this.image_ = undefined
super.destroy()
}
protected setImage(img: ImageG) {
this.image_ = img
}
public bindXfrm(xfrm: Affine) {
this.xfrm_ = xfrm
}
public occlusions(bounds: Bounds) {
return Occlusions.FromSprite(this, bounds)
}
public isOffScreen(): boolean {
return (
this.xfrm.worldPos.x + (this.width >> 1) < Screen.LEFT_EDGE ||
this.xfrm.worldPos.y + (this.height >> 1) < Screen.TOP_EDGE ||
this.xfrm.worldPos.x - (this.width >> 1) > Screen.RIGHT_EDGE ||
this.xfrm.worldPos.y - (this.height >> 1) > Screen.BOTTOM_EDGE
)
}
public isClipped(): boolean {
return (
this.xfrm.worldPos.x - (this.width >> 1) < Screen.LEFT_EDGE ||
this.xfrm.worldPos.y - (this.height >> 1) < Screen.TOP_EDGE ||
this.xfrm.worldPos.x + (this.width >> 1) > Screen.RIGHT_EDGE ||
this.xfrm.worldPos.y + (this.height >> 1) > Screen.BOTTOM_EDGE
)
}
/* override */ draw() {
if (this.invisible) {
return
}
if (this.isOffScreen()) {
return
}
Screen.drawTransparentImage(
this.image_,
Math.floor(this.xfrm.worldPos.x - (this.image_.width >> 1)),
Math.floor(this.xfrm.worldPos.y - (this.image_.height >> 1))
)
//this.hitbox.dbgRect(15);
}
}
}