diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bullet.c | 1 | ||||
| -rw-r--r-- | src/enemies.c | 257 | ||||
| -rw-r--r-- | src/enemies.h | 11 | ||||
| -rw-r--r-- | src/main.c | 4 | ||||
| -rw-r--r-- | src/player.c | 98 | ||||
| -rw-r--r-- | src/player.h | 6 | ||||
| -rw-r--r-- | src/wall.c | 49 | ||||
| -rw-r--r-- | src/wall.h | 6 |
8 files changed, 363 insertions, 69 deletions
diff --git a/src/bullet.c b/src/bullet.c index cfb12bf..41264ed 100644 --- a/src/bullet.c +++ b/src/bullet.c @@ -166,3 +166,4 @@ detect_bullet_wall_collisions(void) rodeo_collision_2d_world_compare_other(&player_bullet_collision_world, get_wall_world(), bullet_wall_resolver); rodeo_collision_2d_world_compare_other(&enemy_bullet_collision_world, get_wall_world(), bullet_wall_resolver); } + diff --git a/src/enemies.c b/src/enemies.c index 2640aae..388c05f 100644 --- a/src/enemies.c +++ b/src/enemies.c @@ -6,8 +6,12 @@ #include "wall.h" static rodeo_collision_2d_world_t collision_enemies_world = {0}; -static rodeo_texture_2d_t enemy_texture; +static rodeo_collision_2d_world_t collision_ghosts_world = {0}; +static rodeo_texture_2d_t hinotamatchi_texture; +static rodeo_texture_2d_t amonghost_texture; +static rodeo_texture_2d_t squid_texture; static cvec_enemy_t enemies = {0}; +static cvec_enemy_t ghosts = {0}; static float spawn_cooldown = 0; void @@ -15,31 +19,45 @@ init_enemies(void) { //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")); + squid_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/squid.png")); + hinotamatchi_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/hinotamatchi.png")); + amonghost_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/amonghost.png")); } void deinit_enemies(void) { rodeo_collision_2d_world_destroy(&collision_enemies_world); + rodeo_collision_2d_world_destroy(&collision_ghosts_world); cvec_enemy_t_drop(&enemies); - rodeo_texture_2d_destroy(&enemy_texture); + rodeo_texture_2d_destroy(&hinotamatchi_texture); + rodeo_texture_2d_destroy(&squid_texture); + rodeo_texture_2d_destroy(&amonghost_texture); } enemy_t* spawn_enemy(float x, float y) { - rodeo_collision_2d_world_item_t enemy_collision = (rodeo_collision_2d_world_item_t){.x = x, .y = y, .width = 26, .height = 38}; + rodeo_collision_2d_world_item_t enemy_collision = (rodeo_collision_2d_world_item_t){.x = x, .y = y, .width = 40, .height = 40}; world_id id = rodeo_collision_2d_world_item_create(&collision_enemies_world, enemy_collision)->id; + uint64_t rng = rodeo_random_uint64_get(); + + cvec_enemy_t *enemy_collision_world = &enemies; + + if(rng % 3 == enemy_weapon_basic) + { + enemy_collision_world = &ghosts; + } + return cvec_enemy_t_push( - &enemies, + enemy_collision_world, (enemy_t){ - .hp = 100.0, - .move_speed = 1.0f, + .hp = 50.0, + .move_speed = ((float)(rng % 3) + 1.0f) * 0.3f, .behavior = enemy_ai_follow, .weapon = { - .firerate = 1.0f, - .type = enemy_weapon_basic, + .firerate = -((rng % 3) - 2.0f) * 1.6f, + .type = rng % 3, .cooldown = 0, }, .id = id @@ -47,10 +65,41 @@ spawn_enemy(float x, float y) } void -draw_enemies(void) +draw_enemy(cvec_collision_2d_world_item_value *enemy) { - c_foreach(i, cvec_collision_2d_world_item, collision_enemies_world) { - cvec_collision_2d_world_item_value *enemy = i.ref; + rodeo_texture_2d_t *texture; + rodeo_color_RGBAFloat_t color; + + enemy_t *enemy_obj = get_enemy_by_id(enemy->id); + + switch(enemy_obj->weapon.type) + { + case enemy_weapon_none: + { + texture = &hinotamatchi_texture; + color = (rodeo_color_RGBAFloat_t){ + .array = { 0.8f, 0.5f, 0.1f, 1.0f } + }; + } + break; + case enemy_weapon_basic: + { + texture = &amonghost_texture; + color = (rodeo_color_RGBAFloat_t){ + .array = { 0.25f, 0.95f, 0.25f, 0.6f } + }; + } + break; + case enemy_weapon_fourcross: + { + texture = &squid_texture; + color = (rodeo_color_RGBAFloat_t){ + .array = { 0.1f, 0.2f, 0.75f, 1.0f } + }; + } + break; + } + rodeo_texture_2d_draw( &(rodeo_rectangle_t){ @@ -62,12 +111,23 @@ draw_enemies(void) &(rodeo_rectangle_t){ .x = 0, .y = 0, - .width = 26, - .height = 38 + .width = 40, + .height = 40 }, - NULL, - &enemy_texture + &color, + texture ); + +} + +void +draw_enemies(void) +{ + c_foreach(i, cvec_collision_2d_world_item, collision_enemies_world) { + draw_enemy(i.ref); + } + c_foreach(i, cvec_collision_2d_world_item, collision_ghosts_world) { + draw_enemy(i.ref); } } @@ -103,6 +163,7 @@ move_enemies(void) { rodeo_collision_2d_world_compare_self(get_enemies_world(), enemy_overlap_resolver); + rodeo_collision_2d_world_compare_self(get_ghosts_world(), enemy_overlap_resolver); c_foreach(i, cvec_enemy_t, enemies) { rodeo_collision_2d_world_item_t *enemy = rodeo_collision_2d_world_item_get_by_id(i.ref->id); enemy->x += enemy->dx; @@ -110,12 +171,22 @@ move_enemies(void) enemy->y += enemy->dy; enemy->dy = 0; } + c_foreach(i, cvec_enemy_t, ghosts) { + rodeo_collision_2d_world_item_t *enemy = rodeo_collision_2d_world_item_get_by_id(i.ref->id); + enemy->x += enemy->dx; + enemy->dx = 0; + enemy->y += enemy->dy; + enemy->dy = 0; + } } void enemies_attempt_weapon_fire(void) { - c_foreach(i, cvec_enemy_t, enemies) { + cvec_enemy_t enemy_arry[2] = { enemies, ghosts }; + for(int32_t j = 0; j < 2; ++j) + { + c_foreach(i, cvec_enemy_t, enemy_arry[j]) { switch(i.ref->weapon.type) { case enemy_weapon_none: @@ -138,7 +209,18 @@ enemies_attempt_weapon_fire(void) dest ); glm_vec2_normalize(dest); + + vec2 rand1 = { + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.250f, + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.250f, + }; + + glm_vec2_add(dest, rand1, dest); + + glm_vec2_normalize(dest); + glm_vec2_scale(dest, 1.0, dest); + spawn_bullet( enemy->x, enemy->y, @@ -159,8 +241,123 @@ enemies_attempt_weapon_fire(void) } } break; + case enemy_weapon_fourcross: + { + 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); + + vec2 bullet1; + vec2 bullet2; + vec2 bullet3; + vec2 bullet4; + + glm_vec2_rotate(dest, 3.14159f/4.0f, bullet1); + glm_vec2_rotate(bullet1, 3.14159f/2.0f, bullet2); + glm_vec2_rotate(bullet2, 3.14159f/2.0f, bullet3); + glm_vec2_rotate(bullet3, 3.14159f/2.0f, bullet4); + + vec2 rand1 = { + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + }; + vec2 rand2 = { + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + }; + vec2 rand3 = { + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + }; + vec2 rand4 = { + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + ((((float)rodeo_random_double_get()) - 0.5f) * 2.0f) * 0.0f, + }; + + glm_vec2_add(bullet1, rand1, bullet1); + glm_vec2_add(bullet2, rand2, bullet2); + glm_vec2_add(bullet3, rand3, bullet3); + glm_vec2_add(bullet4, rand4, bullet4); + + glm_vec2_normalize(bullet1); + glm_vec2_normalize(bullet2); + glm_vec2_normalize(bullet3); + glm_vec2_normalize(bullet4); + + //glm_vec2_scale(dest, 1.0, dest); + + spawn_bullet( + enemy->x, + enemy->y, + bullet1[0], + bullet1[1], + get_enemy_bullet_world(), + (rodeo_color_RGBAFloat_t){ + .colors.alpha = 1, + .colors.red = 0.9f, + .colors.green = 0.1f, + .colors.blue = 0.1f + } + ); + spawn_bullet( + enemy->x, + enemy->y, + bullet2[0], + bullet2[1], + get_enemy_bullet_world(), + (rodeo_color_RGBAFloat_t){ + .colors.alpha = 1, + .colors.red = 0.9f, + .colors.green = 0.1f, + .colors.blue = 0.1f + } + ); + spawn_bullet( + enemy->x, + enemy->y, + bullet3[0], + bullet3[1], + get_enemy_bullet_world(), + (rodeo_color_RGBAFloat_t){ + .colors.alpha = 1, + .colors.red = 0.9f, + .colors.green = 0.1f, + .colors.blue = 0.1f + } + ); + spawn_bullet( + enemy->x, + enemy->y, + bullet4[0], + bullet4[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* @@ -173,6 +370,11 @@ get_enemy_by_id( return i.ref; } } + c_foreach(i, cvec_enemy_t, ghosts) { + if (i.ref->id.id == id.id) { + return i.ref; + } + } return NULL; } @@ -182,6 +384,12 @@ get_enemies_world(void) return &collision_enemies_world; } +rodeo_collision_2d_world_t * +get_ghosts_world(void) +{ + return &collision_ghosts_world; +} + cvec_enemy_t get_enemies_cvec(void) { @@ -217,13 +425,17 @@ damage_enemy_resolver( void detect_bullet_enemy_collisions(void) { + rodeo_collision_2d_world_compare_other(&collision_ghosts_world, get_player_bullet_world(), damage_enemy_resolver); rodeo_collision_2d_world_compare_other(&collision_enemies_world, get_player_bullet_world(), damage_enemy_resolver); } void group_follow_target(rodeo_collision_2d_world_item_t *target) { - c_foreach(i, cvec_enemy_t, enemies) { + cvec_enemy_t enemy_arry[2] = { enemies, ghosts }; + for(int32_t j = 0; j < 2; ++j) + { + c_foreach(i, cvec_enemy_t, enemy_arry[j]) { rodeo_collision_2d_world_item_t *enemy = rodeo_collision_2d_world_item_get_by_id(i.ref->id); /* float direction[2] = { @@ -254,6 +466,7 @@ group_follow_target(rodeo_collision_2d_world_item_t *target) enemy->dx = result[0]; enemy->dy = result[1]; } + } } enemy_t* @@ -285,8 +498,14 @@ attempt_random_enemy_spawn( { spawn_cooldown -= rodeo_frame_time_get(); if (spawn_cooldown <= 0) { - spawn_cooldown += (float)rodeo_random_double_get() * 5000.0f + 1500.0f; + spawn_cooldown += (float)rodeo_random_double_get() * 500.0f + 150.0f; return random_enemy_create(bounds); } return NULL; } + +void +detect_enemy_wall_collisions(void) +{ + rodeo_collision_2d_world_compare_other(&collision_enemies_world, get_wall_world(), moving_wall_resolver); +} diff --git a/src/enemies.h b/src/enemies.h index dc6f721..b111885 100644 --- a/src/enemies.h +++ b/src/enemies.h @@ -14,10 +14,9 @@ enemy_ai_behavior; typedef enum { - enemy_weapon_none, + enemy_weapon_fourcross, enemy_weapon_basic, - //enemy_weapon_fourplus, - //enemy_weapon_fourcross, + enemy_weapon_none, } enemy_weapon_type; @@ -70,6 +69,9 @@ get_enemy_by_id( rodeo_collision_2d_world_t * get_enemies_world(void); +rodeo_collision_2d_world_t * +get_ghosts_world(void); + cvec_enemy_t get_enemies_cvec(void); @@ -92,3 +94,6 @@ enemy_t* attempt_random_enemy_spawn( rodeo_rectangle_t bounds ); + +void +detect_enemy_wall_collisions(void); @@ -54,11 +54,14 @@ main_loop(void) draw_bullets(); draw_player(); draw_enemies(); + draw_hp_bar(); detect_bullet_enemy_collisions(); detect_bullet_wall_collisions(); detect_player_enemy_collisions(); detect_player_wall_collisions(); + detect_enemy_wall_collisions(); + /* rodeo_debug_text_draw( 1, 1, @@ -81,6 +84,7 @@ main_loop(void) " fps: %.2f ", time_var ); + */ } } diff --git a/src/player.c b/src/player.c index 5a8ba0f..d56b7dc 100644 --- a/src/player.c +++ b/src/player.c @@ -7,6 +7,7 @@ #include "sprite.h" #include "wall.h" #include "cglm/vec2.h" +#include "wall.h" struct player_t { @@ -14,6 +15,7 @@ struct player_t rodeo_texture_2d_t texture; rodeo_texture_2d_t shadow_texture; rodeo_texture_2d_t aim_texture; + rodeo_texture_2d_t heart_texture; int32_t hp; float damage_timer; //ms float damage_cooldown_rate; @@ -45,6 +47,7 @@ init_player(void) player.texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/mainblob-128.png")); player.shadow_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/blobshadow.png")); player.aim_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/aim.png")); + player.heart_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/heart.png")); player.sprite.config.texture = &player.texture; player_collision_world = rodeo_collision_2d_world_create(); player.collision_id = rodeo_collision_2d_world_item_create( @@ -110,6 +113,25 @@ draw_player(void) } void +draw_hp_bar(void) +{ + for(int i = 0; i < (player.hp / 10); ++i) + { + /* + rodeo_log( + rodeo_logLevel_info, + "%f\n", + 10.0f + (40.0f * (float)i) + );*/ + rodeo_texture_2d_draw( + &(rodeo_rectangle_t){ .x = 10.0f + (40.0f * (float)i), .y = 10, .width = 35, .height = 35 }, + &(rodeo_rectangle_t){ .x = 0, .y = 0, .width = 35, .height = 35 }, + NULL, + &player.heart_texture); + } +} + +void parse_player_input(void) { cvec_collision_2d_world_item_value *player_position = rodeo_collision_2d_world_item_get_by_id(player.collision_id); @@ -229,6 +251,32 @@ void player_enemy_resolver( ); player.hp -= 10; player.damage_timer = 0; + enemy_t *enemy = get_enemy_by_id(enemy_collision->id); + enemy_destroy(enemy); + } + +} + +void player_bullet_resolver( + rodeo_collision_2d_world_item_t *player_collision, + rodeo_collision_2d_world_item_t *bullet_collision +) +{ + if (player.hp <= 0) { + /*rodeo_log( + rodeo_logLevel_info, + "player is dead" + );*/ + } else if (player.damage_timer >= player.damage_cooldown_rate) { + rodeo_log( + rodeo_logLevel_info, + "player health is now %d", + player.hp + ); + player.hp -= 10; + player.damage_timer = 0; + bullet_t *bullet = get_bullet_by_id(bullet_collision->id); + bullet_destroy(bullet); } } @@ -238,58 +286,13 @@ detect_player_enemy_collisions(void) { player.damage_timer += rodeo_frame_time_get(); rodeo_collision_2d_world_compare_other(&player_collision_world, get_enemies_world(), player_enemy_resolver); -} - -void player_wall_resolver( - rodeo_collision_2d_world_item_t *player_collision, - rodeo_collision_2d_world_item_t *wall_collision -) -{ - rodeo_collision_2d_world_item_t *p = player_collision; - rodeo_collision_2d_world_item_t *w = wall_collision; - rodeo_rectangle_t step = (rodeo_rectangle_t){ - .x = p->x + p->dx * rodeo_frame_time_get(), - .y = p->y + p->dy * rodeo_frame_time_get(), - .width = p->width, - .height = p->height - }; - rodeo_rectangle_t intersection = rodeo_collision_2d_get_collision_rect(p, w); - if (intersection.width < intersection.height) { - if (intersection.x == step.x) { - p->x = w->x + w->width; - if (p->dx < 0) { - p->dx = 0; - } - } else { - p->x = w->x - p->width; - if (p->dx > 0) { - p->dx = 0; - } - } - } - else if (intersection.height < intersection.width) { - if (intersection.y == step.y) { - p->y = w->y + w->height; - if (p->dy < 0) { - p->dy = 0; - } - } else { - p->y = w->y - p->height; - if (p->dy > 0) { - p->dy = 0; - } - } - } - else if (p->width == w->width && p->height == w->height) { - p->dx = 0; - p->dy = 0; - } + rodeo_collision_2d_world_compare_other(&player_collision_world, get_enemy_bullet_world(), player_bullet_resolver); } void detect_player_wall_collisions(void) { - rodeo_collision_2d_world_compare_other(&player_collision_world, get_wall_world(), player_wall_resolver); + rodeo_collision_2d_world_compare_other(&player_collision_world, get_wall_world(), moving_wall_resolver); } cvec_collision_2d_world_item_value * @@ -337,3 +340,4 @@ draw_aim(float player_x, float player_y, float scale) &player.aim_texture ); } + diff --git a/src/player.h b/src/player.h index 45c81b1..f0614d1 100644 --- a/src/player.h +++ b/src/player.h @@ -32,6 +32,9 @@ void draw_player(void); void +draw_hp_bar(void); + +void move_player(void); void @@ -44,6 +47,9 @@ void detect_player_enemy_collisions(void); void +detect_player_bullet_collisions(void); + +void detect_player_wall_collisions(void); cvec_collision_2d_world_item_value * @@ -1,4 +1,5 @@ #include "wall.h" +#include "rodeo.h" static rodeo_collision_2d_world_t collision_wall_world; @@ -53,3 +54,51 @@ coords_inside_wall( } return false; } + +void +moving_wall_resolver( + rodeo_collision_2d_world_item_t *obj_collision, + rodeo_collision_2d_world_item_t *wall_collision +) +{ + rodeo_collision_2d_world_item_t *p = obj_collision; + rodeo_collision_2d_world_item_t *w = wall_collision; + rodeo_rectangle_t step = (rodeo_rectangle_t){ + .x = p->x + p->dx * rodeo_frame_time_get(), + .y = p->y + p->dy * rodeo_frame_time_get(), + .width = p->width, + .height = p->height + }; + rodeo_rectangle_t intersection = rodeo_collision_2d_get_collision_rect(p, w); + if (intersection.width < intersection.height) { + if (intersection.x == step.x) { + p->x = w->x + w->width; + if (p->dx < 0) { + p->dx = 0; + } + } else { + p->x = w->x - p->width; + if (p->dx > 0) { + p->dx = 0; + } + } + } + else if (intersection.height < intersection.width) { + if (intersection.y == step.y) { + p->y = w->y + w->height; + if (p->dy < 0) { + p->dy = 0; + } + } else { + p->y = w->y - p->height; + if (p->dy > 0) { + p->dy = 0; + } + } + } + else if (p->width == w->width && p->height == w->height) { + p->dx = 0; + p->dy = 0; + } +} + @@ -22,3 +22,9 @@ coords_inside_wall( float x, float y ); + +void +moving_wall_resolver( + rodeo_collision_2d_world_item_t *obj_collision, + rodeo_collision_2d_world_item_t *wall_collision +); |
