diff options
| author | Aldrik Ramaekers <aldrik@amftech.nl> | 2023-10-28 10:52:18 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik@amftech.nl> | 2023-10-28 10:52:18 +0200 |
| commit | 18c1dfbb78d98516f5480f8199910fe41ac904df (patch) | |
| tree | 8ee69fde44ac3b8969ebf529883656a2d05b1e45 /src | |
| parent | 641f81317a5b5cea6b3c5a4c65b1ca4313b0d8c0 (diff) | |
sound layer, bouncing
Diffstat (limited to 'src')
| -rw-r--r-- | src/audio.c | 28 | ||||
| -rw-r--r-- | src/math_helper.c | 2 | ||||
| -rw-r--r-- | src/players.c | 2 | ||||
| -rw-r--r-- | src/throwables.c | 74 |
4 files changed, 84 insertions, 22 deletions
diff --git a/src/audio.c b/src/audio.c new file mode 100644 index 0000000..ed25d4c --- /dev/null +++ b/src/audio.c @@ -0,0 +1,28 @@ +#include "../include/audio.h" + +void play_sound(int channel, Mix_Chunk* wav) { + Mix_PlayChannel(channel, wav, 0); +} + +void play_positioned_sound(int channel, Mix_Chunk* wav, vec3f pos, float max_audible_dist) { + player* p = get_player_by_id(player_id); + if (!p) return; + + // calculate volume + int tiles_between_throwable_and_player = distance_between_3f((vec3f){.x = p->playerx, .y = p->playery, .z = p->height}, pos); + float volume = (tiles_between_throwable_and_player / max_audible_dist); + if (volume > 1.0f) volume = 1.0f; + + // calculate angle + /* + float dirx = (throwables[i].position.x - p->playerx); + float diry = (throwables[i].position.y - p->playery); + float rads = atan2(diry, dirx) * 180.0f/M_PI; + if (rads < 0) rads = 360 + rads; + rads += 90; + if (rads > 360) rads -= 360; + */ + + Mix_SetPosition(0, 0, volume*255); + Mix_PlayChannel(channel, wav, 0); +} diff --git a/src/math_helper.c b/src/math_helper.c index 6bb89e2..b795fe2 100644 --- a/src/math_helper.c +++ b/src/math_helper.c @@ -12,7 +12,7 @@ bool onSegment(vec2f p, vec2f q, vec2f r) int orientation(vec2f p, vec2f q, vec2f r) { - int val = (q.y - p.y) * (r.x - q.x) - + float val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if (val == 0) return 0; // collinear diff --git a/src/players.c b/src/players.c index fda8700..4ea763d 100644 --- a/src/players.c +++ b/src/players.c @@ -202,7 +202,7 @@ void take_player_input(platform_window* window) { double length = sqrt(dirx * dirx + diry * diry); dirx /= length; diry /= length; - log_info("THROWING!"); + throw_throwable(window, player_id, THROWABLE_GRENADE, dirx, diry); //network_message message = create_protocol_user_shoot(player_id, dirx, diry); //add_message_to_outgoing_queuex(message, *global_state.client); diff --git a/src/throwables.c b/src/throwables.c index 9676c3a..33f6137 100644 --- a/src/throwables.c +++ b/src/throwables.c @@ -1,4 +1,5 @@ #include "../include/throwables.h" +#include "../include/audio.h" void clear_throwables() { for (int i = 0; i < max_throwables; i++) { @@ -27,48 +28,81 @@ void throw_throwable(platform_window* window, u32 id, throwable_type type, float } } +bool check_if_throwable_collided_with_object(throwable* b, platform_window* window, vec3f oldpos, vec3f newpos, vec3f* direction) { + map_info info = get_map_info(window); + float size = get_bullet_size_in_tile(window); + + bool result = false; + + for (int i = 0; i < MAX_OBJECTS; i++) { + object o = loaded_map.objects[i]; + if (!o.active) continue; + if (b->position.z <= o.h + o.size.z && b->position.z >= o.h) { + box obj_box = get_box_of_square((vec3f){o.position.x, o.position.y, o.h}, o.size); + + if (lines_intersect((vec2f){.x = oldpos.x, .y = oldpos.y}, (vec2f){.x = newpos.x, .y = newpos.y}, + (vec2f){.x = o.position.x, .y = o.position.y}, (vec2f){.x = o.position.x + o.size.x, .y = o.position.y})) { + result = true; + } + if (lines_intersect((vec2f){.x = oldpos.x, .y = oldpos.y}, (vec2f){.x = newpos.x, .y = newpos.y}, + (vec2f){.x = o.position.x, .y = o.position.y + o.size.y}, (vec2f){.x = o.position.x + o.size.x, .y = o.position.y + o.size.y})) { + result = true; + } + if (lines_intersect((vec2f){.x = oldpos.x, .y = oldpos.y}, (vec2f){.x = newpos.x, .y = newpos.y}, + (vec2f){.x = o.position.x, .y = o.position.y}, (vec2f){.x = o.position.x, .y = o.position.y + o.size.y})) { + result = true; + } + if (lines_intersect((vec2f){.x = oldpos.x, .y = oldpos.y}, (vec2f){.x = newpos.x, .y = newpos.y}, + (vec2f){.x = o.position.x + o.size.x, .y = o.position.y}, (vec2f){.x = o.position.x + o.size.x, .y = o.position.y + o.position.y})) { + result = true; + } + + if (result) { + b->position = oldpos; + direction->x = -direction->x; + direction->y = -direction->y; + return true; + } + } + } + + return result; +} void update_throwables_server(platform_window* window) { float speed = 7.0f * SERVER_TICK_RATE; float gravity = 0.015f; - float max_audible_throwable_dist = 10.0f; - for (int i = 0; i < max_throwables; i++) { throwable b = throwables[i]; if (!b.active) continue; player *p = get_player_by_id(b.player_id); if (!p) continue; + + vec3f oldpos = throwables[i].position; + // move forward throwables[i].position.x += throwables[i].direction.x * speed; throwables[i].position.y += throwables[i].direction.y * speed; + // gravity if (throwables[i].direction.z != 0) throwables[i].direction.z += gravity; throwables[i].position.z -= throwables[i].direction.z; + // bouncing off floor float floor = get_height_of_tile_under_coords(throwables[i].position.x, throwables[i].position.y); if (throwables[i].position.z < floor && throwables[i].direction.z != 0) { throwables[i].position.z = floor; throwables[i].direction.z = -throwables[i].direction.z*0.7; + throwables[i].bounces++; + + play_positioned_sound(CHANNEL_THROWABLES, wav_throwable_bounce, b.position, 8); + + if (throwables[i].bounces >= 3) throwables[i].direction.z = 0; + } - // calculate volume - int tiles_between_throwable_and_player = distance_between_3f((vec3f){.x = p->playerx, .y = p->playery, .z = p->height}, b.position); - float volume = (tiles_between_throwable_and_player / max_audible_throwable_dist); - if (volume > 1.0f) volume = 1.0f; - - // calculate angle - float dirx = (throwables[i].position.x - p->playerx); - float diry = (throwables[i].position.y - p->playery); - float rads = atan2(diry, dirx) * 180.0f/M_PI; - if (rads < 0) rads = 360 + rads; - rads += 90; - if (rads > 360) rads -= 360; - - log_infox("rads: %.2f", rads) - - Mix_SetPosition(0, rads, volume*255); - Mix_PlayChannel(0, wav_throwable_bounce, 0); - if (throwables[i].direction.z > -0.03f && throwables[i].direction.z < 0.03f) throwables[i].direction.z = 0; + if (check_if_throwable_collided_with_object(&throwables[i], window, oldpos, throwables[i].position, &throwables[i].direction)) { + play_positioned_sound(CHANNEL_THROWABLES, wav_throwable_bounce, b.position, 8); } } } |
