Skip to content

Commit

Permalink
session_data revived
Browse files Browse the repository at this point in the history
  • Loading branch information
eadium committed May 16, 2018
1 parent 3ffbe3b commit 615791c
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 15 deletions.
81 changes: 79 additions & 2 deletions session_data.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
#include "session_data.h"
// #include "event_factory.h"
#include "mapscanner.h"

#include <algorithm>

using std::vector;

//---------------------------------------------------------
//---------------- SurfaceData ----------------------------
//---------------------------------------------------------

bool SurfaceData::Init() {
// open map, read it
// asign map pointers
// init factories
str locationName = SessionData::gameData.locationID;
MapScanner scanner;
MapData data = scanner.getMap(locationName);
surfaceMatrix = data.surfaceMatrix;
mapHeight = data.mapHeight;
mapWidth = data.mapWidth;
SessionData::gameData.mapHeight = mapHeight;
SessionData::gameData.mapWidth = mapWidth;
return true;
}

int SurfaceData::getWidth() {
return mapWidth;
}

int SurfaceData::getHeight() {
return mapHeight;
}

short SurfaceData::getSurface(coord& point) {
if ((point.x <= 0 || point.x >= mapWidth) || (point.y <= 0 || point.y >= mapHeight))
return -1;
return surfaceMatrix[point.x][point.y];
}

bool SurfaceData::IsValidCoord(coord& point) {
if ((point.x < 0 || point.x > mapWidth) || (point.y < 0 || point.y > mapHeight))
return false;
return true;
}

bool SurfaceData::IsValidRadius(coord& point, short radius) {
if (
(point.x - radius <= 0) ||
(point.x + radius >= mapWidth) ||
(point.y + radius >= mapHeight) ||
(point.y - radius <= 0) ||
(!IsValidCoord(point))
) return false;

bool flag = false;
coord temp;
for (int i = point.x - radius; i < point.x + radius; i++) {
for (int j = point.y - radius; j < point.y + radius; j++) {
temp.x = i; temp.y = j;
if (getSurface(temp)) flag = true;
}
}

return flag;
}

bool SurfaceData::IsWalkable(coord& point) {
if (getSurface(point)) return true;
return false;
}

//---------------------------------------------------------
//----------------- EventsData ----------------------------
//---------------------------------------------------------
Expand Down Expand Up @@ -34,8 +102,8 @@ add event to map
for (auto i : list) {
Event event = i.second;

int width = GameData::mapWidth;
int height = GameData::mapHeight;
int width = SessionData::gameData.mapWidth;
int height = SessionData::gameData.mapHeight;

coord eventCenter = event.GetCoord();
if (eventCenter.x > width || eventCenter.y > height) throw "coordinates are out of range";
Expand Down Expand Up @@ -87,3 +155,12 @@ bool ArtifactsData::Init() {
Artifact& ArtifactsData::getArtifact(const str key) {
return currentArtifactsList.at(key);
}

//---------------------------------------------------------
//--------------------- GameData --------------------------
//---------------------------------------------------------

bool GameData::Init() {
using namespace SessionData;
return eventsData.Init() && surfaceData.Init() && artifactsData.Init();
}
49 changes: 36 additions & 13 deletions session_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
#include "event_factory.h"
#include "artifact.h"
// #include "artifact_factory.h"
#include "mapdata.h"
#include "mapscanner.h"
#include "surface.h"

#include "surface.h" //here is SurfaceData, temporary solution.

class SystemData {
// coming soon
Expand All @@ -21,21 +18,34 @@ class SystemData {
class GameData {
// stores finished events and other stuff
public:
// bool Init();
static short getHeight();
static short getWidth();
static void setHeight(short);
static void setWidth(short);
bool Init();
int mapHeight;
int mapWidth;
str locationID; // ID of a map going to be loaded
};

class SurfaceData {
public:
bool Init();
int getWidth();
int getHeight();
bool IsValidRadius(coord&, short);
bool IsValidCoord(coord&);
bool IsWalkable(coord&);
short getSurface(coord&);
Surface& getSurface(str);
private:
static short mapHeight;
static short mapWidth;
int mapWidth, mapHeight;
short** surfaceMatrix; // std::unordered_map<str, Surface> currentSurfaceList; // currently useless
// std::unordered_map<str, Surface> globalSurfaceList; //will be added in future
};

class EventsData {
public:
bool Init();
Event& getEvent(const str);
Event& getEvent(coord);
void removeFrontEvent(coord); // no realization yet
// more methods coming in future
private:
void SortEventVector(std::vector<Event>*);
Expand All @@ -53,8 +63,21 @@ class ArtifactsData {
Artifact& getArtifact(const str);
private:
std::unordered_map<str, Artifact> currentArtifactsList;
std::unordered_map<str, Artifact> globalArtifactsList;

// std::unordered_map<str, Artifact> globalArtifactsList;
};

namespace SessionData {
SystemData systemData;
GameData gameData;
SurfaceData surfaceData;
EventsData eventsData;
ArtifactsData artifactsData;

// hero initialization
str name = "Hero";
str id = "super_hero";
coord start;
Hero hero(name, id, 1, 10, start, nullptr);
}

#endif //SESSION

0 comments on commit 615791c

Please sign in to comment.