eco2d/code/games/sandbox/src/platform.c

196 lines
5.5 KiB
C
Raw Normal View History

2022-09-28 05:29:32 +00:00
#include "platform/platform.h"
#include "raylib.h"
#include "raymath.h"
#include "net/network.h"
#include "core/game.h"
#include "world/entity_view.h"
#include "world/prediction.h"
#include "core/camera.h"
#include "math.h"
#include "platform/input.h"
2022-09-28 05:29:32 +00:00
#include "world/blocks.h"
2022-09-29 18:11:47 +00:00
#include "models/assets.h"
2022-09-28 05:29:32 +00:00
#include "platform/profiler.h"
#include "debug/debug_ui.h"
#include "debug/debug_draw.h"
#include "utils/raylib_helpers.h"
2022-09-29 11:59:51 +00:00
#include "platform/renderer.h"
2022-09-28 05:29:32 +00:00
2022-09-29 11:59:51 +00:00
#define ARCH_IMPL
#include "platform/arch.h"
2022-09-28 05:29:32 +00:00
#include "renderer.c"
// NOTE(zaklaus): add-ins
#include "gui/build_mode.c"
#include "gui/inventory.c"
void platform_init() {
2022-09-29 11:59:51 +00:00
platform_create_window("eco2d");
2022-09-28 05:29:32 +00:00
renderer_init();
2022-10-18 17:37:56 +00:00
2022-09-29 18:49:00 +00:00
target_zoom = 2.70f;
2022-09-28 05:29:32 +00:00
}
inline static
void display_conn_status() {
if (game_is_networked()) {
if (network_client_is_connected()) {
DrawText("Connection: online", 5, 5, 12, GREEN);
} else {
DrawText("Connection: offline", 5, 5, 12, RED);
}
} else {
DrawText("Connection: single-player", 5, 5, 12, BLUE);
}
}
void platform_shutdown() {
renderer_shutdown();
CloseWindow();
}
void platform_input() {
float mouse_z = (GetMouseWheelMove()*0.5f);
2022-09-28 20:01:47 +00:00
float mouse_modified = target_zoom < 4 ? mouse_z / (zpl_exp(4 - (target_zoom))) : mouse_z;
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
if (mouse_z != 0.0f) {
2022-09-28 20:01:47 +00:00
target_zoom = zpl_clamp(target_zoom + mouse_modified, 0.1f, 11.0f);
2022-09-28 05:29:32 +00:00
}
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
// NOTE(zaklaus): keystate handling
{
float x=0.0f, y=0.0f;
uint8_t use, sprint, drop, ctrl, pick;
if (input_is_down(IN_RIGHT)) x += 1.0f;
if (input_is_down(IN_LEFT)) x -= 1.0f;
if (input_is_down(IN_UP)) y += 1.0f;
if (input_is_down(IN_DOWN)) y -= 1.0f;
2022-10-18 17:37:56 +00:00
use = input_is_pressed(IN_USE);
sprint = input_is_down(IN_SPRINT);
ctrl = input_is_down(IN_CTRL);
drop = input_is_pressed(IN_DROP) || player_inv.drop_item || storage_inv.drop_item;
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
// NOTE(zaklaus): NEW! mouse movement
Vector2 mouse_pos = GetMousePosition();
mouse_pos.x /= screenWidth;
mouse_pos.y /= screenHeight;
mouse_pos.x -= 0.5f;
mouse_pos.y -= 0.5f;
mouse_pos = Vector2Normalize(mouse_pos);
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
if (game_get_kind() == GAMEKIND_SINGLE && IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) {
x = mouse_pos.x;
y = -mouse_pos.y;
}
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
inv_keystate *inv = (inv_is_storage_action) ? &storage_inv : &player_inv;
inv_keystate *inv2 = (!inv_is_storage_action) ? &storage_inv : &player_inv;
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
// NOTE(zaklaus): don't perform picking if we manipulate our inventories
pick = (inv_is_inside||inv->item_is_held||inv2->item_is_held) ? false : IsMouseButtonDown(MOUSE_LEFT_BUTTON);
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
game_keystate_data in_data = {
.x = x,
.y = y,
.mx = mouse_pos.x,
.my = mouse_pos.y,
.use = use,
.sprint = sprint,
.ctrl = ctrl,
.pick = pick,
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
.drop = drop,
.storage_action = inv_is_storage_action,
.selected_item = player_inv.selected_item,
.storage_selected_item = storage_inv.selected_item,
.swap = inv->swap,
.swap_storage = inv_swap_storage,
.swap_from = inv->swap_from,
.swap_to = inv->swap_to,
2022-10-18 17:37:56 +00:00
.craft_item = inv->craft_item,
2022-09-28 05:29:32 +00:00
.deletion_mode = build_is_deletion_mode,
};
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
if (build_submit_placements) {
in_data.placement_num = build_num_placements;
zpl_memcopy(in_data.placements, build_placements, build_num_placements*zpl_size_of(item_placement));
}
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
platform_input_update_input_frame(in_data);
}
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
// NOTE(zaklaus): cycle through viewers
{
if (IsKeyPressed(KEY_Q)) {
game_world_view_cycle_active(-1);
}
else if (IsKeyPressed(KEY_E)) {
game_world_view_cycle_active(1);
}
}
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
// NOTE(zaklaus): toggle debug drawing
#ifndef ECO2D_PROD
{
if (IsKeyPressed(KEY_T)) {
debug_draw_enable(!debug_draw_state());
}
}
#endif
}
void draw_selected_item() {
camera cam = camera_get();
entity_view *oe = game_world_view_active_get_entity(cam.ent_id);
if (oe) {
// NOTE(zaklaus): sel item
entity_view *e = game_world_view_active_get_entity(oe->sel_ent);
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
if (e && e->kind == EKIND_DEVICE) {
renderer_draw_single(e->x, e->y, ASSET_BLANK, ColorAlpha(RED, 0.4f));
}else{
// NOTE(zaklaus): hover item
entity_view *e = game_world_view_active_get_entity(oe->pick_ent);
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
if (e && e->kind == EKIND_DEVICE) {
renderer_draw_single(e->x, e->y, ASSET_BLANK, ColorAlpha(RED, 0.1f));
}
}
}
}
void platform_render() {
2022-09-29 11:59:51 +00:00
platform_resize_window();
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
profile(PROF_ENTITY_LERP) {
game_world_view_active_entity_map(lerp_entity_positions);
game_world_view_active_entity_map(do_entity_fadeinout);
}
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
assets_frame();
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
BeginDrawing();
{
profile (PROF_RENDER) {
renderer_draw();
draw_selected_item();
}
renderer_debug_draw();
{
// NOTE(zaklaus): add-ins
buildmode_draw();
inventory_draw();
}
display_conn_status();
debug_draw();
}
EndDrawing();
2022-10-18 17:37:56 +00:00
2022-09-28 05:29:32 +00:00
if (request_shutdown) {
CloseWindow();
}
}