2021-01-11 20:11:03 +00:00
|
|
|
#include "platform.h"
|
2021-01-10 16:42:01 +00:00
|
|
|
#include "raylib.h"
|
2021-05-07 22:11:09 +00:00
|
|
|
#include "raymath.h"
|
2021-05-05 09:25:05 +00:00
|
|
|
#include "network.h"
|
2021-05-04 20:44:55 +00:00
|
|
|
#include "game.h"
|
2021-05-05 09:25:05 +00:00
|
|
|
#include "entity_view.h"
|
2021-05-05 10:14:52 +00:00
|
|
|
#include "camera.h"
|
2021-01-10 16:42:01 +00:00
|
|
|
|
2021-05-06 21:45:55 +00:00
|
|
|
const uint16_t screenWidth = 1600;
|
|
|
|
const uint16_t screenHeight = 900;
|
2021-01-10 16:42:01 +00:00
|
|
|
|
2021-05-08 00:21:27 +00:00
|
|
|
#define GFX_WORLD_SCALE 20.0
|
|
|
|
|
2021-05-05 10:14:52 +00:00
|
|
|
static Camera2D render_camera;
|
|
|
|
|
2021-05-07 20:48:15 +00:00
|
|
|
void DrawTextEco(const char *text, int posX, int posY, int fontSize, Color color, float spacing) {
|
|
|
|
// Check if default font has been loaded
|
|
|
|
if (GetFontDefault().texture.id != 0) {
|
2021-05-08 00:21:27 +00:00
|
|
|
Vector2 position = { (float)posX * GFX_WORLD_SCALE, (float)posY * GFX_WORLD_SCALE };
|
2021-05-07 20:48:15 +00:00
|
|
|
|
|
|
|
int defaultFontSize = 10; // Default Font chars height in pixel
|
|
|
|
int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing;
|
|
|
|
|
2021-05-08 00:21:27 +00:00
|
|
|
DrawTextEx(GetFontDefault(), text, position, (float)fontSize * GFX_WORLD_SCALE, (float)new_spacing * GFX_WORLD_SCALE, color);
|
2021-05-07 20:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeasureTextEco(const char *text, int fontSize, float spacing) {
|
|
|
|
Vector2 vec = { 0.0f, 0.0f };
|
|
|
|
|
|
|
|
// Check if default font has been loaded
|
|
|
|
if (GetFontDefault().texture.id != 0) {
|
|
|
|
int defaultFontSize = 10; // Default Font chars height in pixel
|
|
|
|
int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing;
|
|
|
|
|
|
|
|
vec = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)new_spacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)vec.x;
|
|
|
|
}
|
2021-05-08 00:21:27 +00:00
|
|
|
void DrawCircleEco(int centerX, int centerY, float radius, Color color)
|
|
|
|
{
|
|
|
|
DrawCircleV((Vector2){ (float)centerX * GFX_WORLD_SCALE, (float)centerY * GFX_WORLD_SCALE }, radius * GFX_WORLD_SCALE, color);
|
|
|
|
}
|
|
|
|
void DrawRectangleEco(int posX, int posY, int width, int height, Color color)
|
|
|
|
{
|
|
|
|
DrawRectangleV((Vector2){ (float)posX * GFX_WORLD_SCALE, (float)posY * GFX_WORLD_SCALE }, (Vector2){ (float)width * GFX_WORLD_SCALE, (float)height * GFX_WORLD_SCALE }, color);
|
|
|
|
}
|
2021-05-07 20:48:15 +00:00
|
|
|
|
|
|
|
|
2021-01-11 20:11:03 +00:00
|
|
|
void platform_init() {
|
2021-01-11 13:47:14 +00:00
|
|
|
InitWindow(screenWidth, screenHeight, "eco2d - client");
|
2021-01-10 16:42:01 +00:00
|
|
|
SetTargetFPS(60);
|
2021-05-05 10:14:52 +00:00
|
|
|
|
|
|
|
render_camera.target = (Vector2){0.0f,0.0f};
|
|
|
|
render_camera.offset = (Vector2){screenWidth/2.0f, screenHeight/2.0f};
|
|
|
|
render_camera.rotation = 0.0f;
|
2021-05-08 00:21:27 +00:00
|
|
|
render_camera.zoom = 4.0f/GFX_WORLD_SCALE;
|
2021-01-10 16:42:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 20:11:03 +00:00
|
|
|
void platform_shutdown() {
|
2021-01-10 16:42:01 +00:00
|
|
|
CloseWindow();
|
|
|
|
}
|
2021-05-07 20:48:15 +00:00
|
|
|
|
2021-01-11 20:11:03 +00:00
|
|
|
uint8_t platform_is_running() {
|
2021-01-11 20:08:16 +00:00
|
|
|
return !WindowShouldClose();
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:27:51 +00:00
|
|
|
void platform_input() {
|
|
|
|
float mouse_z = GetMouseWheelMove();
|
|
|
|
|
|
|
|
if (mouse_z != 0.0f) {
|
2021-05-08 00:21:27 +00:00
|
|
|
render_camera.zoom = zpl_clamp(render_camera.zoom+mouse_z*0.04f, 0.2f/GFX_WORLD_SCALE, 320.0f/GFX_WORLD_SCALE);
|
2021-05-07 12:27:51 +00:00
|
|
|
}
|
2021-05-07 14:43:54 +00:00
|
|
|
|
|
|
|
// NOTE(zaklaus): keystate handling
|
|
|
|
{
|
|
|
|
double x=0.0, y=0.0;
|
2021-05-07 20:48:15 +00:00
|
|
|
uint8_t use, sprint;
|
2021-05-07 14:43:54 +00:00
|
|
|
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) 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_DOWN) || IsKeyDown(KEY_S)) y += 1.0f;
|
|
|
|
|
|
|
|
use = IsKeyPressed(KEY_SPACE);
|
2021-05-07 20:48:15 +00:00
|
|
|
sprint = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
|
2021-05-07 14:43:54 +00:00
|
|
|
|
2021-05-07 22:11:09 +00:00
|
|
|
// NOTE(zaklaus): NEW! mouse movement
|
|
|
|
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) {
|
|
|
|
Vector2 mouse_pos = GetMousePosition();
|
|
|
|
mouse_pos.x /= screenWidth;
|
|
|
|
mouse_pos.y /= screenHeight;
|
|
|
|
x = mouse_pos.x-0.5;
|
|
|
|
y = mouse_pos.y-0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-07 20:48:15 +00:00
|
|
|
game_action_send_keystate(x, y, use, sprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2021-05-07 14:43:54 +00:00
|
|
|
}
|
2021-05-07 12:27:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 20:44:55 +00:00
|
|
|
void display_conn_status();
|
|
|
|
|
2021-05-05 09:25:05 +00:00
|
|
|
void DEBUG_draw_entities(uint64_t key, entity_view data);
|
2021-05-06 21:45:55 +00:00
|
|
|
void DEBUG_draw_ground(uint64_t key, entity_view data);
|
2021-05-05 09:25:05 +00:00
|
|
|
|
2021-01-11 20:11:03 +00:00
|
|
|
void platform_render() {
|
2021-05-05 10:14:52 +00:00
|
|
|
camera game_camera = camera_get();
|
2021-05-08 00:21:27 +00:00
|
|
|
render_camera.target = (Vector2){game_camera.x * GFX_WORLD_SCALE, game_camera.y * GFX_WORLD_SCALE};
|
2021-05-05 10:14:52 +00:00
|
|
|
|
2021-01-10 16:42:01 +00:00
|
|
|
BeginDrawing();
|
2021-05-04 20:44:55 +00:00
|
|
|
ClearBackground(BLACK);
|
2021-05-05 10:14:52 +00:00
|
|
|
BeginMode2D(render_camera);
|
2021-05-06 21:45:55 +00:00
|
|
|
entity_view_map(&game_world_view_get_active()->entities, DEBUG_draw_ground);
|
2021-05-06 15:30:38 +00:00
|
|
|
entity_view_map(&game_world_view_get_active()->entities, DEBUG_draw_entities);
|
2021-05-05 10:14:52 +00:00
|
|
|
EndMode2D();
|
2021-05-05 09:25:05 +00:00
|
|
|
display_conn_status();
|
2021-01-10 16:42:01 +00:00
|
|
|
EndDrawing();
|
|
|
|
}
|
2021-05-04 20:44:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-05-08 00:21:27 +00:00
|
|
|
|
|
|
|
DrawFPS(0, 20);
|
2021-05-05 09:25:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 21:45:55 +00:00
|
|
|
void DEBUG_draw_ground(uint64_t key, entity_view data) {
|
|
|
|
world_view *view = game_world_view_get_active();
|
|
|
|
|
|
|
|
int32_t x = data.x * view->chunk_size * view->block_size;
|
|
|
|
int32_t y = data.y * view->chunk_size * view->block_size;
|
|
|
|
|
|
|
|
int32_t size = view->chunk_size * view->block_size;
|
|
|
|
int32_t half_size = size/2;
|
2021-05-08 00:21:27 +00:00
|
|
|
int16_t block_size = view->block_size;
|
|
|
|
int32_t half_block_size = block_size/2;
|
2021-05-07 20:48:15 +00:00
|
|
|
int16_t offset = 10;
|
|
|
|
int16_t block_offset = 1;
|
2021-05-06 21:45:55 +00:00
|
|
|
|
|
|
|
switch (data.kind) {
|
|
|
|
case EKIND_CHUNK: {
|
2021-05-08 00:21:27 +00:00
|
|
|
DrawRectangleEco(x+offset-half_size, y+offset-half_size, size-offset, size-offset, LIME);
|
2021-05-07 20:48:15 +00:00
|
|
|
for (uint16_t i = 0; i < view->chunk_size*view->chunk_size; i++) {
|
2021-05-08 00:21:27 +00:00
|
|
|
int32_t bx = i % view->block_size * block_size + x - half_size + offset;
|
|
|
|
int32_t by = i / view->block_size * block_size + y - half_size + offset;
|
|
|
|
DrawRectangleEco(bx+block_offset-half_block_size,
|
2021-05-07 20:48:15 +00:00
|
|
|
by+block_offset-half_block_size,
|
2021-05-08 00:21:27 +00:00
|
|
|
block_size-block_offset,
|
|
|
|
block_size-block_offset,
|
2021-05-07 20:48:15 +00:00
|
|
|
GREEN);
|
|
|
|
}
|
|
|
|
|
2021-05-08 00:21:27 +00:00
|
|
|
DrawTextEco(TextFormat("%.01f %.01f", data.x, data.y), x-half_size+5, y-half_size+5, 65 , BLACK, 0.0);
|
2021-05-06 21:45:55 +00:00
|
|
|
}break;
|
|
|
|
|
|
|
|
default:break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:48:15 +00:00
|
|
|
static inline float lerp(float a, float b, float t) { return a * (1.0f - t) + b * t; }
|
|
|
|
|
2021-05-05 09:25:05 +00:00
|
|
|
void DEBUG_draw_entities(uint64_t key, entity_view data) {
|
2021-05-06 18:24:01 +00:00
|
|
|
world_view *view = game_world_view_get_active();
|
2021-05-07 20:48:15 +00:00
|
|
|
uint16_t size = 4;
|
2021-05-08 00:21:27 +00:00
|
|
|
uint16_t font_size = (uint16_t)lerp(4, 32, 0.5f / GFX_WORLD_SCALE/render_camera.zoom);
|
2021-05-07 20:48:15 +00:00
|
|
|
float font_spacing = 1.1f;
|
2021-05-08 00:21:27 +00:00
|
|
|
float title_bg_offset = 4;
|
|
|
|
float fixed_title_offset = 2;
|
2021-05-06 18:24:01 +00:00
|
|
|
|
|
|
|
switch (data.kind) {
|
|
|
|
case EKIND_PLAYER: {
|
2021-05-08 00:21:27 +00:00
|
|
|
double x = data.x;
|
|
|
|
double y = data.y;
|
2021-05-07 20:48:15 +00:00
|
|
|
const char *title = TextFormat("Player %d", key);
|
|
|
|
int title_w = MeasureTextEco(title, font_size, font_spacing);
|
2021-05-08 00:21:27 +00:00
|
|
|
DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, BLACK);
|
|
|
|
DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, RAYWHITE, font_spacing);
|
|
|
|
DrawCircleEco(x, y, size, RED);
|
|
|
|
}break;
|
2021-05-06 18:24:01 +00:00
|
|
|
default:break;
|
|
|
|
}
|
2021-05-04 20:44:55 +00:00
|
|
|
}
|