-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathship.cpp
More file actions
322 lines (296 loc) · 6.86 KB
/
ship.cpp
File metadata and controls
322 lines (296 loc) · 6.86 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <iostream>
#include <stdlib.h>
#include "ship.h"
#include "entity.h"
#include "main.h"
using namespace std;
Ship::Ship(bool serv){
//Load texture data
//Make data array
data = new int*[dunXSize];
for(int i=0;i<dunXSize;i++){
data[i] = new int[dunYSize];
}
//Set array to zero cause just in case
for(int y=0;y<dunYSize;y++){
for(int x=0;x<dunXSize;x++){
data[x][y] = EMPTY;
}
}
//How many rooms to make
numOfRooms = rand() % (maxRooms-minRooms+1) + minRooms;
roomList = new Room[numOfRooms];
//Make sure no rooms are inside eachother
bool recheck = false;
for(int i=0;i<numOfRooms;i++){
for(int j=0;j<numOfRooms;j++){
//Cause problems if we don't temp store them
Room room1 = roomList[i];
Room room2 = roomList[j];
if(j == i){
continue;
}
//Check if the rooms are inside eachother
if(room1.intersects(room2)){
roomList[j].rndRoom();
recheck = true;
//j--;
}
//Check to make sure the rooms are actually on the grid
else if(room2.xPos+room2.xSize > dunXSize-1 ||
room2.yPos+room2.ySize > dunYSize-1){
roomList[j].rndRoom();
//j--;
recheck = true;
}
if(recheck){
j--;
recheck = false;
}
}
}
//Place rooms on map
for(int i=0;i<numOfRooms;i++){
for(int y=0;y<roomList[i].ySize;y++){
for(int x=0;x<roomList[i].xSize;x++){
data[x+roomList[i].xPos][y+roomList[i].yPos] = FLOOR;
}
}
}
//Make hallways
for(int i=0;i<numOfRooms-1;i++){
Room room1 = roomList[i];
Room room2 = roomList[i+1];
//Find room 1 middle (x,y) cord
int room1x = room1.xPos+(room1.xSize/2);
int room1y = room1.yPos+(room1.ySize/2);
//Find room 2 middle(x,y) cord
int room2x = room2.xPos+(room2.xSize/2);
int room2y = room2.yPos+(room2.ySize/2);
int lastType = -1;
int type;
//Draw the y part of the hallway
while(room1y != room2y){
if(room1y < room2y){
room1y++;
type = 1;
}else if(room1y > room2y){
room1y--;
type = -1;
}
if(data[room1x][room1y] == FLOOR && lastType == EMPTY){
data[room1x][room1y] = NODE;
lastType = NODE;
} else if(data[room1x][room1y] == EMPTY && lastType == FLOOR){
data[room1x][room1y-type] = NODE;
data[room1x][room1y] = FLOOR;
lastType = NODE;
}else if(data[room1x][room1y] == EMPTY){
data[room1x][room1y] = FLOOR;
lastType = EMPTY;
} else {
lastType = FLOOR;
}
}
data[room1x][room1y] = NODE;
lastType = NODE;
//Draw the x part of the hallway
while(room1x != room2x){
if(room1x < room2x){
room1x++;
type = 1;
}else if(room1x > room2x){
room1x--;
type = -1;
}
if(data[room1x][room1y] == FLOOR && lastType == EMPTY){
data[room1x][room1y] = NODE;
lastType = NODE;
} else if(data[room1x][room1y] == EMPTY && lastType == FLOOR){
data[room1x-type][room1y] = NODE;
data[room1x][room1y] = FLOOR;
lastType = NODE;
}else if(data[room1x][room1y] == EMPTY){
data[room1x][room1y] = FLOOR;
lastType = EMPTY;
} else {
lastType = FLOOR;
}
}
}
//Make walls
for(int y=0;y<dunYSize;y++){
for(int x=0;x<dunXSize;x++){
if(data[x][y] == FLOOR || data[x][y] == NODE){
//Left
if(data[x-1][y] == EMPTY){
data[x-1][y] = LWALL;
}
//Right
if(data[x+1][y] == EMPTY){
data[x+1][y] = RWALL;
}
//Up
if(data[x][y-1] == EMPTY){
data[x][y-1] = TWALL;
}
//Down
if(data[x][y+1] == EMPTY){
data[x][y+1] = WALL;
}
/*//Left-Down
if(data[x-1][y-1] == EMPTY){
data[x-1][y-1] = LTCORNWALL;
}
//Left-Up
else if(data[x-1][y+1] == EMPTY){
data[x-1][y+1] = LBCORNWALL;
}
//Right-Down
else if(data[x+1][y-1] == EMPTY){
data[x+1][y-1] = RTCORNWALL;
}
//Right-Up
else if(data[x+1][y+1] == EMPTY){
data[x+1][y+1] = RBCORNWALL;
}*/
}
}
}
//Make walls
for(int y=0;y<dunYSize;y++){
for(int x=0;x<dunXSize;x++){
if(data[x][y] == FLOOR || data[x][y] == NODE){
//Left-Down
if(data[x-1][y-1] == EMPTY){
data[x-1][y-1] = LTCORNWALL;
}
//Left-Up
else if(data[x-1][y+1] == EMPTY){
data[x-1][y+1] = LBCORNWALL;
}
//Right-Down
else if(data[x+1][y-1] == EMPTY){
data[x+1][y-1] = RTCORNWALL;
}
//Right-Up
else if(data[x+1][y+1] == EMPTY){
data[x+1][y+1] = RBCORNWALL;
}
}
}
}
//Try to make 100 random bloody tiles
for(int i=0;i<500;i++){
sf::Vector2i pos;
pos.x = rand()%dunXSize;
pos.y = rand()%dunYSize;
if(data[pos.x][pos.y] % 2 != 0 && data[pos.x][pos.y] != NODE && data[pos.x][pos.y] != WALL){
data[pos.x][pos.y]++;
}
}
/*
//debug room
data[0][0] = WALL;*/
//drawRoom();
}
Ship::~Ship(){
for(int i=0;i<dunXSize;i++){
delete[] data[i];
}
delete[] data;
delete[] roomList;
}
void Ship::drawRoom(){
for(int y=0;y<dunYSize;y++){
for(int x=0;x<dunXSize;x++){
cout << data[x][y];
}
cout << endl;
}
}
void Ship::drawMap(sf::RenderWindow *window,int screenx, int screeny){
sf::Sprite mapTile;
mapTile.setTexture(tilesTex);
for(int y=0;y<dunYSize;y++){
for(int x=0;x<dunXSize;x++){
if(data[x][y] == 0){
continue;
} else if((x*32+32) >= (screenx) && (x*32) <= (screenx+800)&&
(y*32+32) >= (screeny) && (y*32) <= (screeny+600)){
int xx = (data[x][y] % 4) * 32;
int yy = (data[x][y] / 4) * 32;
if(data[x][y] == NODE){
xx = 32;
yy = 0;
}
mapTile.setTextureRect(sf::IntRect(xx,yy,32,32));
mapTile.setPosition(x*32,y*32);
mapTile.move(-screenx,-screeny);
window->draw(mapTile);
}
}
}
}
Room::Room(){
rndRoom();
}
void Room::rndRoom(){
//Set random (x,y) position
xPos = rand() % (dunXSize-1) + 1;
yPos = rand() % (dunYSize-1) + 1;
//Make ship have mix size 4x4 and max 10x10
xSize = rand() % 7 + 4;
ySize = rand() % 7 + 4;
}
bool Room::intersects(Room otherRoom){
if(xPos+xSize < otherRoom.xPos) return false;
if(xPos > otherRoom.xPos + otherRoom.xSize) return false;
if(yPos + ySize < otherRoom.yPos) return false;
if(yPos > otherRoom.yPos + otherRoom.ySize) return false;
return true;
}
ShipEntity::ShipEntity(bool serv){
type = "map";
drawable = true;
collides = true;
readyToUpdate = false;
alive = true;
x = 0;
y = 0;
map = new Ship(serv);
getColBoxes();
}
ShipEntity::~ShipEntity(){
delete map;
}
void ShipEntity::getColBoxes(){
collisionBoxes.clear();
//Get all collision boxes
for(int x=0;x<dunXSize;x++){
for(int y=0;y<dunYSize;y++){
if(map->data[x][y] > 2 && map->data[x][y] < 20){
//cout << "pushing back collisionBoxes " << x << "," << y << endl;
collisionBoxes.push_back(sf::FloatRect(x*32,y*32,32,32));
}
}
}
}
void ShipEntity::draw(sf::RenderWindow *screen, int screenx, int screeny){
map->drawMap(screen,screenx,screeny);
}
sf::Vector2f ShipEntity::getRandomFloorTile(){
sf::Vector2f vec;
int x;
int y;
while(true){
x = rand() % dunXSize;
y = rand() % dunYSize;
if(map->data[x][y] == FLOOR){
break;
}
}
vec.x = x;
vec.y = y;
return vec;
}