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