载入中...
搜索中...
未找到
ArchSpaceTileMapProjector.cpp
浏览该文件的文档.
2
4#include "map/TileLayer.h"
5
6#include <cmath>
7#include <utility>
8
10namespace {
11
13
15 switch (kind) {
16 case archspace::GridCellKind::Floor: return palette.floorGid;
17 case archspace::GridCellKind::Wall: return palette.wallGid;
18 case archspace::GridCellKind::Portal: return palette.portalGid;
19 case archspace::GridCellKind::Solid: return palette.solidGid;
20 case archspace::GridCellKind::Empty: return 0;
21 }
22 return 0;
23}
24
25} // namespace
26
28
29const char* TileMapProjector::kind() const noexcept { return orientation_ == "isometric" ? "tile.iso" : "tile.ortho"; }
30
31void TileMapProjector::setLevelId(std::string levelId) { levelId_ = std::move(levelId); }
32
33void TileMapProjector::setCellSize(float cellSize) { cellSize_ = cellSize; }
34
36 if (orientation != "orthogonal" && orientation != "isometric")
38 eve::DiagnosticCode::InvalidArgument, "ArchSpace tile orientation must be orthogonal or isometric",
39 "orientation", {}, "archspace.map"));
40 orientation_ = std::move(orientation);
41 return ok();
42}
43
45
47 if (ownsLayer_ && layer_ && layer_ != layer) {
48 layer_->release();
49 layer_ = nullptr;
50 }
51 layer_ = layer;
52 ownsLayer_ = false;
53}
54
55eve::Result<void> TileMapProjector::ensureLayer(int width, int height, float tileSize) {
56 if (width <= 0 || height <= 0)
58 "ArchSpace tile layer size must be positive", "size",
59 {}, "archspace.map"));
60 if (!layer_) {
61 layer_ = map::TileLayer::createLayer(width, height, tileSize, tileSize);
62 ownsLayer_ = true;
63 if (!layer_)
65 eve::DiagnosticCode::Failed, "ArchSpace failed to allocate TileLayer", "layer", {}, "archspace.map"));
66 } else {
67 layer_->resize(width, height);
68 layer_->setTileSize(tileSize, tileSize);
69 }
70 const std::string config = std::string("{\"orientation\":\"") + orientation_ + "\"}";
71 if (!layer_->applyConfig(config))
73 "ArchSpace failed to apply TileLayer orientation",
74 "orientation", {}, "archspace.map"));
75 return ok();
76}
77
79 if (levelId_.empty())
81 "ArchSpace TileMapProjector requires levelId",
82 "levelId", {}, "archspace.map"));
83 if (!std::isfinite(cellSize_) || cellSize_ <= 0.f)
85 eve::DiagnosticCode::InvalidArgument, "ArchSpace TileMapProjector cellSize must be finite and > 0",
86 "cellSize", {}, "archspace.map"));
87
88 auto baked = archspace::bakeGridProjection(document, levelId_, static_cast<double>(cellSize_), orientation_);
89 if (!baked.ok()) return eve::Result<void>::failure(baked.status());
90 grid_ = std::move(baked).takeValue();
91
92 if (grid_.width == 0 || grid_.height == 0) {
93 if (layer_) layer_->clear();
94 revision_ = revision;
95 return ok();
96 }
97
98 auto prepared = ensureLayer(grid_.width, grid_.height, cellSize_);
99 if (!prepared.ok()) return prepared;
100
101 layer_->setOrigin(static_cast<float>(grid_.originX), static_cast<float>(grid_.originZ));
102 layer_->clear();
103 layer_->setTileNavigationProfile(palette_.floorGid, true, 1.f, 15, 15, false);
104 layer_->setTileNavigationProfile(palette_.portalGid, true, 1.f, 15, 15, false);
105 layer_->setTileNavigationProfile(palette_.wallGid, false, 0.f, 0, 0, true);
106 layer_->setTileNavigationProfile(palette_.solidGid, false, 0.f, 0, 0, true);
107
108 for (int z = 0; z < grid_.height; ++z) {
109 for (int x = 0; x < grid_.width; ++x) {
110 const std::size_t idx = static_cast<std::size_t>(z * grid_.width + x);
111 const int gid = gidFor(palette_, grid_.cells[idx]);
112 if (gid != 0) layer_->setTile(x, z, gid);
113 }
114 }
115 revision_ = revision;
116 return ok();
117}
118
120 if (ownsLayer_ && layer_) {
121 layer_->release();
122 layer_ = nullptr;
123 ownsLayer_ = false;
124 } else {
125 layer_ = nullptr;
126 }
127 grid_ = {};
128 revision_ = 0;
129}
130
131} // namespace eve::archspace_map
double z
double x
std::uint64_t revision
std::string palette
std::uint32_t height
std::uint32_t width
TileLayer * layer
int idx
std::string kind
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Authoritative ArchSpace document (Pascal-like node graph).
eve::Result< void > sync(const archspace::Document &document, std::uint64_t revision) override
Rebuild derived presentation from document at revision.
void setLevelId(std::string levelId)
Level id to rasterize on sync (required, non-empty).
map::TileLayer * layer() const noexcept
Last synced TileLayer (owned or borrowed). @ownership Non-owning borrow; do not free unless caller ow...
void setCellSize(float cellSize)
Metres per cell / tile edge (must be finite and > 0).
std::uint64_t revision() const noexcept
Revision last successfully synced.
void setPalette(ArchSpaceTilePalette palette)
GID mapping for floor/wall/portal/solid.
void clear() override
Drop derived presentation state.
const std::string & orientation() const noexcept
Current orientation wire name.
void setLayer(map::TileLayer *layer)
Borrow an existing layer; projector will not delete it. @ownership Non-owning; caller retains ownersh...
eve::Result< void > setOrientation(std::string orientation)
"orthogonal" or "isometric".
const char * kind() const noexcept override
Stable projector kind. @ownership Non-owning pointer to a static string literal; do not free....
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
Definition TileLayer.h:33
void clear()
Clears the layer to empty.
void setOrigin(float x, float y)
World origin of the layer (pixels).
Definition TileLayer.cpp:27
void setTile(int tx, int ty, int gid)
Tile GID access; 0 = empty.
Definition TileLayer.cpp:56
void setTileNavigationProfile(int gid, bool walkable, float cost, int enterMask, int exitMask, bool opaque, int semanticFlags=0)
Sets canonical navigation, collision and visibility metadata for one GID.
void resize(int mapW, int mapH)
Resizes the tile grid (existing GIDs preserved where possible).
Definition TileLayer.cpp:46
bool applyConfig(const std::string &json)
Applies a map JSON config (Config + Tiles + Tileset + Draw).
static TileLayer * createLayer(int mapW, int mapH, float tileW=32.f, float tileH=32.f)
Definition TileLayer.cpp:14
void setTileSize(float tileW, float tileH)
Definition TileLayer.cpp:40
void release() override
Entity.
Definition TileLayer.h:39
GridCellKind
Semantic cell kind for grid projections (tilemap / HD2D hosts).
eve::Result< GridProjection > bakeGridProjection(const Document &document, const std::string &levelId, double cellSize, const std::string &orientation)
Rasterize one level into a grid for ortho/iso/HD2D hosts.
std::vector< GridCellKind > cells
GID palette written into a TileLayer by TileMapProjector.