-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemap_base
206 lines (173 loc) · 5.16 KB
/
tilemap_base
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Tilemap and Occluder with centered tile based movement in P3 by vvixi
// added helper function for collisions
// todo: fog of war, collision
PImage tilemapimg;
TileMap tilemap;
int blk = 16;
int tileCols = 10;
int tileRows = 10;
ArrayList<PImage> tiles;
int wrldCols, wrldRows;
int cols = 40, rows = 40;
int [][] map = new int [cols][rows];
Player player;
int tileW, tileH;
void setup() {
surface.setTitle("Tilemap Base");
size(640, 640);
tilemapimg = loadImage("assets/Dungeon_Tileset.png");
imageMode(CENTER);
// calculate size of a tile
tileW = tilemapimg.width/tileCols;
tileH = tilemapimg.height/tileRows;
// install room values into map
PVector[] locs = new PVector[4];
// add tiles from tilemap
tiles = new ArrayList();
for (int y = 0; y < tileRows; y++) {
for (int x = 0; x < tileCols; x++) {
tiles.add(tilemapimg.get(x * 16, y * 16, tileW, tileH));
}
}
tilemap = new TileMap();
// offset needs adjusted for floor with first two parameters
tilemap.drawRoom(0, 0, cols, rows);
player = new Player(width/2, height/2);
}
void draw() {
background(0);
tilemap.displayMap();
player.display();
}
class TileMap {
int offsX, offsY;
// occluder details
int ocStartX = 1, ocEndX = width/16-1;
int ocStartY = 1, ocEndY = width/16-1;
TileMap() {
int offsX = 0;
int offsY = 0;
}
PVector world2screen(int _x, int _y) {
return new PVector(_x + player.posx/16, _y + player.posy/16);
}
void drawRoom(int _startX, int _startY, int _xSize, int _ySize) {
map = new int [_xSize + _startX][_ySize + _startY];
for (int y = _startX; y < _xSize+_startX; y++) {
for (int x = _startY; x < _ySize+_startY; x++) {
// place floors
//map[x][y] = 0;
//map[int(random(x+1, _xSize-2))][int(random(y+1, _ySize-2))] = 4;
// place outer walls
map[x][_startY] = 1;
map[_startX][y] = 1;
map[x][_ySize-1] = 1;
map[_xSize-1][y] = 1;
// place doors
map[_startX][floor(_xSize/2)] = 3;
map[_ySize-1][floor(_xSize/2)] = 3;
map[floor(_ySize/2)][_xSize-1] = 3;
map[floor(_ySize/2)][_startY] = 3;
}
}
}
void displayMap() {
for (int i = max(0, ocStartY); i < min(ocEndY, cols); i++) {
for (int j = max(0, ocStartX); j < min(ocEndX, rows); j++) {
if(true) {
switch (map[i][j]) {
case 0:
// floor tile
image(tiles.get(6), j * tileW + tileW/2 + offsX, i * tileH + tileH/2 + offsY, tileW, tileH);
break;
case 1:
// wall tile
image(tiles.get(1), j * tileW + tileW/2 + offsX, i * tileH + tileH/2 + offsY, tileW, tileH);
//image(tiles.get(2), j * tileW + tileW/2 + offsX, i * tileH + tileH/2 + offsY, tileW, tileH);
//image(tiles.get(3), j * tileW + tileW/2 + offsX, i * tileH + tileH/2 + offsY, tileW, tileH);
//image(tiles.get(4), j * tileW + tileW/2 + offsX, i * tileH + tileH/2 + offsY, tileW, tileH);
break;
case 3:
// door
image(tiles.get(36), j * tileW + tileW/2 + offsX, i * tileH + tileH/2 + offsY, tileW, tileH);
break;
case 4:
// door
fill(0);
image(tiles.get(68), j * tileW + tileW/2 + offsX, i * tileH + tileH/2 + offsY, tileW, tileH);
break;
default:
fill(0);
//image(tiles.get(7), j * tileW, i * tileH, tileW, tileH);
break;
}
}
}
}
}
}
void keyPressed() {
player.keyPressed();
}
class Player{
boolean spawned = true;
float posx, posy;
PVector dir = new PVector(0, 0);
PVector up = new PVector(0, -1);
PVector down = new PVector(0, 1);
PVector left = new PVector(-1, 0);
PVector right = new PVector(1, 0);
Player(float _posx, float _posy) {
posx = _posx;
posy = _posy;
}
void update() {
}
void display() {
// char sprite
fill(200);
noStroke();
rect(posx, posy, blk, blk);
}
public boolean canMove(PVector _dir) {
dir = _dir;
boolean move = true;
return move;
// need check for a wall tile in passed in dir
}
void keyPressed() {
if(spawned) {
if(key == CODED) {
if(keyCode == UP) {
// basic collision test before allowing movement
if (canMove(up)) {
tilemap.offsY += blk;
tilemap.ocStartY -= 1;
tilemap.ocEndY -= 1;
}
}
else if(keyCode == DOWN) {
if (canMove(down)) {
tilemap.offsY -= blk;
tilemap.ocStartY += 1;
tilemap.ocEndY += 1;
}
}
else if(keyCode == LEFT) {
if (canMove(left)) {
tilemap.offsX += blk;
tilemap.ocStartX -= 1;
tilemap.ocEndX -= 1;
}
}
else if(keyCode == RIGHT) {
if (canMove(right)) {
tilemap.offsX -= blk;
tilemap.ocStartX += 1;
tilemap.ocEndX += 1;
}
}
}
}
}
}