diff --git a/.DS_Store b/.DS_Store index 0a8e871..f51f562 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/include/bullet.h b/include/bullet.h index f4356f5..1b64752 100644 --- a/include/bullet.h +++ b/include/bullet.h @@ -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 diff --git a/include/common.h b/include/common.h index d334647..8cfcdbc 100644 --- a/include/common.h +++ b/include/common.h @@ -2,6 +2,7 @@ #define COMMON_H #include +#include #include "raylib.h" #define RENDER_WIDTH 720 @@ -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 { @@ -31,9 +32,10 @@ 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 = { @@ -41,24 +43,28 @@ static struct enemy basic_enemy = { .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 @@ -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 diff --git a/include/enemy.h b/include/enemy.h index 22d5a1a..e227e22 100644 --- a/include/enemy.h +++ b/include/enemy.h @@ -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 diff --git a/include/utils.h b/include/utils.h index cb456bd..100bc7e 100644 --- a/include/utils.h +++ b/include/utils.h @@ -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 diff --git a/src/bullet.c b/src/bullet.c index 89845aa..fad9971 100644 --- a/src/bullet.c +++ b/src/bullet.c @@ -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; } } diff --git a/src/enemy.c b/src/enemy.c index d0b1715..38bafc1 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -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) { @@ -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; + } } diff --git a/src/main.c b/src/main.c index 5c8cdf8..1e73226 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/player.c b/src/player.c index ea0fd12..ce42ae9 100644 --- a/src/player.c +++ b/src/player.c @@ -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) { @@ -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 { diff --git a/src/screens/game.c b/src/screens/game.c index d974bc2..aa823f4 100644 --- a/src/screens/game.c +++ b/src/screens/game.c @@ -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 + +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}; } } @@ -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); @@ -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; @@ -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; + } } diff --git a/src/utils.c b/src/utils.c index 30bdd2c..3b73388 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,6 +1,5 @@ #include "utils.h" #include "scan_frag.h" -#include void init_renderer(RenderTexture2D *target, Shader *shader, float *resize_scale, Camera2D *camera) { @@ -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; @@ -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; + } +}