summaryrefslogtreecommitdiffstats
path: root/src/Map.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Map.java')
-rw-r--r--src/Map.java154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/Map.java b/src/Map.java
new file mode 100644
index 0000000..7b178cf
--- /dev/null
+++ b/src/Map.java
@@ -0,0 +1,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);
+ }
+ }
+}