1 |
|
package KITExcellence; |
2 |
|
|
3 |
|
import javax.microedition.lcdui.Graphics; |
4 |
|
|
5 |
|
import KITExcellence.SpriteData.Action; |
6 |
|
|
7 |
|
public class Sprite { |
8 |
|
private GameTimer timer; |
9 |
|
private SpriteData data; |
10 |
|
private Action currentAction; |
11 |
|
private float last_ticks; |
12 |
|
private float frame; |
13 |
1 |
private float speed = 1.0f; |
14 |
|
|
15 |
1 |
protected Sprite(SpriteData data, GameTimer timer) { |
16 |
1 |
this.timer = timer; |
17 |
1 |
this.data = data; |
18 |
1 |
last_ticks = timer.get(); |
19 |
1 |
currentAction = data.getDefaultAction(); |
20 |
1 |
} |
21 |
|
|
22 |
|
public void setAction(String name) { |
23 |
0 |
currentAction = data.getAction(name); |
24 |
0 |
} |
25 |
|
|
26 |
|
private void update() { |
27 |
221 |
float speed = this.speed * currentAction.getFps(); |
28 |
221 |
float frame_inc = speed * (timer.get() - last_ticks); |
29 |
221 |
last_ticks = timer.get(); |
30 |
|
|
31 |
221 |
frame += frame_inc; |
32 |
221 |
if (frame >= currentAction.getFrameCount()) { |
33 |
7 |
frame = frame_inc % currentAction.getFrameCount(); |
34 |
|
} |
35 |
221 |
} |
36 |
|
|
37 |
|
public void draw(Graphics g, int x, int y) { |
38 |
221 |
update(); |
39 |
221 |
currentAction.drawFrame(g, x, y, (int) frame); |
40 |
221 |
} |
41 |
|
} |