Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Sixth tutorial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyMansfield committed Aug 5, 2014
1 parent 07c8467 commit 2c57c57
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
Binary file added city_map.dat
Binary file not shown.
65 changes: 65 additions & 0 deletions game_state_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
void GameStateEditor::draw(const float dt)
{
this->game->window.clear(sf::Color::Black);

this->game->window.setView(this->guiView);
this->game->window.draw(this->game->background);

this->game->window.setView(this->gameView);
this->map.draw(this->game->window, dt);

return;
}

Expand All @@ -24,6 +29,54 @@ void GameStateEditor::handleInput()
{
switch(event.type)
{
case sf::Event::MouseMoved:
{
/* Pan the camera */
if(this->actionState == ActionState::PANNING)
{
sf::Vector2f pos = sf::Vector2f(sf::Mouse::getPosition(this->game->window) - this->panningAnchor);
gameView.move(-1.0f * pos * this->zoomLevel);
panningAnchor = sf::Mouse::getPosition(this->game->window);
}
break;
}
case sf::Event::MouseButtonPressed:
{
/* Start panning */
if(event.mouseButton.button == sf::Mouse::Middle)
{
if(this->actionState != ActionState::PANNING)
{
this->actionState = ActionState::PANNING;
this->panningAnchor = sf::Mouse::getPosition(this->game->window);
}
}
break;
}
case sf::Event::MouseButtonReleased:
{
/* Stop panning */
if(event.mouseButton.button == sf::Mouse::Middle)
{
this->actionState = ActionState::NONE;
}
break;
}
/* Zoom the view */
case sf::Event::MouseWheelMoved:
{
if(event.mouseWheel.delta < 0)
{
gameView.zoom(2.0f);
zoomLevel *= 2.0f;
}
else
{
gameView.zoom(0.5f);
zoomLevel *= 0.5f;
}
break;
}
/* Close the window */
case sf::Event::Closed:
{
Expand All @@ -34,6 +87,7 @@ void GameStateEditor::handleInput()
case sf::Event::Resized:
{
gameView.setSize(event.size.width, event.size.height);
gameView.zoom(zoomLevel);
guiView.setSize(event.size.width, event.size.height);
this->game->background.setPosition(this->game->window.mapPixelToCoords(sf::Vector2i(0, 0), this->guiView));
this->game->background.setScale(
Expand All @@ -57,4 +111,15 @@ GameStateEditor::GameStateEditor(Game* game)
pos *= 0.5f;
this->guiView.setCenter(pos);
this->gameView.setCenter(pos);

map = Map("city_map.dat", 64, 64, game->tileAtlas);

this->zoomLevel = 1.0f;

/* Centre the camera on the map */
sf::Vector2f centre(this->map.width, this->map.height*0.5);
centre *= float(this->map.tileSize);
gameView.setCenter(centre);

this->actionState = ActionState::NONE;
}
10 changes: 10 additions & 0 deletions game_state_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
#include <SFML/Graphics.hpp>

#include "game_state.hpp"
#include "map.hpp"

enum class ActionState { NONE, PANNING };

class GameStateEditor : public GameState
{
private:

ActionState actionState;

sf::View gameView;
sf::View guiView;

Map map;

sf::Vector2i panningAnchor;
float zoomLevel;

public:

virtual void draw(const float dt);
Expand Down
122 changes: 122 additions & 0 deletions map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,125 @@ void Map::draw(sf::RenderWindow& window, float dt)
}
return;
}

void Map::updateDirection(TileType tileType)
{
for(int y = 0; y < this->height; ++y)
{
for(int x = 0; x < this->width; ++x)
{
int pos = y*this->width+x;

if(this->tiles[pos].tileType != tileType) continue;

bool adjacentTiles[3][3] = {{0,0,0},{0,0,0},{0,0,0}};

/* Check for adjacent tiles of the same type */
if(x > 0 && y > 0)
adjacentTiles[0][0] = (this->tiles[(y-1)*this->width+(x-1)].tileType == tileType);
if(y > 0)
adjacentTiles[0][1] = (this->tiles[(y-1)*this->width+(x )].tileType == tileType);
if(x < this->width-1 && y > 0)
adjacentTiles[0][2] = (this->tiles[(y-1)*this->width+(x+1)].tileType == tileType);
if(x > 0)
adjacentTiles[1][0] = (this->tiles[(y )*this->width+(x-1)].tileType == tileType);
if(x < width-1)
adjacentTiles[1][2] = (this->tiles[(y )*this->width+(x+1)].tileType == tileType);
if(x > 0 && y < this->height-1)
adjacentTiles[2][0] = (this->tiles[(y+1)*this->width+(x-1)].tileType == tileType);
if(y < this->height-1)
adjacentTiles[2][1] = (this->tiles[(y+1)*this->width+(x )].tileType == tileType);
if(x < this->width-1 && y < this->height-1)
adjacentTiles[2][2] = (this->tiles[(y+1)*this->width+(x+1)].tileType == tileType);

/* Change the tile variant depending on the tile position */
if(adjacentTiles[1][0] && adjacentTiles[1][2] && adjacentTiles[0][1] && adjacentTiles[2][1])
this->tiles[pos].tileVariant = 2;
else if(adjacentTiles[1][0] && adjacentTiles[1][2] && adjacentTiles[0][1])
this->tiles[pos].tileVariant = 7;
else if(adjacentTiles[1][0] && adjacentTiles[1][2] && adjacentTiles[2][1])
this->tiles[pos].tileVariant = 8;
else if(adjacentTiles[0][1] && adjacentTiles[2][1] && adjacentTiles[1][0])
this->tiles[pos].tileVariant = 9;
else if(adjacentTiles[0][1] && adjacentTiles[2][1] && adjacentTiles[1][2])
this->tiles[pos].tileVariant = 10;
else if(adjacentTiles[1][0] && adjacentTiles[1][2])
this->tiles[pos].tileVariant = 0;
else if(adjacentTiles[0][1] && adjacentTiles[2][1])
this->tiles[pos].tileVariant = 1;
else if(adjacentTiles[2][1] && adjacentTiles[1][0])
this->tiles[pos].tileVariant = 3;
else if(adjacentTiles[0][1] && adjacentTiles[1][2])
this->tiles[pos].tileVariant = 4;
else if(adjacentTiles[1][0] && adjacentTiles[0][1])
this->tiles[pos].tileVariant = 5;
else if(adjacentTiles[2][1] && adjacentTiles[1][2])
this->tiles[pos].tileVariant = 6;
else if(adjacentTiles[1][0])
this->tiles[pos].tileVariant = 0;
else if(adjacentTiles[1][2])
this->tiles[pos].tileVariant = 0;
else if(adjacentTiles[0][1])
this->tiles[pos].tileVariant = 1;
else if(adjacentTiles[2][1])
this->tiles[pos].tileVariant = 1;
}
}

return;
}

void Map::depthfirstsearch(std::vector<TileType>& whitelist,
sf::Vector2i pos, int label, int regionType=0)
{
if(pos.x < 0 || pos.x >= this->width) return;
if(pos.y < 0 || pos.y >= this->height) return;
if(this->tiles[pos.y*this->width+pos.x].regions[regionType] != 0) return;
bool found = false;
for(auto type : whitelist)
{
if(type == this->tiles[pos.y*this->width+pos.x].tileType)
{
found = true;
break;
}
}
if(!found) return;

this->tiles[pos.y*this->width+pos.x].regions[regionType] = label;

depthfirstsearch(whitelist, pos + sf::Vector2i(-1, 0), label, regionType);
depthfirstsearch(whitelist, pos + sf::Vector2i(0 , 1), label, regionType);
depthfirstsearch(whitelist, pos + sf::Vector2i(1 , 0), label, regionType);
depthfirstsearch(whitelist, pos + sf::Vector2i(0 , -1), label, regionType);

return;
}

void Map::findConnectedRegions(std::vector<TileType> whitelist, int regionType=0)
{
int regions = 1;

for(auto& tile : this->tiles) tile.regions[regionType] = 0;

for(int y = 0; y < this->height; ++y)
{
for(int x = 0; x < this->width; ++x)
{
bool found = false;
for(auto type : whitelist)
{
if(type == this->tiles[y*this->width+x].tileType)
{
found = true;
break;
}
}
if(this->tiles[y*this->width+x].regions[regionType] == 0 && found)
{
depthfirstsearch(whitelist, sf::Vector2i(x, y), regions++, regionType);
}
}
}
this->numRegions[regionType] = regions;
}

0 comments on commit 2c57c57

Please sign in to comment.