/
Sprite.hx
128 lines (105 loc) · 3.34 KB
/
Sprite.hx
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package kha2d;
import kha.Color;
import kha.graphics2.Graphics;
import kha.Image;
import kha.math.FastMatrix3;
import kha.math.Matrix3;
import kha.math.Vector2;
@:expose
class Sprite {
private var image: Image;
private var animation: Animation;
private var collider: Rectangle;
public var x: Float;
public var y: Float;
public var speedx: Float;
public var speedy: Float;
public var accx: Float;
public var accy: Float;
public var maxspeedy: Float;
public var collides: Bool;
public var z: Int;
public var removed: Bool = false;
public var angle: Float = 0.0;
public var originX: Float = 0.0;
public var originY: Float = 0.0;
public var scaleX: Float = 1;
public var scaleY: Float = 1;
public var visible: Bool = true;
var w: Float;
var h: Float;
var tempcollider: Rectangle;
public function new(image: Image, width: Int = 0, height: Int = 0, z: Int = 1) {
this.image = image;
x = 0;
y = 0;
h = height;
w = width;
if (this.width == 0 && image != null) this.width = image.width;
if (this.height == 0 && image != null) this.height = image.height;
this.z = z;
collider = new Rectangle(0, 0, this.width, this.height);
speedx = speedy = 0;
accx = 0;
accy = 0.2;
animation = Animation.create(0);
maxspeedy = 5.0;
collides = true;
tempcollider = new Rectangle(0, 0, 0, 0);
}
// change sprite x,y, width, height as collisionrect and add a image rect
public function collisionRect(): Rectangle {
tempcollider.x = x;
tempcollider.y = y;
tempcollider.width = collider.width * scaleX;
tempcollider.height = collider.height * scaleY;
return tempcollider;
}
public function setAnimation(animation: Animation): Void {
this.animation.take(animation);
}
public function update(): Void {
animation.next();
}
public function render(g: Graphics): Void {
if (image != null && visible) {
g.color = Color.White;
if (angle != 0) g.pushTransformation(g.transformation.multmat(FastMatrix3.translation(x + originX, y + originY)).multmat(FastMatrix3.rotation(angle)).multmat(FastMatrix3.translation(-x - originX, -y - originY)));
g.drawScaledSubImage(image, Std.int(animation.get() * w) % image.width, Math.floor(animation.get() * w / image.width) * h, w, h, Math.round(x - collider.x * scaleX), Math.round(y - collider.y * scaleY), width, height);
if (angle != 0) g.popTransformation();
}
#if debug_collisions
g.color = Color.fromBytes(255, 0, 0);
g.drawRect(x - collider.x * scaleX, y - collider.y * scaleY, width, height);
g.color = Color.fromBytes(0, 255, 0);
g.drawRect(tempcollider.x, tempcollider.y, tempcollider.width, tempcollider.height);
#end
}
public function hitFrom(dir: Direction): Void {
}
public function hit(sprite: Sprite): Void {
}
public function setImage(image: Image): Void {
this.image = image;
}
public function outOfView(): Void {
}
function get_width(): Float {
return w * scaleX;
}
function set_width(value: Float): Float {
return w = value;
}
public var width(get, set): Float;
function get_height(): Float {
return h * scaleY;
}
function set_height(value: Float): Float {
return h = value;
}
public var height(get, set): Float;
public function setPosition(pos : Vector2) {
x = pos.x;
y = pos.y;
}
}