载入中...
搜索中...
未找到
Palette.cpp
浏览该文件的文档.
1#include "procgen/Palette.h"
2
3#include "map/TileLayer.h"
4
5#include "procgen/Semantic.h"
6
7namespace eve::procgen {
8
9void PaletteTable::setGid(const std::string &palette, const std::string &semantic, int gid) {
10 palettes_[palette][semantic] = gid;
11}
12
13int PaletteTable::getGid(const std::string &palette, const std::string &semantic) const {
14 auto pit = palettes_.find(palette);
15 if (pit == palettes_.end()) return 0;
16 auto sit = pit->second.find(semantic);
17 return sit == pit->second.end() ? 0 : sit->second;
18}
19
20int PaletteTable::getGid(const std::string &palette, uint32_t sid) const {
21 return getGid(palette, semanticName(sid));
22}
23
24bool PaletteTable::applyToLayer(const Grid2D &grid, const std::string &palette,
25 map::TileLayer *layer, std::string *error) const {
26 if (!layer) {
27 if (error) *error = "applyToLayer: null TileLayer";
28 return false;
29 }
30 const int w = grid.getWidth();
31 const int h = grid.getHeight();
32 if (w <= 0 || h <= 0) {
33 if (error) *error = "applyToLayer: empty grid";
34 return false;
35 }
36 if (layer->getMapWidth() != w || layer->getMapHeight() != h) {
37 layer->resize(w, h);
38 }
39 for (int y = 0; y < h; ++y) {
40 for (int x = 0; x < w; ++x) {
41 layer->setTile(x, y, getGid(palette, grid.getCell(x, y)));
42 }
43 }
44 return true;
45}
46
47} // namespace eve::procgen
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
int h
int w
std::string error
TileLayer * layer
uint32_t semantic
Definition WfcSimple.cpp:15
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
Definition TileLayer.h:30
Intermediate 2D generation result. cells store semantic ids (see Semantic.h), not tile GIDs — convert...
Definition Grid2D.h:16
int getWidth() const
Definition Grid2D.cpp:20
int getCell(int x, int y) const
Definition Grid2D.cpp:28
int getHeight() const
Definition Grid2D.cpp:21
bool applyToLayer(const Grid2D &grid, const std::string &palette, map::TileLayer *layer, std::string *error) const
Definition Palette.cpp:24
void setGid(const std::string &palette, const std::string &semantic, int gid)
Definition Palette.cpp:9
int getGid(const std::string &palette, const std::string &semantic) const
Definition Palette.cpp:13
const char * semanticName(uint32_t id)
Definition Semantic.cpp:22