code: changed line endings

isolation_bkp/dynres
Vladyslav Hrytsenko 2021-11-01 20:36:26 +02:00
parent 9fe2c68ef4
commit f14f3cdaef
2 changed files with 378 additions and 378 deletions

View File

@ -1,184 +1,184 @@
#include "platform.h" #include "platform.h"
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
#include "network.h" #include "network.h"
#include "game.h" #include "game.h"
#include "entity_view.h" #include "entity_view.h"
#include "prediction.h" #include "prediction.h"
#include "camera.h" #include "camera.h"
#include "math.h" #include "math.h"
#include "world/blocks.h" #include "world/blocks.h"
#include "assets.h" #include "assets.h"
#include "profiler.h" #include "profiler.h"
#include "debug_ui.h" #include "debug_ui.h"
#include "utils/raylib_helpers.h" #include "utils/raylib_helpers.h"
static uint16_t screenWidth = 1600; static uint16_t screenWidth = 1600;
static uint16_t screenHeight = 900; static uint16_t screenHeight = 900;
static float target_zoom = 1.5f; static float target_zoom = 1.5f;
static bool request_shutdown; static bool request_shutdown;
#define GFX_KIND 2 #define GFX_KIND 2
#include "renderer_bridge.c" #include "renderer_bridge.c"
// NOTE(zaklaus): add-ins // NOTE(zaklaus): add-ins
#include "gui/inventory.c" #include "gui/inventory.c"
#include "gui/build_mode.c" #include "gui/build_mode.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 = GetScreenWidth();
screenHeight = GetScreenHeight(); screenHeight = GetScreenHeight();
ToggleFullscreen(); ToggleFullscreen();
renderer_init(); renderer_init();
} }
void display_conn_status() { void display_conn_status() {
if (game_is_networked()) { if (game_is_networked()) {
if (network_client_is_connected()) { if (network_client_is_connected()) {
DrawText("Connection: online", 5, 5, 12, GREEN); DrawText("Connection: online", 5, 5, 12, GREEN);
} else { } else {
DrawText("Connection: offline", 5, 5, 12, RED); DrawText("Connection: offline", 5, 5, 12, RED);
} }
} else { } else {
DrawText("Connection: single-player", 5, 5, 12, BLUE); DrawText("Connection: single-player", 5, 5, 12, BLUE);
} }
} }
void platform_shutdown() { void platform_shutdown() {
renderer_shutdown(); renderer_shutdown();
CloseWindow(); CloseWindow();
} }
uint8_t platform_is_running() { uint8_t platform_is_running() {
return !WindowShouldClose(); return !WindowShouldClose();
} }
void platform_input() { void platform_input() {
float mouse_z = (GetMouseWheelMove()*0.5f); float mouse_z = (GetMouseWheelMove()*0.5f);
if (mouse_z != 0.0f) { if (mouse_z != 0.0f) {
target_zoom = zpl_clamp(target_zoom+mouse_z, 0.1f, 10.0f); target_zoom = zpl_clamp(target_zoom+mouse_z, 0.1f, 10.0f);
} }
// NOTE(zaklaus): keystate handling // NOTE(zaklaus): keystate handling
{ {
float x=0.0f, y=0.0f; float x=0.0f, y=0.0f;
uint8_t use, sprint, drop, ctrl; uint8_t use, sprint, drop, ctrl;
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) x += 1.0f; if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) x += 1.0f;
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) x -= 1.0f; if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) x -= 1.0f;
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) y += 1.0f; if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) y += 1.0f;
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) y -= 1.0f; if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) y -= 1.0f;
use = IsKeyPressed(KEY_SPACE); use = IsKeyPressed(KEY_SPACE);
sprint = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT); sprint = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
ctrl = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL); ctrl = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL);
drop = IsKeyPressed(KEY_G) || inv_drop_item; drop = IsKeyPressed(KEY_G) || inv_drop_item;
// NOTE(zaklaus): NEW! mouse movement // NOTE(zaklaus): NEW! mouse movement
Vector2 mouse_pos = GetMousePosition(); Vector2 mouse_pos = GetMousePosition();
mouse_pos.x /= screenWidth; mouse_pos.x /= screenWidth;
mouse_pos.y /= screenHeight; mouse_pos.y /= screenHeight;
mouse_pos.x -= 0.5f; mouse_pos.x -= 0.5f;
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_RIGHT_BUTTON)) {
x = mouse_pos.x; x = mouse_pos.x;
y = -mouse_pos.y; y = -mouse_pos.y;
} }
game_keystate_data data = { game_keystate_data data = {
.x = x, .x = x,
.y = y, .y = y,
.mx = mouse_pos.x, .mx = mouse_pos.x,
.my = mouse_pos.y, .my = mouse_pos.y,
.use = use, .use = use,
.sprint = sprint, .sprint = sprint,
.ctrl = ctrl, .ctrl = ctrl,
.drop = drop, .drop = drop,
.selected_item = inv_selected_item, .selected_item = inv_selected_item,
.swap = inv_swap, .swap = inv_swap,
.swap_from = inv_swap_from, .swap_from = inv_swap_from,
.swap_to = inv_swap_to, .swap_to = inv_swap_to,
}; };
if (build_submit_placements) { if (build_submit_placements) {
build_submit_placements = false; build_submit_placements = false;
data.placement_num = build_num_placements; data.placement_num = build_num_placements;
zpl_memcopy(data.placements, build_placements, build_num_placements*zpl_size_of(item_placement)); zpl_memcopy(data.placements, build_placements, build_num_placements*zpl_size_of(item_placement));
} }
game_action_send_keystate(&data); game_action_send_keystate(&data);
} }
// NOTE(zaklaus): cycle through viewers // NOTE(zaklaus): cycle through viewers
{ {
if (IsKeyPressed(KEY_Q)) { if (IsKeyPressed(KEY_Q)) {
game_world_view_cycle_active(-1); game_world_view_cycle_active(-1);
} }
else if (IsKeyPressed(KEY_E)) { else if (IsKeyPressed(KEY_E)) {
game_world_view_cycle_active(1); game_world_view_cycle_active(1);
} }
} }
// NOTE(zaklaus): switch render modes // NOTE(zaklaus): switch render modes
{ {
if (IsKeyPressed(KEY_O)) { if (IsKeyPressed(KEY_O)) {
renderer_switch(1-gfx_kind); renderer_switch(1-gfx_kind);
} }
} }
// NOTE(zaklaus): toggle debug drawing // NOTE(zaklaus): toggle debug drawing
#ifndef ECO2D_PROD #ifndef ECO2D_PROD
{ {
if (IsKeyPressed(KEY_T)) { if (IsKeyPressed(KEY_T)) {
debug_draw_enable(!debug_draw_state()); debug_draw_enable(!debug_draw_state());
} }
} }
#endif #endif
} }
void platform_render() { void platform_render() {
profile(PROF_ENTITY_LERP) { profile(PROF_ENTITY_LERP) {
game_world_view_active_entity_map(lerp_entity_positions); game_world_view_active_entity_map(lerp_entity_positions);
game_world_view_active_entity_map(do_entity_fadeinout); game_world_view_active_entity_map(do_entity_fadeinout);
} }
BeginDrawing(); BeginDrawing();
{ {
profile (PROF_RENDER) { profile (PROF_RENDER) {
renderer_draw(); renderer_draw();
} }
renderer_debug_draw(); renderer_debug_draw();
{ {
// NOTE(zaklaus): add-ins // NOTE(zaklaus): add-ins
inventory_draw(); inventory_draw();
buildmode_draw(); buildmode_draw();
} }
display_conn_status(); display_conn_status();
debug_draw(); debug_draw();
} }
EndDrawing(); EndDrawing();
if (request_shutdown) { if (request_shutdown) {
CloseWindow(); CloseWindow();
} }
} }
float platform_frametime() { float platform_frametime() {
return GetFrameTime(); return GetFrameTime();
} }
float platform_zoom_get(void) { float platform_zoom_get(void) {
return target_zoom; return target_zoom;
} }
void platform_request_close(void) { void platform_request_close(void) {
request_shutdown = true; request_shutdown = true;
} }

View File

@ -1,194 +1,194 @@
#include "debug_draw.h" #include "debug_draw.h"
#define VEH_ENTER_RADIUS 45.0f #define VEH_ENTER_RADIUS 45.0f
void LeaveVehicle(ecs_iter_t *it) { void LeaveVehicle(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1); Input *in = ecs_column(it, Input, 1);
IsInVehicle *vehp = ecs_column(it, IsInVehicle, 2); IsInVehicle *vehp = ecs_column(it, IsInVehicle, 2);
Velocity *v = ecs_column(it, Velocity, 3); Velocity *v = ecs_column(it, Velocity, 3);
for (int i = 0; i < it->count; i++) { for (int i = 0; i < it->count; i++) {
if (!in[i].use) continue; if (!in[i].use) continue;
Vehicle *veh = 0; Vehicle *veh = 0;
if ((veh = ecs_get_mut_if(it->world, vehp->veh, Vehicle))) { if ((veh = ecs_get_mut_if(it->world, vehp->veh, Vehicle))) {
for (int k = 0; k < 4; k++) { for (int k = 0; k < 4; k++) {
if (veh->seats[k] == it->entities[i]) { if (veh->seats[k] == it->entities[i]) {
veh->seats[k] = 0; veh->seats[k] = 0;
break; break;
} }
} }
in[i].use = false; in[i].use = false;
ecs_remove(it->world, it->entities[i], IsInVehicle); ecs_remove(it->world, it->entities[i], IsInVehicle);
// NOTE(zaklaus): push passenger out // NOTE(zaklaus): push passenger out
{ {
float px = zpl_cos(veh->heading)*400.0f; float px = zpl_cos(veh->heading)*400.0f;
float py = zpl_sin(veh->heading)*400.0f; float py = zpl_sin(veh->heading)*400.0f;
v->x += py; v->x += py;
v->y -= px; v->y -= px;
} }
} else { } else {
ZPL_PANIC("unreachable code"); ZPL_PANIC("unreachable code");
} }
} }
} }
void EnterVehicle(ecs_iter_t *it) { void EnterVehicle(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1); Input *in = ecs_column(it, Input, 1);
Position *p = ecs_column(it, Position, 2); Position *p = ecs_column(it, Position, 2);
for (int i = 0; i < it->count; i++) { for (int i = 0; i < it->count; i++) {
if (!in[i].use) continue; if (!in[i].use) continue;
size_t ents_count; size_t ents_count;
int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2); int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2);
bool has_entered_veh = false; bool has_entered_veh = false;
for (size_t j = 0; j < ents_count; j++) { for (size_t j = 0; j < ents_count; j++) {
Vehicle *veh = 0; Vehicle *veh = 0;
if (has_entered_veh) break; if (has_entered_veh) break;
if ((veh = ecs_get_mut_if(it->world, ents[j], Vehicle))) { if ((veh = ecs_get_mut_if(it->world, ents[j], Vehicle))) {
Position const* p2 = ecs_get(it->world, ents[j], Position); Position const* p2 = ecs_get(it->world, ents[j], Position);
float dx = p2->x - p[i].x; float dx = p2->x - p[i].x;
float dy = p2->y - p[i].y; float dy = p2->y - p[i].y;
float range = zpl_sqrt(dx*dx + dy*dy); float range = zpl_sqrt(dx*dx + dy*dy);
if (range <= VEH_ENTER_RADIUS) { if (range <= VEH_ENTER_RADIUS) {
for (int k = 0; k < 4; k++) { for (int k = 0; k < 4; k++) {
if (veh->seats[k] != 0) continue; if (veh->seats[k] != 0) continue;
// NOTE(zaklaus): We can enter the vehicle, yay! // NOTE(zaklaus): We can enter the vehicle, yay!
veh->seats[k] = it->entities[i]; veh->seats[k] = it->entities[i];
ecs_set(it->world, it->entities[i], IsInVehicle, { ecs_set(it->world, it->entities[i], IsInVehicle, {
.veh = ents[j] .veh = ents[j]
}); });
p[i] = *p2; p[i] = *p2;
in[i].use = false; in[i].use = false;
has_entered_veh = true; has_entered_veh = true;
break; break;
} }
} }
} }
} }
} }
} }
#define VEHICLE_FORCE 340.8f #define VEHICLE_FORCE 340.8f
#define VEHICLE_ACCEL 0.12f #define VEHICLE_ACCEL 0.12f
#define VEHICLE_DECEL 0.28f #define VEHICLE_DECEL 0.28f
#define VEHICLE_STEER 9.89f #define VEHICLE_STEER 9.89f
#define VEHICLE_STEER_REVERT 6.0941816f #define VEHICLE_STEER_REVERT 6.0941816f
#define VEHICLE_POWER 97.89f #define VEHICLE_POWER 97.89f
#define VEHICLE_BRAKE_FORCE 0.84f #define VEHICLE_BRAKE_FORCE 0.84f
void VehicleHandling(ecs_iter_t *it) { void VehicleHandling(ecs_iter_t *it) {
Vehicle *veh = ecs_column(it, Vehicle, 1); Vehicle *veh = ecs_column(it, Vehicle, 1);
Position *p = ecs_column(it, Position, 2); Position *p = ecs_column(it, Position, 2);
Velocity *v = ecs_column(it, Velocity, 3); Velocity *v = ecs_column(it, Velocity, 3);
for (int i = 0; i < it->count; i++) { for (int i = 0; i < it->count; i++) {
Vehicle *car = &veh[i]; Vehicle *car = &veh[i];
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
// NOTE(zaklaus): Perform seat cleanup // NOTE(zaklaus): Perform seat cleanup
if (!world_entity_valid(veh[i].seats[j])) { if (!world_entity_valid(veh[i].seats[j])) {
veh[i].seats[j] = 0; veh[i].seats[j] = 0;
continue; continue;
} }
ecs_entity_t pe = veh[i].seats[j]; ecs_entity_t pe = veh[i].seats[j];
// NOTE(zaklaus): Handle driver input // NOTE(zaklaus): Handle driver input
if (j == 0) { if (j == 0) {
Input const* in = ecs_get(it->world, pe, Input); Input const* in = ecs_get(it->world, pe, Input);
car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL*safe_dt(it)); car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL*safe_dt(it));
if (in->sprint) { if (in->sprint) {
car->force = zpl_lerp(car->force, 0.0f, VEHICLE_BRAKE_FORCE*safe_dt(it)); car->force = zpl_lerp(car->force, 0.0f, VEHICLE_BRAKE_FORCE*safe_dt(it));
if (zpl_abs(car->force) < 5.5f) if (zpl_abs(car->force) < 5.5f)
car->force = 0.0f; car->force = 0.0f;
} }
car->steer = zpl_lerp(car->steer, 0.0f, safe_dt(it)*VEHICLE_STEER_REVERT); car->steer = zpl_lerp(car->steer, 0.0f, safe_dt(it)*VEHICLE_STEER_REVERT);
car->steer += (in->x * VEHICLE_STEER)*safe_dt(it); car->steer += (in->x * VEHICLE_STEER)*safe_dt(it);
car->steer = zpl_clamp(car->steer, -60.0f, 60.0f); car->steer = zpl_clamp(car->steer, -60.0f, 60.0f);
} }
} }
car->force = zpl_clamp(car->force, car->reverse_speed, car->speed); car->force = zpl_clamp(car->force, car->reverse_speed, car->speed);
// NOTE(zaklaus): Vehicle physics // NOTE(zaklaus): Vehicle physics
float fr_x = p[i].x + (car->wheel_base/2.0f) * zpl_cos(car->heading); float fr_x = p[i].x + (car->wheel_base/2.0f) * zpl_cos(car->heading);
float fr_y = p[i].y + (car->wheel_base/2.0f) * zpl_sin(car->heading); float fr_y = p[i].y + (car->wheel_base/2.0f) * zpl_sin(car->heading);
float bk_x = p[i].x - (car->wheel_base/2.0f) * zpl_cos(car->heading); float bk_x = p[i].x - (car->wheel_base/2.0f) * zpl_cos(car->heading);
float bk_y = p[i].y - (car->wheel_base/2.0f) * zpl_sin(car->heading); float bk_y = p[i].y - (car->wheel_base/2.0f) * zpl_sin(car->heading);
world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y);
float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f); float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f);
bk_x += car->force * drag * zpl_cos(car->heading) * safe_dt(it); bk_x += car->force * drag * zpl_cos(car->heading) * safe_dt(it);
bk_y += car->force * drag * zpl_sin(car->heading) * safe_dt(it); bk_y += car->force * drag * zpl_sin(car->heading) * safe_dt(it);
fr_x += car->force * drag * zpl_cos(car->heading + zpl_to_radians(car->steer)) * safe_dt(it)*VEHICLE_POWER; fr_x += car->force * drag * zpl_cos(car->heading + zpl_to_radians(car->steer)) * safe_dt(it)*VEHICLE_POWER;
fr_y += car->force * drag * zpl_sin(car->heading + zpl_to_radians(car->steer)) * safe_dt(it)*VEHICLE_POWER; fr_y += car->force * drag * zpl_sin(car->heading + zpl_to_radians(car->steer)) * safe_dt(it)*VEHICLE_POWER;
v[i].x += ((fr_x + bk_x) / 2.0f - p[i].x); v[i].x += ((fr_x + bk_x) / 2.0f - p[i].x);
v[i].y += ((fr_y + bk_y) / 2.0f - p[i].y); v[i].y += ((fr_y + bk_y) / 2.0f - p[i].y);
car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x); 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)); world_block_lookup lookahead = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y+PHY_LOOKAHEAD(v[i].y));
uint32_t flags = blocks_get_flags(lookahead.block_id); uint32_t flags = blocks_get_flags(lookahead.block_id);
if (flags & BLOCK_FLAG_COLLISION) { if (flags & BLOCK_FLAG_COLLISION) {
car->force = 0.0f; car->force = 0.0f;
} }
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
if (!world_entity_valid(veh[i].seats[j])) continue; if (!world_entity_valid(veh[i].seats[j])) continue;
ecs_entity_t pe = veh[i].seats[j]; ecs_entity_t pe = veh[i].seats[j];
// NOTE(zaklaus): Update passenger position // NOTE(zaklaus): Update passenger position
{ {
Position *p2 = ecs_get_mut(it->world, pe, Position, NULL); Position *p2 = ecs_get_mut(it->world, pe, Position, NULL);
Velocity *v2 = ecs_get_mut(it->world, pe, Velocity, NULL); Velocity *v2 = ecs_get_mut(it->world, pe, Velocity, NULL);
*p2 = p[i]; *p2 = p[i];
*v2 = v[i]; *v2 = v[i];
} }
} }
{ {
debug_v2 b2 = {p[i].x + zpl_cos(car->heading)*(car->wheel_base), p[i].y + zpl_sin(car->heading)*(car->wheel_base)}; debug_v2 b2 = {p[i].x + zpl_cos(car->heading)*(car->wheel_base), p[i].y + zpl_sin(car->heading)*(car->wheel_base)};
debug_push_line((debug_v2){p[i].x, p[i].y}, b2, 0x0000FFFF); debug_push_line((debug_v2){p[i].x, p[i].y}, b2, 0x0000FFFF);
// NOTE(zaklaus): force // NOTE(zaklaus): force
{ {
float dx = zpl_cos(car->heading); float dx = zpl_cos(car->heading);
float dy = zpl_sin(car->heading); float dy = zpl_sin(car->heading);
debug_push_circle((debug_v2){p[i].x+dx*car->force, p[i].y+dy*car->force}, 5.0f, 0x00FF00FF); debug_push_circle((debug_v2){p[i].x+dx*car->force, p[i].y+dy*car->force}, 5.0f, 0x00FF00FF);
} }
// NOTE(zaklaus): steer // NOTE(zaklaus): steer
{ {
float dx = zpl_sin(car->heading); float dx = zpl_sin(car->heading);
float dy = -zpl_cos(car->heading); float dy = -zpl_cos(car->heading);
float steer_mult = -80.0f; float steer_mult = -80.0f;
debug_push_circle((debug_v2){p[i].x+dx*car->steer*steer_mult, p[i].y+dy*car->steer*steer_mult}, 5.0f, 0x00FFAAFF); debug_push_circle((debug_v2){p[i].x+dx*car->steer*steer_mult, p[i].y+dy*car->steer*steer_mult}, 5.0f, 0x00FFAAFF);
} }
} }
} }
} }
void ClearVehicle(ecs_iter_t *it) { void ClearVehicle(ecs_iter_t *it) {
Vehicle *veh = ecs_column(it, Vehicle, 1); Vehicle *veh = ecs_column(it, Vehicle, 1);
for (int i = 0; i < it->count; i++) { for (int i = 0; i < it->count; i++) {
for (int k = 0; k < 4; k++) { for (int k = 0; k < 4; k++) {
if (world_entity_valid(veh[i].seats[k])) { if (world_entity_valid(veh[i].seats[k])) {
ecs_remove(it->world, veh[i].seats[k], IsInVehicle); ecs_remove(it->world, veh[i].seats[k], IsInVehicle);
} }
} }
} }
} }