From 89176142b6c19675a589edcfc850b3d3aa012391 Mon Sep 17 00:00:00 2001 From: En Yi Date: Wed, 21 Aug 2024 12:52:19 +0800 Subject: [PATCH] Readjust arrows and bombs spawning Changelog: - Make use of anchor point function - Readjust offsets and anchor points of sprites - Readjust hitboxes offset and sizes --- scenes/game_systems.c | 39 +++++++++++++++++------------ scenes/items_ent.c | 57 ++++++++++++++++++++++++++---------------- scenes/scene_systems.c | 14 ++++++----- 3 files changed, 67 insertions(+), 43 deletions(-) diff --git a/scenes/game_systems.c b/scenes/game_systems.c index ce303f1..9c16cb0 100644 --- a/scenes/game_systems.c +++ b/scenes/game_systems.c @@ -1722,6 +1722,7 @@ void boulder_destroy_wooden_tile_system(Scene_t* scene) void container_destroy_system(Scene_t* scene) { + LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); unsigned int ent_idx; CContainer_t* p_container; sc_map_foreach(&scene->ent_manager.component_map[CCONTAINER_T], ent_idx, p_container) @@ -1737,23 +1738,28 @@ void container_destroy_system(Scene_t* scene) } Entity_t* new_ent; + AnchorPoint_t spawn_anchor = AP_MID_CENTER; switch (p_container->item) { case CONTAINER_LEFT_ARROW: new_ent = create_arrow(&scene->ent_manager, 0); play_sfx(scene->engine, ARROW_RELEASE_SFX); + spawn_anchor = AP_MID_LEFT; break; case CONTAINER_RIGHT_ARROW: new_ent = create_arrow(&scene->ent_manager, 1); play_sfx(scene->engine, ARROW_RELEASE_SFX); + spawn_anchor = AP_MID_RIGHT; break; case CONTAINER_UP_ARROW: new_ent = create_arrow(&scene->ent_manager, 2); play_sfx(scene->engine, ARROW_RELEASE_SFX); + spawn_anchor = AP_TOP_CENTER; break; case CONTAINER_DOWN_ARROW: new_ent = create_arrow(&scene->ent_manager, 3); play_sfx(scene->engine, ARROW_RELEASE_SFX); + spawn_anchor = AP_BOT_CENTER; break; case CONTAINER_BOMB: if (dmg_src != NULL && dmg_src->m_tag == PLAYER_ENT_TAG) @@ -1786,24 +1792,25 @@ void container_destroy_system(Scene_t* scene) new_ent = NULL; break; } - if ( - p_container->item != CONTAINER_EXPLOSION - && p_container->item != CONTAINER_BOMB - ) - { - if (p_container->material == WOODEN_CONTAINER) - { - play_sfx(scene->engine, WOOD_DESTROY_SFX); - } - else - { - play_sfx(scene->engine, METAL_DESTROY_SFX); - } - } - if (new_ent != NULL) { - new_ent->position = Vector2Add(new_ent->position, p_ent->position); + new_ent->position = Vector2Add( + p_ent->position, + get_anchor_offset( + (Vector2){data->tilemap.tile_size, data->tilemap.tile_size} + , spawn_anchor, false + ) + ); + } + + // In case there's more materials + if (p_container->material == WOODEN_CONTAINER) + { + play_sfx(scene->engine, WOOD_DESTROY_SFX); + } + else + { + play_sfx(scene->engine, METAL_DESTROY_SFX); } } } diff --git a/scenes/items_ent.c b/scenes/items_ent.c index ed5bc1e..57e856f 100644 --- a/scenes/items_ent.c +++ b/scenes/items_ent.c @@ -9,12 +9,21 @@ bool init_item_creation(Assets_t* assets) item_sprite_map[0].sprite = get_sprite(assets, "w_crate"); item_sprite_map[1].sprite = get_sprite(assets, "m_crate"); item_sprite_map[2].sprite = get_sprite(assets, "r_arrow"); - item_sprite_map[2].offset = (Vector2){-8, 4}; + //item_sprite_map[2].offset = (Vector2){-8, 6}; + item_sprite_map[2].src_anchor = AP_MID_CENTER; + item_sprite_map[2].src_anchor = AP_MID_CENTER; item_sprite_map[3].sprite = get_sprite(assets, "u_arrow"); + item_sprite_map[3].src_anchor = AP_MID_CENTER; + item_sprite_map[3].src_anchor = AP_MID_CENTER; item_sprite_map[4].sprite = get_sprite(assets, "l_arrow"); + item_sprite_map[4].src_anchor = AP_MID_CENTER; + item_sprite_map[4].src_anchor = AP_MID_CENTER; item_sprite_map[5].sprite = get_sprite(assets, "d_arrow"); + item_sprite_map[5].src_anchor = AP_MID_CENTER; + item_sprite_map[5].src_anchor = AP_MID_CENTER; item_sprite_map[6].sprite = get_sprite(assets, "bomb"); - item_sprite_map[6].offset = (Vector2){0, -4}; + item_sprite_map[6].src_anchor = AP_MID_CENTER; + item_sprite_map[6].src_anchor = AP_MID_CENTER; item_sprite_map[7].sprite = get_sprite(assets, "w_ra_crate"); item_sprite_map[8].sprite = get_sprite(assets, "m_ra_crate"); item_sprite_map[9].sprite = get_sprite(assets, "w_ua_crate"); @@ -26,7 +35,8 @@ bool init_item_creation(Assets_t* assets) item_sprite_map[15].sprite = get_sprite(assets, "w_b_crate"); item_sprite_map[16].sprite = get_sprite(assets, "m_b_crate"); item_sprite_map[17].sprite = get_sprite(assets, "explode"); - item_sprite_map[17].offset = (Vector2){-12, -12}; + item_sprite_map[17].src_anchor = AP_MID_CENTER; + item_sprite_map[17].src_anchor = AP_MID_CENTER; item_sprite_map[18].sprite = get_sprite(assets, "chest"); item_sprite_map[19].sprite = get_sprite(assets, "boulder"); item_sprite_map[20].sprite = get_sprite(assets, "exit"); @@ -132,25 +142,29 @@ Entity_t* create_arrow(EntityManager_t* ent_manager, uint8_t dir) p_cspr->sprites = item_sprite_map; p_cspr->current_idx = 2; //p_hitbox->boxes[0] = (Rectangle){TILE_SIZE - 5, TILE_SIZE / 2 - 5, 5, 5}; + const int HITBOX_LONG_SIDE = 10; + const int HITBOX_SHORT_SIDE = 4; + const int CENTER_POSITION = (TILE_SIZE - HITBOX_SHORT_SIDE) >> 1; + const int HITBOX_CENTER = (HITBOX_SHORT_SIDE >> 1); switch(dir) { case 0: - p_hitbox->boxes[0] = (Rectangle){10, TILE_SIZE / 2 - 5, 10, 5}; p_ctransform->velocity.x = -ARROW_SPEED; p_cspr->current_idx += 2; + p_hitbox->boxes[0] = (Rectangle){-CENTER_POSITION, -HITBOX_CENTER, HITBOX_LONG_SIDE, HITBOX_SHORT_SIDE}; break; case 2: - p_hitbox->boxes[0] = (Rectangle){TILE_SIZE / 2 - 5, 10, 5, 10}; + p_hitbox->boxes[0] = (Rectangle){-HITBOX_CENTER, -CENTER_POSITION, HITBOX_SHORT_SIDE, HITBOX_LONG_SIDE}; p_ctransform->velocity.y = -ARROW_SPEED; p_cspr->current_idx += 1; break; case 3: - p_hitbox->boxes[0] = (Rectangle){TILE_SIZE / 2 - 5, 10, 5, 10}; + p_hitbox->boxes[0] = (Rectangle){-HITBOX_CENTER, CENTER_POSITION - HITBOX_LONG_SIDE, HITBOX_SHORT_SIDE, HITBOX_LONG_SIDE}; p_ctransform->velocity.y = ARROW_SPEED; p_cspr->current_idx += 3; break; default: - p_hitbox->boxes[0] = (Rectangle){10, TILE_SIZE / 2 - 5, 10, 5}; + p_hitbox->boxes[0] = (Rectangle){CENTER_POSITION - HITBOX_LONG_SIDE, -HITBOX_CENTER, HITBOX_LONG_SIDE, HITBOX_SHORT_SIDE}; p_ctransform->velocity.x = ARROW_SPEED; break; } @@ -163,22 +177,22 @@ Entity_t* create_bomb(EntityManager_t* ent_manager, Vector2 launch_dir) Entity_t* p_bomb = add_entity(ent_manager, DESTRUCTABLE_ENT_TAG); if (p_bomb == NULL) return NULL; - p_bomb->position.x += (TILE_SIZE - 25) / 2; - p_bomb->position.y += (TILE_SIZE - 25) / 2; - if (launch_dir.x > 0) - { - p_bomb->position.x += TILE_SIZE/ 2; - } - else if (launch_dir.x < 0) - { - p_bomb->position.x -= TILE_SIZE / 2; - } + //p_bomb->position.x += (TILE_SIZE - 25) / 2; + //p_bomb->position.y += (TILE_SIZE - 25) / 2; + //if (launch_dir.x > 0) + //{ + // p_bomb->position.x += TILE_SIZE/ 2; + //} + //else if (launch_dir.x < 0) + //{ + // p_bomb->position.x -= TILE_SIZE / 2; + //} add_component(p_bomb, CTILECOORD_COMP_T); add_component(p_bomb, CMOVEMENTSTATE_T); CHitBoxes_t* p_hitbox = add_component(p_bomb, CHITBOXES_T); p_hitbox->n_boxes = 1; - p_hitbox->boxes[0] = (Rectangle){0, 0, 25, 25}; + p_hitbox->boxes[0] = (Rectangle){-13, -13, 26, 26}; p_hitbox->atk = 0; p_hitbox->one_hit = true; @@ -205,8 +219,8 @@ Entity_t* create_explosion(EntityManager_t* ent_manager) Entity_t* p_explosion = add_entity(ent_manager, DESTRUCTABLE_ENT_TAG); if (p_explosion == NULL) return NULL; - p_explosion->position.x -= 16; - p_explosion->position.y -= 16; + //p_explosion->position.x -= 16; + //p_explosion->position.y -= 16; add_component(p_explosion, CTILECOORD_COMP_T); CHitBoxes_t* p_hitbox = add_component(p_explosion, CHITBOXES_T); p_hitbox->n_boxes = 1; @@ -216,7 +230,8 @@ Entity_t* create_explosion(EntityManager_t* ent_manager) CTransform_t* p_ctransform = add_component(p_explosion, CTRANSFORM_COMP_T); p_ctransform->movement_mode = KINEMATIC_MOVEMENT; p_ctransform->active = true; - p_hitbox->boxes[0] = (Rectangle){0, 0, TILE_SIZE + 32, TILE_SIZE + 32}; + const int hitbox_sz = TILE_SIZE + 40; + p_hitbox->boxes[0] = (Rectangle){-(hitbox_sz >> 1), -(hitbox_sz >> 1), hitbox_sz, hitbox_sz}; CSprite_t* p_cspr = add_component(p_explosion, CSPRITE_T); p_cspr->sprites = item_sprite_map; diff --git a/scenes/scene_systems.c b/scenes/scene_systems.c index d34d9fb..2467e91 100644 --- a/scenes/scene_systems.c +++ b/scenes/scene_systems.c @@ -255,30 +255,32 @@ void change_a_tile(TileGrid_t* tilemap, unsigned int tile_idx, TileType_t new_ty tilemap->tiles[tile_idx].rotation = TILE_NOROTATE; + const int SPIKE_HITBOX_LONGSIDE = 30; + const int SPIKE_HITBOX_SHORTSIDE = 12; if (new_type == SPIKES) { // Priority: Down, Up, Left, Right if (tile_idx + tilemap->width < tilemap->n_tiles && tilemap->tiles[tile_idx + tilemap->width].tile_type == SOLID_TILE) { - tilemap->tiles[tile_idx].offset = (Vector2){0,16}; - tilemap->tiles[tile_idx].size = (Vector2){32,16}; + tilemap->tiles[tile_idx].offset = (Vector2){0,tilemap->tile_size - SPIKE_HITBOX_SHORTSIDE}; + tilemap->tiles[tile_idx].size = (Vector2){SPIKE_HITBOX_LONGSIDE, SPIKE_HITBOX_SHORTSIDE}; } else if (tile_idx - tilemap->width >= 0 && tilemap->tiles[tile_idx - tilemap->width].tile_type == SOLID_TILE) { tilemap->tiles[tile_idx].offset = (Vector2){0,0}; - tilemap->tiles[tile_idx].size = (Vector2){32,16}; + tilemap->tiles[tile_idx].size = (Vector2){SPIKE_HITBOX_LONGSIDE, SPIKE_HITBOX_SHORTSIDE}; tilemap->tiles[tile_idx].rotation = TILE_180ROT; } else if (tile_idx % tilemap->width != 0 && tilemap->tiles[tile_idx - 1].tile_type == SOLID_TILE) { tilemap->tiles[tile_idx].offset = (Vector2){0,0}; - tilemap->tiles[tile_idx].size = (Vector2){16,32}; + tilemap->tiles[tile_idx].size = (Vector2){SPIKE_HITBOX_SHORTSIDE, SPIKE_HITBOX_LONGSIDE}; tilemap->tiles[tile_idx].rotation = TILE_90CWROT; } else if ((tile_idx + 1) % tilemap->width != 0 && tilemap->tiles[tile_idx + 1].tile_type == SOLID_TILE) { - tilemap->tiles[tile_idx].offset = (Vector2){16,0}; - tilemap->tiles[tile_idx].size = (Vector2){16,32}; + tilemap->tiles[tile_idx].offset = (Vector2){SPIKE_HITBOX_SHORTSIDE >> 1,0}; + tilemap->tiles[tile_idx].size = (Vector2){SPIKE_HITBOX_SHORTSIDE, SPIKE_HITBOX_LONGSIDE}; tilemap->tiles[tile_idx].rotation = TILE_90CCWROT; } else