summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2022-12-14 17:08:23 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2022-12-14 17:08:23 +0100
commitee4906ef5fc89f3f10cd6aaf95845a0ae9b2f47e (patch)
tree83abd90aca3de2f6bceac8ef742e6c5eb0688bb2
parent99f328fa19bb9cb266d9525629813cc0268a889e (diff)
render order fix for bullets, ammo implementation
-rw-r--r--build/zombies.exebin1716616 -> 1719322 bytes
-rw-r--r--include/asset_defs.h11
-rw-r--r--include/guns.h7
-rw-r--r--include/math_helper.h1
-rw-r--r--include/overlay.h8
-rw-r--r--include/players.h6
-rw-r--r--main.c11
-rw-r--r--project-base.code-workspace3
-rw-r--r--src/asset_defs.c6
-rw-r--r--src/bullets.c17
-rw-r--r--src/game.c19
-rw-r--r--src/map.c2
-rw-r--r--src/objects.c5
-rw-r--r--src/overlay.c27
-rw-r--r--src/players.c12
15 files changed, 109 insertions, 26 deletions
diff --git a/build/zombies.exe b/build/zombies.exe
index a620285..b0308a8 100644
--- a/build/zombies.exe
+++ b/build/zombies.exe
Binary files differ
diff --git a/include/asset_defs.h b/include/asset_defs.h
new file mode 100644
index 0000000..9e50895
--- /dev/null
+++ b/include/asset_defs.h
@@ -0,0 +1,11 @@
+#ifndef INCLUDE_ASSET_DEFS
+#define INCLUDE_ASSET_DEFS
+
+#include <projectbase/project_base.h>
+
+font* fnt_24;
+font* fnt_20;
+
+void load_assets();
+
+#endif \ No newline at end of file
diff --git a/include/guns.h b/include/guns.h
index 62d43df..0363cae 100644
--- a/include/guns.h
+++ b/include/guns.h
@@ -11,6 +11,7 @@ typedef enum t_gun_type {
typedef struct t_gun {
gun_type type;
+ char* name;
int magazine_size;
int max_ammunition;
float bullet_spread;
@@ -19,9 +20,9 @@ typedef struct t_gun {
} gun;
gun guns[GUN_ALL] = {
- {GUN_DESERTEAGLE, 8, 64, 0.0f, 1, 4.0f},
- {GUN_MP5, 30, 120, 0.1f, 1, 10.0f},
- {GUN_NOVA, 12, 80, 0.2f, 3, 1.2f},
+ {GUN_DESERTEAGLE, "Desert Eagle", 8, 64, 0.0f, 1, 4.0f},
+ {GUN_MP5, "MP5", 30, 120, 0.1f, 1, 10.0f},
+ {GUN_NOVA, "Nova", 12, 80, 0.2f, 3, 1.2f},
};
gun get_gun_by_type(gun_type type);
diff --git a/include/math_helper.h b/include/math_helper.h
index 7040086..b3a6cfe 100644
--- a/include/math_helper.h
+++ b/include/math_helper.h
@@ -10,6 +10,7 @@
#define MAP_RENDER_DEPTH renderer->set_render_depth(1);
#define BULLET_RENDER_DEPTH(_h) renderer->set_render_depth(5 + ceil(_h));
#define OBJECT_RENDER_DEPTH(_h) renderer->set_render_depth(5 + ceil(_h));
+#define OVERLAY_RENDER_DEPTH() renderer->set_render_depth(100);
bool onSegment(vec2f p, vec2f q, vec2f r);
int orientation(vec2f p, vec2f q, vec2f r);
diff --git a/include/overlay.h b/include/overlay.h
new file mode 100644
index 0000000..7441003
--- /dev/null
+++ b/include/overlay.h
@@ -0,0 +1,8 @@
+#ifndef INCLUDE_OVERLAY
+#define INCLUDE_OVERLAY
+
+#include <projectbase/project_base.h>
+
+void draw_overlay(platform_window* window);
+
+#endif \ No newline at end of file
diff --git a/include/players.h b/include/players.h
index 4e148c9..258a1fb 100644
--- a/include/players.h
+++ b/include/players.h
@@ -18,12 +18,16 @@ typedef struct t_player {
float gunx;
float guny;
float gun_height;
+ int total_ammo;
+ int ammo_in_mag;
gun_type guntype;
} player;
#include "protocol.h"
-u32 my_id = 1;
+u32 my_id = -1;
+
+camera _next_camera_pos;
int max_players = 10;
player players[10] = {0};
diff --git a/main.c b/main.c
index 025a459..dbc06cd 100644
--- a/main.c
+++ b/main.c
@@ -12,6 +12,8 @@
#include "include/game.h"
#include "include/protocol.h"
#include "include/guns.h"
+#include "include/overlay.h"
+#include "include/asset_defs.h"
#include "src/map.c"
#include "src/players.c"
@@ -25,17 +27,14 @@
#include "src/game.c"
#include "src/guns.c"
#include "src/protocol.c"
+#include "src/overlay.c"
+#include "src/asset_defs.c"
#define CONFIG_DIRECTORY "zombieshooter"
-font* fnt;
void update_func(platform_window* window) {
renderer->render_clear(window, rgb(0,255,0));
update_game(window);
-
- char id[50];
- sprintf(id, "%d", my_id);
- renderer->render_text(fnt, 0, 0, id, rgb(255,255,255));
}
@@ -72,8 +71,6 @@ int main(int argc, char **argv)
init_game();
handle_args(argc, argv);
- fnt = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 24);
-
while(platform_keep_running(window)) {
platform_handle_events();
}
diff --git a/project-base.code-workspace b/project-base.code-workspace
index 1e9bd72..c96785b 100644
--- a/project-base.code-workspace
+++ b/project-base.code-workspace
@@ -63,7 +63,8 @@
"queue": "cpp",
"streambuf": "c",
"game.h": "c",
- "bullets.h": "c"
+ "bullets.h": "c",
+ "guns.h": "c"
}
}
} \ No newline at end of file
diff --git a/src/asset_defs.c b/src/asset_defs.c
new file mode 100644
index 0000000..d7fc6a5
--- /dev/null
+++ b/src/asset_defs.c
@@ -0,0 +1,6 @@
+#include "../include/asset_defs.h"
+
+void load_assets() {
+ fnt_24 = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 24);
+ fnt_20 = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 20);
+} \ No newline at end of file
diff --git a/src/bullets.c b/src/bullets.c
index 0357025..0feddef 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -13,7 +13,17 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) {
}
p->sec_since_last_shot = 0.0f;
- for (int i = 0; i < g.bullets_per_shot; i++)
+ int bullets_to_shoot = g.bullets_per_shot;
+ if (bullets_to_shoot > p->ammo_in_mag) bullets_to_shoot = p->ammo_in_mag;
+ p->ammo_in_mag -= bullets_to_shoot;
+ if (p->ammo_in_mag == 0) {
+ int amount_to_reload = g.magazine_size;
+ if (amount_to_reload > p->total_ammo) amount_to_reload = p->total_ammo;
+ p->total_ammo -= amount_to_reload;
+ p->ammo_in_mag = amount_to_reload;
+ }
+
+ for (int i = 0; i < bullets_to_shoot; i++)
{
map_info info = get_map_info(window);
float bullet_range = 100.0f;
@@ -180,8 +190,6 @@ void draw_bullets(platform_window* window) {
bullets[i].position.x = p->gunx;
bullets[i].position.y = p->guny;
bullets[i].position.z = p->gun_height;
-
- printf("%d\n", i);
if (check_if_bullet_collided_with_ground(&b, window)) {
bullets[i].endy = b.endy;
@@ -202,7 +210,8 @@ void draw_bullets(platform_window* window) {
bullets[i].alive_time += update_delta;
bullets[i].active = false;
- BULLET_RENDER_DEPTH(b.position.z);
+ if ((int)bullets[i].position.y < (int)bullets[i].endy) { BULLET_RENDER_DEPTH((int)bullets[i].position.y); }
+ else { BULLET_RENDER_DEPTH((int)bullets[i].endy); }
float bullet_render_x = b.position.x*info.tile_width + (b.position.y*info.px_incline);
float bullet_render_y = b.position.y*info.tile_height - (b.position.z*info.px_raised_per_h);
diff --git a/src/game.c b/src/game.c
index bb3b987..1bc9f0d 100644
--- a/src/game.c
+++ b/src/game.c
@@ -46,6 +46,7 @@ void init_game() {
global_state.state = IDLE;
global_state.network_state = DISCONNECTED;
+ load_assets();
load_map();
}
@@ -103,7 +104,6 @@ void update_server(platform_window* window) {
case MESSAGE_USER_SHOOT: {
protocol_user_shoot* shoot_msg = (protocol_user_shoot*)msg->message;
shoot(window, shoot_msg->id, shoot_msg->dirx, shoot_msg->diry);
- printf("Player %d shot\n", shoot_msg->id);
} break;
default:
@@ -148,7 +148,15 @@ void update_client(platform_window* window) {
player copy;
if (p) copy = *p;
memcpy(players, msg_players->players, sizeof(players));
- if (p) *p = copy;
+
+ // These properties are simulated locally so dont overwrite.
+ if (p) {
+ p->playerx = copy.playerx;
+ p->playery = copy.playery;
+ p->gunx = copy.gunx;
+ p->guny = copy.guny;
+ p->gun_height = copy.gun_height;
+ }
} break;
case MESSAGE_ZOMBIE_LIST: {
@@ -158,6 +166,7 @@ void update_client(platform_window* window) {
} break;
case MESSAGE_BULLET_LIST: {
+ if (global_state.server) break; // bullets are simulated on server so dont overwrite data.
protocol_bullets_list* msg_bullets = (protocol_bullets_list*)msg;
memcpy(bullets, msg_bullets->bullets, sizeof(bullets));
} break;
@@ -173,10 +182,10 @@ void update_client(platform_window* window) {
}
void update_game(platform_window* window) {
+ update_client(window);
if (global_state.server) {
update_server(window);
}
- update_client(window);
if (global_state.network_state == CONNECTED) {
if (!global_state.server) {
@@ -186,5 +195,9 @@ void update_game(platform_window* window) {
draw_grid(window);
draw_spawners(window);
+ draw_overlay(window);
+
+ _global_camera.x = (int)_next_camera_pos.x;
+ _global_camera.y = (int)_next_camera_pos.y;
}
} \ No newline at end of file
diff --git a/src/map.c b/src/map.c
index e466f5f..03aaf5f 100644
--- a/src/map.c
+++ b/src/map.c
@@ -233,6 +233,8 @@ void draw_grid(platform_window* window) {
draw_objects_at_row(window, y);
}
+
+ draw_bullets(window);
}
inline map_info get_map_info(platform_window* window) {
diff --git a/src/objects.c b/src/objects.c
index ae65af2..e4e01d9 100644
--- a/src/objects.c
+++ b/src/objects.c
@@ -33,13 +33,10 @@ void draw_objects_at_row(platform_window* window, int row) {
for (int i = MAP_SIZE_X-1; i >= 0; i--) {
object o = get_object_at_tile(i, row);
- OBJECT_RENDER_DEPTH(o.h);
draw_players_at_tile(window, i, row);
+ OBJECT_RENDER_DEPTH((int)o.position.y);
draw_zombies_at_tile(window, i, row);
-
- draw_bullets(window);
- OBJECT_RENDER_DEPTH(o.h);
if (!o.active) continue;
box box = get_box_of_object(window, o);
diff --git a/src/overlay.c b/src/overlay.c
new file mode 100644
index 0000000..5a81726
--- /dev/null
+++ b/src/overlay.c
@@ -0,0 +1,27 @@
+#include "../include/overlay.h"
+#include "../include/guns.h"
+
+#define EDGE_PADDING 10
+
+static void draw_gun_info(platform_window* window) {
+ player *p = get_player_by_id(my_id);
+ if (!p) return;
+
+ gun g = get_gun_by_type(p->guntype);
+
+
+ int y = window->height - fnt_24->px_h - EDGE_PADDING;
+ int x = EDGE_PADDING;
+
+ renderer->render_text(fnt_24, (int)_global_camera.x + x, (int)_global_camera.y + y, g.name, rgb(255,255,255));
+ y -= fnt_24->px_h;
+
+ char ammo_txt[50];
+ sprintf(ammo_txt, "%d / %d", p->ammo_in_mag, p->total_ammo);
+ renderer->render_text(fnt_20, (int)_global_camera.x + x, (int)_global_camera.y + y, ammo_txt, rgb(255,255,255));
+}
+
+void draw_overlay(platform_window* window) {
+ OVERLAY_RENDER_DEPTH();
+ draw_gun_info(window);
+} \ No newline at end of file
diff --git a/src/players.c b/src/players.c
index 877e2b6..1b86944 100644
--- a/src/players.c
+++ b/src/players.c
@@ -28,7 +28,11 @@ void spawn_player(int id) {
players[i].guny = 0.0f;
players[i].gun_height = 0.0f;
players[i].id = id;
- players[i].guntype = GUN_NOVA;
+ players[i].guntype = GUN_MP5;
+
+ gun g = get_gun_by_type(players[i].guntype);
+ players[i].total_ammo = g.max_ammunition;
+ players[i].ammo_in_mag = g.magazine_size;
return;
}
}
@@ -196,6 +200,8 @@ void draw_players_at_tile(platform_window* window, int x, int y) {
if (!players[i].active) continue;
if ((int)players[i].playerx != x || (int)(players[i].playery+get_player_size_in_tile()) != y) continue;
+ OBJECT_RENDER_DEPTH((int)(players[i].playery+get_player_size_in_tile()));
+
int size = get_tile_width(window) / 2;
map_info info = get_map_info(window);
float height = get_height_of_tile_under_coords(window, players[i].playerx, players[i].playery);
@@ -211,8 +217,8 @@ void draw_players_at_tile(platform_window* window, int x, int y) {
renderer->render_rectangle(gun_render_x, gun_render_y, size/4, size/4, rgb(20,255,20));
if (players[i].id == my_id) {
- _global_camera.x = -(window->width / 2) + player_render_x;
- _global_camera.y = -(window->height / 2) + player_render_y;
+ _next_camera_pos.x = -(window->width / 2) + player_render_x;
+ _next_camera_pos.y = -(window->height / 2) + player_render_y;
}
}
}