From 6bca6d021e632cf397df59bc4aaf8c1a35e3bf58 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 10 Feb 2025 12:58:24 -0500 Subject: [PATCH] Sprite is now a component we can place on anything to render it. --- assets/config.json | 6 +++--- assets/devices.json | 9 ++++++--- assets/enemies.json | 20 +++++++++++++++++--- assets/items.json | 18 ++++++++++++------ components.cpp | 2 ++ components.hpp | 4 ++++ raycaster.cpp | 15 ++++----------- worldbuilder.cpp | 2 ++ 8 files changed, 50 insertions(+), 26 deletions(-) diff --git a/assets/config.json b/assets/config.json index f08cc4d..d9e9b5c 100644 --- a/assets/config.json +++ b/assets/config.json @@ -14,8 +14,8 @@ "player": { }, "worldgen": { - "enemy_probability": 20, - "empty_room_probability": 10, - "device_probability": 20 + "enemy_probability": 100, + "empty_room_probability": 0, + "device_probability": 0 } } diff --git a/assets/devices.json b/assets/devices.json index fa58b10..31ce4ac 100644 --- a/assets/devices.json +++ b/assets/devices.json @@ -13,7 +13,8 @@ }, {"_type": "Device", "config": {"test": true}, - "events": ["Events::GUI::STAIRS_DOWN"]} + "events": ["Events::GUI::STAIRS_DOWN"]}, + {"_type": "Sprite", "name": "barrel"} ] }, "STAIRS_UP": { @@ -29,7 +30,8 @@ }, {"_type": "Device", "config": {"test": true}, - "events": ["Events::GUI::STAIRS_UP"]} + "events": ["Events::GUI::STAIRS_UP"]}, + {"_type": "Sprite", "name": "barrel"} ] }, "SPIKE_TRAP": { @@ -44,7 +46,8 @@ }, {"_type": "Device", "config": {"test": true}, - "events": ["Events::GUI::TRAP"]} + "events": ["Events::GUI::TRAP"]}, + {"_type": "Sprite", "name": "barrel"} ] } } diff --git a/assets/enemies.json b/assets/enemies.json index 0cad1c6..2f0f995 100644 --- a/assets/enemies.json +++ b/assets/enemies.json @@ -11,15 +11,29 @@ {"_type": "EnemyConfig", "hearing_distance": 5} ] }, + "KNIGHT": { + "components": [ + {"_type": "Tile", "display": "\u088d", + "foreground": [131, 213, 238], + "background": [30, 20, 75] + }, + {"_type": "Combat", "hp": 200, "damage": 15, "dead": false}, + {"_type": "Motion", "dx": 0, "dy": 0, "random": false}, + {"_type": "LightSource", "strength": 70, "radius": 2}, + {"_type": "EnemyConfig", "hearing_distance": 5}, + {"_type": "Sprite", "name": "armored_knight"} + ] + }, "EVIL_EYE": { "components": [ - {"_type": "Tile", "display": "\u08ac", - "foreground": [75, 200, 125], + {"_type": "Tile", "display": "\u0758", + "foreground": [205, 164, 246], "background": [30, 20, 75] }, {"_type": "Combat", "hp": 100, "damage": 50, "dead": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false}, - {"_type": "EnemyConfig", "hearing_distance": 10} + {"_type": "EnemyConfig", "hearing_distance": 10}, + {"_type": "Sprite", "name": "evil_eye"} ] } } diff --git a/assets/items.json b/assets/items.json index e190e3a..0d57dbe 100644 --- a/assets/items.json +++ b/assets/items.json @@ -9,7 +9,8 @@ {"_type": "Tile", "display": "\u0f08", "foreground": [24, 120, 189], "background": [230,120, 120] - } + }, + {"_type": "Sprite", "name": "barrel"} ] }, "SWORD_RUSTY": { @@ -22,7 +23,8 @@ {"_type": "Tile", "display": "\u1e37", "foreground": [24, 120, 189], "background": [24, 120, 189] - } + }, + {"_type": "Sprite", "name": "barrel"} ] }, "SWORD_LIGHT_AND_FLAME": { @@ -36,7 +38,8 @@ "foreground": [24, 205, 210], "background": [24, 205, 210] }, - {"_type": "Weapon", "damage": 30} + {"_type": "Weapon", "damage": 30}, + {"_type": "Sprite", "name": "barrel"} ] }, "CHEST_SMALL": { @@ -48,7 +51,8 @@ "foreground": [150, 100, 189], "background": [150, 100, 189] }, - {"_type": "Loot", "amount": 10} + {"_type": "Loot", "amount": 10}, + {"_type": "Sprite", "name": "barrel"} ], "inventory_count": 1 }, @@ -62,7 +66,8 @@ "foreground": [24, 205, 210], "background": [24, 205, 210] }, - {"_type": "LightSource", "strength": 60, "radius": 1.8} + {"_type": "LightSource", "strength": 60, "radius": 1.8}, + {"_type": "Sprite", "name": "barrel"} ] }, "POTION_HEALING_SMALL": { @@ -75,7 +80,8 @@ "foreground": [255, 205, 189], "background": [255, 205, 189] }, - {"_type": "Curative", "hp": 20} + {"_type": "Curative", "hp": 20}, + {"_type": "Sprite", "name": "barrel"} ] } } diff --git a/components.cpp b/components.cpp index 8e8575c..a390365 100644 --- a/components.cpp +++ b/components.cpp @@ -11,6 +11,7 @@ namespace components { ENROLL_COMPONENT(Combat, hp, damage, dead); ENROLL_COMPONENT(LightSource, strength, radius); ENROLL_COMPONENT(Device, config, events); + ENROLL_COMPONENT(Sprite, name); void configure_entity(const ComponentMap& component_map, DinkyECS::World& world, DinkyECS::Entity ent, json& data) { for (auto &i : data) { @@ -31,5 +32,6 @@ namespace components { components::enroll(component_map); components::enroll(component_map); components::enroll(component_map); + components::enroll(component_map); } } diff --git a/components.hpp b/components.hpp index fcdb869..603485c 100644 --- a/components.hpp +++ b/components.hpp @@ -77,6 +77,10 @@ namespace components { void configure_events(std::vector &event_names); }; + struct Sprite { + string name; + }; + void configure(ComponentMap& component_map); // these need to be here if you're using components::convert outside of components.cpp diff --git a/raycaster.cpp b/raycaster.cpp index 7fa2918..7dc33bf 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -343,19 +343,12 @@ void Raycaster::set_level(GameLevel level) { auto& player = world->get_the(); $map = $textures.convert_char_to_texture(tiles.$tile_ids); - world->query([&](const auto ent, auto &pos) { + world->query([&](const auto ent, auto& sprite) { // player doesn't need a sprite if(player.entity == ent) return; - - if(world->has(ent)) { - fmt::println("enemy: {}, pos={},{}", ent, pos.location.x, pos.location.y); - auto sprite_txt = $textures.sprite_textures.at("evil_eye"); - $sprites.try_emplace(ent, sprite_txt); - } else { - fmt::println("item or device: {}, pos={},{}", ent, pos.location.x, pos.location.y); - auto sprite_txt = $textures.sprite_textures.at("barrel"); - $sprites.try_emplace(ent, sprite_txt); - } + fmt::println("entity {} will have sprite named {}", ent, sprite.name); + auto sprite_txt = $textures.sprite_textures.at(sprite.name); + $sprites.try_emplace(ent, sprite_txt); }); } diff --git a/worldbuilder.cpp b/worldbuilder.cpp index ea5f36c..9cca7c7 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -234,6 +234,8 @@ void WorldBuilder::randomize_entities(DinkyECS::World &world, GameConfig &config int rand_entity = Random::uniform(0, keys.size() - 1); std::string key = keys[rand_entity]; + // BUG: this may crash if PLAYER_TILE isn't first + if(key == "PLAYER_TITLE") key = keys[rand_entity + 1]; auto entity_data = entity_db[key]; // pass that to the config as it'll be a generic json