summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2023-11-05 13:21:22 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2023-11-05 13:21:22 +0100
commitdc81516d860885e7e6b75c4ad978a09a2efb705d (patch)
treeba90215f2aed47f4688e8c91924e6463fca1beda /src
parent812da60594a9371b6883acacfd10b6be50673b4a (diff)
bullet cone
Diffstat (limited to 'src')
-rw-r--r--src/bullets.c17
-rw-r--r--src/game.c2
-rw-r--r--src/players.c49
-rw-r--r--src/protocol.c4
4 files changed, 55 insertions, 17 deletions
diff --git a/src/bullets.c b/src/bullets.c
index ed7ca58..26c7766 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -31,13 +31,22 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) {
float bullet_range = 100.0f;
float hh = get_height_of_tile_under_coords(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 rads = atan2(dirx, diry);
+ float target1 = rads - (g.bullet_spread/2);
+ float target2 = rads + (g.bullet_spread/2);
+
+ float target_rand = 0.0f;
+ {
+ float random = ((float) rand()) / (float) RAND_MAX;
+ float diff = target2 - target1;
+ float r = random * diff;
+ target_rand = target1 + r;
+ }
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 bullet_end_point_x = bulletx+(sin(target_rand))*bullet_range;
+ float bullet_end_point_y = bullety+(cos(target_rand))*bullet_range;
for (int i = 0; i < max_bullets; i++) {
bullet b = bullets[i];
diff --git a/src/game.c b/src/game.c
index ded72e7..1f71495 100644
--- a/src/game.c
+++ b/src/game.c
@@ -116,6 +116,8 @@ static void rotate_user(platform_window* window, protocol_user_look *message) {
p->gunx = p->playerx + message->gunx;
p->guny = p->playery + message->guny;
+ p->dirx = message->dirx;
+ p->diry = message->diry;
}
static void set_ping_for_player(protocol_generic_message* msg) {
diff --git a/src/players.c b/src/players.c
index 164b2a8..fbb413d 100644
--- a/src/players.c
+++ b/src/players.c
@@ -225,10 +225,11 @@ void take_player_input(platform_window* window) {
if (is_editing_map) return;
#endif
+ box box = get_render_box_of_square(window, (vec3f){p->playerx, p->playery, p->height}, (vec3f){1.0,1.0,1.0f});
// Send gun position
{
- float dirx = (_global_mouse.x - (window->width/2));
- float diry = (_global_mouse.y - (window->height/2));
+ float dirx = (_global_mouse.x - (box.tl_u.x - _global_camera.x));
+ float diry = (_global_mouse.y - (box.tl_u.y - _global_camera.y));
double length = sqrt(dirx * dirx + diry * diry);
dirx /= length;
diry /= length;
@@ -236,11 +237,10 @@ void take_player_input(platform_window* window) {
float gun_offset_x = (get_player_size_in_tile()/2) + (dirx/4);
float gun_offset_y = (get_player_size_in_tile()/2) + (diry/4);
- add_message_to_outgoing_queuex(create_protocol_user_look(player_id, gun_offset_x, gun_offset_y), *global_state.client);
+ add_message_to_outgoing_queuex(create_protocol_user_look(player_id, gun_offset_x, gun_offset_y, dirx, diry), *global_state.client);
}
- {
- box box = get_render_box_of_square(window, (vec3f){p->playerx, p->playery, p->height}, (vec3f){1.0,1.0,1.0f});
+ {
float dirx = (_global_mouse.x - (box.tl_u.x - _global_camera.x));
float diry = (_global_mouse.y - (box.tl_u.y - _global_camera.y));
double length = sqrt(dirx * dirx + diry * diry);
@@ -324,6 +324,36 @@ void update_players_server() {
update_players_client();
}
+static void draw_player_bullet_cone(platform_window* window, player* p) {
+ map_info info = get_map_info(window);
+ float bullet_range = 100.0f;
+
+ float bulletx = p->gunx;
+ float bullety = p->guny;
+ gun g = get_gun_by_type(p->guntype);
+
+ // https://stackoverflow.com/questions/11462239/how-to-move-point-along-circle
+ float rads = -atan2(p->diry, p->dirx);
+ rads += M_PI/2;
+ float target1 = rads - (g.bullet_spread/2);
+ float target2 = rads + (g.bullet_spread/2);
+
+ float cone_end_left_x = bulletx+(sin(target1))*bullet_range;
+ float cone_end_left_y = bullety+(cos(target1))*bullet_range;
+
+ float cone_end_right_x = bulletx+(sin(target2))*bullet_range;
+ float cone_end_right_y = bullety+(cos(target2))*bullet_range;
+
+ float x1 = bulletx*info.tile_width + (bullety*info.px_incline);
+ float y1 = bullety*info.tile_height - (p->height*info.px_raised_per_h);
+ float x2 = cone_end_left_x*info.tile_width + (bullety*info.px_incline);
+ float y2 = cone_end_left_y*info.tile_height - (p->height*info.px_raised_per_h);
+ float x3 = cone_end_right_x*info.tile_width + (bullety*info.px_incline);
+ float y3 = cone_end_right_y*info.tile_height - (p->height*info.px_raised_per_h);
+
+ renderer->render_tri(x1,y1,x2,y2,x3,y3, rgba(255,0,0,40));
+}
+
void draw_players(platform_window* window) {
float size = get_player_size_in_tile();
map_info info = get_map_info(window);
@@ -336,14 +366,9 @@ void draw_players(platform_window* window) {
float height = get_height_of_tile_under_coords(players[i].playerx, players[i].playery);
players[i].height = height;
+ draw_player_bullet_cone(window, &players[i]);
+
box box = get_render_box_of_square(window, (vec3f){players[i].playerx, players[i].playery, height}, (vec3f){size,size,1.0f});
-
- /*
- render_quad_with_outline(box.tl_d, box.tr_d, box.bl_d, box.br_d, rgb(200,150,120));
- render_quad_with_outline(box.tl_u, box.tr_u, box.bl_u, box.br_u, rgb(200,150,120));
- render_quad_with_outline(box.tl_u, box.tl_d, box.bl_u, box.bl_d, rgb(200,150,120));
- render_quad_with_outline(box.bl_u, box.br_u, box.bl_d, box.br_d, rgb(200,150,120));
- */
sprite_frame frame = sprite_get_frame(&players[i].sprite);
renderer->render_image_quad_partial(img_player_running,
box.tl_u.x, box.tl_u.y,
diff --git a/src/protocol.c b/src/protocol.c
index d8506b7..de1a8d1 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -107,13 +107,15 @@ network_message create_protocol_user_shoot(u32 id, float dirx, float diry)
return network_create_message((u8*)buf, sizeof(protocol_user_shoot), MAX_NETWORK_BUFFER_SIZE);
}
-network_message create_protocol_user_look(u32 id, float gunx, float guny)
+network_message create_protocol_user_look(u32 id, float gunx, float guny, float dirx, float diry)
{
protocol_user_look *buf = alloc_network_message(protocol_user_look);
buf->type = MESSAGE_USER_LOOK;
buf->id = id;
buf->gunx = gunx;
buf->guny = guny;
+ buf->dirx = dirx;
+ buf->diry = diry;
return network_create_message((u8*)buf, sizeof(protocol_user_look), MAX_NETWORK_BUFFER_SIZE);
}