1 |
|
package KITExcellence; |
2 |
|
import java.util.Enumeration; |
3 |
|
import java.util.Vector; |
4 |
|
|
5 |
|
import javax.microedition.lcdui.Displayable; |
6 |
|
import javax.microedition.lcdui.Graphics; |
7 |
|
import javax.microedition.lcdui.game.GameCanvas; |
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
public class GameEngine implements Runnable { |
13 |
|
|
14 |
|
private GameCanvasProxy canvas; |
15 |
|
private Graphics graphics; |
16 |
|
|
17 |
|
private GameModel models; |
18 |
|
private Vector views; |
19 |
|
private int fps; |
20 |
|
private GameTimer timer; |
21 |
|
|
22 |
|
private boolean paused; |
23 |
|
private boolean stoped; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
1 |
protected GameEngine(GameModel model, int fps) { |
34 |
1 |
this.canvas = new GameCanvasProxy(); |
35 |
1 |
this.views = new Vector(); |
36 |
1 |
this.timer = new GameTimer(); |
37 |
|
|
38 |
1 |
this.graphics = this.canvas.getGraphics(); |
39 |
1 |
this.models = model; |
40 |
1 |
this.stoped = true; |
41 |
1 |
this.fps = fps; |
42 |
1 |
} |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public Displayable getDrawingArea() { |
50 |
2 |
return this.canvas; |
51 |
|
} |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
public void start() { |
57 |
1 |
if (stoped) { |
58 |
1 |
Thread t = new Thread(this); |
59 |
1 |
t.start(); |
60 |
|
} |
61 |
1 |
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
public void stop() { |
67 |
0 |
if (!stoped) { |
68 |
0 |
this.stoped = true; |
69 |
|
} |
70 |
0 |
} |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
public void resume() { |
76 |
1 |
synchronized (this) { |
77 |
1 |
if (paused) { |
78 |
1 |
this.paused = false; |
79 |
|
} |
80 |
1 |
this.notifyAll(); |
81 |
1 |
} |
82 |
1 |
} |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
public void pause() { |
88 |
1 |
synchronized (this) { |
89 |
1 |
if (!paused) { |
90 |
1 |
this.paused = true; |
91 |
|
} |
92 |
1 |
} |
93 |
1 |
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
public void attach(GameView view) { |
102 |
1 |
if (!this.views.contains(view)) { |
103 |
1 |
this.views.addElement(view); |
104 |
|
} |
105 |
1 |
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
public void detach(GameView view) { |
114 |
0 |
this.views.removeElement(view); |
115 |
0 |
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
public void paint(Graphics g) { |
121 |
|
|
122 |
221 |
float time_delta = 1.0f / fps; |
123 |
221 |
int keys = this.canvas.getKeyStates(); |
124 |
|
|
125 |
221 |
timer.tick(time_delta); |
126 |
221 |
this.models.update(keys, this.canvas.getWidth(), this.canvas.getHeight(), |
127 |
|
time_delta); |
128 |
|
|
129 |
221 |
Enumeration enumeration = this.views.elements(); |
130 |
442 |
while (enumeration.hasMoreElements()) { |
131 |
221 |
GameView view = (GameView) enumeration.nextElement(); |
132 |
221 |
view.render(g); |
133 |
221 |
} |
134 |
221 |
} |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
public void run() { |
140 |
1 |
this.stoped = false; |
141 |
221 |
while (!stoped) { |
142 |
|
|
143 |
|
try { |
144 |
221 |
synchronized (this) { |
145 |
221 |
if (paused) { |
146 |
1 |
this.wait(); |
147 |
|
} |
148 |
221 |
} |
149 |
|
|
150 |
221 |
long interval = System.currentTimeMillis(); |
151 |
221 |
graphics.setColor(255, 255, 255); |
152 |
221 |
graphics.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); |
153 |
221 |
this.paint(this.graphics); |
154 |
221 |
this.canvas.flushGraphics(); |
155 |
221 |
interval = System.currentTimeMillis() - interval; |
156 |
221 |
interval = ((1000 / fps) - interval); |
157 |
221 |
Thread.sleep(interval > 0 ? interval : 0x00); |
158 |
0 |
} catch (InterruptedException e) { |
159 |
0 |
e.printStackTrace(); |
160 |
220 |
} |
161 |
|
} |
162 |
0 |
this.stoped = true; |
163 |
0 |
} |
164 |
|
|
165 |
|
public GameTimer getTimer() { |
166 |
1 |
return timer; |
167 |
|
} |
168 |
|
|
169 |
|
private class GameCanvasProxy extends GameCanvas { |
170 |
|
|
171 |
|
protected GameCanvasProxy() { |
172 |
|
super(true); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
public Graphics getGraphics() { |
179 |
|
return super.getGraphics(); |
180 |
|
} |
181 |
|
} |
182 |
|
|
183 |
|
} |