1 | package jpacman.controller; |
2 | |
3 | import java.io.IOException; |
4 | |
5 | import jpacman.model.Engine; |
6 | |
7 | /** |
8 | * Top level Pacman class. The main method creates the engine, the GUI, and the |
9 | * controllers; the methods of the Pacman object created get invoked by the GUI |
10 | * and the controllers. |
11 | * |
12 | * @author Arie van Deursen; Aug 31, 2003 |
13 | * @version $Id: Pacman.java,v 1.6 2008/02/03 19:43:38 arie Exp $ |
14 | */ |
15 | public class Pacman { |
16 | |
17 | /** |
18 | * The model of the game. |
19 | */ |
20 | private Engine theEngine; |
21 | |
22 | /** |
23 | * The display of the game. |
24 | */ |
25 | private PacmanUI theViewer; |
26 | |
27 | /** |
28 | * A controller that moves monsters around. |
29 | */ |
30 | private IMonsterController monsterTicker; |
31 | |
32 | /** |
33 | * A controller triggering animations. |
34 | */ |
35 | private Animator theAnimator; |
36 | |
37 | /** |
38 | * Create a default new game, containing an egnine, a gui, and a monster |
39 | * driver. |
40 | * |
41 | * @throws IOException If underlying figures can't be found. |
42 | */ |
43 | public Pacman() throws IOException { |
44 | this(new Engine()); |
45 | assert invariant(); |
46 | } |
47 | |
48 | /** |
49 | * Create a new game around a given engine, using the default random |
50 | * monster mover. |
51 | * |
52 | * @param e |
53 | * The Engine to be used. |
54 | * @throws IOException |
55 | * If animations can't be found. |
56 | */ |
57 | public Pacman(Engine e) throws IOException { |
58 | this(e, new RandomMonsterMover(e)); |
59 | assert invariant(); |
60 | } |
61 | |
62 | /** |
63 | * Create a new game from a given engine and monster mover. |
64 | * |
65 | * @param e |
66 | * The Engine to be used, not null. |
67 | * @param m |
68 | * The monster mover to be used, not null. |
69 | * @throws IOException |
70 | * If images can't be found. |
71 | */ |
72 | public Pacman(Engine e, IMonsterController m) throws IOException { |
73 | assert e != null; |
74 | assert m != null; |
75 | theEngine = e; |
76 | monsterTicker = m; |
77 | theViewer = new PacmanUI(theEngine, this); |
78 | theAnimator = new Animator(theViewer.getBoardViewer()); |
79 | theViewer.display(); |
80 | assert invariant(); |
81 | } |
82 | |
83 | /** |
84 | * Instance variables that can't be null. |
85 | * @return True iff selected instance variables all aren't null. |
86 | */ |
87 | protected boolean invariant() { |
88 | return theEngine != null && monsterTicker != null && theViewer != null; |
89 | } |
90 | |
91 | /** |
92 | * Start the new game. |
93 | */ |
94 | public void start() { |
95 | assert invariant(); |
96 | theEngine.start(); |
97 | monsterTicker.start(); |
98 | theAnimator.start(); |
99 | assert invariant(); |
100 | } |
101 | |
102 | /** |
103 | * Halt the pacman game. |
104 | */ |
105 | public void quit() { |
106 | assert invariant(); |
107 | monsterTicker.stop(); |
108 | theEngine.quit(); |
109 | theAnimator.stop(); |
110 | assert invariant(); |
111 | } |
112 | |
113 | |
114 | /** |
115 | * Terminate the game. |
116 | */ |
117 | public void exit() { |
118 | assert invariant(); |
119 | quit(); |
120 | theViewer.dispose(); |
121 | // No need for a hard exit using, e.g., System.exit(0): |
122 | // we'd like to be able to run a series of pacman's in a single |
123 | // JUnit test suite. |
124 | assert invariant(); |
125 | } |
126 | |
127 | /** |
128 | * Respond to an up request from the GUI. |
129 | */ |
130 | public void up() { |
131 | assert invariant(); |
132 | theEngine.movePlayer(0, -1); |
133 | assert invariant(); |
134 | } |
135 | |
136 | /** |
137 | * Respond to a down request from the GUI. |
138 | */ |
139 | public void down() { |
140 | assert invariant(); |
141 | theEngine.movePlayer(0, 1); |
142 | assert invariant(); |
143 | } |
144 | |
145 | /** |
146 | * Respond to a left request from the GUI. |
147 | */ |
148 | public void left() { |
149 | assert invariant(); |
150 | theEngine.movePlayer(-1, 0); |
151 | assert invariant(); |
152 | } |
153 | |
154 | /** |
155 | * Respond to a right request from the GUI. |
156 | */ |
157 | public void right() { |
158 | assert invariant(); |
159 | theEngine.movePlayer(1, 0); |
160 | assert invariant(); |
161 | } |
162 | |
163 | /** |
164 | * @return the Engine of this pacman game |
165 | */ |
166 | public Engine getEngine() { |
167 | return theEngine; |
168 | } |
169 | |
170 | /** |
171 | * Start me up. |
172 | * |
173 | * @param args |
174 | * Are ignored. |
175 | * @throws IOException |
176 | * If images can't be found. |
177 | */ |
178 | public static void main(String[] args) throws IOException { |
179 | if (args.length > 0) { |
180 | System.err.println("Ignoring command line arguments."); |
181 | } |
182 | new Pacman(); |
183 | } |
184 | } |