2 * FrenzyController.java
5 * $Id: FrenzyController.java,v 1.2 2008/04/30 04:10:52 rmh3093 Exp $
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
18 Copyright (c) 2008, Ryan M. Hope
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
.*;
48 * Controls the Frenzy view and updates the model
52 public class FrenzyController
{
54 // Random number generator
55 private BetterRandom betterRandom
;
57 private FrenzyModel f_model
;
58 private FrenzyView f_view
;
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
;
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
) {
94 int num
= betterRandom
.rand
.nextInt(255);
96 // These characters don't print
97 if (num
< 33 || ((num
> 126) && (num
< 161)) || num
== 173)
100 // Can't use players symbol
101 if (String
.valueOf((char)num
) == FrenzyPlayer
.symbol
)
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
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
) {
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
;
136 new FrenzyEnemy(f_model
, x
, y
, symbol
, mp
,
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();
150 * Handles moving the Frenzy player around the game board
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();
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
]);
182 x_new
= f_model
.boardsize
-1;
187 f_model
.player
.setXposition(x_new
);
193 y_new
= f_model
.boardsize
-1;
198 f_model
.player
.setYposition(y_new
);
203 if (x_old
== f_model
.boardsize
-1) {
209 f_model
.player
.setXposition(x_new
);
214 if (y_old
== f_model
.boardsize
-1) {
220 f_model
.player
.setYposition(y_new
);
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
) {
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
269 class QuitListener
implements ActionListener
{
270 public void actionPerformed(ActionEvent e
) {
277 * Handles resetting Frenzy
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
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!");
302 f_model
.gamestate
= true;
303 f_view
.statusLabel
.setText("Game Running!");
309 * Handles spawning of new enemies
313 class SpawnListener
implements ActionListener
{
315 public void actionPerformed(ActionEvent e
) {
316 if (f_model
.gamestate
) spawnEnemy();
322 * Handles moving enemies
328 public void move(String symbol
) {
329 FrenzyEnemy enemy
= f_model
.enimies
.get(symbol
);
330 FrenzyEnemy eaten
= 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
);
345 f_view
.lastActionLabel
.setText(
346 symbol
+ " ate " + occupant
);
348 while (eaten
!= null && eaten
.moverThread
.isAlive()) {
351 f_view
.removeOccupant(
352 f_view
.boardSquares
[enemy
.Yposition_old
][enemy
.Xposition_old
]);
353 f_view
.addOccupant(enemy
, f_view
.boardSquares
[y
][x
]);