1 package se
.umu
.cs
.dit06ajnajs
;
3 import java
.awt
.Dimension
;
5 import java
.util
.ArrayList
;
7 import java
.util
.logging
.Logger
;
9 import se
.umu
.cs
.dit06ajnajs
.agent
.Agent
;
10 import se
.umu
.cs
.dit06ajnajs
.agent
.Tower
;
11 import se
.umu
.cs
.dit06ajnajs
.map
.GoalSquare
;
12 import se
.umu
.cs
.dit06ajnajs
.map
.Level
;
13 import se
.umu
.cs
.dit06ajnajs
.map
.StartSquare
;
14 import se
.umu
.cs
.dit06ajnajs
.agent
.Unit
;
15 import java
.util
.Collection
;
17 public class ATDModel
{
18 private static Logger logger
= Logger
.getLogger("AntiTD");
25 public ATDModel(List
<Level
> levels
) {
27 this.currentLevel
= levels
.get(0);
31 * Creates a new player and initialises the first level.
33 public void newGame() {
34 this.player
= new Player();
36 // TODO: ...reset stuff
40 * Restart the current level
42 public void restartLevel() {
43 player
.loadSavedScore();
47 public boolean advanceLevel() {
48 if (player
.getCurrentLevel() + 1 < levels
.size()) {
49 player
.setCurrentLevel(player
.getCurrentLevel() + 1);
53 // game won, ask for highscore entry
59 * Initialises the level that is next in turn for the player
61 public void initLevel() {
62 // Check if there is more levels
63 if (player
.getCurrentLevel() < levels
.size()) {
64 this.currentLevel
= levels
.get(player
.getCurrentLevel());
65 // If restartet game, the level must be reset
66 this.currentLevel
.resetLevel();
67 this.agents
= new ArrayList
<Agent
>();
69 for (Tower tower
: currentLevel
.getTowers()) {
74 //TODO vad händer om det är slut på banor?
79 public void addTower(Tower t
) {
80 currentLevel
.addTower(t
);
84 public void addUnit(Unit unit
) {
85 // TODO Auto-generated method stub
86 if (player
.getCredit() >= unit
.getCost()) {
87 this.player
.removeCredit(unit
.getCost());
88 StartSquare startSquare
= this.currentLevel
.getActiveStartSquare();
89 unit
.setMap(this.currentLevel
);
90 startSquare
.addUnit(unit
);
92 System
.err
.println("Can't afford unit");
96 public void releaseUnit(Unit unit
) {
100 public List
<Agent
> getAgents() {
101 // TODO Auto-generated method stub
103 List
<Agent
> resultList
= new ArrayList
<Agent
>(this.agents
.size());
104 resultList
.addAll(this.agents
);
109 * Removes all supplied units from this model. This is called when dead
110 * units are cleared from a game.
112 * @param units A collection of units to remove.
114 public void removeUnits(Collection
<Unit
> units
) {
115 this.agents
.removeAll(units
);
119 * Removes all supplied agents from this model. This is called when dead
120 * agents are cleared from a game.
122 * @param units A collection of agents to remove.
124 public void removeAgents(Collection
<Agent
> agents
) {
125 this.agents
.removeAll(agents
);
128 public Level
getLevel() {
129 return this.currentLevel
;
132 public Image
getMapImage() {
133 return currentLevel
.getMapImage();
136 public Dimension
getMapDimension() {
137 return currentLevel
.getDimension();
140 public GoalSquare
[] getGoalSquares() {
141 return this.currentLevel
.getGoalSquares();
144 public StartSquare
[] getStartSquares() {
145 return this.currentLevel
.getStartSquares();
149 * Adds credit to the player
150 * @param credit The credit to be added
152 public void addCredit(int credit
) {
153 player
.addCredit(credit
);
156 public int getCredit() {
157 return player
.getCredit();
160 public int getScore() {
161 return player
.getScore();
164 public int getSavedScore() {
165 return this.player
.getSavedScore();
168 public void saveScore() {
172 public int getNumOfClearedUnits() {
173 return currentLevel
.getNumOfClearedUnits();
176 public int getUnitsToWin() {
177 return currentLevel
.getUnitsToWin();
181 * Adds to the number of units that have reached the goalsquare
182 * @param num Number of units that have reached the goalsquare
185 public void addGoalUnit(int num
) {
186 currentLevel
.addClearedUnits(num
);
190 * Checks if the goal for the current level is reached
191 * @return True if the goal is reached
193 public boolean isGoalReached() {
194 return currentLevel
.getNumOfClearedUnits()
195 >= currentLevel
.getUnitsToWin();