keystate activators

isolation_bkp/dynres
Dominik Madarász 2021-11-02 10:22:04 +01:00
parent 28c41baa93
commit 9482b41fb9
7 changed files with 78 additions and 41 deletions

View File

@ -3,7 +3,6 @@
static debug_draw_queue draw_queue = {0}; static debug_draw_queue draw_queue = {0};
#define _DEBUG 1
#ifndef _DEBUG #ifndef _DEBUG
static bool draw_is_enabled = false; static bool draw_is_enabled = false;
#else #else

View File

@ -6,9 +6,20 @@ static bool build_is_in_draw_mode = false;
static uint8_t build_num_placements = 0; static uint8_t build_num_placements = 0;
static item_placement build_placements[BUILD_MAX_PLACEMENTS] = {0}; static item_placement build_placements[BUILD_MAX_PLACEMENTS] = {0};
void buildmode_draw() { #ifndef zpl_square
if (inv_is_open) return; #define zpl_square(x) ((x) * (x))
#endif
void buildmode_clear_buffers(void) {
item_placement empty_placement = { .x = 0.0f, .y = 0.0f, .kind = -1 };
for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) {
zpl_memcopy(&build_placements[i], &empty_placement, zpl_size_of(item_placement));
}
}
void buildmode_draw(void) {
camera cam = camera_get(); camera cam = camera_get();
camera old_cam = cam;
Vector2 mpos = GetMousePosition(); Vector2 mpos = GetMousePosition();
entity_view *e = game_world_view_active_get_entity(cam.ent_id); entity_view *e = game_world_view_active_get_entity(cam.ent_id);
if (!e) return; if (!e) return;
@ -22,15 +33,22 @@ void buildmode_draw() {
cam.x += WORLD_BLOCK_SIZE/2.0f; cam.x += WORLD_BLOCK_SIZE/2.0f;
cam.y += WORLD_BLOCK_SIZE/2.0f; cam.y += WORLD_BLOCK_SIZE/2.0f;
if (e->has_items && !e->inside_vehicle && e->items[e->selected_item].quantity > 0) { // NOTE(zaklaus): Check distance
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) && !build_is_in_draw_mode) { double dx = old_cam.x - cam.x;
double dy = old_cam.y - cam.y;
double dsq = (dx*dx + dy*dy);
bool is_outside_range = (dsq > zpl_square(WORLD_BLOCK_SIZE*8));
if (build_submit_placements) {
build_submit_placements = false;
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_is_in_draw_mode = true;
build_num_placements = 0; build_num_placements = 0;
buildmode_clear_buffers();
item_placement empty_placement = { .x = 0.0f, .y = 0.0f, .kind = -1 };
for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) {
zpl_memcopy(&build_placements[i], &empty_placement, zpl_size_of(item_placement));
}
} }
uint32_t qty = e->items[e->selected_item].quantity; uint32_t qty = e->items[e->selected_item].quantity;
@ -49,23 +67,28 @@ void buildmode_draw() {
} }
} }
build_num_placements = zpl_min(build_num_placements, qty);
for (size_t i = 0; i < build_num_placements; i++) {
item_placement *it = &build_placements[i];
renderer_draw_single(it->x, it->y, ASSET_DEBUG_TILE, ColorAlpha(BLUE, 0.4f));
}
if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) {
build_is_in_draw_mode = false;
}
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) {
build_submit_placements = true;
build_is_in_draw_mode = false;
}
} }
renderer_draw_single(cam.x, cam.y, ASSET_DEBUG_TILE, ColorAlpha(BLUE, 0.4f)); if (!is_outside_range)
renderer_draw_single(cam.x, cam.y, ASSET_DEBUG_TILE, ColorAlpha(BLUE, 0.4f));
build_num_placements = zpl_min(build_num_placements, qty);
} }
for (size_t i = 0; i < build_num_placements; i++) {
item_placement *it = &build_placements[i];
renderer_draw_single(it->x, it->y, ASSET_DEBUG_TILE, ColorAlpha(BLUE, 0.4f));
}
if (build_is_in_draw_mode) {
if (IsKeyPressed(KEY_SPACE)) {
build_is_in_draw_mode = false;
}
if (IsMouseButtonReleased(MOUSE_RIGHT_BUTTON)) {
build_submit_placements = true;
build_is_in_draw_mode = false;
}
}
} }

View File

@ -24,7 +24,7 @@ void inventory_draw() {
inv_is_open = !inv_is_open; inv_is_open = !inv_is_open;
} }
if (!inv_is_open) { if (!inv_is_open || build_is_in_draw_mode) {
return; return;
} }

View File

@ -51,18 +51,21 @@ int32_t pkt_send_keystate_handler(pkt_header *header) {
i->y = zpl_clamp(table.y, -1.0f, 1.0f); i->y = zpl_clamp(table.y, -1.0f, 1.0f);
i->mx = table.mx; i->mx = table.mx;
i->my = table.my; i->my = table.my;
i->use = table.use; i->use |= table.use;
i->sprint = table.sprint; i->sprint = table.sprint;
i->ctrl = table.ctrl; i->ctrl = table.ctrl;
i->selected_item = zpl_clamp(table.selected_item, 0, ITEMS_INVENTORY_SIZE-1); i->selected_item = zpl_clamp(table.selected_item, 0, ITEMS_INVENTORY_SIZE-1);
i->drop = table.drop; i->drop |= table.drop;
i->swap = table.swap; i->swap |= table.swap;
i->swap_from = zpl_clamp(table.swap_from, 0, ITEMS_INVENTORY_SIZE-1); i->swap_from = zpl_clamp(table.swap_from, 0, ITEMS_INVENTORY_SIZE-1);
i->swap_to = zpl_clamp(table.swap_to, 0, ITEMS_INVENTORY_SIZE-1); i->swap_to = zpl_clamp(table.swap_to, 0, ITEMS_INVENTORY_SIZE-1);
i->num_placements = zpl_clamp(table.placement_num, 0, BUILD_MAX_PLACEMENTS);
for (uint8_t j = 0; j < i->num_placements; j++) { if (table.placement_num > 0) {
i->placements_x[j] = table.placements[j].x; i->num_placements = zpl_clamp(table.placement_num, 0, BUILD_MAX_PLACEMENTS);
i->placements_y[j] = table.placements[j].y; for (uint8_t j = 0; j < i->num_placements; j++) {
i->placements_x[j] = table.placements[j].x;
i->placements_y[j] = table.placements[j].y;
}
} }
debug_replay_record_keystate(table); debug_replay_record_keystate(table);
} }

View File

@ -22,15 +22,15 @@ static bool request_shutdown;
#include "renderer_bridge.c" #include "renderer_bridge.c"
// NOTE(zaklaus): add-ins // NOTE(zaklaus): add-ins
#include "gui/inventory.c"
#include "gui/build_mode.c" #include "gui/build_mode.c"
#include "gui/inventory.c"
void platform_init() { void platform_init() {
InitWindow(screenWidth, screenHeight, "eco2d"); InitWindow(screenWidth, screenHeight, "eco2d");
SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT); SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT);
screenWidth = GetScreenWidth(); screenWidth = (uint16_t)GetScreenWidth();
screenHeight = GetScreenHeight(); screenHeight = (uint16_t)GetScreenHeight();
// ToggleFullscreen(); // ToggleFullscreen();
// SetTargetFPS(60.0); // SetTargetFPS(60.0);
@ -110,7 +110,7 @@ void platform_input() {
mouse_pos.y -= 0.5f; mouse_pos.y -= 0.5f;
mouse_pos = Vector2Normalize(mouse_pos); mouse_pos = Vector2Normalize(mouse_pos);
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { if (IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) {
x = mouse_pos.x; x = mouse_pos.x;
y = -mouse_pos.y; y = -mouse_pos.y;
} }
@ -132,8 +132,6 @@ void platform_input() {
}; };
if (build_submit_placements) { if (build_submit_placements) {
build_submit_placements = false;
in_data.placement_num = build_num_placements; in_data.placement_num = build_num_placements;
zpl_memcopy(in_data.placements, build_placements, build_num_placements*zpl_size_of(item_placement)); zpl_memcopy(in_data.placements, build_placements, build_num_placements*zpl_size_of(item_placement));
} }
@ -182,8 +180,8 @@ void platform_render() {
renderer_debug_draw(); renderer_debug_draw();
{ {
// NOTE(zaklaus): add-ins // NOTE(zaklaus): add-ins
inventory_draw();
buildmode_draw(); buildmode_draw();
inventory_draw();
} }
display_conn_status(); display_conn_status();
debug_draw(); debug_draw();

View File

@ -123,6 +123,17 @@ void RegenerateHP(ecs_iter_t *it) {
} }
} }
void ResetActivators(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1);
for (int i = 0; i < it->count; i++) {
in[i].use = false;
in[i].swap = false;
in[i].drop = false;
in[i].num_placements = 0;
}
}
void ApplyWorldDragOnVelocity(ecs_iter_t *it) { void ApplyWorldDragOnVelocity(ecs_iter_t *it) {
Position *p = ecs_column(it, Position, 1); Position *p = ecs_column(it, Position, 1);
Velocity *v = ecs_column(it, Velocity, 2); Velocity *v = ecs_column(it, Velocity, 2);
@ -168,6 +179,8 @@ void SystemsImport(ecs_world_t *ecs) {
//ECS_SYSTEM(ecs, MergeItems, EcsPostUpdate, components.Position, components.ItemDrop); //ECS_SYSTEM(ecs, MergeItems, EcsPostUpdate, components.Position, components.ItemDrop);
ECS_SYSTEM(ecs, UseItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle); ECS_SYSTEM(ecs, UseItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle);
ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input);
ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, components.Position, components.Velocity); ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, components.Position, components.Velocity);
ECS_SYSTEM(ecs, ClearVehicle, EcsUnSet, components.Vehicle); ECS_SYSTEM(ecs, ClearVehicle, EcsUnSet, components.Vehicle);

View File

@ -196,6 +196,7 @@ void UseItem(ecs_iter_t *it) {
Position pos = {.x = in[i].placements_x[j], .y = in[i].placements_y[j]}; Position pos = {.x = in[i].placements_x[j], .y = in[i].placements_y[j]};
item_use(it->world, item, pos); item_use(it->world, item, pos);
} }
in[i].num_placements = 0;
} }
} }
} }