Add water surface animated sprite to editor

main
En Yi 2025-09-01 11:09:16 +08:00
parent 2946921edc
commit d8e71caed4
4 changed files with 47 additions and 4 deletions

View File

@ -1,3 +1,5 @@
#ifndef SCENES_COMPONENTS_H
#define SCENES_COMPONENTS_H
#include "EC.h" #include "EC.h"
#include "engine.h" #include "engine.h"
#include "render_queue.h" #include "render_queue.h"
@ -178,3 +180,4 @@ typedef struct _CEmitter_t
typedef struct _CSquishable_t { typedef struct _CSquishable_t {
bool active; //< Dummy variable bool active; //< Dummy variable
} CSquishable_t; } CSquishable_t;
#endif // SCENES_COMPONENTS_H

View File

@ -617,8 +617,8 @@ static void render_editor_game_scene(Scene_t* scene)
draw_particle_system(&scene->part_sys); draw_particle_system(&scene->part_sys);
// Draw water tile // Draw water tile
Sprite_t* surface_spr = get_sprite(&scene->engine->assets, "wsurf"); CSprite_t* water_surface_spr = get_component_wtih_id(CSPRITE_T, data->water_surface_spr_cidx);
Vector2 surf_offset = get_anchor_offset(surface_spr->frame_size, AP_BOT_LEFT, false); const SpriteRenderInfo_t surface_spr = water_surface_spr->sprites[water_surface_spr->current_idx];
for (int tile_y = min.y; tile_y < max.y; tile_y++) for (int tile_y = min.y; tile_y < max.y; tile_y++)
{ {
for (int tile_x = min.x; tile_x < max.x; tile_x++) for (int tile_x = min.x; tile_x < max.x; tile_x++)
@ -647,8 +647,13 @@ static void render_editor_game_scene(Scene_t* scene)
) )
{ {
Vector2 surf_pos = {x, y + (TILE_SIZE - water_height)}; Vector2 surf_pos = {x, y + (TILE_SIZE - water_height)};
surf_pos = Vector2Subtract(surf_pos, surf_offset);
draw_sprite_pro(surface_spr, 0, surf_pos, 0, false, (Vector2){1,1}, water_colour); surf_pos = Vector2Subtract(
surf_pos,
get_anchor_offset(surface_spr.sprite->frame_size, AP_BOT_LEFT, false)
);
draw_sprite_pro(surface_spr.sprite, water_surface_spr->current_frame, surf_pos, 0, false, (Vector2){1,1}, water_colour);
} }
} }
} }

View File

@ -5,6 +5,7 @@
#ifndef __SCENE_IMPL_H #ifndef __SCENE_IMPL_H
#define __SCENE_IMPL_H #define __SCENE_IMPL_H
#include "engine.h" #include "engine.h"
#include "components.h"
#include "gui.h" #include "gui.h"
#define CONTAINER_OF(ptr, type, member) ({ \ #define CONTAINER_OF(ptr, type, member) ({ \
@ -74,6 +75,8 @@ typedef struct LevelSceneData {
LevelPack_t* level_pack; LevelPack_t* level_pack;
const char* restart_keyname; const char* restart_keyname;
const char* look_keyname; const char* look_keyname;
//CSprite_t* water_surface_spr;
unsigned long water_surface_spr_cidx;
unsigned int current_level; unsigned int current_level;
CoinCounter_t coins; CoinCounter_t coins;
bool show_grid; bool show_grid;

View File

@ -3,6 +3,7 @@
#include "water_flow.h" #include "water_flow.h"
#include "ent_impl.h" #include "ent_impl.h"
#include "constants.h" #include "constants.h"
#include "mempool.h"
#include "raymath.h" #include "raymath.h"
@ -49,6 +50,32 @@ void init_level_scene_data(LevelSceneData_t* data, uint32_t max_tiles, Tile_t* t
memset(&data->sm, 0, sizeof(data->sm)); memset(&data->sm, 0, sizeof(data->sm));
static SpriteRenderInfo_t water_sprite_map[2] = {0};
Scene_t* scene = &(CONTAINER_OF(data, LevelScene_t, data)->scene);
water_sprite_map[0].sprite = get_sprite(&scene->engine->assets, "wsurf");
/**
* Add water surface animation sprite
* This is not associated with any entity, but manually held by the scene
* Thus, will need to manually add request for the component
* and add it to the entity manager.
* This is so that I can make use of the sprite animation system, which is partially entity-independent
* This is basically a hack/abuse of the current engine
* Will need to account for such use case in version 2
*/
{
unsigned long comp_idx;
CSprite_t* p_cspr = new_component_from_mempool(CSPRITE_T, &comp_idx);
p_cspr->sprites = water_sprite_map;
p_cspr->current_idx = 0;
p_cspr->node.scale = (Vector2){1, 1};
p_cspr->node.colour = WHITE;
p_cspr->depth = 2;
assert(p_cspr != NULL);
data->water_surface_spr_cidx = comp_idx;
// Use invalid entity id as it is not associated with any entity
sc_map_put_64v(&scene->ent_manager.component_map[CSPRITE_T], MAX_COMP_POOL_SIZE+1, p_cspr);
}
} }
void term_level_scene_data(LevelSceneData_t* data) void term_level_scene_data(LevelSceneData_t* data)
@ -57,6 +84,11 @@ void term_level_scene_data(LevelSceneData_t* data)
{ {
sc_map_term_64v(&data->tilemap.tiles[i].entities_set); sc_map_term_64v(&data->tilemap.tiles[i].entities_set);
} }
// Manually remove the water surface sprite
Scene_t* scene = &(CONTAINER_OF(data, LevelScene_t, data)->scene);
sc_map_del_64v(&scene->ent_manager.component_map[CSPRITE_T], MAX_COMP_POOL_SIZE+1);
free_component_to_mempool(CSPRITE_T, data->water_surface_spr_cidx);
} }
void clear_an_entity(Scene_t* scene, TileGrid_t* tilemap, Entity_t* p_ent) void clear_an_entity(Scene_t* scene, TileGrid_t* tilemap, Entity_t* p_ent)