summaryrefslogtreecommitdiffstats
path: root/src/Map.java
blob: a870604a382560cc9620e9cf53c20311046bbdc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.awt.Dimension;
import java.util.ArrayList;

public class Map {
	private final Dimension size;
	// TODO: is there a particular reason to not use a 2D array?
	private final Tile grid[];
	private ArrayList<Actor> actors = new ArrayList<Actor>();
	// TODO: rename to selectedActor?
	private int actorIndex = -1;
	// TODO: make selectedTile? Tile contains x and y members
	private int selectedX;
	private int selectedY;

	public Map(Dimension size) {
		this.size = size;

		// TODO: replace with Dimension.height and Dimension.width which are integer public members
		this.grid = new Tile[(int) this.size.getWidth() * (int) this.size.getHeight()];
		for (int x = 0; x < this.size.getWidth(); x++) {
			for (int y = 0; y < this.size.getHeight(); y++) {
				this.grid[x * (int) this.size.getWidth() + y] = new Tile(Tile.Type.GRASS, x, y);
			}
		}
	}

	// pathfinding algorithm
	public ArrayList<Tile> getPath(Actor actor, int x, int y) {
		ArrayList<Tile> unsettled = new ArrayList<Tile>();
		ArrayList<Tile> settled = new ArrayList<Tile>();
		Tile startTile = this.getTile(actor.getX(), actor.getY());

		for (Tile i : this.getGrid()) {
			i.setDistance(10000000);
		}

		startTile.setDistance(0);
		unsettled.add(startTile);
		settled.add(startTile);

		boolean stop = false;
		while (unsettled.size() != 0) {
			Tile workingTile = unsettled.get(0);
			unsettled.remove(workingTile);
			for (Tile i : workingTile.getAdjacent(this)) {
				if (settled.contains(i)) {
					continue;
				}
				i.setDistance(i.getCost(actor) + workingTile.getDistance());
				unsettled.add(i);
				settled.add(i);
			}	
		}

		ArrayList<Tile> out = new ArrayList<Tile>();
		Tile workingTile = this.getTile(x, y);
		double bestDistance = 1000000000;
		Tile bestTile = workingTile;
		
		while (true) {
			for (Tile i : workingTile.getAdjacent(this)) {
				if (i.getDistance() < bestDistance) {
					bestDistance = i.getDistance();
					bestTile = i;
				}	
			}	
			out.add(workingTile);
			if (workingTile.getX() == startTile.getX() && workingTile.getY() == startTile.getY()) {
				break;
			}
			workingTile = bestTile;
			bestDistance = 1000000000;
		}

		return out;
	}

	public void update(ArrayList<Actor> actorsList, Tile[] tileGrid) {
		// TODO: if this is needed for something, implement copy constructors for Actor and Tile
		//       and delete this code
		for (Actor actor : actorsList) {
			Actor newActor = new Actor(actor.getName(), actor.getHP(), actor.isEnemy(), actor.getAgility());
			newActor.place(actor.getX(), actor.getY());
			this.actors.add(newActor);		
		}

		for (int x = 0; x < this.size.getWidth(); x++) {
			for (int y = 0; y < this.size.getHeight(); y++) {
				Tile.Type oldTileType = tileGrid[x * (int) this.size.getWidth() + y].getType();
				this.grid[x * (int) this.size.getWidth() + y] = new Tile(oldTileType, x, y);
			}
		}
	}

	// TODO: refractor to resetActorActions();
	public void resetActors() {
		for (Actor actor : this.actors) {
			if (!actor.isEnemy()) {
				actor.resetActions();
			}
		}
	}

	public Actor getNextActor() {
		int counter = 0;
		this.actorIndex++;
		if (this.actorIndex >= this.actors.size()) {
			this.actorIndex = 0;
		}
		Actor actor = this.actors.get(this.actorIndex);
		while (actor.isEnemy() || actor.getActionsLeft() <= 0) {
			this.actorIndex++;
			if (this.actorIndex >= this.actors.size()) {
				this.actorIndex = 0;
			}
			actor = this.actors.get(this.actorIndex);
			if (counter > this.actors.size()*2) {
				return null;
			}
			counter++;
		}
		return actor;
	}

	// TODO: there is the Map cursor and the Mouse cursor,
	//       a solution to the naming shall be found
	public void setCursor(int x, int y) {
		this.getTile(this.selectedX, this.selectedY).setCursor(false);
		if (x == -1 && y == -1) {
			return;
		}
		this.getTile(x, y).setCursor(true);
		this.selectedX = x;
		this.selectedY = y;
	}

	public Tile[] getGrid() {
		return this.grid;
	}

	public Tile getTile(int x, int y) {
		return this.grid[x * (int) this.size.getWidth() + y];
	}

	public int getSize() {
		return (int) this.size.getWidth();
	}

	public ArrayList<Actor> getActors() {
		return this.actors;
	}

	public void removeActor(Actor actor) {
		this.actors.remove(actor);
	}

	public void addActor(Actor actor) {
		this.actors.add(actor);
	}

	// TODO: remove by introducing selectedTile member
	public void clearSelected() {
		for (Tile tile : this.grid) {
			tile.setSelected(false);
		}
	}
}