From 8ca11b3ec795b7a1643ef30f9c5f4c893444c7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 14 Sep 2022 13:12:12 +0000 Subject: [PATCH] drop tree on car collision --- code/game/src/world/blocks.h | 3 ++- code/game/src/world/blocks_list.c | 6 +++--- code/game/src/world/world.c | 1 + code/modules/source/system_vehicle.c | 11 +++++++++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/code/game/src/world/blocks.h b/code/game/src/world/blocks.h index 5f48ffb..f5dc6f8 100644 --- a/code/game/src/world/blocks.h +++ b/code/game/src/world/blocks.h @@ -6,6 +6,7 @@ typedef enum { BLOCK_FLAG_COLLISION = (1 << 1), BLOCK_FLAG_HAZARD = (1 << 2), BLOCK_FLAG_ESSENTIAL = (1 << 3), + BLOCK_FLAG_DESTROY_ON_COLLISION = (1 << 4), } block_flags; typedef uint16_t block_id; @@ -29,4 +30,4 @@ void *blocks_get_img(block_id id); void blocks_build_chunk_tex(uint64_t id, block_id *blocks, void *view); void *blocks_get_chunk_tex(uint64_t id); -void blocks_remove_chunk_tex(uint64_t id); \ No newline at end of file +void blocks_remove_chunk_tex(uint64_t id); diff --git a/code/game/src/world/blocks_list.c b/code/game/src/world/blocks_list.c index 1d241a4..7944e3c 100644 --- a/code/game/src/world/blocks_list.c +++ b/code/game/src/world/blocks_list.c @@ -16,12 +16,12 @@ static block blocks[] = { BLOCK(ASSET_LAVA, BLOCK_FLAG_HAZARD, '!', .drag = 6.2f , .friction = 4.0f), BLOCK(ASSET_FENCE, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 1.0f), BLOCK(ASSET_WOOD, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f), - BLOCK(ASSET_TREE, BLOCK_FLAG_COLLISION, '@', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f), - + BLOCK(ASSET_TREE, BLOCK_FLAG_COLLISION|BLOCK_FLAG_DESTROY_ON_COLLISION, '@', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f), + BLOCK(ASSET_BELT_LEFT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = -150.0f), BLOCK(ASSET_BELT_RIGHT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = 150.0f), BLOCK(ASSET_BELT_UP, 0, '@', .drag = 1.0f , .friction = 1.0f, .vely = -150.0f), BLOCK(ASSET_BELT_DOWN, 0, '@', .drag = 1.0f , .friction = 1.0f, .vely = 150.0f), }; -ZPL_STATIC_ASSERT(sizeof(blocks)/sizeof(block) < ZPL_U16_MAX, "too many registered blocks! (max. 65536)"); \ No newline at end of file +ZPL_STATIC_ASSERT(sizeof(blocks)/sizeof(block) < ZPL_U16_MAX, "too many registered blocks! (max. 65536)"); diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index 9e79fbe..99b779a 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -508,6 +508,7 @@ void world_chunk_destroy_block(float x, float y, bool drop_item) { Position *dest = ecs_get_mut(world_ecs(), e, Position); dest->x = x; dest->y = y; + entity_set_position(e, dest->x, dest->y); } } diff --git a/code/modules/source/system_vehicle.c b/code/modules/source/system_vehicle.c index 1377ca3..239163b 100644 --- a/code/modules/source/system_vehicle.c +++ b/code/modules/source/system_vehicle.c @@ -143,10 +143,17 @@ void VehicleHandling(ecs_iter_t *it) { v[i].y += ((fr_y + bk_y) / 2.0f - p[i].y); car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x); - world_block_lookup lookahead = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y+PHY_LOOKAHEAD(v[i].y)); + float check_x = p[i].x+PHY_LOOKAHEAD(v[i].x); + float check_y = p[i].y+PHY_LOOKAHEAD(v[i].y); + world_block_lookup lookahead = world_block_from_realpos(check_x, check_y); uint32_t flags = blocks_get_flags(lookahead.bid); if (flags & BLOCK_FLAG_COLLISION) { - car->force = 0.0f; + if (flags & BLOCK_FLAG_DESTROY_ON_COLLISION) { + world_chunk_destroy_block(check_x, check_y, true); + car->force *= 0.8f; + } else { + car->force = 0.0f; + } } for (int j = 0; j < 4; j++) {