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
|
#include "floor.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Mouse.hpp>
#include <algorithm>
#include <iterator>
namespace silly::editor {
void TileFloor::update(const sf::RenderWindow &window) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) ||
sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
for (int x = 0; x < this->get_width(); x++) {
for (int y = 0; y < this->get_height(); y++) {
int rx = x * 16, ry = y * 16;
if ((rx < mousePosition.x && mousePosition.x < rx + 16) &&
(ry < mousePosition.y && mousePosition.y < ry + 16)) {
sf::Vector2i pos(x, y);
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
this->place_tile(pos);
} else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
this->remove_tile(pos);
}
}
}
}
}
}
void TileFloor::render(sf::RenderWindow &window) const {
std::for_each(
this->tiles.begin(), this->tiles.end(), [&window](const Tile &t) {
sf::RectangleShape shape({16, 16});
shape.setFillColor(sf::Color(255, 190, 190));
shape.setPosition({t.position.x * 16.0f, t.position.y * 16.0f});
window.draw(shape);
});
}
void TileFloor::place_tile(const sf::Vector2i &position) {
if (!std::any_of(
this->tiles.begin(), this->tiles.end(),
[&position](const Tile &t) { return t.position == position; })) {
this->tiles.push_back({position});
}
}
void TileFloor::remove_tile(const sf::Vector2i &position) {
this->tiles.resize(std::distance(
this->tiles.begin(),
std::remove_if(
this->tiles.begin(), this->tiles.end(),
[&position](const Tile &t) { return t.position == position; })));
}
const int TileFloor::get_width() const { return this->width; }
const int TileFloor::get_height() const { return this->height; }
}
|