summary refs log tree commit diff stats
path: root/src/map.cpp
blob: c4f31d1225ce43c6b8246476ac422af1d5cad7a1 (plain) (blame)
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
#include "map.h"
#include <cstdlib>
#include <cstring>
#include <map>
#include "entityfactory.h"
#include "entity.h"
#include "game.h"
#include "consts.h"

Map::Map(int id)
{
  this->id = id;
  mapdata = (int*) calloc(1, sizeof(int));
}

Map::Map(const Map& map)
{
  mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
  memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int));
  
  id = map.id;
  title = map.title;
  adjacents = map.adjacents;
  entities = map.entities;
}

Map::Map(Map&& map) : Map()
{
  swap(*this, map);
}

Map::~Map()
{
  free(mapdata);
}

Map& Map::operator= (Map map)
{
  swap(*this, map);
  
  return *this;
}

void swap(Map& first, Map& second)
{
  std::swap(first.mapdata, second.mapdata);
  std::swap(first.title, second.title);
  std::swap(first.adjacents, second.adjacents);
  std::swap(first.id, second.id);
  std::swap(first.entities, second.entities);
}

int Map::getID() const
{
  return id;
}

const int* Map::getMapdata() const
{
  return mapdata;
}

std::string Map::getTitle() const
{
  return title;
}

void Map::createEntities(std::list<std::shared_ptr<Entity>>& entities) const
{
  for (auto data : this->entities)
  {
    auto entity = EntityFactory::createNamedEntity(data.name);
    entity->position = data.position;
    
    entities.push_back(entity);
  }
}

bool Map::operator==(const Map& other) const
{
  return id == other.id;
}

bool Map::operator!=(const Map& other) const
{
  return id != other.id;
}

Map::MoveType Map::moveTypeForShort(std::string str)
{
  if (str == "wrap") return MoveType::Wrap;
  if (str == "warp") return MoveType::Warp;
  if (str == "reverseWarp") return MoveType::ReverseWarp;
  
  return MoveType::Wall;
}

Map::MoveDir Map::moveDirForShort(std::string str)
{
  if (str == "right") return MoveDir::Right;
  if (str == "up") return MoveDir::Up;
  if (str == "down") return MoveDir::Down;
  
  return MoveDir::Left;
}

static const Map::Adjacent defaultAdjacent {};
const Map::Adjacent& Map::getAdjacent(MoveDir dir) const
{
  if (adjacents.count(dir) > 0)
  {
    return adjacents.at(dir);
  } else {
    return defaultAdjacent;
  }
}

bool Map::moveTypeTakesMap(MoveType type)
{
  switch (type)
  {
    case MoveType::Wall: return false;
    case MoveType::Wrap: return false;
    case MoveType::Warp: return true;
    case MoveType::ReverseWarp: return true;
  }
}

void Map::setMapdata(int* mapdata)
{
  free(this->mapdata);
  this->mapdata = mapdata;
}

void Map::setTitle(std::string title)
{
  this->title = title;
}

void Map::setAdjacent(MoveDir dir, MoveType type, int map)
{
  Adjacent& cur = adjacents[dir];
  cur.type = type;
  if (map != -1) cur.map = map;
}

void Map::addEntity(EntityData& data)
{
  entities.push_back(data);
}