-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.cpp
152 lines (130 loc) · 4.74 KB
/
World.cpp
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
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <string>
#include "headers/World.h"
#include "headers/AssetManager.h"
#include "headers/Entity.h"
#include "headers/Constants.h"
#include "headers/Projectile.h"
// ========================= World Class =========================
World::World(Game *game)
{
this->game = game;
}
World::~World()
{
for (int i = 0; i < MAPSIZE * MAPSIZE; i++)
delete entities[i];
}
void World::createNewWorld()
{
srand((unsigned)time(NULL));
for (int i = 0; i < MAPSIZE * MAPSIZE; i++)
entities[i] = nullptr; // NULLPTR
// Generates tilemap.
for (int i = 0; i < MAPSIZE; i++)
for (int j = 0; j < MAPSIZE; j++)
this->tilemap[i][j] = 0;
// Adds tiles with grass and rocks.
for (int i = 40 + rand() % 80; i > 0; i--)
{
this->tilemap[rand() % MAPSIZE][rand() % MAPSIZE] = rand() % 3 + 1; // Sets array to REGULARTILE, GRASSTILE, ROCKTILE or PLANTTILE
}
for (int i = 0; i < 8; i++)
{
this->tilemap[rand() % MAPSIZE][rand() % MAPSIZE] = MINETILE; // Sets array to REGULARTILE, GRASSTILE, ROCKTILE, PLANTTILE or MINETILE.
}
// Adds water pools in available spaces.
for (int i = 4 + rand() % 5; i > 0; i--) // Amount of pools
{
int x = rand() % MAPSIZE;
int y = rand() % MAPSIZE;
int direction = rand() % 4; // top, right, bottom, left
std::string waterSchematics[8] = {
"1100011011111100",
"1011011011100100",
"0010011101100000",
"0000001011110110",
"1100111011110011",
"0000110001110010",
"0000011000100000",
"0110101101100010"};
int schematic = rand() % 8;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
if (waterSchematics[schematic][i * 4 + j] == '1')
this->tilemap[std::max(std::min(x - 1 + i, MAPSIZE - 1), 0)][std::max(std::min(y - 1 + j, MAPSIZE - 1), 0)] = WATERTILE;
}
}
// Generates grass under altar.
for (int i = 9; i < 13; i++)
for (int j = 9; j < 13; j++)
this->tilemap[i][j] = rand() % 3 + 1;
// Creates entities.
Building *main_base = new Building();
main_base->createEntity(10, 10, "base", "main_base", -11, 25, 9999, this);
entities[10 * MAPSIZE + 10] = main_base;
Building *base_fragments1 = new Building();
base_fragments1->createEntity(10, 11, "base", "", 0, 0, 9999, this);
entities[10 * MAPSIZE + 11] = base_fragments1;
Building *base_fragments2 = new Building();
base_fragments2->createEntity(10, 11, "base", "", 0, 0, 9999, this);
entities[11 * MAPSIZE + 10] = base_fragments2;
Building *base_fragments3 = new Building();
base_fragments3->createEntity(10, 11, "base", "", 0, 0, 9999, this);
entities[11 * MAPSIZE + 11] = base_fragments3;
for (int i = 7 + rand() % 5; i > 0; i--)
{
int x, y;
do
{
x = rand() % MAPSIZE;
y = rand() % MAPSIZE;
} while (getEntity(x, y) || tilemap[x][y] == WATERTILE || tilemap[x][y] == MINETILE);
Building *tree = new Building();
tree->createEntity(x, y, "tree", "tree", 8, 15, 9999, this);
entities[x * MAPSIZE + y] = tree;
tilemap[x][y] = TREETILE;
}
}
Entity *World::getEntity(int x, int y)
{
return entities[x * MAPSIZE + y];
}
bool World::placeNewBuilding(int x, int y, std::string type, std::string spriteName, int xOffset, int yOffset, int health)
{
if (!entities[x * MAPSIZE + y])
{
Building *building = new Building();
building->createEntity(x, y, type, spriteName, xOffset, yOffset, health, this);
entities[x * MAPSIZE + y] = building;
return true;
}
return false;
}
void World::destroyEntity(int x, int y)
{
if (getEntity(x, y))
{
// Checks if entity is an enemy - if yes, add crytals.
if (getEntity(x, y)->getType() == "wasp" || getEntity(x, y)->getType() == "slime")
{
game->crystals += 5;
game->monstersKilled++;
game->crystalsEarned += 5;
}
else if (getEntity(x, y)->getType() == "turret")
{
game->destroyTurretParticle(x, y);
}
// Deletes entity.
float scale = 1.5f;
int mapX = game->tileSize * x - game->tileSize * y - game->tileSize + game->mapXOffset + 3 * game->tileScale * scale;
int mapY = (game->tileSize * y + game->tileSize * x) / 2 + game->mapYOffset - game->tileScale * scale - 4 * game->tileScale;
Particle particle(mapX, mapY, "particles/explosion", 9, scale);
game->addParticle(particle);
delete getEntity(x, y);
entities[x * MAPSIZE + y] = nullptr;
}
}