commit e77fcf28
Add a spell for spawning prefabs
Changed files
| M | data/saland/spells/base_spells.json before |
| M | src/saland/Game.cpp before |
| M | src/saland/Prefabs.cpp before |
| M | src/saland/Prefabs.hpp before |
diff --git a/data/saland/spells/base_spells.json b/data/saland/spells/base_spells.json
index 9ad49cc..75e63f9 100644
--- a/data/saland/spells/base_spells.json
+++ b/data/saland/spells/base_spells.json
@@ -76,5 +76,9 @@
"name" : "spell_spawn_item",
"item_name" : "tree_pine"
}
+ ,{
+ "name" : "spell_place_prefab",
+ "type" : "tile"
+ }
]
}
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp
index 6373280..4d84ee3 100644
--- a/src/saland/Game.cpp
+++ b/src/saland/Game.cpp
@@ -209,6 +209,7 @@ struct Game::GameImpl {
bool consoleActive = false;
bool debugMenuActive = false;
int brushSize = 1; // Size of brush for tile placement/removal (1-5)
+ size_t selectedPrefabIndex = 0; // Which prefab is loaded for spell_place_prefab
};
static SpawnPoint GetSpawnpoint(const sago::tiled::TileMap& tm) {
@@ -475,9 +476,17 @@ void Game::Draw(SDL_Renderer* target) {
if (data->world_mouse_x >= 0 && data->world_mouse_y >= 0) {
int mousebox_x = data->world_mouse_x - data->world_mouse_x % 32 - data->topx;
int mousebox_y = data->world_mouse_y - data->world_mouse_y % 32 - data->topy;
- if (data->spell_holder->slot_spell.at(data->spell_holder->slot_selected).type == SpellCursorType::tile) {
+ const Spell& drawSelectedSpell = data->spell_holder->slot_spell.at(data->spell_holder->slot_selected);
+ if (drawSelectedSpell.type == SpellCursorType::tile) {
+ int boxWidth = 32 * data->brushSize;
+ int boxHeight = 32 * data->brushSize;
+ if (drawSelectedSpell.name == "spell_place_prefab" && getPrefabCount() > 0) {
+ const Prefab& prefab = getPrefabByIndex(data->selectedPrefabIndex);
+ boxWidth = 32 * prefab.width;
+ boxHeight = 32 * prefab.height;
+ }
int phys_x1 = mousebox_x, phys_y1 = mousebox_y;
- int phys_x2 = mousebox_x + 32 * data->brushSize, phys_y2 = mousebox_y + 32 * data->brushSize;
+ int phys_x2 = mousebox_x + boxWidth, phys_y2 = mousebox_y + boxHeight;
globalData.logicalResize.LogicalToPhysical(&phys_x1, &phys_y1);
globalData.logicalResize.LogicalToPhysical(&phys_x2, &phys_y2);
rectangleRGBA(globalData.screen, phys_x1, phys_y1, phys_x2, phys_y2, 255, 255, 0, 255);
@@ -514,6 +523,10 @@ void Game::Draw(SDL_Renderer* target) {
if (data->world_mouse_x >= 0 && data->world_mouse_y >= 0) {
std::string s = std::format("world_x = {}, world_y = {}, layer_info:{}", data->world_mouse_x/32, data->world_mouse_y/32,
GetLayerInfoForTile(data->gameRegion.world, data->world_mouse_x/32, data->world_mouse_y/32));
+ const Spell& bottomSelectedSpell = data->spell_holder->slot_spell.at(data->spell_holder->slot_selected);
+ if (bottomSelectedSpell.name == "spell_place_prefab" && getPrefabCount() > 0) {
+ s += std::format(", prefab: {}", getPrefabByIndex(data->selectedPrefabIndex).name);
+ }
data->bottomField.SetText(s);
}
else {
@@ -694,7 +707,17 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) {
}
}
if (event.type == SDL_MOUSEWHEEL) {
- if (event.wheel.y > 0) { // Scroll up
+ const Spell& selectedSpell = data->spell_holder->slot_spell.at(data->spell_holder->slot_selected);
+ if (selectedSpell.name == "spell_place_prefab" && getPrefabCount() > 0) {
+ size_t count = getPrefabCount();
+ if (event.wheel.y > 0) { // Scroll up
+ data->selectedPrefabIndex = (data->selectedPrefabIndex + 1) % count;
+ }
+ else if (event.wheel.y < 0) { // Scroll down
+ data->selectedPrefabIndex = (data->selectedPrefabIndex + count - 1) % count;
+ }
+ }
+ else if (event.wheel.y > 0) { // Scroll up
if (data->brushSize < 5) {
data->brushSize++;
}
@@ -1219,6 +1242,15 @@ void Game::Update() {
}
}
}
+ if (data->spell_holder->slot_spell.at(data->spell_holder->slot_selected).name == "spell_place_prefab") {
+ if (data->human->castTimeRemaining == 0 && getPrefabCount() > 0) {
+ data->human->castTimeRemaining = data->human->castTime;
+ data->human->animation = "spellcast";
+ int base_tile_x = data->world_mouse_x/32;
+ int base_tile_y = data->world_mouse_y/32;
+ data->gameRegion.SpawnPrefab(getPrefabByIndex(data->selectedPrefabIndex), base_tile_x, base_tile_y);
+ }
+ }
}
data->middleField.SetText(middleText);
}
diff --git a/src/saland/Prefabs.cpp b/src/saland/Prefabs.cpp
index 61926e5..bfb4729 100644
--- a/src/saland/Prefabs.cpp
+++ b/src/saland/Prefabs.cpp
@@ -118,6 +118,18 @@ Prefab getPrefab(const char* name) {
return p;
}
+size_t getPrefabCount() {
+ return prefabs.size();
+}
+
+const Prefab& getPrefabByIndex(size_t index) {
+ static Prefab blankPrefab;
+ if (index >= prefabs.size()) {
+ return blankPrefab;
+ }
+ return prefabs.at(index);
+}
+
void TestApplyPrefab(sago::tiled::TileMap& dest, int destX, int destY) {
if (prefabs.size() == 0) {
return;
diff --git a/src/saland/Prefabs.hpp b/src/saland/Prefabs.hpp
index 6b413ec..642f704 100644
--- a/src/saland/Prefabs.hpp
+++ b/src/saland/Prefabs.hpp
@@ -49,3 +49,7 @@ void ApplyPrefab(sago::tiled::TileMap& dest, int destX, int destY, const Prefab&
Prefab getPrefab(const char* name);
+size_t getPrefabCount();
+
+const Prefab& getPrefabByIndex(size_t index);
+