/
GameMap.java
154 lines (124 loc) · 4 KB
/
GameMap.java
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
package com.matoking.dungeoncrawler.state;
import com.matoking.dungeoncrawler.state.entities.Key;
import java.util.ArrayList;
import java.util.Random;
/**
* Contains the map of the game as well as any entities inside, apart from the player
*
* @author matoking
*/
public class GameMap {
public static int MAP_DEFAULT_WIDTH = 64;
public static int MAP_DEFAULT_HEIGHT = 64;
private GameState gameState;
private int width;
private int height;
private Tile[][] tiles;
private ArrayList<Entity> entities;
public GameMap(GameState gameState, int width, int height) {
this.width = width;
this.height = height;
this.gameState = gameState;
this.tiles = new Tile[width][height];
this.entities = new ArrayList<Entity>();
for (int x=0; x < width; x++) {
for (int y=0; y < height; y++) {
this.tiles[x][y] = new Tile(x, y, TileType.EMPTY);
}
}
}
public GameMap(GameState gameState) {
this(gameState, MAP_DEFAULT_WIDTH, MAP_DEFAULT_HEIGHT);
}
/**
* Generate the map
* This will probably be offloaded to a different class but for now do some
* placeholder testing
*
*/
public void generateMap() {
Random random = new Random();
int length = random.nextInt(25) + 30;
for (int x=0; x < length; x++) {
for (int y=0; y < length; y++) {
this.setTile(x, y, TileType.WALL);
}
}
for (int x=1; x < length - 1; x++) {
for (int y=1; y < length - 1; y++) {
this.setTile(x, y, random.nextInt(100) < 85 ? TileType.FLOOR : TileType.WALL);
}
}
this.setTile(5, 5, TileType.FLOOR);
int generatedKeys = 0;
while (generatedKeys < 1000) {
int x = random.nextInt(length);
int y = random.nextInt(length);
if (!this.isTileBlocked(x, y)) {
this.addEntity(new Key(this.gameState, x, y));
generatedKeys++;
}
}
}
/**
* Set selected tile to a new type
*
* @param x X-coordinate of the tile
* @param y Y-coordinate of the tile
* @param type New coordinate of the tile
*/
public void setTile(int x, int y, TileType type) {
this.tiles[x][y].setType(type);
}
public Tile getTile(int x, int y) {
if (x < 0 || x > this.getWidth()-1 ||
y < 0 || y > this.getHeight()-1) {
return null;
}
return this.tiles[x][y];
}
/**
* Is the tile blocked by an immovable object such as a wall
*
* @return true if tile is blocked, false otherwise
*/
public boolean isTileBlocked(int x, int y) {
if (x < 0 || x > this.getWidth()-1 ||
y < 0 || y > this.getHeight()-1) {
return true;
}
Tile tile = this.tiles[x][y];
return tile.getType() == TileType.WALL;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void addEntity(Entity entity) {
this.entities.add(entity);
}
public void removeEntity(Entity entity) {
this.entities.remove(entity);
}
public ArrayList<Entity> getEntities() {
return entities;
}
/**
* Get all entities that occupy the selected coordinate
*
* @param x X-coordinate
* @param y Y-coordinate
* @return ArrayList<Entity> with matching entities
*/
public ArrayList<Entity> getEntitiesAt(int x, int y) {
ArrayList<Entity> entities = new ArrayList<Entity>();
for (Entity entity : this.entities) {
if (entity.getX() == x && entity.getY() == y) {
entities.add(entity);
}
}
return entities;
}
}