diff options
| author | realtradam <[email protected]> | 2024-05-12 17:34:21 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2024-05-12 17:34:21 -0400 |
| commit | 8475767975a0f07507908ba53dba894b97b48795 (patch) | |
| tree | 4d5bf2f8c49eb92f8d9777e0606d944dca01905d | |
| parent | f5311cc940fd94aa70808f19a38581c80b764615 (diff) | |
| download | tojam2024-8475767975a0f07507908ba53dba894b97b48795.tar.gz tojam2024-8475767975a0f07507908ba53dba894b97b48795.zip | |
shooting bullets
| -rw-r--r-- | src/bullet.c | 68 | ||||
| -rw-r--r-- | src/bullet.h | 13 | ||||
| -rw-r--r-- | src/input.h | 1 | ||||
| -rw-r--r-- | src/main.c | 40 | ||||
| -rw-r--r-- | src/player.c | 105 | ||||
| -rw-r--r-- | src/player.h | 4 | ||||
| -rw-r--r-- | src/render.c | 3 |
7 files changed, 172 insertions, 62 deletions
diff --git a/src/bullet.c b/src/bullet.c new file mode 100644 index 0000000..5cff129 --- /dev/null +++ b/src/bullet.c @@ -0,0 +1,68 @@ +#include "bullet.h" + +typedef +struct +{ + int team; // 0 means not in use + Vector3 position; + Vector3 direction; + int lifetime; +} +Bullet; + +Bullet bullets[100]; + + void +spawn_bullet(int team, Vector3 position, Vector3 direction) +{ + if(team == 0) + { + return; + } + for(int i = 0; i < 100; ++i) + { + if(bullets[i].team != 0) + { + continue; + } + bullets[i].team = team; + bullets[i].position = position; + bullets[i].direction = direction; + bullets[i].lifetime = 100; + return; + }; +} + + void +render_bullets(void) +{ + for(int i = 0; i < 100; ++i) + { + if(bullets[i].team == 0) + { + continue; + } + DrawCube(bullets[i].position, 0.25, 0.25, 0.25, RED); + } +} + + void +bullet_collision_check(void) +{ + for(int i = 0; i < 100; ++i) + { + if(bullets[i].team == 0) + { + continue; + } + bullets[i].lifetime -= 1; + if(bullets[i].lifetime <= 0) + { + bullets[i].team = 0; + } + bullets[i].position.x += bullets[i].direction.x * 0.25; + bullets[i].position.y += bullets[i].direction.y * 0.25; + bullets[i].position.z += bullets[i].direction.z * 0.25; + } + +} diff --git a/src/bullet.h b/src/bullet.h new file mode 100644 index 0000000..85cd454 --- /dev/null +++ b/src/bullet.h @@ -0,0 +1,13 @@ +#ifndef GAME_BULLET_H +#define GAME_BULLET_H +#include "raylib.h" + +void +spawn_bullet(int team, Vector3 position, Vector3 direction); + +void +render_bullets(void); + + void +bullet_collision_check(void); +#endif diff --git a/src/input.h b/src/input.h index a65c4a3..8913e35 100644 --- a/src/input.h +++ b/src/input.h @@ -1,6 +1,5 @@ #ifndef GAME_INPUT_H #define GAME_INPUT_H - #include <libdragon.h> extern joypad_buttons_t pressed_p1; @@ -29,6 +29,7 @@ bool xflag=false; #include "input.h" #include "render.h" #include "world.h" +#include "bullet.h" Camera camera = { 0 }; Camera camera2 = { 0 }; @@ -59,12 +60,14 @@ World world = { .direction = { 1, 0, 0 }, .speed = 1.0f/60.0f, .color = BLUE, + .camera_mode = 1, }, { .position = { 2, 0, 0 }, .direction = { -1, 0, 0 }, .speed = 1.0f/60.0f, - .color = RED, + .color = MAGENTA, + .camera_mode = 1, } } }; @@ -115,8 +118,41 @@ int main(void) // Update //----------------------------------------------------- //controller_read(&controllers); + updateController(); + + if(pressed_p1.r) + { + if(world.players[0].camera_mode == 0) + { + world.players[0].camera_mode = 1; + } + else + { + world.players[0].camera_mode = 0; + } + } + + if(pressed_p2.r) + { + if(world.players[1].camera_mode == 0) + { + world.players[1].camera_mode = 1; + } + else + { + world.players[1].camera_mode = 0; + } + } + if(pressed_p1.z) + { + spawn_bullet(1, world.players[0].position, world.players[0].direction); + } + bullet_collision_check(); + + movePlayers(); camera = lookThroughPlayer(camera, world.players[0]); + camera2 = lookThroughPlayer(camera2, world.players[1]); // Update //---------------------------------------------------------------------------------- @@ -147,8 +183,6 @@ int main(void) //DrawCubeTextureRec(texture, (Rectangle){ 0, texture.height/2, texture.width/2, texture.height/2 }, // (Vector3){ 2.0f, 1.0f, 0.0f }, 2.0f, 2.0f, 2.0f, WHITE); - movePlayers(); - updateController(); char text[50]; sprintf(text, "Joystick %d,%d", inputs_p1.stick_x, inputs_p1.stick_y); diff --git a/src/player.c b/src/player.c index 3b30b05..4e02b78 100644 --- a/src/player.c +++ b/src/player.c @@ -11,16 +11,34 @@ drawPlayer(Player *player) DrawCube(player->position, 0.5f, 0.5f, 0.5f, player->color); } -Camera + Camera lookThroughPlayer(Camera camera, Player player) { - //camera.position = player.position; - camera.position.x = player.position.x - (player.direction.x * 2); - camera.position.y = player.position.y - (player.direction.y * 2); - camera.position.z = player.position.z - (player.direction.z * 2) + 1; - camera.target.x = player.position.x; - camera.target.y = player.position.y; - camera.target.z = player.position.z; + if(player.camera_mode == 0) + { + float normalized = sqrtf( + (player.direction.x * player.direction.x) + + (player.direction.y * player.direction.y) + + (player.direction.z * player.direction.z) + ); + + camera.position.x = player.position.x + player.direction.x / normalized; + camera.position.y = player.position.y + player.direction.y / normalized; + camera.position.z = player.position.z + player.direction.z / normalized; + camera.target.x = player.position.x + player.direction.x * 10 / normalized; + camera.target.y = player.position.y + player.direction.y * 10 / normalized; + camera.target.z = player.position.z + player.direction.z * 10 / normalized; + } + else if(player.camera_mode == 1) + { + //camera.position = player.position; + camera.position.x = player.position.x - (player.direction.x * 2); + camera.position.y = player.position.y - (player.direction.y * 2); + camera.position.z = player.position.z - (player.direction.z * 2) + player.camera_mode; + camera.target.x = player.position.x; + camera.target.y = player.position.y; + camera.target.z = player.position.z; + } camera.up = (Vector3){ 0.0f, 0.0f, 1.0f }; //camera.target = player2.position; //camera.target = (Vector3){ @@ -34,59 +52,30 @@ lookThroughPlayer(Camera camera, Player player) void movePlayers(void) { - float xinput = inputs_p1.stick_x / 126.0f; - float yinput = -inputs_p1.stick_y / 126.0f; - - //float distance = sqrtf(xinput * xinput + yinput * yinput); - //if(distance == 0) - //{ - // distance = 1; - //} - - Vector2 direction = { - world.players[0].direction.x, - world.players[0].direction.y + char stick[4] = { + inputs_p1.stick_x, + inputs_p1.stick_y, + inputs_p2.stick_x, + inputs_p2.stick_y }; + for(int i = 0; i < 2; ++i) + { + float xinput = stick[0 + (i * 2)] / 126.0f; + float yinput = stick[1 + (i * 2)] / 126.0f; - char text[50]; - - sprintf(text, "x: %f", direction.x); - DrawText(text, 160, 45, 12, GREEN); - sprintf(text, "y: %f", direction.y); - DrawText(text, 160, 60, 12, GREEN); - sprintf(text, "z: %f", world.players[0].direction.z); - DrawText(text, 160, 75, 12, GREEN); - - direction = Vector2Rotate(direction, 0.01745329 * -xinput * 7); - - sprintf(text, "x: %f", direction.x); - DrawText(text, 160, 175, 12, GREEN); - sprintf(text, "y: %f", direction.y); - DrawText(text, 160, 190, 12, GREEN); - sprintf(text, "z: %f", world.players[0].direction.z); - DrawText(text, 160, 205, 12, GREEN); - - world.players[0].direction.z = yinput; - world.players[0].direction.x = direction.x; - world.players[0].direction.y = direction.y; - - //float x = world.players[0].direction.x; - //float y = world.players[0].direction.z; - + Vector2 direction = { + world.players[i].direction.x, + world.players[i].direction.y + }; - //if(distance != 0) - //{ - // world.players[0].direction.x = x / distance; - // world.players[0].direction.y = y / distance; - //} + direction = Vector2Rotate(direction, 0.01745329 * -xinput * 7); - //world.players[1].direction.y = inputs_p2.stick_x / 126.0f; - //world.players[1].direction.z = -inputs_p2.stick_y / 126.0f; + world.players[i].direction.z = yinput; + world.players[i].direction.x = direction.x; + world.players[i].direction.y = direction.y; - world.players[0].position.x += world.players[0].direction.x * 0.1f; //* world.players[0].speed; - world.players[0].position.y += world.players[0].direction.y * 0.1f; //* world.players[0].speed; - world.players[0].position.z += world.players[0].direction.z * 0.1f; //* world.players[0].speed; - world.players[1].position.x += world.players[1].direction.x * world.players[1].speed; - world.players[1].position.y += world.players[1].direction.y * world.players[1].speed; - world.players[1].position.z += world.players[1].direction.z * world.players[1].speed; + world.players[i].position.x += world.players[i].direction.x * world.players[i].speed; + world.players[i].position.y += world.players[i].direction.y * world.players[i].speed; + world.players[i].position.z += world.players[i].direction.z * world.players[i].speed; + } } diff --git a/src/player.h b/src/player.h index 3d9db39..0e94080 100644 --- a/src/player.h +++ b/src/player.h @@ -4,6 +4,9 @@ #include "raylib.h" #include "input.h" +// for camera_style +// 0 = first person +// 1 = third person typedef struct { @@ -11,6 +14,7 @@ struct Vector3 direction; float speed; Color color; + int camera_mode; } Player; diff --git a/src/render.c b/src/render.c index 2206f13..7086ac8 100644 --- a/src/render.c +++ b/src/render.c @@ -1,5 +1,6 @@ #include "render.h" #include "rlgl.h" +#include "bullet.h" void drawLine(Vector3 start, Vector3 end, float width, int up, Color color) @@ -129,6 +130,8 @@ renderWorld(World* world, Camera camera) drawPlayer(&world->players[0]); drawPlayer(&world->players[1]); + render_bullets(); + //drawGrid((Vector3){0}, 10, 1); for(int i = -3; i <= 3; ++i){ |
