1 |
|
package KITExcellence; |
2 |
|
|
3 |
|
import java.io.IOException; |
4 |
|
import java.io.InputStream; |
5 |
|
import java.util.Hashtable; |
6 |
|
import java.util.Vector; |
7 |
|
|
8 |
|
import javax.microedition.lcdui.Graphics; |
9 |
|
import javax.microedition.lcdui.Image; |
10 |
|
|
11 |
|
public class SpriteData { |
12 |
|
private Hashtable actions = new Hashtable(); |
13 |
|
|
14 |
|
private static class Frame { |
15 |
|
public Image image; |
16 |
|
|
17 |
|
} |
18 |
|
|
19 |
|
public Action getDefaultAction() { |
20 |
|
return (Action) actions.elements().nextElement(); |
21 |
|
} |
22 |
|
|
23 |
|
public Action getAction(String name) { |
24 |
|
Action action = (Action) actions.get(name); |
25 |
|
if (action == null) |
26 |
|
throw new RuntimeException("Action " + name + " not found"); |
27 |
|
return action; |
28 |
|
} |
29 |
|
|
30 |
|
protected static class Action { |
31 |
|
|
32 |
|
public Action(String name) { |
33 |
|
this.name = name; |
34 |
|
this.fps = 10; |
35 |
|
} |
36 |
|
|
37 |
|
public void addFrame(Image image) { |
38 |
|
Frame frame = new Frame(); |
39 |
|
frame.image = image; |
40 |
|
frames.addElement(frame); |
41 |
|
} |
42 |
|
|
43 |
|
public void addFrame(String imageName) throws IOException { |
44 |
|
InputStream input = Action.class.getResourceAsStream(imageName); |
45 |
|
if (input == null) { |
46 |
|
throw new IOException("File " + input + " not found"); |
47 |
|
} |
48 |
|
Image image = Image.createImage(input); |
49 |
|
addFrame(image); |
50 |
|
} |
51 |
|
|
52 |
|
public String getName() { |
53 |
|
return name; |
54 |
|
} |
55 |
|
|
56 |
|
public int getFrameCount() { |
57 |
|
return frames.size(); |
58 |
|
} |
59 |
|
|
60 |
|
public float getFps() { |
61 |
|
return fps; |
62 |
|
} |
63 |
|
|
64 |
|
public void drawFrame(Graphics g, int x, int y, int num) { |
65 |
|
Frame frame = (Frame) frames.elementAt(num); |
66 |
|
g.drawImage(frame.image, x, y, Graphics.TOP | Graphics.LEFT); |
67 |
|
} |
68 |
|
|
69 |
|
private String name; |
70 |
|
private Vector frames = new Vector(); |
71 |
|
private float fps; |
72 |
|
} |
73 |
|
|
74 |
|
public void testCreateTux() throws IOException { |
75 |
|
|
76 |
|
Action walkright = new Action("walk-right"); |
77 |
|
String base = "/tux/"; |
78 |
|
walkright.addFrame(base + "largetux-walk-right-0.png"); |
79 |
|
walkright.addFrame(base + "largetux-walk-right-1.png"); |
80 |
|
walkright.addFrame(base + "largetux-walk-right-2.png"); |
81 |
|
walkright.addFrame(base + "largetux-walk-right-3.png"); |
82 |
|
walkright.addFrame(base + "largetux-walk-right-4.png"); |
83 |
|
walkright.addFrame(base + "largetux-walk-right-5.png"); |
84 |
|
actions.put(walkright.getName(), walkright); |
85 |
|
} |
86 |
|
|
87 |
|
public void testCreateSnowball() throws IOException { |
88 |
|
Action roll = new Action("roll"); |
89 |
|
String base = "/snowball/"; |
90 |
|
roll.addFrame(base + "left-0.png"); |
91 |
|
roll.addFrame(base + "left-1.png"); |
92 |
|
roll.addFrame(base + "left-2.png"); |
93 |
|
roll.addFrame(base + "left-3.png"); |
94 |
|
roll.addFrame(base + "left-4.png"); |
95 |
|
roll.addFrame(base + "left-5.png"); |
96 |
|
actions.put(roll.getName(), roll); |
97 |
|
} |
98 |
|
} |