From 4ee2524773680358b1a103a7205dbc00d0820ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 2 Nov 2021 19:14:54 +0100 Subject: [PATCH] build mode straight lines support --- code/game/src/gui/build_mode.c | 78 ++++++++++++++++++++---------- code/game/src/items.h | 7 ++- code/modules/source/system_items.c | 3 +- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/code/game/src/gui/build_mode.c b/code/game/src/gui/build_mode.c index f4bcc8b..125cc0b 100644 --- a/code/game/src/gui/build_mode.c +++ b/code/game/src/gui/build_mode.c @@ -17,6 +17,11 @@ void buildmode_clear_buffers(void) { } } +// TODO(zaklaus): +#ifndef zpl_sign0 +#define zpl_sign0(x) (x == 0.0f) ? 0.0f : ((x) >= 0.0f ? 1.0f : -1.0f) +#endif + void buildmode_draw(void) { camera cam = camera_get(); camera old_cam = cam; @@ -44,35 +49,56 @@ void buildmode_draw(void) { buildmode_clear_buffers(); } - if (e->has_items && !e->inside_vehicle && e->items[e->selected_item].quantity > 0 && !is_outside_range) { - if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON) && !build_is_in_draw_mode) { - build_is_in_draw_mode = true; - build_num_placements = 0; - buildmode_clear_buffers(); - } - - uint32_t qty = e->items[e->selected_item].quantity; - - if (build_is_in_draw_mode) { - for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) { - item_placement *it = &build_placements[i]; - if (it->kind == -1) { - it->x = (float)cam.x; - it->y = (float)cam.y; - it->kind = 0; - build_num_placements++; - break; - } else if (it->x == (float)cam.x && it->y == (float)cam.y) { - break; - } + ItemDrop *item = &e->items[e->selected_item]; + + if (e->has_items && !e->inside_vehicle && item->quantity > 0 && !is_outside_range) { + uint16_t item_id = item_find(item->kind); + item_usage usage = item_get_usage(item_id); + if (usage < UKIND_END_PLACE) { + if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON) && !build_is_in_draw_mode) { + build_is_in_draw_mode = true; + build_num_placements = 0; + buildmode_clear_buffers(); } + uint32_t qty = item->quantity; + bool directional = item_get_place_directional(item_id); + + if (build_is_in_draw_mode) { + for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) { + item_placement *it = &build_placements[i]; + if (it->kind == -1) { + if (directional && build_num_placements > 2) { + float p1x = build_placements[0].x; + float p1y = build_placements[0].y; + float p2x = build_placements[1].x; + float p2y = build_placements[1].y; + float p3x = (float)cam.x; + float p3y = (float)cam.y; + float sx = zpl_sign0(p2x-p1x); + float sy = zpl_sign0(p2y-p1y); + float sxx = zpl_sign0(p3x-p1x); + float syy = zpl_sign0(p3y-p1y); + + if (sx != sxx || sy != syy) break; + } + it->x = (float)cam.x; + it->y = (float)cam.y; + it->kind = 0; + build_num_placements++; + break; + } else if (it->x == (float)cam.x && it->y == (float)cam.y) { + break; + } + } + + } + + if (!is_outside_range) + renderer_draw_single(cam.x, cam.y, ASSET_EMPTY, ColorAlpha(BLUE, 0.4f)); + + build_num_placements = zpl_min(build_num_placements, qty); } - - if (!is_outside_range) - renderer_draw_single(cam.x, cam.y, ASSET_EMPTY, ColorAlpha(BLUE, 0.4f)); - - build_num_placements = zpl_min(build_num_placements, qty); } for (size_t i = 0; i < build_num_placements; i++) { diff --git a/code/game/src/items.h b/code/game/src/items.h index 36f608b..b1904ae 100644 --- a/code/game/src/items.h +++ b/code/game/src/items.h @@ -6,11 +6,14 @@ #include "modules/components.h" typedef enum { - UKIND_HOLD, - UKIND_PROXY, + // NOTE(zaklaus): hardcoded fields for placement ops UKIND_PLACE, UKIND_PLACE_ITEM, UKIND_END_PLACE, + + // NOTE(zaklaus): the rest of possible actions + UKIND_HOLD, + UKIND_PROXY, } item_usage; typedef struct { diff --git a/code/modules/source/system_items.c b/code/modules/source/system_items.c index 05eb634..515d917 100644 --- a/code/modules/source/system_items.c +++ b/code/modules/source/system_items.c @@ -182,8 +182,9 @@ void SwapItems(ecs_iter_t *it) { } } +// TODO(zaklaus): #ifndef zpl_sign0 -#define zpl_sign0(x) (x == 0.0f) ? 0 : ((x) >= 0 ? 1 : -1) +#define zpl_sign0(x) (x == 0.0f) ? 0.0f : ((x) >= 0.0f ? 1.0f : -1.0f) #endif void UseItem(ecs_iter_t *it) {