summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2023-10-28 10:52:18 +0200
committerAldrik Ramaekers <aldrik@amftech.nl>2023-10-28 10:52:18 +0200
commit18c1dfbb78d98516f5480f8199910fe41ac904df (patch)
tree8ee69fde44ac3b8969ebf529883656a2d05b1e45
parent641f81317a5b5cea6b3c5a4c65b1ca4313b0d8c0 (diff)
sound layer, bouncing
-rw-r--r--.vscode/settings.json4
-rw-r--r--build/data/sounds/throwable_bounce.wavbin33124 -> 80936 bytes
-rw-r--r--build/zombies.exebin1973919 -> 1975680 bytes
-rw-r--r--data/sounds/throwable_bounce.wavbin33124 -> 80936 bytes
-rw-r--r--include/audio.h11
-rw-r--r--include/throwables.h1
-rw-r--r--main.c3
-rw-r--r--src/audio.c28
-rw-r--r--src/math_helper.c2
-rw-r--r--src/players.c2
-rw-r--r--src/throwables.c74
11 files changed, 102 insertions, 23 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 2df0a7b..b3222ee 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -2,6 +2,8 @@
"files.associations": {
"*.ejs": "html",
"throwables.h": "c",
- "cstdlib": "c"
+ "cstdlib": "c",
+ "audio.h": "c",
+ "project_base.h": "c"
}
} \ No newline at end of file
diff --git a/build/data/sounds/throwable_bounce.wav b/build/data/sounds/throwable_bounce.wav
index 7d968d9..f06fdaa 100644
--- a/build/data/sounds/throwable_bounce.wav
+++ b/build/data/sounds/throwable_bounce.wav
Binary files differ
diff --git a/build/zombies.exe b/build/zombies.exe
index e6fb5b8..13c301b 100644
--- a/build/zombies.exe
+++ b/build/zombies.exe
Binary files differ
diff --git a/data/sounds/throwable_bounce.wav b/data/sounds/throwable_bounce.wav
index 7d968d9..f06fdaa 100644
--- a/data/sounds/throwable_bounce.wav
+++ b/data/sounds/throwable_bounce.wav
Binary files differ
diff --git a/include/audio.h b/include/audio.h
new file mode 100644
index 0000000..98cb356
--- /dev/null
+++ b/include/audio.h
@@ -0,0 +1,11 @@
+#ifndef INCLUDE_AUDIO_
+#define INCLUDE_AUDIO_
+
+#include <projectbase/project_base.h>
+
+#define CHANNEL_THROWABLES 0
+
+void play_sound(int channel, Mix_Chunk* wav);
+void play_positioned_sound(int channel, Mix_Chunk* wav, vec3f pos, float max_audible_dist);
+
+#endif \ No newline at end of file
diff --git a/include/throwables.h b/include/throwables.h
index 7a6d4eb..f4507b6 100644
--- a/include/throwables.h
+++ b/include/throwables.h
@@ -20,6 +20,7 @@ typedef struct t_throwable {
vec3f position;
vec3f direction;
float alive_time;
+ int bounces;
} throwable;
throwable throwables[500] = {0};
diff --git a/main.c b/main.c
index db768e6..e414f81 100644
--- a/main.c
+++ b/main.c
@@ -8,6 +8,7 @@
#include "include/players.h"
#include "include/objects.h"
#include "include/map.h"
+#include "include/audio.h"
#include "include/zombies.h"
#include "include/math_helper.h"
#include "include/bullets.h"
@@ -29,6 +30,7 @@
#include "src/map.c"
#include "src/players.c"
#include "src/objects.c"
+#include "src/audio.c"
#include "src/zombies.c"
#include "src/bullets.c"
#include "src/throwables.c"
@@ -90,6 +92,7 @@ int main(int argc, char **argv)
if (Mix_OpenAudio(48000, AUDIO_F32SYS, 2, 2048) == 0) {
log_info("Audio system initialized.");
+ Mix_MasterVolume(MIX_MAX_VOLUME/4);
}
else {
log_info("Audio failed.");
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);
}
}
}