From 52cccf4665fd6b0ef629fe86429831e88ee52e17 Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Wed, 10 Jan 2024 16:59:17 +0100 Subject: [PATCH] Inspection for Animation component --- game/components.c | 26 ++++++++++++++- game/game_tileset.h | 31 ++++++++++++++---- game/main.c | 67 +++++++++++++++++++++----------------- scripts/extract_common.py | 19 +++++++++-- scripts/extract_tileset.py | 5 +-- 5 files changed, 107 insertions(+), 41 deletions(-) diff --git a/game/components.c b/game/components.c index db3d70b..6cd45f8 100644 --- a/game/components.c +++ b/game/components.c @@ -166,11 +166,35 @@ void igTextureRegion(ecs_world_t *ecs, tex->flipY = flipY; } +static void igEntityType(EntityType *type) { + const char *prettyStrings[ENTITY_COUNT]; + for (int i = 0; i < ENTITY_COUNT; i++) { + prettyStrings[i] = getEntityStr(i); + } + igCombo_Str_arr("EntityType", type, prettyStrings, ENTITY_COUNT, -1); +} +static void igAnimType(AnimType *type) { + const char *prettyStrings[ANIM_COUNT]; + for (int i = 0; i < ANIM_COUNT; i++) { + prettyStrings[i] = getEntityAnimationStr(i); + } + igCombo_Str_arr("AnimType", type, prettyStrings, ANIM_COUNT, -1); +} void igAnimation(ecs_world_t *ecs, ecs_entity_t entity, ecs_entity_t comp) { - //Animation *anim = ecs_get_mut_id(ecs, entity, comp); + Animation *anim = ecs_get_mut_id(ecs, entity, comp); + EntityType curEntityType = anim->entityType; + igEntityType(&curEntityType); + anim->entityType = curEntityType; + AnimType curAnimType = anim->animType; + igAnimType(&curAnimType); + anim->animType = curAnimType; + + igText("tileset: %p", anim->tileset); + igText("curFrame: %d", anim->curFrame); + igText("elapsed: %.2f < %.2F", anim->elapsed, anim->frame.duration); } void igEasing(ecs_world_t *ecs, ecs_entity_t entity, ecs_entity_t comp) { diff --git a/game/game_tileset.h b/game/game_tileset.h index f51875c..08c936a 100644 --- a/game/game_tileset.h +++ b/game/game_tileset.h @@ -286,11 +286,6 @@ static BzTileID getBuildingTile(BuildingType type) { } } -static BuildingType getBuildingFromStr(const char *str) { - if (strncmp("keep", str, 4) == 0) return BUILDING_KEEP; - return BUILDING_COUNT; -} - static const char *getBuildingStr(BuildingType type) { switch (type) { case BUILDING_KEEP: return "keep"; @@ -298,7 +293,7 @@ static const char *getBuildingStr(BuildingType type) { } } -static BuildingType getBuildingSize(BuildingType type, BzTileID *outWidth, BzTileID *outHeight) { +static BuildingType getBuildingSize(BuildingType type, i32 *outWidth, i32 *outHeight) { switch (type) { case BUILDING_KEEP: if (outWidth) *outWidth = 3; @@ -344,6 +339,30 @@ static BzTileID getEntityTile(EntityType type) { } } +static const char *getEntityStr(EntityType type) { + switch (type) { + case ENTITY_WORKER: return "worker"; + case ENTITY_WOOD: return "wood"; + case ENTITY_STONE: return "stone"; + case ENTITY_APPLE: return "apple"; + case ENTITY_GOLD: return "gold"; + case ENTITY_POP: return "pop"; + default: return NULL; + } +} + +static const char *getEntityAnimationStr(AnimType type) { + switch (type) { + case ANIM_IDLE: return "ANIM_IDLE"; + case ANIM_WALK: return "ANIM_WALK"; + case ANIM_HURT: return "ANIM_HURT"; + case ANIM_DIE: return "ANIM_DIE"; + case ANIM_COUNT: return "ANIM_COUNT"; + case ANIM_NONE: return "ANIM_NONE"; + default: return NULL; + } +} + static bool entityHasAnimation(EntityType entity, AnimType type) { switch (entity) { case ENTITY_WORKER: diff --git a/game/main.c b/game/main.c index 2de2cdf..eeb293a 100644 --- a/game/main.c +++ b/game/main.c @@ -686,24 +686,29 @@ void render(float dt, void *userData) { void igInspectComp(const char *label, ecs_entity_t entity, ecs_entity_t comp, ImGuiCompFn fn) { igPushID_Int(comp); - igSeparatorText(label); - bool isAttached = ecs_has_id(ECS, entity, comp); - igCheckbox("Attached", &isAttached); + if (igTreeNode_Str(label)) { + bool isAttached = ecs_has_id(ECS, entity, comp); + igCheckbox("Attached", &isAttached); - if (isAttached) - fn(ECS, entity, comp); + if (isAttached) + fn(ECS, entity, comp); - if (isAttached != ecs_has_id(ECS, entity, comp)) { - if (!isAttached) { - ecs_remove_id(ECS, entity, comp); - } else { - ecs_set_id(ECS, entity, comp, 0, NULL); + if (isAttached != ecs_has_id(ECS, entity, comp)) { + if (!isAttached) { + ecs_remove_id(ECS, entity, comp); + } else { + ecs_set_id(ECS, entity, comp, 0, NULL); + } } + igTreePop(); } - igPopID(); } void igInspectWindow(ecs_entity_t entity, bool *open) { + if (!ecs_is_alive(ECS, entity)) { + *open = false; + return; + } igSetNextWindowSize((ImVec2) {300, 440}, ImGuiCond_FirstUseEver); char buf[64]; snprintf(buf, sizeof(buf), "Entity: %ld", entity); @@ -731,25 +736,27 @@ void igInspectWindow(ecs_entity_t entity, bool *open) { else igTextColored((ImVec4) {1, 0, 0, 1}, "NONE"); } - igInspectComp("Resource", entity, ecs_id(Resource), igResource); - igInspectComp("Owner", entity, ecs_id(Owner), igOwner); - igInspectComp("SpatialGridID", entity, ecs_id(SpatialGridID), igSpatialGridID); - igInspectComp("Position", entity, ecs_id(Position), igVec2Comp); - igInspectComp("Size", entity, ecs_id(Size), igVec2Comp); - igInspectComp("Velocity", entity, ecs_id(Velocity), igVec2Comp); - igInspectComp("TargetPosition", entity, ecs_id(TargetPosition), igVec2Comp); - igInspectComp("Steering", entity, ecs_id(Steering), igVec2Comp); - igInspectComp("Rotation", entity, ecs_id(Rotation), igFloat); - igInspectComp("Path", entity, ecs_id(Path), igPath); - igInspectComp("TextureRegion", entity, ecs_id(TextureRegion), igTextureRegion); - igInspectComp("Animation", entity, ecs_id(Animation), igAnimation); - igInspectComp("Easing", entity, ecs_id(Easing), igEasing); - igInspectComp("Arms", entity, ecs_id(Arms), igArms); - igInspectComp("Arm", entity, ecs_id(Arm), igArm); - igInspectComp("BzBTState", entity, ecs_id(BzBTState), igBzBTState); - igInspectComp("AIBlackboard", entity, ecs_id(AIBlackboard), igAIBlackboard); - igInspectComp("Worker", entity, ecs_id(Worker), igWorker); - igInspectComp("Unit", entity, ecs_id(Unit), igUnit); + if (igCollapsingHeader_TreeNodeFlags("Components", 0)) { + igInspectComp("Resource", entity, ecs_id(Resource), igResource); + igInspectComp("Owner", entity, ecs_id(Owner), igOwner); + igInspectComp("SpatialGridID", entity, ecs_id(SpatialGridID), igSpatialGridID); + igInspectComp("Position", entity, ecs_id(Position), igVec2Comp); + igInspectComp("Size", entity, ecs_id(Size), igVec2Comp); + igInspectComp("Velocity", entity, ecs_id(Velocity), igVec2Comp); + igInspectComp("TargetPosition", entity, ecs_id(TargetPosition), igVec2Comp); + igInspectComp("Steering", entity, ecs_id(Steering), igVec2Comp); + igInspectComp("Rotation", entity, ecs_id(Rotation), igFloat); + igInspectComp("Path", entity, ecs_id(Path), igPath); + igInspectComp("TextureRegion", entity, ecs_id(TextureRegion), igTextureRegion); + igInspectComp("Animation", entity, ecs_id(Animation), igAnimation); + igInspectComp("Easing", entity, ecs_id(Easing), igEasing); + igInspectComp("Arms", entity, ecs_id(Arms), igArms); + igInspectComp("Arm", entity, ecs_id(Arm), igArm); + igInspectComp("BzBTState", entity, ecs_id(BzBTState), igBzBTState); + igInspectComp("AIBlackboard", entity, ecs_id(AIBlackboard), igAIBlackboard); + igInspectComp("Worker", entity, ecs_id(Worker), igWorker); + igInspectComp("Unit", entity, ecs_id(Unit), igUnit); + } } igEnd(); } diff --git a/scripts/extract_common.py b/scripts/extract_common.py index 57a3511..cd0536c 100644 --- a/scripts/extract_common.py +++ b/scripts/extract_common.py @@ -222,7 +222,7 @@ class EnumWriter: def output_enum_tile_size(self, func_name): writer = self.writer writer.output( - f"static {self.enum_type} {func_name}({self.enum_type} type, BzTileID *outWidth, BzTileID *outHeight) ") + f"static {self.enum_type} {func_name}({self.enum_type} type, i32 *outWidth, i32 *outHeight) ") writer.block_start() writer.output("switch (type) ") @@ -330,7 +330,22 @@ class EnumWriter: def output_anim_enum(self): writer = self.writer writer.enum_list(self.anim_type, self.animations) - pass + + def output_anim_enum_to_str(self, func_name): + writer = self.writer + writer.output(f"static const char *{func_name}({self.anim_type} type) ") + writer.block_start() + + writer.output("switch (type) ") + writer.block_start() + + for name in self.animations: + writer.output(f"case {name}: return \"{name}\";\n") + writer.output(f"default: return NULL;\n") + + writer.block_end() + writer.block_end() + writer.empty_line() def output_has_anim(self, func_name): writer = self.writer diff --git a/scripts/extract_tileset.py b/scripts/extract_tileset.py index 300d624..428f198 100644 --- a/scripts/extract_tileset.py +++ b/scripts/extract_tileset.py @@ -64,7 +64,6 @@ writer.empty_line() building_writer.output_enum() building_writer.output_tile_to_enum("getTileBuilding") building_writer.output_enum_to_tile("getBuildingTile") -building_writer.output_str_to_enum("getBuildingFromStr") building_writer.output_enum_to_str("getBuildingStr") building_writer.output_enum_tile_size("getBuildingSize") writer.empty_line() @@ -75,11 +74,13 @@ anim_writer.output_enum() anim_writer.output_anim_enum() anim_writer.output_enum_to_tile("getEntityTile") +anim_writer.output_enum_to_str("getEntityStr") +anim_writer.output_anim_enum_to_str("getEntityAnimationStr") anim_writer.output_has_anim("entityHasAnimation") anim_writer.output_anim_sequence("entityGetAnimationSequence") anim_writer.output_anim_frame("entityGetAnimationFrame") -print(item_tiles) +#print(item_tiles) item_writer = EnumWriter(writer, item_tiles, "item", "item_anim") item_writer.output_enum() #item_writer.output_anim_enum()