From 4933a7c038087ae465e588fafb392a57d7f92b87 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Fri, 9 Dec 2022 21:56:43 +0100 Subject: gun angle --- build/zombies.exe | Bin 1659927 -> 1662213 bytes bullets.c | 13 +++++++++++-- include/map.h | 2 +- include/players.h | 3 +++ main.c | 1 + math_helper.c | 3 --- objects.c | 2 +- players.c | 20 ++++++++++++++++++-- zombies.c | 1 - 9 files changed, 35 insertions(+), 10 deletions(-) diff --git a/build/zombies.exe b/build/zombies.exe index e6ff922..072f96c 100644 Binary files a/build/zombies.exe and b/build/zombies.exe differ diff --git a/bullets.c b/bullets.c index dae2a9b..9475729 100644 --- a/bullets.c +++ b/bullets.c @@ -12,8 +12,12 @@ void shoot(platform_window* window) { dirx /= length; diry /= length; - float bulletx = playerx + (get_player_size_in_tile()/2); - float bullety = playery + (get_player_size_in_tile()/2); + #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 bulletx = gunx; + float bullety = guny; float bullet_end_point_x = bulletx+dirx*bullet_range; float bullet_end_point_y = bullety+diry*bullet_range; @@ -163,8 +167,13 @@ void draw_bullets(platform_window* window) { map_info info = get_map_info(window); for (int i = 0; i < max_bullets; i++) { + bullets[i].position.x = gunx; + bullets[i].position.y = guny; + bullets[i].position.z = gun_height; bullet b = bullets[i]; if (!b.active) continue; + + if (check_if_bullet_collided_with_ground(&b, window)) { bullets[i].endy = b.endy; diff --git a/include/map.h b/include/map.h index 7c0716d..82584e9 100644 --- a/include/map.h +++ b/include/map.h @@ -36,8 +36,8 @@ typedef struct t_map_info { // data data that is stored on disk int map[MAP_SIZE_Y][MAP_SIZE_X] = { - {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, diff --git a/include/players.h b/include/players.h index ef94489..7107797 100644 --- a/include/players.h +++ b/include/players.h @@ -11,6 +11,9 @@ float sec_since_last_shot = 10.0f; float playerx = 3; float playery = 3; +float gunx = 0.0f; +float guny = 0.0f; +float gun_height = 0.0f; void shoot(platform_window* window); void draw_player(platform_window* window); diff --git a/main.c b/main.c index bf6ee05..ea17371 100644 --- a/main.c +++ b/main.c @@ -37,6 +37,7 @@ int main(int argc, char **argv) platform_window *window = platform_open_window_ex("Zombies!", 700, 700, 1200, 1000, 500, 500, FLAGS_MINIMIZE, update_func, resize_func, close_func, 0, 0); settings_set_number("USE_GPU", 1); + platform_toggle_vsync(window, true); //fnt = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 16); load_map_from_data(); diff --git a/math_helper.c b/math_helper.c index 9ae2c9e..782599d 100644 --- a/math_helper.c +++ b/math_helper.c @@ -50,9 +50,6 @@ bool lines_intersect(vec2f p1, vec2f q1, vec2f p2, vec2f q2) vec2f bdir = get_dir_of_line(p1, q1); vec2f pdir = get_dir_of_line(p1, p2); if ((neg2(bdir.x, pdir.x) || pos2(bdir.x, pdir.x)) && (neg2(bdir.y, pdir.y) || pos2(bdir.y, pdir.y))) return true; // going down - //return true; - - //return false; } // Special Cases diff --git a/objects.c b/objects.c index 77a4d29..39e0f30 100644 --- a/objects.c +++ b/objects.c @@ -32,7 +32,7 @@ void draw_objects_at_row(platform_window* window, int row) { bool did_player_draw = false; int x_of_player = playerx; - int y_of_player = ceil(playery); + int y_of_player = playery+get_player_size_in_tile(); for (int i = MAP_SIZE_X-1; i >= 0; i--) { object o = get_object_at_tile(i, row); diff --git a/players.c b/players.c index 0ad042f..40b2de3 100644 --- a/players.c +++ b/players.c @@ -90,9 +90,11 @@ void draw_player(platform_window* window) { take_player_input(window); 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 (sec_since_last_shot > 0.1f) { - shoot(window); + if (sec_since_last_shot > time_between_bullets) { + for (int i = 0; i < 3; i++) shoot(window); sec_since_last_shot = 0.0f; } } @@ -101,6 +103,20 @@ void draw_player(platform_window* window) { float player_render_y = playery*info.tile_height - (height*info.px_raised_per_h); renderer->render_rectangle(player_render_x, player_render_y, size, size, rgb(200,150,120)); + 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; + + gunx = playerx + (get_player_size_in_tile()/2) + dirx/2; + guny = playery + (get_player_size_in_tile()/2) + diry/2; + gun_height = height+0.5; + float gun_render_x = gunx*info.tile_width + (guny*info.px_incline); + float gun_render_y = guny*info.tile_height - (gun_height*info.px_raised_per_h); + + renderer->render_rectangle(gun_render_x, gun_render_y, size/4, size/4, rgb(20,255,20)); + _global_camera.x = -(window->width / 2) + player_render_x; _global_camera.y = -(window->height / 2) + player_render_y; } diff --git a/zombies.c b/zombies.c index 6b646fe..80a34ad 100644 --- a/zombies.c +++ b/zombies.c @@ -1,7 +1,6 @@ #include "include/zombies.h" void spawn_zombie(int x, int y) { - return; for (int i = 0; i < max_zombies; i++) { zombie o = zombies[i]; if (o.alive) continue; -- cgit v1.2.3-70-g09d2