1 package se
.umu
.cs
.dit06ajnajs
;
3 import java
.applet
.Applet
;
4 import java
.applet
.AudioClip
;
5 import java
.awt
.Dimension
;
8 import java
.io
.IOException
;
9 import java
.net
.MalformedURLException
;
11 import java
.net
.URISyntaxException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
17 import java
.util
.logging
.Logger
;
19 import se
.umu
.cs
.dit06ajnajs
.agent
.AgentPrototypeFactory
;
20 import se
.umu
.cs
.dit06ajnajs
.agent
.Tower
;
21 import se
.umu
.cs
.dit06ajnajs
.level
.GoalSquare
;
22 import se
.umu
.cs
.dit06ajnajs
.level
.Level
;
23 import se
.umu
.cs
.dit06ajnajs
.level
.StartSquare
;
24 import se
.umu
.cs
.dit06ajnajs
.agent
.Unit
;
25 import java
.util
.Collection
;
27 public class ATDModel
{
28 private static Logger logger
= Logger
.getLogger("AntiTD");
30 private Player player
;
31 private List
<Unit
> units
;
32 private List
<Tower
> towers
;
33 private List
<Level
> levels
;
34 private Level currentLevel
;
35 private Map
<String
, AudioClip
> sounds
;
36 //private Map<String, File> tracks;
37 private List
<File
> tracks
;
38 private int currentTrack
;
41 public ATDModel(List
<Level
> levels
) {
43 this.currentLevel
= levels
.get(0);
45 // Load sounds for model
47 URL soundsURL
= getClass().getResource("/resources/sounds/");
48 sounds
= new HashMap
<String
, AudioClip
>();
49 AudioClip sound
= Applet
.newAudioClip(new URL(soundsURL
, "goal.wav"));
50 sounds
.put("goal", sound
);
51 sound
= Applet
.newAudioClip(new URL(soundsURL
, "victory.wav"));
52 sounds
.put("victory", sound
);
55 // tracks = new HashMap<String, File>();
56 // File trackFile = new File(new URL(soundsURL, "track1.mid").toURI());
57 // tracks.put("track1", trackFile);
58 // trackFile = new File(new URL(soundsURL, "track2.mid").toURI());
59 // tracks.put("track2", trackFile);
60 // trackFile = new File(new URL(soundsURL, "track3.mid").toURI());
61 // tracks.put("track3", trackFile);
62 // trackFile = new File(new URL(soundsURL, "track4.mid").toURI());
63 // tracks.put("track4", trackFile);
66 tracks
= new ArrayList
<File
>();
68 File trackFile
= new File(new URL(soundsURL
, "track1.mid").toURI());
69 tracks
.add(trackFile
);
70 trackFile
= new File(new URL(soundsURL
, "track2.mid").toURI());
71 tracks
.add(trackFile
);
72 trackFile
= new File(new URL(soundsURL
, "track3.mid").toURI());
73 tracks
.add(trackFile
);
74 trackFile
= new File(new URL(soundsURL
, "track4.mid").toURI());
75 tracks
.add(trackFile
);
77 } catch (IOException e
) {
78 System
.err
.println("Couldn't find sound. Exception: "
80 } catch (URISyntaxException e
) {
81 System
.err
.println("Couldn't find midi. Exception: "
88 * Creates a new player and initialises the first level.
90 public void newGame() {
91 this.player
= new Player();
93 // TODO: ...reset stuff
97 * Restart the current level
99 public void restartLevel() {
100 player
.loadSavedScore();
104 public boolean advanceLevel() {
106 if (player
.getCurrentLevel() + 1 < levels
.size()) {
107 player
.setCurrentLevel(player
.getCurrentLevel() + 1);
111 // game won, ask for highscore entry
117 * Initialises the level that is next in turn for the player
119 private void initLevel() {
120 // Check if there is more levels
121 if (player
.getCurrentLevel() < levels
.size()) {
122 this.currentLevel
= levels
.get(player
.getCurrentLevel());
123 // If restartet game, the level must be reset
124 this.currentLevel
.resetLevel();
125 this.towers
= new ArrayList
<Tower
>();
126 this.units
= new ArrayList
<Unit
>();
128 for (Tower tower
: currentLevel
.getTowers()) {
133 //TODO vad händer om det är slut på banor?
137 public void addTower(Tower t
) {
138 currentLevel
.addTower(t
);
142 public boolean addUnit(Unit unit
) {
143 // TODO Auto-generated method stub
144 if (player
.getCredit() >= unit
.getCost()) {
145 this.player
.removeCredit(unit
.getCost());
146 StartSquare startSquare
= this.currentLevel
.getActiveStartSquare();
147 unit
.setMap(this.currentLevel
);
148 startSquare
.addUnit(unit
);
155 public void releaseUnit(Unit unit
) {
159 public List
<Unit
> getUnits() {
160 // TODO Auto-generated method stub
162 List
<Unit
> resultList
= new ArrayList
<Unit
>(this.units
.size());
163 resultList
.addAll(this.units
);
167 public List
<Tower
> getTowers() {
169 List
<Tower
> resultList
= new ArrayList
<Tower
>(this.towers
.size());
170 resultList
.addAll(this.towers
);
175 * Removes all supplied units from this model. This is called when dead
176 * units are cleared from a game.
178 * @param units A collection of units to remove.
180 public void removeUnits(Collection
<Unit
> units
) {
181 this.units
.removeAll(units
);
185 * Returns the current level.
187 * @return The current level.
189 public Level
getCurrentLevel() {
190 return this.currentLevel
;
193 public Image
getMapImage() {
194 return currentLevel
.getMapImage();
197 public Dimension
getMapDimension() {
198 return currentLevel
.getDimension();
201 public GoalSquare
[] getGoalSquares() {
202 return this.currentLevel
.getGoalSquares();
205 public StartSquare
[] getStartSquares() {
206 return this.currentLevel
.getStartSquares();
210 * Adds credit to the player
211 * @param credit The credit to be added
213 public void addCredit(int credit
) {
214 player
.addCredit(credit
);
217 public int getCredit() {
218 return player
.getCredit();
221 public int getScore() {
222 return player
.getScore();
225 public int getSavedScore() {
226 return this.player
.getSavedScore();
229 public void saveScore() {
233 public int getNumOfClearedUnits() {
234 return currentLevel
.getNumOfClearedUnits();
237 public int getUnitsToWin() {
238 return currentLevel
.getUnitsToWin();
242 * Adds to the number of units that have reached the goalsquare
243 * @param num Number of units that have reached the goalsquare
246 public void addGoalUnit(int num
) {
247 currentLevel
.addClearedUnits(num
);
251 * Checks if the goal for the current level is reached
252 * @return True if the goal is reached
254 public boolean isLevelComplete() {
255 return currentLevel
.getNumOfClearedUnits()
256 >= currentLevel
.getUnitsToWin();
260 * Checks if the level is lost
261 * @return True if no units on the map and no credits to buy one unit
263 public boolean isLevelLost() {
264 AgentPrototypeFactory factory
= AgentPrototypeFactory
.getInstance();
265 int lowestCost
= factory
.getLowestCost();
266 return (player
.getCredit() < lowestCost
&& units
.size() == 0);
270 * Returns the sound matching the String
271 * @param s The key for the sound
272 * @return The sound as AudioClip
274 public AudioClip
getSound(String s
) {
275 return sounds
.get(s
);
280 * Returns the track matching the String
281 * @param s The key for the sound
282 * @return The sound as File
284 public File
getNextTrack() {
285 int nextIndex
= (currentTrack
+ 1) % tracks
.size();
286 currentTrack
= nextIndex
;
287 // TODO, verify. Should get next track from list.
288 return tracks
.get(currentTrack
);