-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tile.hpp
executable file
·299 lines (231 loc) · 9.13 KB
/
Tile.hpp
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
/*
Puzzle Dungeon ((C) 2019)
@authors Omar Junaid, Sam Dockery
This work is licensed under the
Creative Commons Attribution-NonCommercial-NoDerivatives
4.0 International License. To view a copy of this license,
visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
*/
#ifndef _TILE_HPP_
#define _TILE_HPP_
#include <SFML/Graphics.hpp>
#include <iostream>
#include <functional>
#include <vector>
#include <string.h>
#include "Utilities.hpp"
#include <cmath>
enum TileType {
Normal = 0,
PressurePlate = 1,
Door = 2,
Viewable = 3,
Chest = 4,
CustomCodeOnInteraction = 5
Crushable = 6
};
class Tile {
public:
void setFeatures(std::string texture, float x, float y);
void checkIntersect(sf::Sprite spriteTwo, std::vector<Tile> &tileList);
void draw(sf::RenderWindow& window, sf::Sprite& characterSprite);
void checkMouseOver(sf::RenderWindow& window);
TileType type;
std::function<void()> clickCallback;
/*
*** SPECIAL CONDITIONAL MEMBERS ***
*/
sf::Text clickOnMeMessageText;
sf::Sprite viewableImage;
std::function<void()> customCodeOnInteraction;
sf::Sprite tilesprite;
bool isMouseOver = false;
void setCollideCallback(std::function<void()> callback);
/*
*** PHYSICS MEMBERS ***
*/
// If this is true, the tile will move around if the player pushes it.
bool usesPhysics = false;
/*
* How fast the tile is moving in either the X or Y direction.
* By the way: velocity = speed
* directionalVelocities[0] = positive velocity in the X direction
* directionalVelocities[1] = positive velocity in the Y direction
* directionalVelocities[2] = negative velocity in the X direction
* directionalVelocities[3] = negative velocity in the Y direction
* These are FLOATING POINT ARRAYS. Do not perform integer maths
* without casting! We don't want warnings.
*/
float directionalVelocities[5] = {0, 0, 0, 0, 0};
private:
bool standTimeout = true;
sf::Texture t;
std::function<void()> collideCallback;
};
//Set the texture, x and y position all at once.
void Tile::setFeatures(std::string texture, float x, float y) {
t.loadFromFile(texture);
tilesprite.setTexture(t);
tilesprite.setPosition(x, y);
}
void Tile::draw(sf::RenderWindow& window, sf::Sprite& characterSprite) {
sf::Vector2i mousePos = sf::Mouse::getPosition();
/* We are an object that can be picked up,
* and the cursor is currently over us.
*/
if (isMouseOver) {
clickOnMeMessageText.setFont(Utilities::basicFont);
clickOnMeMessageText.setCharacterSize(14);
clickOnMeMessageText.setFillColor(sf::Color::White);
clickOnMeMessageText.setPosition(mousePos.x - 250, mousePos.y - 170);
if (Utilities::euclideanDistance(characterSprite.getPosition(),
tilesprite.getPosition())
> 100.f) {
clickOnMeMessageText.setString("Move closer to interact");
window.draw(tilesprite);
window.draw(clickOnMeMessageText);
return;
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
try {
clickCallback();
}
catch(std::bad_function_call){
std::cerr << "[CRITICAL] Bad function call to Tile (clickCallback())" << std::endl;
}
}
clickOnMeMessageText.setString("Click to interact");
tilesprite.setColor(sf::Color(200, 200, 200, 200));
window.draw(tilesprite);
window.draw(clickOnMeMessageText);
}
else {
tilesprite.setColor(sf::Color(255, 255, 255));
window.draw(tilesprite);
}
}
void Tile::checkIntersect(sf::Sprite spriteTwo, std::vector<Tile> &tileList) {
/*
* @brief If the speed in any direction is greater than 0,
* it will gradually decrease. Otherwise, it will be set to 0
* anyway.
*/
for (int i = 0; i < sizeof(directionalVelocities); i++) {
directionalVelocities[i] = std::fmax(0, directionalVelocities[i] - 0.2f);
}
// Move the sprite based on the directional velocities.
// If they are 0, this doesn't do anything.
// ALSO, check for collisions.
for (Tile &tile : tileList) {
// Ensure we're not doing collision detection
// against ourselves
if (tile.tilesprite.getGlobalBounds()
== tilesprite.getGlobalBounds()) {
continue;
}
/* If we collide with something else, transfer half our energy to the other tile.
* This means we also lose half our energy.
*/
if (tile.tilesprite.getGlobalBounds().intersects(tilesprite.getGlobalBounds())) {
if (!tile.usesPhysics) {
tile.directionalVelocities[2] = directionalVelocities[0] / 2.0f;
tile.directionalVelocities[1] = directionalVelocities[3] / 2.0f;
tile.directionalVelocities[3] = directionalVelocities[1] / 2.0f;
tile.directionalVelocities[0] = directionalVelocities[2] / 2.0f;
}
if (tile.usesPhysics) {
for (int i = 0; i < sizeof(directionalVelocities); i++) {
directionalVelocities[i] /= 1.8f;
}
}
}
}
tilesprite.move(directionalVelocities[0] - directionalVelocities[2],
directionalVelocities[1] - directionalVelocities[3]);
if (!tilesprite.getGlobalBounds().intersects(spriteTwo.getGlobalBounds()))
{
standTimeout = true;
return;
}
try {
if (!standTimeout) {
standTimeout = false;
collideCallback();
}
}
catch (...) {
// Do nothing; callback not set. THIS IS NORMAL.
}
if (!usesPhysics)
return;
/*
* Calculate all this centre/differences stuff.
* We use this later to determine which side of the
* tile sprite was collided with.
* Reference:
* https://www.gamedev.net/forums/topic/
* 662182-sfml-bounding-box-but-how-to-implment-collision-with-side-detection/
* (MODIFIED)
* (sorry for the double lines - we have an 80 column standard!)
*/
sf::IntRect tileRect = tilesprite.getTextureRect();
sf::IntRect playerRect = spriteTwo.getTextureRect();
int tileXCentre = (tileRect.left + (tileRect.width / 2));
int tileYCentre = (tileRect.top + (tileRect.height / 2));
int playerXCentre = (playerRect.left + (playerRect.width / 2));
int playerYCentre = (playerRect.top + (playerRect.height / 2));
int xDifference = playerXCentre - tileXCentre;
int yDifference = playerYCentre - tileYCentre;
int positiveX = 0;
int positiveY = 0;
/*
positive X = to the right
positive Y = to the top
*/
float xAxisDistance = spriteTwo.getPosition().x - tilesprite.getPosition().x;
float yAxisDistance = tilesprite.getPosition().y - spriteTwo.getPosition().y;
if (xAxisDistance > 0)
directionalVelocities[2] = xAxisDistance / 8;
else
directionalVelocities[0] = fabsf(xAxisDistance / 5);
if (yAxisDistance > 0)
directionalVelocities[1] = yAxisDistance / 8;
else
directionalVelocities[3] = fabsf(yAxisDistance / 5);
}
inline void Tile::checkMouseOver(sf::RenderWindow &window)
{
/* If we're not an object on the floor, don't
* bother doing all these calculations.
*/
if(type != TileType::Viewable && type != TileType::CustomCodeOnInteraction)
return;
/* Detect if the cursor is intersecting the tile.
* Note: The cursor has a size of exactly 1x1 pixels.
*/
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f pixelPos = window.mapPixelToCoords(mousePos);
// Super ugly collision detection. But it works.
// Detect if the mouse is intersecting the tile.
if (pixelPos.x <= tilesprite.getPosition().x + tilesprite.getTextureRect().width
&& pixelPos.x + 1 >= tilesprite.getPosition().x && pixelPos.y <= tilesprite.getPosition().y +
tilesprite.getTextureRect().height &&
pixelPos.y + 1 >= tilesprite.getPosition().y)
{
isMouseOver = true;
}
else {
isMouseOver = false;
}
}
/*
* @brief When the user collides with this tile,
* the lambda below will be executed.
* @param callback lambda to be executed
*/
inline void Tile::setCollideCallback(std::function<void()> callback)
{
collideCallback = callback;
}
#endif