-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
176 lines (152 loc) · 4.94 KB
/
Map.cpp
File metadata and controls
176 lines (152 loc) · 4.94 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
#include "Map.h"
#include "Entity.h"
void Block::updatePlayerDistance(sf::RectangleShape shape)
{
this->distanceToPlayer = util::distanceToCenter(shape.getGlobalBounds(), this->getGlobalBounds());
}
// for std::sort
bool Block::operator<(Block *other) const
{
return this->distanceToPlayer < other->distanceToPlayer;
}
// for std::sort
bool Block::operator>(const Block& other) const
{
return this->distanceToPlayer > other.distanceToPlayer;
}
// class initializer
Map::Map(sf::Image image, sf::RenderWindow *window)
{
this->window = window;
this->mapImage = image;
this->entities = new std::vector<Entity*>;
}
// rendering is another word for reading the info of each pixel and creating a
// new object of the corrisponding location
void Map::render()
{
for (unsigned int i = 0; i < this->mapImage.getSize().x; i++)
{
for (unsigned int j = 0; j < this->mapImage.getSize().y; j++)
{
//if (mapImage.getPixel(i, j) == sf::Color::White)
//{
// sf::RectangleShape* newRect = new sf::RectangleShape;
// newRect->setPosition(i * GAME_SCALE, j * GAME_SCALE);
// newRect->setSize(sf::Vector2f(GAME_SCALE, GAME_SCALE));
// newRect->setFillColor(sf::Color::Transparent);
// map.push_back(newRect);
//}
// black is the default color of the ground
if (mapImage.getPixel(i, j) == sf::Color::Black)
{
Block* newRect = new Block;
newRect->setPosition(i * GAME_SCALE, j * GAME_SCALE);
newRect->setSize(sf::Vector2f(GAME_SCALE, GAME_SCALE));
newRect->setFillColor(sf::Color::Black);
map.push_back(newRect);
}
// block for testing color accuracy
if (mapImage.getPixel(i, j) == sf::Color(255, 255, 95))
{
Block* newRect = new Block;
newRect->setPosition(i * GAME_SCALE, j * GAME_SCALE);
newRect->setSize(sf::Vector2f(GAME_SCALE, GAME_SCALE));
newRect->setFillColor(sf::Color(255, 255, 95));
map.push_back(newRect);
}
// red pixels are kept for enemies or dynamic entites other than the player
if (mapImage.getPixel(i, j) == sf::Color::Red)
{
Entity* newEntity = new Entity;
newEntity->rect.setPosition(i * GAME_SCALE, j * GAME_SCALE);
newEntity->rect.setSize(sf::Vector2f(GAME_SCALE, GAME_SCALE));
newEntity->rect.setFillColor(sf::Color::Red);
newEntity->vel = sf::Vector2f(0, 0);
newEntity->direction = true;
newEntity->speed = 120.f;
newEntity->alive = true;
entities->push_back(newEntity);
}
}
}
}
// to check the direction of the player
CollisionDirection Map::checkCollisionDirection(sf::RectangleShape r1, sf::RectangleShape r2, bool debug)
{
// this just fucking works, took me like 6 hours
CollisionDirection overlap = CollisionDirection::none;
if (r1.getGlobalBounds().intersects(r2.getGlobalBounds())) {
float playerX = r1.getPosition().x;
float playerY = r1.getPosition().y;
float objX = r2.getPosition().x;
float objY = r2.getPosition().y;
float playerBottom = playerY + r1.getLocalBounds().height;
float objBottom = objY + r2.getLocalBounds().height;
float playerRight = playerX + r1.getLocalBounds().width;
float objRight = objX + r2.getLocalBounds().width;
float bottomCollision = objBottom - playerY - 8;
float topCollision = playerBottom - objY - 8;
float leftCollision = playerRight - objX + 8;
float rightCollision = objRight - playerX + 8;
sf::Color debugColor;
// Check if two objects are being touched on the BOTTOM
if (bottomCollision <= topCollision
&& bottomCollision <= leftCollision
&& bottomCollision <= rightCollision)
{
overlap = CollisionDirection::bottom;
debugColor = sf::Color::Blue;
}
// Check if two objects are being touched on the TOP
if (topCollision <= bottomCollision
&& topCollision <= leftCollision
&& topCollision <= rightCollision)
{
overlap = CollisionDirection::top;
debugColor = sf::Color::Yellow;
}
// Check if two objects are being touched on the RIGHT
if (rightCollision <= leftCollision
&& rightCollision <= topCollision
&& rightCollision <= bottomCollision)
{
overlap = CollisionDirection::right;
debugColor = sf::Color::Green;
}
// Check if two objects are being touched on the LEFT
if (leftCollision <= rightCollision
&& leftCollision <= topCollision
&& leftCollision <= bottomCollision)
{
overlap = CollisionDirection::left;
debugColor = sf::Color::Magenta;
}
if (debug)
{
sf::RectangleShape playerDebug;
playerDebug.setSize(sf::Vector2f(32, 32));
playerDebug.setPosition(playerX, playerY);
playerDebug.setOutlineColor(debugColor);
playerDebug.setOutlineThickness(3);
sf::RectangleShape objectDebug;
objectDebug.setSize(sf::Vector2f(32, 32));
objectDebug.setPosition(objX, objY);
objectDebug.setOutlineColor(debugColor);
objectDebug.setOutlineThickness(3);
this->window->draw(playerDebug);
this->window->draw(objectDebug);
}
}
return overlap;
}
void Map::updateBlocks(Player player)
{
for (Block* block : this->map)
{
block->updatePlayerDistance(player.rect);
}
}
Map::~Map()
{
}