载入中...
搜索中...
未找到
Grid2D.cpp
浏览该文件的文档.
1#include "procgen/Grid2D.h"
2
3#include "procgen/Semantic.h"
4
5#include <algorithm>
6
7namespace eve::procgen {
8
9bool Grid2D::inBounds(int x, int y) const {
10 return x >= 0 && y >= 0 && x < width_ && y < height_;
11}
12
13void Grid2D::resize(int width, int height) {
14 width_ = width > 0 ? width : 0;
15 height_ = height > 0 ? height : 0;
16 cells_.assign(size_t(width_ * height_), Semantic::Empty);
17 detail_.assign(size_t(width_ * height_), 0);
18}
19
20int Grid2D::getWidth() const { return width_; }
21int Grid2D::getHeight() const { return height_; }
22
23void Grid2D::setCell(int x, int y, int semantic) {
24 if (!inBounds(x, y)) return;
25 cells_[size_t(y * width_ + x)] = uint32_t(semantic < 0 ? 0 : semantic);
26}
27
28int Grid2D::getCell(int x, int y) const {
29 if (!inBounds(x, y)) return int(Semantic::Empty);
30 return int(cells_[size_t(y * width_ + x)]);
31}
32
34 std::fill(cells_.begin(), cells_.end(), uint32_t(semantic < 0 ? 0 : semantic));
35}
36
37void Grid2D::setDetail(int x, int y, int value) {
38 if (!inBounds(x, y)) return;
39 detail_[size_t(y * width_ + x)] = uint8_t(value < 0 ? 0 : (value > 255 ? 255 : value));
40}
41
42int Grid2D::getDetail(int x, int y) const {
43 if (!inBounds(x, y)) return 0;
44 return int(detail_[size_t(y * width_ + x)]);
45}
46
47void Grid2D::setMeta(const std::string &key, const std::string &value) { meta_[key] = value; }
48
49std::string Grid2D::getMeta(const std::string &key, const std::string &defaultValue) const {
50 auto it = meta_.find(key);
51 return it == meta_.end() ? defaultValue : it->second;
52}
53
54void Grid2D::clearObjects() { objects_.clear(); }
55
56void Grid2D::addObjectAt(const std::string &name, const std::string &type, float x, float y) {
57 addObject(name, type, x, y, 0.f, 0.f, 0);
58}
59
60void Grid2D::addObject(const std::string &name, const std::string &type, float x, float y,
61 float width, float height, int gid) {
63 o.name = name;
64 o.type = type;
65 o.x = x;
66 o.y = y;
67 o.width = width;
68 o.height = height;
69 o.gid = uint32_t(gid < 0 ? 0 : gid);
70 objects_.push_back(std::move(o));
71}
72
73int Grid2D::getObjectCount() const { return int(objects_.size()); }
74
75std::string Grid2D::getObjectName(int i) const {
76 if (i < 0 || i >= int(objects_.size())) return {};
77 return objects_[size_t(i)].name;
78}
79std::string Grid2D::getObjectType(int i) const {
80 if (i < 0 || i >= int(objects_.size())) return {};
81 return objects_[size_t(i)].type;
82}
83float Grid2D::getObjectX(int i) const {
84 if (i < 0 || i >= int(objects_.size())) return 0.f;
85 return objects_[size_t(i)].x;
86}
87float Grid2D::getObjectY(int i) const {
88 if (i < 0 || i >= int(objects_.size())) return 0.f;
89 return objects_[size_t(i)].y;
90}
91float Grid2D::getObjectWidth(int i) const {
92 if (i < 0 || i >= int(objects_.size())) return 0.f;
93 return objects_[size_t(i)].width;
94}
95float Grid2D::getObjectHeight(int i) const {
96 if (i < 0 || i >= int(objects_.size())) return 0.f;
97 return objects_[size_t(i)].height;
98}
99int Grid2D::getObjectGid(int i) const {
100 if (i < 0 || i >= int(objects_.size())) return 0;
101 return int(objects_[size_t(i)].gid);
102}
103
104} // namespace eve::procgen
std::string value
std::string type
int y
Definition Grass.cpp:135
float height
Definition Grass.cpp:235
int x
Definition Grass.cpp:135
int width
const char * name
Definition RockMesh.cpp:21
uint32_t semantic
Definition WfcSimple.cpp:15
void setDetail(int x, int y, int value)
Per-cell detail layer (0..255), parallel to cells. Semantics stay in cells (palette/GID compatible); ...
Definition Grid2D.cpp:37
std::string getObjectType(int i) const
Definition Grid2D.cpp:79
int getWidth() const
Definition Grid2D.cpp:20
float getObjectWidth(int i) const
Definition Grid2D.cpp:91
int getDetail(int x, int y) const
Definition Grid2D.cpp:42
void resize(int width, int height)
Definition Grid2D.cpp:13
float getObjectY(int i) const
Definition Grid2D.cpp:87
void fill(int semantic)
Definition Grid2D.cpp:33
void setCell(int x, int y, int semantic)
Definition Grid2D.cpp:23
void addObject(const std::string &name, const std::string &type, float x, float y, float width, float height, int gid)
Definition Grid2D.cpp:60
int getCell(int x, int y) const
Definition Grid2D.cpp:28
float getObjectX(int i) const
Definition Grid2D.cpp:83
void addObjectAt(const std::string &name, const std::string &type, float x, float y)
Script-friendly: name/type + tile coords.
Definition Grid2D.cpp:56
int getObjectCount() const
Definition Grid2D.cpp:73
int getObjectGid(int i) const
Definition Grid2D.cpp:99
float getObjectHeight(int i) const
Definition Grid2D.cpp:95
std::string getObjectName(int i) const
Definition Grid2D.cpp:75
void setMeta(const std::string &key, const std::string &value)
Definition Grid2D.cpp:47
int getHeight() const
Definition Grid2D.cpp:21
std::string getMeta(const std::string &key, const std::string &defaultValue) const
Definition Grid2D.cpp:49
constexpr uint32_t Empty
Definition Semantic.h:10
A named, typed rectangle object from a Tiled objectgroup layer. Used by Map::setObjects / getObject* ...
Definition MapObject.h:12
uint32_t gid
Optional tileset GID (tile objects); 0 = none.
Definition MapObject.h:22
std::string type
Definition MapObject.h:14
float x
Top-left corner in pixels.
Definition MapObject.h:16
float width
Extent in pixels.
Definition MapObject.h:19
std::string name
Definition MapObject.h:13