1 |
|
package KITExcellence; |
2 |
|
import java.io.IOException; |
3 |
|
|
4 |
|
import javax.microedition.lcdui.Command; |
5 |
|
import javax.microedition.lcdui.CommandListener; |
6 |
|
import javax.microedition.lcdui.Display; |
7 |
|
import javax.microedition.lcdui.Displayable; |
8 |
|
import javax.microedition.lcdui.Graphics; |
9 |
|
import javax.microedition.midlet.MIDlet; |
10 |
|
import javax.microedition.midlet.MIDletStateChangeException; |
11 |
|
|
12 |
|
public class EliteSolutionsMIDLet extends MIDlet implements CommandListener { |
13 |
|
|
14 |
|
private GameEngine engine; |
15 |
|
private SpriteManager manager; |
16 |
|
private Display display; |
17 |
|
private MyViewB viewB; |
18 |
|
private Command exit; |
19 |
|
private Command pause; |
20 |
|
private Command resume; |
21 |
|
|
22 |
|
public EliteSolutionsMIDLet() throws IOException { |
23 |
|
this.display = Display.getDisplay(this); |
24 |
|
|
25 |
|
MyModel model = new MyModel(10, 100, 50, 50, 50); |
26 |
|
this.engine = new GameEngine(model, 50); |
27 |
|
manager = new SpriteManager(engine.getTimer()); |
28 |
|
try { |
29 |
|
this.viewB = new MyViewB(model, "tux"); |
30 |
|
this.engine.attach(viewB); |
31 |
|
} catch(Exception e) { |
32 |
|
System.out.println("Exception: " + e.getMessage()); |
33 |
|
e.printStackTrace(); |
34 |
|
} |
35 |
|
|
36 |
|
Displayable drawingArea = this.engine.getDrawingArea(); |
37 |
|
|
38 |
|
drawingArea.setCommandListener(this); |
39 |
|
|
40 |
|
this.resume = new Command("Resume", Command.SCREEN, 2); |
41 |
|
drawingArea.addCommand(this.resume); |
42 |
|
|
43 |
|
this.pause = new Command("Pause", Command.SCREEN, 1); |
44 |
|
drawingArea.addCommand(this.pause); |
45 |
|
|
46 |
|
this.exit = new Command("Exit", Command.EXIT, 1); |
47 |
|
drawingArea.addCommand(this.exit); |
48 |
|
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
protected void startApp() throws MIDletStateChangeException { |
54 |
|
this.display.setCurrent(this.engine.getDrawingArea()); |
55 |
|
System.err.println("startApp called"); |
56 |
|
this.engine.start(); |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
public void commandAction(Command c, Displayable d) { |
63 |
|
if (c == this.exit) { |
64 |
|
System.err.println("Exit..."); |
65 |
|
try { |
66 |
|
destroyApp(true); |
67 |
|
} catch (MIDletStateChangeException e) { |
68 |
|
return; |
69 |
|
} |
70 |
|
notifyDestroyed(); |
71 |
|
} else |
72 |
|
if (c == this.resume) { |
73 |
|
this.engine.resume(); |
74 |
|
} else |
75 |
|
if (c == this.pause) { |
76 |
|
this.engine.pause(); |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { |
84 |
|
System.err.println("destroyApp called"); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
protected void pauseApp() { |
91 |
|
System.err.println("pauseApp called"); |
92 |
|
} |
93 |
|
|
94 |
|
private class MyViewB implements GameView { |
95 |
|
|
96 |
|
private MyModel model; |
97 |
|
private Sprite sprite; |
98 |
|
|
99 |
1 |
public MyViewB(MyModel model, String spriteName) throws IOException { |
100 |
1 |
this.model = model; |
101 |
1 |
sprite = manager.createSprite(spriteName); |
102 |
1 |
} |
103 |
|
|
104 |
|
public void render(Graphics g) { |
105 |
221 |
sprite.draw(g, (int) model.x, (int) model.y); |
106 |
221 |
} |
107 |
|
} |
108 |
|
|
109 |
|
private class MyModel implements GameModel { |
110 |
|
|
111 |
|
private float speedx; |
112 |
|
private float speedy; |
113 |
|
|
114 |
|
float x, y; |
115 |
|
int w, h; |
116 |
|
|
117 |
|
public MyModel(int x, int y, int w, int h, int speed) { |
118 |
|
this.x = x; |
119 |
|
this.y = y; |
120 |
|
this.w = w; |
121 |
|
this.h = h; |
122 |
|
|
123 |
|
this.speedx = speed; |
124 |
|
this.speedy = 0; |
125 |
|
} |
126 |
|
|
127 |
|
public void update(int keys, int width, int height, float time_delta) { |
128 |
|
if (x <= 0 || x + w >= width) { |
129 |
|
speedx *= -1; |
130 |
|
} |
131 |
|
|
132 |
|
if (y <= 0 || y + h >= height) { |
133 |
|
speedy *= -1; |
134 |
|
} |
135 |
|
|
136 |
|
x += speedx * time_delta; |
137 |
|
y += speedy * time_delta; |
138 |
|
} |
139 |
|
} |
140 |
|
} |