Skip to content

Commit

Permalink
moved bullets to player object / game refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
savaughn committed Dec 14, 2023
1 parent 2999d44 commit b7b8dc1
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 97 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 1 addition & 2 deletions include/bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "common.h"

void draw_bullet(int x, int y, bool draw_debug);
void update_bullet_position(Vector2 *pos, const float speed, const float delta_time);
void check_bullet_collision(struct bullet *bullet, struct enemy *enemy);
void update_bullet_position(struct bullet *bullet, const float delta_time);

#endif // BULLET_H
48 changes: 37 additions & 11 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define COMMON_H

#include <stdbool.h>
#include <stdio.h>
#include "raylib.h"

#define RENDER_WIDTH 720
Expand All @@ -10,10 +11,10 @@
#define DEVICE_HEIGHT 379
#define MAX_FPS 60

#define MAX_BULLETS 100
#define MAX_BULLETS 10
#define MAX_ENEMIES 10

typedef unsigned char uint8;
#define MAX_FRAMES_LEVEL1 200

enum player_action
{
Expand All @@ -31,34 +32,39 @@ struct enemy
Vector2 position;
float speed;
bool active;
uint8 health;
u_int8_t health;
bool hit;
enum enemy_type type;
u_int8_t hitbox_r;
};

static struct enemy basic_enemy = {
.position = (Vector2){0, 0},
.speed = 96.0f,
.active = false,
.health = 1,
.type = BASIC
.type = BASIC,
.hitbox_r = 24,
};

struct player
struct bullet
{
Vector2 position;
float speed;
enum player_action action;
int score;
Vector2 velocity;
bool active;
int hitbox_r;
};

struct bullet
struct player
{
Vector2 position;
float speed;
bool active;
struct player *player;
enum player_action action;
int score;
Vector2 velocity;
int bullet_count;
struct bullet bullets[MAX_BULLETS];
u_int8_t hitbox_r;
};

struct opts
Expand All @@ -75,4 +81,24 @@ enum screen
GAME
};

struct enemy_spawn_info
{
Vector2 spawn_position;
enum enemy_type type;
};

static struct enemy *level1SpawnScript[MAX_FRAMES_LEVEL1] = {
[0 ... 199] = &(struct enemy){.active = false},
[60] = &(struct enemy){.position = (Vector2){100, -100}, .type = BASIC, .speed = 96.0f, .active = true, .health = 1},
[120] = &(struct enemy){.position = (Vector2){200, -200}, .type = BASIC, .speed = 96.0f, .active = true, .health = 1},
[180] = &(struct enemy){.position = (Vector2){300, -300}, .type = BASIC, .speed = 96.0f, .active = true, .health = 1},
};

static struct bullet common_bullet = {
.position = (Vector2){0, 0},
.speed = 800.0f,
.active = false,
.hitbox_r = 12,
};

#endif // COMMON_H
2 changes: 1 addition & 1 deletion include/enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

void init_enemy(struct enemy *enemy);
void draw_enemy(int x, int y, bool hit, bool draw_debug, enum enemy_type type);
void update_enemy_position(Vector2 *pos, const float speed, const float delta_time);
void update_enemy_position(struct enemy *enemy, const float delta_time);

#endif // ENEMY_H
1 change: 1 addition & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
void init_renderer(RenderTexture2D *target, Shader *shader, float *resize_scale, Camera2D *camera);
void load_shaders_and_textures(RenderTexture2D *target, Shader *shader, float *resize_scale);
void handle_opts_input(int *selectedWidth, int *selectedHeight, float *resize, Shader *shader, struct opts *opts, enum screen *current_screen);
void check_collisions(struct player *player, struct enemy *enemy, Camera2D *camera);

#endif // UTILS_H
22 changes: 3 additions & 19 deletions src/bullet.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,11 @@ void draw_bullet(int x, int y, bool draw_debug)
}
}

void update_bullet_position(Vector2 *pos, const float speed, const float delta_time)
void update_bullet_position(struct bullet *bullet, const float delta_time)
{
pos->y -= speed * delta_time;
}

void check_bullet_collision(struct bullet *bullet, struct enemy *enemy)
{
if (CheckCollisionCircles(bullet->position, 5, enemy->position, 24))
bullet->position.y -= bullet->speed * delta_time;
if (bullet->position.y < 0)
{
if (enemy->health > 0)
{
enemy->health--;
}
if (enemy->health == 0)
{
enemy->active = false;
bullet->player->score += 100;
}
enemy->hit = true;
enemy->position.y -= 5;
enemy->speed *= 0.9;
bullet->active = false;
}
}
9 changes: 7 additions & 2 deletions src/enemy.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void init_enemy(struct enemy *enemy)

void draw_basic_enemy(int x, int y, bool hit, bool draw_debug)
{
DrawRectangle(x - 20, y - 20, 40, 40, BLACK);
DrawRectangleLines(x - 20, y - 20, 40, 40, hit ? RED : BLUE);
if (draw_debug)
{
Expand All @@ -28,7 +29,11 @@ void draw_enemy(int x, int y, bool hit, bool draw_debug, enum enemy_type type)
}
}

void update_enemy_position(Vector2 *pos, const float speed, const float delta_time)
void update_enemy_position(struct enemy *enemy, const float delta_time)
{
pos->y += speed * delta_time;
enemy->position.y += enemy->speed * delta_time;
if (enemy->hit)
{
enemy->position.y += 3;
}
}
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(void)
static Shader shader;
static float resize_scale;

struct opts opts = {.is_paused = false, .draw_debug = false, .show_shader = true, .show_device_scale = false};
struct opts opts = {.is_paused = false, .draw_debug = false, .show_shader = false, .show_device_scale = false};
Camera2D camera;
int selectedWidth = 720;
int selectedHeight = 720;
Expand Down
13 changes: 12 additions & 1 deletion src/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
void draw_player(int x, int y, bool draw_debug)
{
// DrawTriangle((Vector2){x, y - 20}, (Vector2){x - 20, y + 20}, (Vector2){x + 20, y + 20}, LIGHTGRAY);
DrawTriangle((Vector2){x, y - 20}, (Vector2){x - 20, y + 20}, (Vector2){x + 20, y + 20}, BLACK);
DrawTriangleLines((Vector2){x, y - 20}, (Vector2){x - 20, y + 20}, (Vector2){x + 20, y + 20}, LIME);
if (draw_debug)
{
Expand All @@ -15,7 +16,17 @@ void update_player_action(struct player *player)
{
if (IsKeyPressed(KEY_SPACE))
{
player->action = SHOOT;
if (player->bullet_count >= MAX_BULLETS)
{
return;
}
player->bullets[player->bullet_count] = (struct bullet){
.position = (Vector2){player->position.x, player->position.y},
.active = true,
.speed = common_bullet.speed,
.hitbox_r = common_bullet.hitbox_r};
player->bullet_count++;
player->bullet_count %= MAX_BULLETS;
}
else
{
Expand Down
98 changes: 40 additions & 58 deletions src/screens/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@
#include "bullet.h"
#include "pause.h"

static struct player player;
static struct bullet player_bullets[MAX_BULLETS];
static struct enemy enemies[MAX_ENEMIES];
static int player_bullet_count = 0;
#include <stdio.h>

static struct player player = {
.position = (Vector2){360, 650},
.speed = 300.0f,
.action = NONE,
.score = 0,
.velocity = (Vector2){0, 0},
.bullet_count = 0,
.hitbox_r = 12,
};
static struct enemy enemies[200];
static int enemy_count = 0;
static bool is_level_initialized = false;
static int frame_count = 0;

void init_game(void)
{
player.position = (Vector2){360, 650};
player.speed = 300.0f; // Adjusted speed for smooth movement
player.action = NONE;
player.score = 0;
player.velocity = (Vector2){0, 0};

// bullet array
for (int i = 0; i < MAX_BULLETS; i++)
{
player_bullets[i] = (struct bullet){.active = false}; // initialize all bullets to inactive
}

for (int i = 0; i < MAX_ENEMIES; i++)
{
init_enemy(&enemies[i]);
player.bullets[i] = (struct bullet){
.active = false,
.speed = 800.0f,
.position = (Vector2){0, 0},
.hitbox_r = 12};
}
}

Expand All @@ -46,19 +48,6 @@ void draw_game_screen(const struct opts *opts, const int *selectedWidth, const i
update_player_position(&player.position, &player.velocity, delta_time);
update_player_action(&player);

if (player.action == SHOOT)
{
struct bullet bullet = {
.position = {player.position.x, player.position.y},
.speed = 800.0f, // Adjusted bullet speed for smooth movement
.active = true,
.player = &player};

player_bullets[player_bullet_count] = bullet;
player_bullet_count++;
player_bullet_count %= MAX_BULLETS;
}

// Draw
BeginDrawing();
ClearBackground(BLACK);
Expand All @@ -67,51 +56,31 @@ void draw_game_screen(const struct opts *opts, const int *selectedWidth, const i
BeginMode2D(*camera);
ClearBackground(BLACK);

// Update and draw player bullets
int active_bullet_count = 0;
for (int i = 0; i < MAX_BULLETS; i++)
{
if (player_bullets[i].active)
if (player.bullets[i].active)
{
update_bullet_position(&player_bullets[i].position, player_bullets[i].speed, delta_time);
for (int j = 0; j < MAX_ENEMIES; j++)
{
if (enemies[j].active)
{
check_bullet_collision(&player_bullets[i], &enemies[j]);
}
}
if (player_bullets[i].position.y < 0)
{
player_bullets[i].active = false;
}
draw_bullet(player_bullets[i].position.x, player_bullets[i].position.y, opts->draw_debug);
update_bullet_position(&player.bullets[i], delta_time);
draw_bullet(player.bullets[i].position.x, player.bullets[i].position.y, opts->draw_debug);
active_bullet_count++;
}
}

draw_player(player.position.x, player.position.y, opts->draw_debug);

for (int i = 0; i < MAX_ENEMIES; i++)
for (int i = 0; i < enemy_count; i++)
{
// if active
if (enemies[i].active)
{
update_enemy_position(&enemies[i].position, enemies[i].speed, delta_time);
update_enemy_position(&enemies[i], delta_time);
if (enemies[i].position.y > 750)
{
enemies[i].active = false;
}
draw_enemy(enemies[i].position.x, enemies[i].position.y, enemies[i].hit, opts->draw_debug, enemies[i].type);
if (CheckCollisionCircles(player.position, 12, enemies[i].position, 24))
{
enemies[i].active = false;
player.position.y += 10;
}
if (enemies[i].hit)
enemies[i].position.y += 3;
}
else
{
init_enemy(&enemies[i]);
check_collisions(&player, &enemies[i], camera);
}

enemies[i].hit = false;
Expand Down Expand Up @@ -149,4 +118,17 @@ void draw_game_screen(const struct opts *opts, const int *selectedWidth, const i
}

EndDrawing();

if (frame_count < MAX_FRAMES_LEVEL1)
{
struct enemy *enemy_spawn = level1SpawnScript[frame_count];
if (enemy_spawn->active)
{
enemies[enemy_count] = *enemy_spawn;
enemies[enemy_count].active = true;
enemy_count++;
}
frame_count++;
// frame_count %= MAX_FRAMES_LEVEL1;
}
}
27 changes: 25 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "utils.h"
#include "scan_frag.h"
#include <stdio.h>

void init_renderer(RenderTexture2D *target, Shader *shader, float *resize_scale, Camera2D *camera)
{
Expand All @@ -26,8 +25,8 @@ void handle_opts_input(int *selectedWidth, int *selectedHeight, float *resize, S
if (IsKeyPressed(KEY_ONE))
{
opts->is_paused = !opts->is_paused;
printf("Paused %s\n", opts->is_paused ? "true" : "false"); }
*current_screen = opts->is_paused ? PAUSE : GAME;
}
if (IsKeyPressed(KEY_TWO))
{
opts->show_shader = !opts->show_shader;
Expand All @@ -46,3 +45,27 @@ void handle_opts_input(int *selectedWidth, int *selectedHeight, float *resize, S
opts->draw_debug = !opts->draw_debug;
}
}

void check_collisions(struct player *player, struct enemy *enemy, Camera2D *camera)
{
for (int i = 0; i < MAX_BULLETS; i++)
{
if (player->bullets[i].active)
{
if (CheckCollisionCircles(player->bullets[i].position, 12, enemy->position, 24))
{
enemy->health--;
if (enemy->health <= 0)
{
enemy->active = false;
player->score += 100;
}
player->bullets[i].active = false;
}
}
}
if (CheckCollisionCircles(player->position, player->hitbox_r, enemy->position, enemy->hitbox_r))
{
player->position.y += 10;
}
}

0 comments on commit b7b8dc1

Please sign in to comment.