-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiledMap.java
More file actions
121 lines (93 loc) · 3.38 KB
/
TiledMap.java
File metadata and controls
121 lines (93 loc) · 3.38 KB
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
package com.sixfootgeek;
import java.util.Random;
/**
* File: TiledMap.java
* Version: 0.32476
* Date: 28th February 2015.
* Author: Andy Barlow
*
* Description:
*
* Provides the functionality required by the client to the map object
* TiledMap method creates a simple map and sets the immediate groundtype to whatever is specified.
*
* SetBorder method provides the border. makes a perimeter of type Hedge.
* SetRandomSubArea creates a randomly sized subarea of specified type by picking a 4 random values from the map.
* The other methods are just getters and setters of various map related things such as height and width.
* RandomRange method just returns an int from within a specific range.
*
*/
public class TiledMap implements iTiledMap {
public GroundType[][] map;
public int TILE_SIZE, mapHeight, mapWidth;
private iRenderer renderer;
public TiledMap(int createWidth, int createHeight, GroundType a) {
TILE_SIZE = 15;
this.map = new GroundType[createWidth][createHeight];
//make entire map start true or false
for (int x = 0; x < createWidth; x++) {
for (int y = 0; y < createHeight; y++) {
map[x][y] = a;
}
}
//this initialises the borders after the main map is created
setBorder(GroundType.DIRT, this);
}
public void setBorder(GroundType a,TiledMap map) {
try {
// Add the impassable boundary hedge.
// The top and bottom boundaries ...
for (int x = 0; x < getMapWidth(); x++) {
map.set(x, 0, a);
map.set(x, getMapHeight() - 1, a);
}
// Set the entrance gap at the middle of the bottom boundary ...
final int startPosition = getMapWidth() / 2 - 2;
for (int x = getMapWidth()/2; x < startPosition + 5; x++) {
//TODO uncouple this method from the groundtype so that it can react to the client app and not be hardcoded.
map.set(x, 0, get(getMapWidth()/2, getMapHeight()/2));
}
// set the left and right boundaries ...
for (int y = 0; y < map.getMapHeight(); y++) {
map.set(0, y, a);
map.set(getMapWidth() - 1, y, a);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("map to small to generate fence");
}
}
public void setRenderer(iRenderer r) {
renderer = r;
}
public void render() {
if (renderer == null) return;
renderer.render(this);
}
public int getMapHeight() {
return map[0].length;
}
public int getMapWidth() {
return map.length;
}
public GroundType get(int x, int y) {
return map[x][y];
}
public void set(int x, int y, GroundType a) {
map[x][y] = a;
}
//method to return a random int between a range.
public int randomFromRange(int min, int max) {
Random r = new Random();
int randomNum = r.nextInt((max - min) + 1) + min;
return randomNum;
}
//make a random sub area
public void setRandomSubArea(int startX, int startY, int endX, int endY, GroundType a) {
for (int x = startX; x < endX; x++) {
for (int y = startY; y < endY; y++) {
map[x][y] = a;
}
}
}
}