diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bullet.c | 58 | ||||
| -rw-r--r-- | src/bullet.h | 27 | ||||
| -rw-r--r-- | src/enemies.c | 65 | ||||
| -rw-r--r-- | src/enemies.h | 11 | ||||
| -rw-r--r-- | src/main.c | 2 | ||||
| -rw-r--r-- | src/player.c | 16 |
6 files changed, 158 insertions, 21 deletions
diff --git a/src/bullet.c b/src/bullet.c index 4fe6433..8fca4af 100644 --- a/src/bullet.c +++ b/src/bullet.c @@ -1,12 +1,14 @@ #include "rodeo.h" #include "input.h" #include "player.h" +#include "bullet.h" #include "rodeo/collision.h" static rodeo_texture_2d_t bullet_texture; -static rodeo_collision_2d_world_t bullet_collision_world; -static rodeo_collision_2d_world_t player_bullet_collision_world; -static rodeo_collision_2d_world_t enemy_bullet_collision_world; +//static rodeo_collision_2d_world_t bullet_collision_world; +static rodeo_collision_2d_world_t player_bullet_collision_world = {0}; +static rodeo_collision_2d_world_t enemy_bullet_collision_world = {0}; +static cvec_bullet_t bullets = {0}; void init_bullets(void) @@ -20,6 +22,36 @@ deinit_bullets(void) rodeo_texture_2d_destroy(&bullet_texture); } +bullet_t * +spawn_bullet( + float x, + float y, + float dx, + float dy, + rodeo_collision_2d_world_t *bullet_world, + rodeo_color_RGBAFloat_t color +) +{ + bullet_t bullet = { + .color = color + }; + bullet.id = rodeo_collision_2d_world_item_create( + bullet_world, + (rodeo_collision_2d_world_item_t){ + .x = x, + .y = y, + .dx = dx, + .dy = dy, + .width = 25.0f, + .height = 25.0f + } + )->id; + return cvec_bullet_t_push( + &bullets, + bullet + ); +} + void move_bullets(void) { @@ -36,11 +68,25 @@ move_bullets(void) } +bullet_t* +get_bullet_by_id( + world_id id +) +{ + c_foreach(i, cvec_bullet_t, bullets) { + if (i.ref->id.id == id.id) { + return i.ref; + } + } + return NULL; +} + void draw_bullets(void) { c_foreach(i, cvec_collision_2d_world_item, player_bullet_collision_world) { cvec_collision_2d_world_item_value *bullet = i.ref; + bullet_t *bullet_obj = get_bullet_by_id(i.ref->id); rodeo_texture_2d_draw( &(rodeo_rectangle_t){ .x = bullet->x, @@ -54,12 +100,13 @@ draw_bullets(void) .width = 25, .height = 25 }, - NULL, + &bullet_obj->color, &bullet_texture ); } c_foreach(i, cvec_collision_2d_world_item, enemy_bullet_collision_world) { cvec_collision_2d_world_item_value *bullet = i.ref; + bullet_t *bullet_obj = get_bullet_by_id(i.ref->id); rodeo_texture_2d_draw( &(rodeo_rectangle_t){ .x = bullet->x, @@ -73,12 +120,13 @@ draw_bullets(void) .width = 25, .height = 25 }, - NULL, + &bullet_obj->color, &bullet_texture ); } } + rodeo_collision_2d_world_t * get_enemy_bullet_world(void) { diff --git a/src/bullet.h b/src/bullet.h index f9b57ef..3438291 100644 --- a/src/bullet.h +++ b/src/bullet.h @@ -3,15 +3,42 @@ #include "rodeo.h" #include "rodeo/collision.h" +typedef +struct +{ + world_id id; + rodeo_color_RGBAFloat_t color; +} +bullet_t; + +#define i_val bullet_t +#define i_opt c_no_cmp +#include "stc/cvec.h" + void init_bullets(void); void deinit_bullets(void); +bullet_t * +spawn_bullet( + float x, + float y, + float dx, + float dy, + rodeo_collision_2d_world_t *bullet_world, + rodeo_color_RGBAFloat_t color +); + void move_bullets(void); +bullet_t* +get_bullet_by_id( + world_id id +); + void draw_bullets(void); diff --git a/src/enemies.c b/src/enemies.c index e7de8c3..3779cb5 100644 --- a/src/enemies.c +++ b/src/enemies.c @@ -1,16 +1,17 @@ #include "enemies.h" #include "bullet.h" #include "cglm/vec2.h" +#include "player.h" -static rodeo_collision_2d_world_t collision_enemies_world; +static rodeo_collision_2d_world_t collision_enemies_world = {0}; static rodeo_texture_2d_t enemy_texture; -static cvec_enemy_t enemies; +static cvec_enemy_t enemies = {0}; void init_enemies(void) { - collision_enemies_world = rodeo_collision_2d_world_create(); - enemies = cvec_enemy_t_init(); + //collision_enemies_world = rodeo_collision_2d_world_create(); + //enemies = cvec_enemy_t_init(); enemy_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/enemy.png")); } @@ -33,8 +34,11 @@ spawn_enemy(float x, float y) .hp = 100.0, .move_speed = 1.0f, .behavior = enemy_ai_follow, + .weapon = { .firerate = 1.0f, - .weapon = enemy_weapon_basic, + .type = enemy_weapon_basic, + .cooldown = 0, + }, .id = id }); } @@ -105,6 +109,57 @@ move_enemies(void) } } +void +enemies_attempt_weapon_fire(void) +{ + c_foreach(i, cvec_enemy_t, enemies) { + switch(i.ref->weapon.type) + { + case enemy_weapon_none: + { + // do nothing + } + break; + case enemy_weapon_basic: + { + if(i.ref->weapon.cooldown < 0) + { + i.ref->weapon.cooldown += i.ref->weapon.firerate; + //weapon spawn logic + cvec_collision_2d_world_item_value *player = get_player_position(); + cvec_collision_2d_world_item_value *enemy = rodeo_collision_2d_world_item_get_by_id(i.ref->id); + vec2 dest; + glm_vec2_sub( + (vec2){ player->x, player->y }, + (vec2){ enemy->x, enemy->y }, + dest + ); + glm_vec2_normalize(dest); + glm_vec2_scale(dest, 1.0, dest); + spawn_bullet( + enemy->x, + enemy->y, + dest[0], + dest[1], + get_enemy_bullet_world(), + (rodeo_color_RGBAFloat_t){ + .colors.alpha = 1, + .colors.red = 0.9f, + .colors.green = 0.1f, + .colors.blue = 0.1f + } + ); + } + else + { + i.ref->weapon.cooldown -= rodeo_frame_time_get()/1000.0f; + } + } + break; + } + } +} + enemy_t* get_enemy_by_id( world_id id diff --git a/src/enemies.h b/src/enemies.h index bbf2405..29a147a 100644 --- a/src/enemies.h +++ b/src/enemies.h @@ -14,6 +14,7 @@ enemy_ai_behavior; typedef enum { + enemy_weapon_none, enemy_weapon_basic, //enemy_weapon_fourplus, //enemy_weapon_fourcross, @@ -27,8 +28,12 @@ struct world_id id; enemy_ai_behavior behavior; float move_speed; - enemy_weapon_type weapon; - float firerate; + struct enemy_weapon + { + enemy_weapon_type type; + float firerate; + float cooldown; + } weapon; } enemy_t; @@ -36,6 +41,8 @@ struct #define i_opt c_no_cmp #include "stc/cvec.h" +void +enemies_attempt_weapon_fire(void); void init_enemies(void); @@ -184,6 +184,8 @@ main_loop(void) } */ + enemies_attempt_weapon_fire(); + move_bullets(); move_enemies(); group_follow_target(get_player_position()); diff --git a/src/player.c b/src/player.c index d171c80..99a9c8a 100644 --- a/src/player.c +++ b/src/player.c @@ -3,6 +3,7 @@ #include "player.h" #include "enemies.h" #include "rodeo/collision.h" +#include "bullet.h" #include "sprite.h" struct player_t @@ -139,16 +140,13 @@ player_shoot(rodeo_collision_2d_world_t *bullet_collision_world) const uint32_t bullet_per_frame = 10; for(uint32_t i = 0; i < bullet_per_frame; ++i) { - rodeo_collision_2d_world_item_create( + spawn_bullet( + (float)player_position->x - (orc_size[0] / 2.0f), + (float)player_position->y - (orc_size[1] / 2.0f), + (float)((int8_t)(rodeo_random_uint64_get() % 10) - 5), + (float)((int8_t)(rodeo_random_uint64_get() % 10) - 5), bullet_collision_world, - (rodeo_collision_2d_world_item_t){ - .x = (float)player_position->x - (orc_size[0] / 2.0f), - .y = (float)player_position->y - (orc_size[1] / 2.0f), - .dx = (float)((int8_t)(rodeo_random_uint64_get() % 10) - 5), - .dy = (float)((int8_t)(rodeo_random_uint64_get() % 10) - 5), - .width = 25.0f, - .height = 25.0f - } + (rodeo_color_RGBAFloat_t){ .array = { 1,1,1,1 } } ); } } |
