1 package erland.game.tileadventure;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import erland.game.GamePanelInterface;
22 import erland.game.FpsCounter;
23 import erland.game.GameEnvironmentInterface;
24 import erland.game.tileadventure.rect.RectBlockContainerData;
25 import erland.game.tileadventure.isodiamond.IsoDiamondBlockContainerData;
26
27 import java.awt.event.KeyListener;
28 import java.awt.event.KeyAdapter;
29 import java.awt.event.KeyEvent;
30 import java.awt.*;
31
32 /***
33 * This is the class that controls the drawing on the screen and
34 * receives the commands from the user
35 */
36 public class TileAdventureMain implements GamePanelInterface {
37 /*** Block container for the tile map */
38 protected BlockContainerData cont;
39 /*** Indicates if the game should be quit */
40 protected boolean bQuit;
41 /*** Key listener that listens on keyboard command */
42 protected KeyListener keyListener;
43 /*** FPS counter object */
44 protected FpsCounter fps;
45 /*** Game environment object */
46 protected GameEnvironmentInterface environment;
47 /*** Indicates if cheat mode is activated */
48 protected boolean cheatMode;
49
50 /*** TileAdventure model object that implements all game logic */
51 private TileAdventureModelInterface model;
52 /*** Indicates if the screen should be cleared next time */
53 private boolean bClearScreen = true;
54
55 /***
56 * Creates a TileAdventureMain object using the specified model
57 * @param model The model that implements the game logic
58 */
59 public TileAdventureMain(TileAdventureModelInterface model) {
60 this.model = model;
61 }
62 public boolean isExit()
63 {
64 return bQuit;
65 }
66
67 public void setCheatmode(boolean enable)
68 {
69 cheatMode = enable;
70 if(!enable) {
71 cheatMode = false;
72 }
73 }
74 public void exit()
75 {
76 environment.getScreenHandler().getContainer().removeKeyListener(keyListener);
77 }
78
79 public void init(GameEnvironmentInterface environment)
80 {
81 this.environment = environment;
82 bQuit = false;
83 cheatMode = false;
84
85 keyListener = new KeyAdapter() {
86 public void keyPressed(KeyEvent e) {
87 if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
88 bQuit = true;
89 }else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
90 model.startMoveLeft();
91 }else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
92 model.startMoveRight();
93 }else if(e.getKeyCode() == KeyEvent.VK_UP) {
94 model.startMoveUp();
95 }else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
96 model.startMoveDown();
97 }else if(e.getKeyCode() == KeyEvent.VK_F1) {
98 model.start();
99 }else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
100 model.jump();
101 }
102 if(cheatMode) {
103 if(e.getKeyCode()==KeyEvent.VK_1) {
104
105 }
106 }
107 }
108 public void keyReleased(KeyEvent e) {
109 if(e.getKeyCode() == KeyEvent.VK_LEFT) {
110 model.stopMoveLeft();
111 }else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
112 model.stopMoveRight();
113 }else if(e.getKeyCode() == KeyEvent.VK_UP) {
114 model.stopMoveUp();
115 }else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
116 model.stopMoveDown();
117 }
118 }
119 };
120 environment.getScreenHandler().setCursor(null);
121 environment.getScreenHandler().getContainer().requestFocus();
122 environment.getScreenHandler().getContainer().addKeyListener(keyListener);
123
124 fps = new FpsCounter(50);
125 cont = ((TileGameEnvironmentInterface)(environment.getCustomEnvironment())).createBlockContainer(20, 20, 10,10, 10);
126
127 model.init(environment,cont);
128
129 environment.getScreenHandler().getContainer().setBackground(Color.black);
130 }
131
132 private int getMinPosY() {
133 return cont.getPositionY(0,0,cont.getSizeZ());
134 }
135 private void updateScrollOffset(GameObject obj) {
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 }
170 public void update() {
171 if(model.isInitialized()) {
172 model.update();
173 updateScrollOffset(model.getPlayerObject());
174 }
175 }
176
177 public void draw()
178 {
179 Graphics g = environment.getScreenHandler().getCurrentGraphics();
180 if(bClearScreen) {
181 g.clearRect(0,0,environment.getScreenHandler().getWidth(),environment.getScreenHandler().getHeight());
182 }else {
183 g.clearRect(0,0,environment.getScreenHandler().getWidth(),cont.getOffsetY()-1);
184 }
185 fps.update();
186 fps.draw(g,Color.red, 10,10);
187 g.setColor(Color.white);
188 g.drawString(model.getInfoString(),50,10);
189 g.setColor(Color.white);
190 if(cheatMode) {
191 g.setColor(Color.white);
192 g.drawString("CHEATMODE",80,10);
193 }
194 g.setClip(cont.getOffsetX(),cont.getOffsetY(),cont.getDrawingSizeX(),cont.getDrawingSizeY());
195 if(model.isInitialized()) {
196 model.getMap().draw(g);
197 bClearScreen = true;
198 if(!model.isStarted()) {
199 bClearScreen = true;
200 g.setColor(Color.white);
201 g.clearRect(50,50,400,150);
202 g.drawString("Press space to start game",100,100);
203 if(model.isMultiplayer()) {
204 if(model.getNoOfHumanPlayers()>1) {
205 g.drawString("Currently "+(model.getNoOfHumanPlayers()-1)+" other human player(s) connected",100,120);
206 }else {
207 g.drawString("Only you and computer contolled cars are connected",100,120);
208 }
209 g.drawString("You can wait for more human players to connect",100,140);
210 }
211 }
212 }else {
213 g.setColor(Color.white);
214 g.clearRect(50,50,400,150);
215 g.drawString("Loading game data, please wait...",100,100);
216 }
217 }
218 }