summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2022-12-18 11:24:42 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2022-12-18 11:24:42 +0100
commit1ac44d4ec6b6b51fefe6ca50ef82d5d2fc1f6dfb (patch)
tree8322478a56450878e2f02a6a794a4a664d86e176 /src
parent89984db7afa433e2842c4ef8c8c265a3e3993636 (diff)
wall items
Diffstat (limited to 'src')
-rw-r--r--src/bullets.c5
-rw-r--r--src/game.c9
-rw-r--r--src/math_helper.c17
-rw-r--r--src/objects.c10
-rw-r--r--src/overlay.c11
-rw-r--r--src/players.c2
-rw-r--r--src/wall_item.c84
-rw-r--r--src/zombies.c2
8 files changed, 129 insertions, 11 deletions
diff --git a/src/bullets.c b/src/bullets.c
index 7397e9f..8012c83 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -182,7 +182,9 @@ static bool check_if_bullet_collided_with_ground(bullet *b, platform_window* win
void clear_bullets() {
for (int i = 0; i < max_bullets; i++) {
- bullets[i].active = false;
+ if (bullets[i].alive_time >= 0.05f) {
+ bullets[i].active = false;
+ }
}
}
@@ -213,7 +215,6 @@ void update_bullets(platform_window* window) {
}
bullets[i].alive_time += update_delta;
- //bullets[i].active = false;
}
}
diff --git a/src/game.c b/src/game.c
index 893d482..7a2fc24 100644
--- a/src/game.c
+++ b/src/game.c
@@ -73,8 +73,8 @@ void init_game() {
}
void destroy_game() {
- if (global_state.server) networking_destroy_server(global_state.server);
if (global_state.client) network_client_close(global_state.client);
+ if (global_state.server) networking_destroy_server(global_state.server);
}
static void broadcast_to_clients(network_message message) {
@@ -113,8 +113,6 @@ static void set_ping_for_player(protocol_generic_message* msg) {
float update_timer = 0.0f;
void update_server(platform_window* window) {
- clear_bullets();
-
for (int i = 0; i < messages_received_on_server.length; i++) {
protocol_generic_message* msg = *(protocol_generic_message**)array_at(&messages_received_on_server, i);
set_ping_for_player(msg);
@@ -188,14 +186,13 @@ static void load_bullets_into_existing_list(protocol_bullets_list* msg_bullets)
for (int x = 0; x < max_bullets; x++) {
if (!msg_bullets->bullets[x].active) continue;
bullets[i] = msg_bullets->bullets[x];
+ bullets[i].alive_time = 0.0f;
msg_bullets->bullets[x].active = false;
}
}
}
void update_client(platform_window* window) {
- clear_bullets();
-
for (int i = 0; i < messages_received_on_client.length; i++) {
protocol_generic_client_message* msg = *(protocol_generic_client_message**)array_at(&messages_received_on_client, i);
@@ -243,6 +240,8 @@ void update_client(platform_window* window) {
}
void update_game(platform_window* window) {
+ clear_bullets();
+
if (global_state.server) {
update_server(window);
}
diff --git a/src/math_helper.c b/src/math_helper.c
index 69ad530..436c81b 100644
--- a/src/math_helper.c
+++ b/src/math_helper.c
@@ -105,6 +105,23 @@ box get_box_of_square(vec3f position, vec3f size) {
return (box){rendertl, rendertr, renderbl, renderbr, rendertl, rendertr, renderbl, renderbr};
}
+box get_render_box_of_square_without_incline(platform_window* window, vec3f position, vec3f size) {
+ map_info info = get_map_info(window);
+ float render_x = (info.tile_width * position.x);
+ vec2f rendertl = (vec2f){render_x, info.tile_width * position.y - position.z*info.px_raised_per_h};
+ vec2f rendertr = (vec2f){render_x + info.tile_width*size.x, info.tile_height * position.y - position.z*info.px_raised_per_h};
+ vec2f renderbr = (vec2f){render_x + info.tile_width*size.x, info.tile_height * position.y + info.tile_height*size.y - position.z*info.px_raised_per_h};
+ vec2f renderbl = (vec2f){render_x, info.tile_height * position.y + info.tile_height*size.y - position.z*info.px_raised_per_h};
+
+ position.z += size.z;
+ vec2f rendertl2 = (vec2f){render_x, info.tile_width * position.y - position.z*info.px_raised_per_h};
+ vec2f rendertr2 = (vec2f){render_x + info.tile_width*size.x, info.tile_height * position.y - position.z*info.px_raised_per_h};
+ vec2f renderbr2 = (vec2f){render_x + info.tile_width*size.x, info.tile_height * position.y + info.tile_height*size.y - position.z*info.px_raised_per_h};
+ vec2f renderbl2 = (vec2f){render_x, info.tile_height * position.y + info.tile_height*size.y - position.z*info.px_raised_per_h};
+
+ return (box){rendertl, rendertr, renderbl, renderbr, rendertl2, rendertr2, renderbl2, renderbr2};
+}
+
box get_render_box_of_square(platform_window* window, vec3f position, vec3f size) {
map_info info = get_map_info(window);
float render_x = (info.tile_width * position.x) + (info.px_incline * position.y);
diff --git a/src/objects.c b/src/objects.c
index a4d86e1..a925dc4 100644
--- a/src/objects.c
+++ b/src/objects.c
@@ -5,6 +5,16 @@ box get_box_of_object(platform_window* window, object o) {
return get_render_box_of_square(window, (vec3f){o.position.x, o.position.y, o.h}, o.size);
}
+void render_box_with_outline(box box, color c) {
+ render_quad_with_outline(box.tl_d, box.tr_d, box.bl_d, box.br_d, c); // down
+ render_quad_with_outline(box.tl_u, box.tr_u, box.bl_u, box.br_u, c); // up
+
+ render_quad_with_outline(box.tr_u, box.tr_d, box.br_u, box.br_d, c); // right
+ render_quad_with_outline(box.tl_u, box.tl_d, box.bl_u, box.bl_d, c); // left
+ render_quad_with_outline(box.bl_u, box.br_u, box.bl_d, box.br_d, c); // bottom
+
+}
+
void render_quad_with_outline(vec2f tl, vec2f tr, vec2f bl, vec2f br, color c) {
renderer->render_quad(
tl.x, tl.y,
diff --git a/src/overlay.c b/src/overlay.c
index 9be8329..e8bd2a9 100644
--- a/src/overlay.c
+++ b/src/overlay.c
@@ -62,8 +62,19 @@ static void draw_leaderboard(platform_window* window) {
}
}
+void draw_debug_stats(platform_window* window) {
+ float fps = 1.0f / update_delta;
+ float usage = (update_delta / (1.0f / 60.0f) * 100);
+
+ char fps_text[50];
+ snprintf(fps_text, 50, "FPS: %d, MS: %.4f, USAGE: %.2f", (int)fps, update_delta*1000.0f, usage);
+
+ renderer->render_text(fnt_20, _global_camera.x, _global_camera.y, fps_text, rgb(0,0,0));
+}
+
void draw_overlay(platform_window* window) {
OVERLAY_RENDER_DEPTH();
draw_gun_info(window);
draw_leaderboard(window);
+ draw_debug_stats(window);
} \ No newline at end of file
diff --git a/src/players.c b/src/players.c
index 7244cc4..a8a3cac 100644
--- a/src/players.c
+++ b/src/players.c
@@ -50,7 +50,7 @@ void spawn_player(u32 id, network_client client) {
void move_user(platform_window* window, u32 id, protocol_move_type move) {
- float speed = 0.1f;
+ float speed = 5.5f * update_delta;
float pad_between_player_and_obj = 0.01f;
player* p = get_player_by_id(id);
diff --git a/src/wall_item.c b/src/wall_item.c
index a9f556c..b1b7f5d 100644
--- a/src/wall_item.c
+++ b/src/wall_item.c
@@ -1,4 +1,7 @@
#include "../include/wall_item.h"
+#include "../include/players.h"
+#include "../include/keybindings.h"
+#include "../include/asset_defs.h"
static image* get_wallitem_img(wall_item_type item, wall_item_data data) {
switch(item) {
@@ -8,6 +11,23 @@ static image* get_wallitem_img(wall_item_type item, wall_item_data data) {
return 0;
}
+static char* get_wallitem_name(wall_item_type item, wall_item_data data) {
+ switch(item) {
+ case WALLITEM_GUN: return get_gun_by_type(data.gun).name;
+ }
+
+ return 0;
+}
+
+static void apply_wallitem_to_player(wall_item item, player* p) {
+ if (item.item == WALLITEM_GUN) {
+ gun g = get_gun_by_type(item.data.gun);
+ p->guntype = g.type;
+ p->total_ammo = g.max_ammunition;
+ p->ammo_in_mag = g.magazine_size;
+ }
+}
+
void create_wallitem(vec3f position, wall_item_type type, wall_item_data data) {
wall_item item;
item.active = true;
@@ -38,12 +58,72 @@ void update_wallitems() {
}
}
+static float wallspace_entry_time = 0.0f;
+void draw_keybinding_for_wallitem_purchase(platform_window* window, wall_item item) {
+ OVERLAY_RENDER_DEPTH();
+
+ map_info info = get_map_info(window);
+
+ char purchase_text[50];
+ snprintf(purchase_text, 50, "Press %s to purchase %s", keybind_wall_purchase.text, get_wallitem_name(item.item, item.data));
+ int purchase_text_length = renderer->calculate_text_width(fnt_24, purchase_text);
+
+ float center_tilex = item.position.x;
+ float center_tiley = item.position.y - 2.5f;
+ #define KEY_DRAW_W (0.8f)
+ #define KEY_DRAW_H (0.6f)
+ float height = 0.5;
+ if (((int)wallspace_entry_time % 2) == 1) height = 0.1f;
+ box key_box = get_render_box_of_square_without_incline(window, (vec3f){center_tilex - (purchase_text_length/info.tile_width)/2,
+ center_tiley, 0}, (vec3f){KEY_DRAW_W, KEY_DRAW_H, height});
+
+ #define KEY_INSET ((0.12f)*info.tile_width)
+ key_box.tl_u.x += KEY_INSET; key_box.bl_u.x += KEY_INSET;
+ key_box.tl_u.y -= KEY_INSET; key_box.bl_u.y -= KEY_INSET;
+ key_box.tr_u.x -= KEY_INSET; key_box.br_u.x -= KEY_INSET;
+ key_box.tr_u.y -= KEY_INSET; key_box.br_u.y -= KEY_INSET;
+ render_box_with_outline(key_box, rgb(255,255,255));
+
+ int surface_w = key_box.tr_u.x - key_box.tl_u.x;
+ int surface_h = key_box.br_u.y - key_box.tr_u.y;
+
+ int text_w = renderer->calculate_text_width(fnt_24, keybind_wall_purchase.text);
+ renderer->render_text(fnt_24, key_box.tl_u.x + surface_w/2 - text_w/2,
+ key_box.tl_u.y + surface_h/2 - fnt_24->px_h/2, keybind_wall_purchase.text, rgb(0,0,0));
+
+ renderer->render_text(fnt_24, key_box.tr_d.x + 10, key_box.tr_d.y + (surface_h/2) - (fnt_24->px_h/2), purchase_text, rgb(255,255,255));
+
+ wallspace_entry_time += update_delta;
+
+ if (keyboard_is_key_pressed(keybind_wall_purchase.key)) {
+ player* p = get_player_by_id(player_id);
+ if (!p) return;
+ apply_wallitem_to_player(item, p);
+ }
+}
+
void draw_wallitems(platform_window* window) {
for (int i = 0; i < MAX_WALLITEMS; i++) {
wall_item item = wallitems[i];
if (!item.active) continue;
- box box = get_render_box_of_square(window, item.position, (vec3f){1,1,2});
- renderer->render_image(item.img, box.tl_u.x, box.tl_u.y, box.tr_u.x - box.tl_u.x, box.br_u.y - box.tr_u.y);
+ {
+ OBJECT_RENDER_DEPTH((int)item.position.y);
+ box box = get_render_box_of_square(window, item.position, (vec3f){1,1,2});
+ renderer->render_image(item.img, box.tl_u.x, box.tl_u.y, box.tr_u.x - box.tl_u.x, box.br_u.y - box.tr_u.y);
+ }
+
+ player* p = get_player_by_id(player_id);
+ if (!p) continue;
+ float dirx = ((item.position.x + (0.5)) - (p->playerx + (get_player_size_in_tile()/2)));
+ float diry = ((item.position.y) - (p->playery + (get_player_size_in_tile()/2)));
+ float length = sqrt(dirx * dirx + diry * diry);
+
+ if (length < 1.0f) {
+ draw_keybinding_for_wallitem_purchase(window, item);
+ }
+ else {
+ wallspace_entry_time = 0.0f;
+ }
}
} \ No newline at end of file
diff --git a/src/zombies.c b/src/zombies.c
index 8289ad4..88d8e05 100644
--- a/src/zombies.c
+++ b/src/zombies.c
@@ -173,7 +173,7 @@ static vec2f get_random_point_around_player(player p, zombie o) {
}
void update_zombies_server(platform_window* window) {
- float speed = 0.05f;
+ float speed = 4.0f * update_delta;
for (int i = 0; i < MAX_ZOMBIES; i++) {
zombie o = zombies[i];