summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bullets.c53
-rw-r--r--src/game.c20
-rw-r--r--src/guns.c5
-rw-r--r--src/players.c46
-rw-r--r--src/protocol.c17
5 files changed, 97 insertions, 44 deletions
diff --git a/src/bullets.c b/src/bullets.c
index 191b342..0357025 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -1,32 +1,39 @@
#include "../include/bullets.h"
-void shoot(platform_window* window, player p) {
- map_info info = get_map_info(window);
- float bullet_range = 100.0f;
+void shoot(platform_window* window, u32 id, float dirx, float diry) {
+ player* p = get_player_by_id(id);
+ if (!p) {
+ log_info("User with unknown id shot");
+ }
+ gun g = get_gun_by_type(p->guntype);
+ float time_between_bullets = 1.0f/g.shots_per_second;
- float hh = get_height_of_tile_under_coords(window, p.playerx, p.playery);
+ if (p->sec_since_last_shot < time_between_bullets) {
+ return;
+ }
+ p->sec_since_last_shot = 0.0f;
- float dirx = (_global_mouse.x - (window->width/2));
- float diry = (_global_mouse.y - (window->height/2));
- double length = sqrt(dirx * dirx + diry * diry);
- dirx /= length;
- diry /= length;
+ for (int i = 0; i < g.bullets_per_shot; i++)
+ {
+ map_info info = get_map_info(window);
+ float bullet_range = 100.0f;
- #define SPRAY_BOUNDS (0.1f)
- dirx += ((float)rand()/(float)(RAND_MAX/SPRAY_BOUNDS)-(SPRAY_BOUNDS/2));
- diry += ((float)rand()/(float)(RAND_MAX/SPRAY_BOUNDS)-(SPRAY_BOUNDS/2));
+ float hh = get_height_of_tile_under_coords(window, p->playerx, p->playery);
+ dirx += ((float)rand()/(float)(RAND_MAX/g.bullet_spread)-(g.bullet_spread/2));
+ diry += ((float)rand()/(float)(RAND_MAX/g.bullet_spread)-(g.bullet_spread/2));
- float bulletx = p.gunx;
- float bullety = p.guny;
- float bullet_end_point_x = bulletx+dirx*bullet_range;
- float bullet_end_point_y = bullety+diry*bullet_range;
+ float bulletx = p->gunx;
+ float bullety = p->guny;
+ float bullet_end_point_x = bulletx+dirx*bullet_range;
+ float bullet_end_point_y = bullety+diry*bullet_range;
- for (int i = 0; i < max_bullets; i++) {
- bullet b = bullets[i];
- if (b.active) continue;
+ for (int i = 0; i < max_bullets; i++) {
+ bullet b = bullets[i];
+ if (b.active) continue;
- bullets[i] = (bullet){p.id, true, bulletx, bullety, hh + 0.5, bullet_end_point_x, bullet_end_point_y};
- break;
+ bullets[i] = (bullet){p->id, true, bulletx, bullety, hh + 0.5, bullet_end_point_x, bullet_end_point_y};
+ break;
+ }
}
}
@@ -173,6 +180,8 @@ 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;
@@ -193,8 +202,6 @@ void draw_bullets(platform_window* window) {
bullets[i].alive_time += update_delta;
bullets[i].active = false;
- if (!b.active) continue;
-
BULLET_RENDER_DEPTH(b.position.z);
float bullet_render_x = b.position.x*info.tile_width + (b.position.y*info.px_incline);
diff --git a/src/game.c b/src/game.c
index 4daf6c8..bb3b987 100644
--- a/src/game.c
+++ b/src/game.c
@@ -69,13 +69,14 @@ static void rotate_user(platform_window* window, protocol_user_look *message) {
return;
}
- p->gunx = message->gunx;
- p->guny = message->guny;
+ p->gunx = p->playerx + message->gunx;
+ p->guny = p->playery + message->guny;
}
float update_timer = 0.0f;
void update_server(platform_window* window) {
update_spawners();
+ update_players_server();
update_zombies_server(window);
for (int i = 0; i < messages_received_on_server.length; i++) {
@@ -98,6 +99,12 @@ void update_server(platform_window* window) {
case MESSAGE_USER_LOOK: {
rotate_user(window, (protocol_user_look*)msg->message);
} break;
+
+ 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:
log_info("Unhandled message received");
@@ -113,6 +120,7 @@ void update_server(platform_window* window) {
if (update_timer > 0.0f) {
broadcast_to_clients(create_protocol_user_list());
broadcast_to_clients(create_protocol_zombie_list());
+ broadcast_to_clients(create_protocol_bullets_list());
update_timer = 0.0f;
}
@@ -142,11 +150,17 @@ void update_client(platform_window* window) {
memcpy(players, msg_players->players, sizeof(players));
if (p) *p = copy;
} break;
+
case MESSAGE_ZOMBIE_LIST: {
if (global_state.server) break; // zombies are simulated on server so dont overwrite data.
protocol_zombie_list* msg_zombies = (protocol_zombie_list*)msg;
memcpy(zombies, msg_zombies->zombies, sizeof(zombies));
} break;
+
+ case MESSAGE_BULLET_LIST: {
+ protocol_bullets_list* msg_bullets = (protocol_bullets_list*)msg;
+ memcpy(bullets, msg_bullets->bullets, sizeof(bullets));
+ } break;
default:
log_info("Unhandled message received");
break;
@@ -159,10 +173,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) {
diff --git a/src/guns.c b/src/guns.c
new file mode 100644
index 0000000..62f4690
--- /dev/null
+++ b/src/guns.c
@@ -0,0 +1,5 @@
+#include "../include/guns.h"
+
+gun get_gun_by_type(gun_type type) {
+ return guns[type];
+} \ No newline at end of file
diff --git a/src/players.c b/src/players.c
index e9260a8..877e2b6 100644
--- a/src/players.c
+++ b/src/players.c
@@ -28,6 +28,7 @@ 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;
return;
}
}
@@ -159,10 +160,34 @@ void take_player_input(platform_window* window) {
dirx /= length;
diry /= length;
- p->gunx = p->playerx + (get_player_size_in_tile()/2) + dirx/2;
- p->guny = p->playery + (get_player_size_in_tile()/2) + diry/2;
+ float gun_offset_x = (get_player_size_in_tile()/2) + dirx/2;
+ float gun_offset_y = (get_player_size_in_tile()/2) + diry/2;
+
+ p->gunx = p->playerx + gun_offset_x;
+ p->guny = p->playery + gun_offset_y;
- network_client_send(global_state.client, create_protocol_user_look(my_id, p->gunx, p->guny));
+ network_client_send(global_state.client, create_protocol_user_look(my_id, gun_offset_x, gun_offset_y));
+ }
+
+ // shoot
+ {
+ if (is_left_down()) {
+ float dirx = (_global_mouse.x - (window->width/2));
+ float diry = (_global_mouse.y - (window->height/2));
+ double length = sqrt(dirx * dirx + diry * diry);
+ dirx /= length;
+ diry /= length;
+
+ network_message message = create_protocol_user_shoot(my_id, dirx, diry);
+ network_client_send(global_state.client, message);
+ }
+ }
+}
+
+void update_players_server() {
+ for (int i = 0; i < max_players; i++) {
+ if (!players[i].active) continue;
+ players[i].sec_since_last_shot += update_delta;
}
}
@@ -171,21 +196,6 @@ 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;
- players[i].sec_since_last_shot += update_delta;
- float bullets_per_sec = 10;
- float time_between_bullets = 1.0f/bullets_per_sec;
- if (is_left_down()) {
- if (players[i].sec_since_last_shot > time_between_bullets) {
- int ix = get_my_player_index();
- if (ix != -1) {
- for (int i = 0; i < 3; i++) {
- shoot(window, players[ix]);
- players[i].sec_since_last_shot = 0.0f;
- }
- }
- }
- }
-
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);
diff --git a/src/protocol.c b/src/protocol.c
index 2381159..dc4783b 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -28,6 +28,14 @@ network_message create_protocol_zombie_list() {
return network_create_message(network_buffer, sizeof(protocol_zombie_list), MAX_NETWORK_BUFFER_SIZE);
}
+network_message create_protocol_bullets_list() {
+ protocol_bullets_list* buf = (protocol_bullets_list*)network_buffer;
+ buf->type = MESSAGE_BULLET_LIST;
+ memcpy(buf->bullets, bullets, sizeof(bullets));
+ return network_create_message(network_buffer, sizeof(protocol_bullets_list), MAX_NETWORK_BUFFER_SIZE);
+}
+
+
network_message create_protocol_user_moved(protocol_move_type move, u32 id) {
protocol_move* buf = (protocol_move*)network_buffer;
buf->type = MESSAGE_USER_MOVED;
@@ -36,6 +44,15 @@ network_message create_protocol_user_moved(protocol_move_type move, u32 id) {
return network_create_message(network_buffer, sizeof(protocol_move), MAX_NETWORK_BUFFER_SIZE);
}
+network_message create_protocol_user_shoot(u32 id, float dirx, float diry) {
+ protocol_user_shoot* buf = (protocol_user_shoot*)network_buffer;
+ buf->type = MESSAGE_USER_SHOOT;
+ buf->id = id;
+ buf->dirx = dirx;
+ buf->diry = diry;
+ return network_create_message(network_buffer, sizeof(protocol_user_shoot), MAX_NETWORK_BUFFER_SIZE);
+}
+
network_message create_protocol_user_look(u32 id, float gunx, float guny) {
protocol_user_look* buf = (protocol_user_look*)network_buffer;
buf->type = MESSAGE_USER_LOOK;