-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode_map_edit.cpp
More file actions
79 lines (70 loc) · 2.61 KB
/
mode_map_edit.cpp
File metadata and controls
79 lines (70 loc) · 2.61 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
#include "mode_map_edit.hpp"
using namespace std;
ModeMapEdit::ModeMapEdit() {
// Initialize the side bar
sideBarBackground.setSize(sf::Vector2f(SIDEBAR_WIDTH, WINDOW_HEIGHT));
sideBarBackground.setPosition(WINDOW_WIDTH-SIDEBAR_WIDTH, 0);
curYOffset = 0.;
curSelectedTile = NULL;
rightClicked = false;
}
void ModeMapEdit::init() {
}
void ModeMapEdit::update(float dt, sf::RenderWindow* screen) {
sf::Event e;
while (screen->pollEvent(e)){
if (e.type == sf::Event::Closed){
screen->close();
}
}
// If the mouse is over the right scrollbar...
sf::Vector2i mousePos = sf::Mouse::getPosition(*screen);
if (mousePos.x > WINDOW_WIDTH-SIDEBAR_WIDTH) {
// Scroll the mouse if it is at either edge
if (mousePos.y > 0 && mousePos.y < BUTTON_HEIGHT) {
curYOffset += dt*SCROLL_SPEED;
} else if (mousePos.y < WINDOW_HEIGHT && mousePos.y > WINDOW_HEIGHT - BUTTON_HEIGHT) {
curYOffset -= dt*SCROLL_SPEED;
}
// Check for a click and switch currently selected tile if possible
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
int tileClicked = (int) (mousePos.y - TILE_Y_OFFSET - curYOffset) / TILE_Y_SIZE;
if (tileClicked >= 0 && tileClicked < (int) curMap->getTileDict().size()) {
curSelectedTile = curMap->getTileDict().at(tileClicked);
cout << curSelectedTile << endl;
}
}
} else {
// Otherwise, we're in the map area
// Check for clicks and swap the selected tile with the currently selected tile
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
point clicked = TexXYToTileXY(mousePos.x, mousePos.y);
cout << "X: " << clicked.tileX << " Y: " << clicked.tileY << endl;
curMap->setTile(clicked.tileX, clicked.tileY, curSelectedTile);
}
if (!rightClicked && sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
rightClicked = true;
std::vector<point> route = AStarSearch(curMap, 0, 0, curMap->width-1, curMap->height-1);
while (!route.empty()) {
point thisPoint = route.back();
route.pop_back();
curMap->setTile(thisPoint.tileX, thisPoint.tileY, 2);
cout << thisPoint.tileX << "," << thisPoint.tileY << endl;
}
} else if (!sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
rightClicked = false;
}
}
}
void ModeMapEdit::render(sf::RenderTarget* screen) {
curMap->render(screen);
entManager->render(screen);
// Draw the side bar
screen->draw(sideBarBackground);
// Get the different tiles, and render each one in the sidebar
std::vector<Tile*> tileDict = curMap->getTileDict();
for (unsigned int i = 0; i < tileDict.size(); i++) {
Tile* thisTile = tileDict.at(i);
thisTile->render(screen, WINDOW_WIDTH-TILE_X_OFFSET, i*TILE_Y_SIZE+TILE_Y_OFFSET+curYOffset);
}
}