sadjfda
[rmh3093.git] / project2 / FrenzyController.java
blob14c23f6c0a2d19ca8f2f22fdb246bb91dbb996f7
1 /*
2 * FrenzyController.java
4 * Version:
5 * $Id: FrenzyController.java,v 1.2 2008/04/30 04:10:52 rmh3093 Exp $
7 * Revisions:
8 * $Log: FrenzyController.java,v $
9 * Revision 1.2 2008/04/30 04:10:52 rmh3093
10 * break out random number generator, first attempt at moving enemies
12 * Revision 1.1 2008/04/26 15:45:41 rmh3093
13 * Initial revision
18 Copyright (c) 2008, Ryan M. Hope
19 All rights reserved.
21 Redistribution and use in source and binary forms, with or without modification,
22 are permitted provided that the following conditions are met:
24 * Redistributions of source code must retain the above copyright notice,
25 this list of conditions and the following disclaimer.
26 * Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29 * Neither the name of the project nor the names of its contributors may be
30 used to endorse or promote products derived from this software without
31 specific prior written permission.
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
40 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 import java.awt.event.*;
47 /**
48 * Controls the Frenzy view and updates the model
50 * @author Ryan Hope
52 public class FrenzyController {
54 // Random number generator
55 private BetterRandom betterRandom;
57 private FrenzyModel f_model;
58 private FrenzyView f_view;
60 /**
61 * Create a new Frenzy Controller
63 * @param f_model the Frenzy game f_model
64 * @param f_view the Frenzy game f_view
66 FrenzyController(FrenzyModel f_model, FrenzyView f_view,
67 BetterRandom betterRandom) {
68 this.f_model = f_model;
69 this.f_view = f_view;
70 this.betterRandom = betterRandom;
72 // Add player to board in random position
73 f_model.player = new FrenzyPlayer(
74 betterRandom.rand.nextInt(f_model.boardsize),
75 betterRandom.rand.nextInt(f_model.boardsize));
77 // Add listeners to f_view
78 f_view.addQuitListener(new QuitListener());
79 f_view.addResetListener(new ResetListener());
80 f_view.addSpawnListener(new SpawnListener());
81 f_view.addPauseListener(new PauseListener());
82 f_view.addPlayerControlsListener(new PlayerControlsListener());
83 f_view.addEnemySpawnListener(new SpawnListener());
87 private synchronized void spawnEnemy() {
88 if (f_model.gamestate) {
89 // Can't spawn new enemies if there are no unique ASCII characters
90 if (f_model.enemyCount < FrenzyModel.MAX_ENIMES) {
92 while (true) {
94 int num = betterRandom.rand.nextInt(255);
96 // These characters don't print
97 if (num < 33 || ((num > 126) && (num < 161)) || num == 173)
98 continue;
100 // Can't use players symbol
101 if (String.valueOf((char)num) == FrenzyPlayer.symbol)
102 continue;
104 // The symbol for the new enemy
105 String symbol = String.valueOf((char)num);
107 // Check to make sure the symbol is already in use
108 if (f_model.enimies.containsKey(symbol)) continue;
110 // Find a random unoccupied location for the enemy to spawn
111 int x = -1;
112 int y = -1;
113 while (true) {
114 x = betterRandom.rand.nextInt(f_model.getBoardSize());
115 y = betterRandom.rand.nextInt(f_model.getBoardSize());
116 if (f_view.boardSquares[y][x].isOccupied() ==
117 FrenzyModel.OCCUPANT.NONE) {
118 break;
122 int move_x = betterRandom.rand.nextInt(2);
123 int move_y = betterRandom.rand.nextInt(2);
124 FrenzyEnemy.MOVEMENT_PATTERN mp = null;
125 // Spawn the new enemy
126 if ((move_x == 1) && (move_y == 1)) {
127 mp = FrenzyEnemy.MOVEMENT_PATTERN.NE;
128 } else if ((move_x == 1) && (move_y == 0)) {
129 mp = FrenzyEnemy.MOVEMENT_PATTERN.SE;
130 } else if ((move_x == 0) && (move_y == 1)) {
131 mp = FrenzyEnemy.MOVEMENT_PATTERN.NW;
132 } else if ((move_x == 0) && (move_y == 0)) {
133 mp = FrenzyEnemy.MOVEMENT_PATTERN.SW;
135 FrenzyEnemy enemy =
136 new FrenzyEnemy(f_model, x, y, symbol, mp,
137 new EnemyMover());
138 f_model.enimies.put(symbol, enemy);
139 f_view.addOccupant(enemy, f_view.boardSquares[y][x]);
140 f_model.increaseEnemyCount();
141 enemy.moverThread.start();
142 break;
150 * Handles moving the Frenzy player around the game board
152 * @author Ryan Hope
154 class PlayerControlsListener implements KeyListener {
156 public void keyPressed(KeyEvent e) {
159 public void keyTyped(KeyEvent e) {
162 // Only perform an action on the release of the key
163 public void keyReleased(KeyEvent e) {
165 if (!f_view.pauseControl.isSelected()) {
167 int keycode = e.getKeyCode();
168 int x_new = -1;
169 int y_new = -1;
170 int x_old = f_model.player.getXposition();
171 int y_old = f_model.player.getYposition();
172 if ((keycode > 36 && keycode < 41) && f_model.gamestate) {
174 // Remove player from current location
175 f_view.removeOccupant(f_view.boardSquares[y_old][x_old]);
177 switch (keycode) {
179 // Move left
180 case 37:
181 if (x_old == 0) {
182 x_new = f_model.boardsize-1;
183 } else {
184 x_new = x_old-1;
186 y_new = y_old;
187 f_model.player.setXposition(x_new);
188 break;
190 // Move up
191 case 38:
192 if (y_old == 0) {
193 y_new = f_model.boardsize-1;
194 } else {
195 y_new = y_old-1;
197 x_new = x_old;
198 f_model.player.setYposition(y_new);
199 break;
201 // Move right
202 case 39:
203 if (x_old == f_model.boardsize-1) {
204 x_new = 0;
205 } else {
206 x_new = x_old+1;
208 y_new = y_old;
209 f_model.player.setXposition(x_new);
210 break;
212 // Move down
213 case 40:
214 if (y_old == f_model.boardsize-1) {
215 y_new = 0;
216 } else {
217 y_new = y_old+1;
219 x_new = x_old;
220 f_model.player.setYposition(y_new);
221 break;
224 // If new location is occupied by an enemy, eat it
225 if (f_view.boardSquares[y_new][x_new].isOccupied() ==
226 FrenzyModel.OCCUPANT.ENEMY) {
228 // Eat enemy
229 String key =
230 f_view.boardSquares[y_new][x_new].getText();
231 f_model.player.eatEnemy();
232 f_model.decreaseEnemyCount();
233 if (f_model.player.getConsumedCount() > 0 &&
234 f_model.enemyCount==0) {
235 f_model.gamestate = false;
236 f_view.statusLabel.setText("Game over.");
239 // Remove key from list so that the symbol can be reused
240 f_model.enimies.remove(key);
242 // ====================================================
243 // These should be handled by events
245 // Update the last action display
246 f_view.lastActionLabel.setText(
247 f_model.player.getSymbol() + " ate " + key);
249 // Update the consumed enemy count display
250 int count = f_model.player.getConsumedCount();
251 f_view.consumedLabel.setText(String.valueOf(count));
253 // ====================================================
256 // Add player to new square
257 f_view.addOccupant(f_model.player,
258 f_view.boardSquares[y_new][x_new]);
265 * Handles quitting Frenzy
267 * @author Ryan Hope
269 class QuitListener implements ActionListener {
270 public void actionPerformed(ActionEvent e) {
271 f_view.dispose();
272 System.exit(0);
277 * Handles resetting Frenzy
279 * @author Ryan Hope
281 class ResetListener implements ActionListener {
282 public void actionPerformed(ActionEvent e) {
283 f_model.gamestate = true;
284 f_view.statusLabel.setText("Game Running!");
285 f_view.pauseControl.setSelected(false);
286 f_model.player.setConsumedCount(0);
287 f_view.consumedLabel.setText("0");
292 * Handles resetting Frenzy
294 * @author Ryan Hope
296 class PauseListener implements ActionListener {
297 public void actionPerformed(ActionEvent e) {
298 if (f_view.pauseControl.isSelected()) {
299 f_model.gamestate = false;
300 f_view.statusLabel.setText("Game Paused!");
301 } else {
302 f_model.gamestate = true;
303 f_view.statusLabel.setText("Game Running!");
309 * Handles spawning of new enemies
311 * @author Ryan Hope
313 class SpawnListener implements ActionListener {
315 public void actionPerformed(ActionEvent e) {
316 if (f_model.gamestate) spawnEnemy();
322 * Handles moving enemies
324 * @author Ryan Hope
326 class EnemyMover {
328 public void move(String symbol) {
329 FrenzyEnemy enemy = f_model.enimies.get(symbol);
330 FrenzyEnemy eaten = null;
331 if (enemy != null) {
332 int x = enemy.getXposition();
333 int y = enemy.getYposition();
334 FrenzyModel.OCCUPANT occupant_t =
335 f_view.boardSquares[y][x].isOccupied();
336 if (occupant_t != FrenzyModel.OCCUPANT.NONE) {
337 String occupant = f_view.boardSquares[y][x].getText();
338 if (occupant_t == FrenzyModel.OCCUPANT.PLAYER) {
339 f_model.gamestate = false;
340 f_model.player.died = true;
341 f_view.statusLabel.setText("Game over.");
343 eaten = f_model.enimies.get(occupant);
344 eaten.move = false;
345 f_view.lastActionLabel.setText(
346 symbol + " ate " + occupant);
348 while (eaten != null && eaten.moverThread.isAlive()) {
349 Thread.yield();
351 f_view.removeOccupant(
352 f_view.boardSquares[enemy.Yposition_old][enemy.Xposition_old]);
353 f_view.addOccupant(enemy, f_view.boardSquares[y][x]);