Update entity spawn + despawn function

Changelog:
- Check for mouse in game pos before mouse click check
- Right click will also remove entities now! Hooray!
scene_man
En Yi 2023-06-26 21:27:22 +08:00
parent dab8303cbe
commit f206d3c981
1 changed files with 138 additions and 130 deletions

View File

@ -291,19 +291,18 @@ static void toggle_block_system(Scene_t* scene)
Vector2 raw_mouse_pos = {GetMouseX(), GetMouseY()};
raw_mouse_pos = Vector2Subtract(raw_mouse_pos, (Vector2){data->game_rec.x, data->game_rec.y});
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
if (
raw_mouse_pos.x > data->game_rec.width
|| raw_mouse_pos.y > data->game_rec.height
) return;
raw_mouse_pos.x < data->game_rec.width
&& raw_mouse_pos.y < data->game_rec.height
)
{
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->cam);
unsigned int tile_idx = get_tile_idx(mouse_pos.x, mouse_pos.y, &tilemap);
if (tile_idx >= MAX_N_TILES) return;
enum EntitySpawnSelection sel = (enum EntitySpawnSelection)current_spawn_selection;
if (tile_idx != last_tile_idx)
if (tile_idx == last_tile_idx) return;
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
enum EntitySpawnSelection sel = (enum EntitySpawnSelection)current_spawn_selection;
switch (sel)
{
case TOGGLE_TILE:
@ -387,6 +386,7 @@ static void toggle_block_system(Scene_t* scene)
spawn_boulder(scene, tile_idx);
break;
}
if (tilemap.tiles[tile_idx].tile_type == SPIKES)
{
// Priority: Down, Up, Left, Right
@ -427,23 +427,30 @@ static void toggle_block_system(Scene_t* scene)
|| tilemap.tiles[tile_idx].tile_type == SPIKES
);
}
}
else if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON))
{
if (
raw_mouse_pos.x > data->game_rec.width
|| raw_mouse_pos.y > data->game_rec.height
) return;
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->cam);
unsigned int tile_idx = get_tile_idx(mouse_pos.x, mouse_pos.y, &tilemap);
if (tile_idx >= MAX_N_TILES) return;
if (tile_idx != last_tile_idx)
{
tilemap.tiles[tile_idx].tile_type = EMPTY_TILE;
tilemap.tiles[tile_idx].solid = NOT_SOLID;
tilemap.tiles[tile_idx].water_level = 0;
tilemap.tiles[tile_idx].moveable = true;
Entity_t* ent;
unsigned int m_id;
sc_map_foreach(&tilemap.tiles[tile_idx].entities_set, m_id, ent)
{
if (ent->m_tag == PLAYER_ENT_TAG) continue;
CTileCoord_t* p_tilecoord = get_component(
ent, CTILECOORD_COMP_T
);
for (size_t i = 0;i < p_tilecoord->n_tiles; ++i)
{
// Use previously store tile position
// Clear from those positions
unsigned int tile_idx = p_tilecoord->tiles[i];
sc_map_del_64v(&(tilemap.tiles[tile_idx].entities_set), m_id);
}
remove_entity(&scene->ent_manager, m_id);
}
}
else
@ -451,6 +458,7 @@ static void toggle_block_system(Scene_t* scene)
last_tile_idx = MAX_N_TILES;
}
}
}
void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)
{