summaryrefslogtreecommitdiffstats
path: root/src/Map.java
blob: 7b178cf77f5f60469545ce51ee68c33231bfeeeb (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
import java.awt.Dimension;
import java.util.ArrayList;

public class Map {
	private final Dimension size;
	private final Tile grid[];
	private ArrayList<Actor> actors = new ArrayList<Actor>();
	private int actorIndex = -1;
	private int selectedX;
	private int selectedY;

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

		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);
			}
		}
	}

	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) {
		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);
			}
		}
	}

	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;
	}

	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);
	}

	public void clearSelected() {
		for (Tile tile : this.grid) {
			tile.setSelected(false);
		}
	}
}