summaryrefslogtreecommitdiff
path: root/src/package.cpp
blob: 9b35c630dd65a201e9af7255cd56ce98436cdf5e (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "package.hpp"

#include <algorithm>
#include <filesystem>
#include <fstream>

namespace silly::editor {
  TileSet &LevelPackage::get_tileset() { return this->tileset; }

  void LevelPackage::add_level(TileLevel level) {
    this->levels.push_back(level);
  }

  TileLevel &LevelPackage::get_current_level() {
    return this->levels.at(this->currentLevelIndex);
  }

  void LevelPackage::move_to_level_index(int index) {
    this->currentLevelIndex = std::min(index, (int)this->levels.size() - 1);
  }

  const int LevelPackage::get_current_level_index() const {
    return this->currentLevelIndex;
  }

  const std::vector<TileLevel> &LevelPackage::get_levels() const {
    return this->levels;
  }

  const std::string &LevelPackage::get_name() const { return this->name; }

  void LevelPackage::set_name(const std::string &name) { this->name = name; }

  void LevelPackage::clear() {
    this->name = "";
    this->currentLevelIndex = 0;
    this->levels.clear();
    this->tileset.clear();
  }

  std::string LevelPackage::export_to_string() const {
    std::ostringstream oss;

    // package data
    oss << "[package]\n"
        << "# name;version\n"
        << name << ";"
        << "0\n";

    // tileset data
    oss << this->tileset.export_to_string() << "\n";

    // level data
    for (auto it = this->levels.begin(); it != this->levels.end(); ++it) {
      oss << it->export_to_string() << "\n";
    }

    return oss.str();
  }

  void LevelPackage::save(LevelPackageFormat format,
                          std::string &file_path) const {
    if (format != PACKAGE_TXT) {
      return;
    }

    // writing package file
    std::stringstream pkg_file_path;

    pkg_file_path << file_path << "/" << this->name << ".txt";

    std::string package = this->export_to_string();

    std::ofstream ifs(pkg_file_path.str());
    ifs << package;
    ifs.close();

    // copying resource files
    for (auto it = this->tileset.get_entries().begin();
         it != this->tileset.get_entries().end(); ++it) {
      TilesetTile *t = it->get();
      std::string old_path = t->path;
      std::string new_path = file_path + "/" + t->name + "." + t->extension;
      std::filesystem::copy_file(old_path, new_path);
    }
  }

  void LevelPackage::load(LevelPackageFormat format, std::string &file_path) {
    if (format != PACKAGE_TXT) {
      return;
    }

    std::string folder_path;
#ifdef WIN32
    char delim = '\\';
#else
    char delim = '/';
#endif

    {
      auto parts = utils::split_text(file_path, delim);
      auto name = parts[parts.size() - 1];
      folder_path = file_path.substr(0, file_path.size() - name.size() - 1);
    }

    this->clear();

    std::ifstream ifs(file_path);

    int sector = 0;
    std::string line;

    while (std::getline(ifs, line)) {
      // skip comments
      if (line[0] == '#' || line.empty()) continue;

      // changing sector
      if (line == "[package]") {
        sector = 0;
        continue;
      } else if (line == "[tileset]") {
        sector = 1;
        continue;
      } else if (line == "[level]") {
        sector = 2;
        continue;
      } else if (line == "[[floor]]") {
        sector = 3;
        continue;
      } else if (line == "[[[tiles]]]") {
        sector = 4;
        continue;
      }

      auto parts = utils::split_text(line, ';');

      switch (sector) {
        // package
        case 0: {
          std::string name = parts.at(0);
          // TODO: do something with version
          int version = std::stoi(parts.at(1));
          this->set_name(name);
          break;
        }
        // tileset
        case 1: {
          int id = std::stoi(parts.at(0));
          int type = std::stoi(parts.at(1));
          std::string file_name = parts.at(2);
          this->tileset.add_entry(id, folder_path + delim + file_name,
                                  (TilesetTileType)type);
          break;
        }
        // level
        case 2: {
          std::string name = parts.at(0);
          TileLevel level(name);
          this->add_level(level);
          this->move_to_level_index(this->levels.size() - 1);
          break;
        }
        // floor
        case 3: {
          int width = std::stoi(parts.at(0));
          int height = std::stoi(parts.at(1));

          TileFloor floor(width, height);
          this->get_current_level().add_floor(floor);
          break;
        }
        // tiles
        case 4: {
          int tile_id = std::stoi(parts.at(0));
          int x = std::stoi(parts.at(1));
          int y = std::stoi(parts.at(2));
          float rotation = std::stof(parts.at(3));

          auto tile =
              std::find_if(this->tileset.get_entries().begin(),
                           this->tileset.get_entries().end(),
                           [&tile_id](const std::shared_ptr<TilesetTile> &t) {
                             return t->id == tile_id;
                           });

          if (tile == this->tileset.get_entries().end()) {
            break;
          }

          this->get_current_level().get_current_floor().place_tile(
              *tile, {x, y}, rotation);
          break;
        }
        default:
          break;
      }
    }

    ifs.close();
  }
}