merge
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / ATDModel.java
blob2d529e0ff08693f980e842ad0226fe3d5e4b5a95
1 package se.umu.cs.dit06ajnajs;
3 import java.applet.Applet;
4 import java.applet.AudioClip;
5 import java.awt.Dimension;
6 import java.awt.Image;
7 import java.io.File;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.net.URI;
11 import java.net.URISyntaxException;
12 import java.net.URL;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
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;
39 public ATDModel(List<Level> levels) {
40 this.levels = levels;
41 this.currentLevel = levels.get(0);
43 // Load sounds for model
44 try {
45 URL soundsURL = getClass().getResource("/resources/sounds/");
46 sounds = new HashMap<String, AudioClip>();
47 AudioClip sound = Applet.newAudioClip(new URL(soundsURL, "goal.wav"));
48 sounds.put("goal", sound);
49 sound = Applet.newAudioClip(new URL(soundsURL, "victory.wav"));
50 sounds.put("victory", sound);
52 // Music tracks
53 tracks = new HashMap<String, File>();
54 File trackFile = new File(new URL(soundsURL, "track1.mid").toURI());
55 tracks.put("track1", trackFile);
56 trackFile = new File(new URL(soundsURL, "track2.mid").toURI());
57 tracks.put("track2", trackFile);
58 trackFile = new File(new URL(soundsURL, "track3.mid").toURI());
59 tracks.put("track3", trackFile);
60 trackFile = new File(new URL(soundsURL, "track4.mid").toURI());
61 tracks.put("track4", trackFile);
62 } catch (IOException e) {
63 System.err.println("Couldn't find sound. Exception: "
64 + e.getMessage());
65 } catch (URISyntaxException e) {
66 System.err.println("Couldn't find midi. Exception: "
67 + e.getMessage());
72 /**
73 * Creates a new player and initialises the first level.
75 public void newGame() {
76 this.player = new Player();
77 initLevel();
78 // TODO: ...reset stuff
81 /**
82 * Restart the current level
84 public void restartLevel() {
85 player.loadSavedScore();
86 initLevel();
89 public boolean advanceLevel() {
90 player.saveScore();
91 if (player.getCurrentLevel() + 1 < levels.size()) {
92 player.setCurrentLevel(player.getCurrentLevel() + 1);
93 initLevel();
94 return true;
95 } else {
96 // game won, ask for highscore entry
97 return false;
102 * Initialises the level that is next in turn for the player
104 private void initLevel() {
105 // Check if there is more levels
106 if (player.getCurrentLevel() < levels.size()) {
107 this.currentLevel = levels.get(player.getCurrentLevel());
108 // If restartet game, the level must be reset
109 this.currentLevel.resetLevel();
110 this.towers = new ArrayList<Tower>();
111 this.units = new ArrayList<Unit>();
113 for (Tower tower : currentLevel.getTowers()) {
114 towers.add(tower);
116 player.initCredit();
117 } else {
118 //TODO vad händer om det är slut på banor?
122 public void addTower(Tower t) {
123 currentLevel.addTower(t);
124 towers.add(t);
127 public boolean addUnit(Unit unit) {
128 // TODO Auto-generated method stub
129 if (player.getCredit() >= unit.getCost()) {
130 this.player.removeCredit(unit.getCost());
131 StartSquare startSquare = this.currentLevel.getActiveStartSquare();
132 unit.setLevel(this.currentLevel);
133 startSquare.addUnit(unit);
134 return true;
135 } else {
136 return false;
140 public void releaseUnit(Unit unit) {
141 units.add(unit);
144 public List<Unit> getUnits() {
145 // TODO Auto-generated method stub
146 // Copy array
147 List<Unit> resultList = new ArrayList<Unit>(this.units.size());
148 resultList.addAll(this.units);
149 return resultList;
152 public List<Tower> getTowers() {
153 // Copy array
154 List<Tower> resultList = new ArrayList<Tower>(this.towers.size());
155 resultList.addAll(this.towers);
156 return resultList;
160 * Removes all supplied units from this model. This is called when dead
161 * units are cleared from a game.
163 * @param units A collection of units to remove.
165 public void removeUnits(Collection<Unit> units) {
166 this.units.removeAll(units);
170 * Returns the current level.
172 * @return The current level.
174 public Level getCurrentLevel() {
175 return this.currentLevel;
178 public Image getMapImage() {
179 return currentLevel.getMapImage();
182 public Dimension getMapDimension() {
183 return currentLevel.getDimension();
186 public GoalSquare[] getGoalSquares() {
187 return this.currentLevel.getGoalSquares();
190 public StartSquare[] getStartSquares() {
191 return this.currentLevel.getStartSquares();
195 * Adds credit to the player
196 * @param credit The credit to be added
198 public void addCredit(int credit) {
199 player.addCredit(credit);
202 public int getCredit() {
203 return player.getCredit();
206 public int getScore() {
207 return player.getScore();
210 public int getSavedScore() {
211 return this.player.getSavedScore();
214 public void saveScore() {
215 player.saveScore();
218 public int getNumOfClearedUnits() {
219 return currentLevel.getNumOfClearedUnits();
222 public int getUnitsToWin() {
223 return currentLevel.getUnitsToWin();
227 * Adds to the number of units that have reached the goalsquare
228 * @param num Number of units that have reached the goalsquare
229 * @author dit06ajs
231 public void addGoalUnit(int num) {
232 currentLevel.addClearedUnits(num);
236 * Checks if the goal for the current level is reached
237 * @return True if the goal is reached
239 public boolean isLevelComplete() {
240 return currentLevel.getNumOfClearedUnits()
241 >= currentLevel.getUnitsToWin();
245 * Checks if the level is lost
246 * @return True if no units on the map and no credits to buy one unit
248 public boolean isLevelLost() {
249 AgentPrototypeFactory factory = AgentPrototypeFactory.getInstance();
250 int lowestCost = factory.getLowestCost();
251 return (player.getCredit() < lowestCost && units.size() == 0);
255 * Returns the sound matching the String
256 * @param s The key for the sound
257 * @return The sound as AudioClip
259 public AudioClip getSound(String s) {
260 return sounds.get(s);
265 * Returns the track matching the String
266 * @param s The key for the sound
267 * @return The sound as File
269 public File getTrack(String s) {
270 return tracks.get(s);