载入中...
搜索中...
未找到
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
55 objects_.clear();
56}
57
58void Grid2D::addObjectAt(const std::string &name, const std::string &type, float x, float y) {
59 addObject(name, type, x, y, 0.f, 0.f, 0);
60}
61
62void Grid2D::addObject(const std::string &name, const std::string &type, float x, float y,
63 float width, float height, int gid) {
64 GridObject o;
65 o.name = name;
66 o.type = type;
67 o.x = x;
68 o.y = y;
69 o.width = width;
70 o.height = height;
71 o.gid = uint32_t(gid < 0 ? 0 : gid);
72 objects_.push_back(std::move(o));
73}
74
75void Grid2D::addAssetObject(const std::string &name, const std::string &role,
76 const std::string &asset, float x, float y, float width, float height,
77 float rotationDegrees, int flags) {
78 addObject(name, role, x, y, width, height, 0);
79 objects_.back().asset = asset;
80 objects_.back().rotationDegrees = rotationDegrees;
81 objects_.back().placementFlags = uint32_t(flags < 0 ? 0 : flags);
82}
83
84int Grid2D::getObjectCount() const { return int(objects_.size()); }
85
86std::string Grid2D::getObjectName(int i) const {
87 if (i < 0 || i >= int(objects_.size())) return {};
88 return objects_[size_t(i)].name;
89}
90std::string Grid2D::getObjectType(int i) const {
91 if (i < 0 || i >= int(objects_.size())) return {};
92 return objects_[size_t(i)].type;
93}
94float Grid2D::getObjectX(int i) const {
95 if (i < 0 || i >= int(objects_.size())) return 0.f;
96 return objects_[size_t(i)].x;
97}
98float Grid2D::getObjectY(int i) const {
99 if (i < 0 || i >= int(objects_.size())) return 0.f;
100 return objects_[size_t(i)].y;
101}
102float Grid2D::getObjectWidth(int i) const {
103 if (i < 0 || i >= int(objects_.size())) return 0.f;
104 return objects_[size_t(i)].width;
105}
106float Grid2D::getObjectHeight(int i) const {
107 if (i < 0 || i >= int(objects_.size())) return 0.f;
108 return objects_[size_t(i)].height;
109}
110int Grid2D::getObjectGid(int i) const {
111 if (i < 0 || i >= int(objects_.size())) return 0;
112 return int(objects_[size_t(i)].gid);
113}
114std::string Grid2D::getObjectAsset(int i) const {
115 if (i < 0 || i >= int(objects_.size())) return {};
116 return objects_[size_t(i)].asset;
117}
118float Grid2D::getObjectRotation(int i) const {
119 if (i < 0 || i >= int(objects_.size())) return 0.f;
120 return objects_[size_t(i)].rotationDegrees;
121}
122int Grid2D::getObjectFlags(int i) const {
123 if (i < 0 || i >= int(objects_.size())) return 0;
124 return int(objects_[size_t(i)].placementFlags);
125}
126
127} // namespace eve::procgen
double value
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float rotationDegrees
std::uint32_t key
std::uint32_t height
std::uint32_t width
std::string name
uint32_t semantic
Definition WfcSimple.cpp:15
std::string getObjectAsset(int i) const
Returns the object asset.
Definition Grid2D.cpp:114
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
Returns the object type.
Definition Grid2D.cpp:90
void clearObjects()
Clears objects.
Definition Grid2D.cpp:54
int getWidth() const
Returns the width.
Definition Grid2D.cpp:20
float getObjectWidth(int i) const
Returns the object width.
Definition Grid2D.cpp:102
int getDetail(int x, int y) const
Returns the detail.
Definition Grid2D.cpp:42
void resize(int width, int height)
Resize.
Definition Grid2D.cpp:13
float getObjectY(int i) const
Returns the object y.
Definition Grid2D.cpp:98
float getObjectRotation(int i) const
Returns the object rotation.
Definition Grid2D.cpp:118
void fill(int semantic)
Fill.
Definition Grid2D.cpp:33
void setCell(int x, int y, int semantic)
Sets the cell.
Definition Grid2D.cpp:23
void addObject(const std::string &name, const std::string &type, float x, float y, float width, float height, int gid)
Adds object.
Definition Grid2D.cpp:62
int getCell(int x, int y) const
Returns the cell.
Definition Grid2D.cpp:28
int getObjectFlags(int i) const
Returns the object flags.
Definition Grid2D.cpp:122
float getObjectX(int i) const
Returns the object x.
Definition Grid2D.cpp:94
void addObjectAt(const std::string &name, const std::string &type, float x, float y)
Script-friendly: name/type + tile coords.
Definition Grid2D.cpp:58
int getObjectCount() const
Returns the object count.
Definition Grid2D.cpp:84
int getObjectGid(int i) const
Returns the object gid.
Definition Grid2D.cpp:110
float getObjectHeight(int i) const
Returns the object height.
Definition Grid2D.cpp:106
std::string getObjectName(int i) const
Returns the object name.
Definition Grid2D.cpp:86
void setMeta(const std::string &key, const std::string &value)
Sets the meta.
Definition Grid2D.cpp:47
int getHeight() const
Returns the height.
Definition Grid2D.cpp:21
std::string getMeta(const std::string &key, const std::string &defaultValue) const
Returns the meta.
Definition Grid2D.cpp:49
void addAssetObject(const std::string &name, const std::string &role, const std::string &asset, float x, float y, float width, float height, float rotationDegrees, int flags)
Add a placed asset while preserving its semantic role, asset id and orientation.
Definition Grid2D.cpp:75
const char * defaultValue
constexpr uint32_t Empty
Definition Semantic.h:12
Named rectangle or asset placement emitted by a procedural generator.
Definition Grid2D.h:19