feat: add enemies projectile interaction
This commit is contained in:
@@ -49,7 +49,7 @@ build: $(SRC)
|
||||
$(COMPILER) $(SRC) -o $(TARGET) $(C_FLAGS) $(INCLUDE_FLAGS) $(EXTERNAL_FLAGS) $(RAYLIB_FLAGS)
|
||||
|
||||
clean:
|
||||
rm -rf bin
|
||||
rm -rf $(TARGET)
|
||||
|
||||
run: clean build
|
||||
./$(TARGET)
|
||||
|
||||
+3
-2
@@ -126,6 +126,7 @@ typedef struct {
|
||||
Physics physics[MAX_ENEMIES];
|
||||
bool arrived[MAX_ENEMIES];
|
||||
Vector2 destPos[MAX_ENEMIES];
|
||||
bool alive[MAX_ENEMIES];
|
||||
} Enemies;
|
||||
|
||||
typedef enum { FLYING, BIRDS_TEX_COUNT } birdsTextureID;
|
||||
@@ -232,7 +233,7 @@ void UpdatePlayer(void);
|
||||
void RenderPlayer(void);
|
||||
|
||||
Enemies CreateEnemies(void);
|
||||
void UpdateEnemies(Enemies *enemies);
|
||||
void UpdateEnemies(Enemies *enemies, Projectiles *projectiles);
|
||||
void RenderEnemies(Enemies *enemies);
|
||||
|
||||
Birds CreateBirds(void);
|
||||
@@ -244,7 +245,7 @@ void UpdateIslands(Islands *islands);
|
||||
void RenderIslands(Islands *islands);
|
||||
|
||||
Projectiles CreateProjectiles(void);
|
||||
void UpdateProjectiles(Projectiles *projectiles);
|
||||
void UpdateProjectiles(Projectiles *projectiles, Enemies *enemies);
|
||||
void RenderProjectiles(Projectiles *projectiles);
|
||||
|
||||
void InitGame(App *app);
|
||||
|
||||
+2
-2
@@ -66,9 +66,9 @@ void UpdateGame(App *app) {
|
||||
UpdateFloor(&g->floor);
|
||||
UpdatePlayer();
|
||||
UpdateBirds(&g->birds);
|
||||
UpdateEnemies(&g->enemies);
|
||||
UpdateEnemies(&g->enemies, &g->projectiles);
|
||||
UpdateIslands(&g->islands);
|
||||
UpdateProjectiles(&g->projectiles);
|
||||
UpdateProjectiles(&g->projectiles, &g->enemies);
|
||||
}
|
||||
|
||||
void RenderComponents(App *app) {
|
||||
|
||||
+39
-26
@@ -36,46 +36,59 @@ Enemies CreateEnemies(void) {
|
||||
|
||||
enemies.arrived[i] = true;
|
||||
enemies.destPos[i] = (Vector2){0.0f, 0.0f};
|
||||
enemies.alive[i] = true;
|
||||
}
|
||||
|
||||
return enemies;
|
||||
}
|
||||
|
||||
void UpdateEnemies(Enemies *enemies) {
|
||||
void UpdateEnemies(Enemies *enemies, Projectiles *projectiles) {
|
||||
Camera2D *camera = GetCamera();
|
||||
Rectangle cameraRec = GetCameraRectangle();
|
||||
|
||||
for (int i = 0; i < enemies->count; i++) {
|
||||
if (enemies->arrived[i]) {
|
||||
enemies->destPos[i] = GetPositionOutsideRectangle(cameraRec, 100, 1000);
|
||||
enemies->arrived[i] = false;
|
||||
if (enemies->alive[i]) {
|
||||
|
||||
for (int j = 0; j < projectiles->count; j++){
|
||||
if (!projectiles->active[j]) continue;
|
||||
if (CheckCollisionCircleRec(projectiles->physics[j].position, projectiles->radius[j], enemies->sprite[i].destRec)){
|
||||
enemies->alive[i] = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (enemies->arrived[i]) {
|
||||
enemies->destPos[i] = GetPositionOutsideRectangle(cameraRec, 100, 1000);
|
||||
enemies->arrived[i] = false;
|
||||
}
|
||||
if (CheckCollisionPointCircle(enemies->physics[i].position,
|
||||
enemies->destPos[i], 10.0F)) {
|
||||
enemies->arrived[i] = true;
|
||||
}
|
||||
if (GetDistanceBetweenTwoVectors(enemies->physics[i].position,
|
||||
camera->target) > 1000) {
|
||||
enemies->physics[i].position =
|
||||
GetPositionOutsideRectangle(cameraRec, 100, 1000);
|
||||
}
|
||||
Vector2 dir = GetDirectionBetweenTwoVectors(enemies->physics[i].position,
|
||||
enemies->destPos[i]);
|
||||
UpdatePhysics(&enemies->physics[i], dir);
|
||||
UpdateSpriteDestRec(&enemies->sprite[i], &enemies->physics[i].position);
|
||||
float rotation = Vector2ToRotation(dir);
|
||||
UpdateSpriteRotation(&enemies->sprite[i], rotation + 90.0f);
|
||||
UpdateAnimation(&enemies->animation[i], GetFrameTime());
|
||||
UpdateSpriteSourceRec(&enemies->sprite[i],
|
||||
&(Vector2){enemies->sprite[i].frameSize.x *
|
||||
enemies->animation[i].currentFrame,
|
||||
enemies->sprite[i].frameSize.y});
|
||||
}
|
||||
if (CheckCollisionPointCircle(enemies->physics[i].position,
|
||||
enemies->destPos[i], 10.0F)) {
|
||||
enemies->arrived[i] = true;
|
||||
}
|
||||
if (GetDistanceBetweenTwoVectors(enemies->physics[i].position,
|
||||
camera->target) > 1000) {
|
||||
enemies->physics[i].position =
|
||||
GetPositionOutsideRectangle(cameraRec, 100, 1000);
|
||||
}
|
||||
Vector2 dir = GetDirectionBetweenTwoVectors(enemies->physics[i].position,
|
||||
enemies->destPos[i]);
|
||||
UpdatePhysics(&enemies->physics[i], dir);
|
||||
UpdateSpriteDestRec(&enemies->sprite[i], &enemies->physics[i].position);
|
||||
float rotation = Vector2ToRotation(dir);
|
||||
UpdateSpriteRotation(&enemies->sprite[i], rotation + 90.0f);
|
||||
UpdateAnimation(&enemies->animation[i], GetFrameTime());
|
||||
UpdateSpriteSourceRec(&enemies->sprite[i],
|
||||
&(Vector2){enemies->sprite[i].frameSize.x *
|
||||
enemies->animation[i].currentFrame,
|
||||
enemies->sprite[i].frameSize.y});
|
||||
}
|
||||
}
|
||||
|
||||
void RenderEnemies(Enemies *enemies) {
|
||||
|
||||
for (int i = 0; i < enemies->count; i++) {
|
||||
RenderSprite(&enemies->sprite[i]);
|
||||
if (enemies->alive[i]){
|
||||
RenderSprite(&enemies->sprite[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ Projectiles CreateProjectiles(void) {
|
||||
return projectiles;
|
||||
}
|
||||
|
||||
void UpdateProjectiles(Projectiles *projectiles) {
|
||||
void UpdateProjectiles(Projectiles *projectiles, Enemies *enemies) {
|
||||
|
||||
static float lastShotTime = 0.0f;
|
||||
const float reloadTime = 1.0f;
|
||||
@@ -25,6 +25,7 @@ void UpdateProjectiles(Projectiles *projectiles) {
|
||||
|
||||
if (IsKeyDown(KEY_Q) && (currentTime - lastShotTime >= reloadTime)) {
|
||||
for (int i = 0; i < projectiles->count; i++) {
|
||||
|
||||
if (!projectiles->active[i]) {
|
||||
projectiles->active[i] = true;
|
||||
Player *player = GetPlayer();
|
||||
@@ -38,7 +39,7 @@ void UpdateProjectiles(Projectiles *projectiles) {
|
||||
}
|
||||
|
||||
if (IsKeyDown(KEY_E) && (currentTime - lastShotTime >= reloadTime)) {
|
||||
for (int i = 0; i < projectiles->count; i++) {
|
||||
for (int i = 0; i < projectiles->count; i++){
|
||||
if (!projectiles->active[i]) {
|
||||
projectiles->active[i] = true;
|
||||
Player *player = GetPlayer();
|
||||
@@ -52,17 +53,23 @@ void UpdateProjectiles(Projectiles *projectiles) {
|
||||
}
|
||||
|
||||
for (int i = 0; i < projectiles->count; i++) {
|
||||
if (projectiles->active[i]) {
|
||||
UpdatePhysics(&projectiles->physics[i],
|
||||
projectiles->physics[i].direction);
|
||||
|
||||
if (!projectiles->active[i]) continue;
|
||||
|
||||
for (int j = 0; j < enemies->count; j++){
|
||||
if (CheckCollisionCircleRec(projectiles->physics[i].position, projectiles->radius[i], enemies->sprite[j].destRec)){
|
||||
projectiles->active[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
UpdatePhysics(&projectiles->physics[i],
|
||||
projectiles->physics[i].direction);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderProjectiles(Projectiles *projectiles) {
|
||||
for (int i = 0; i < projectiles->count; i++) {
|
||||
printf("Projectile Active: %d", projectiles->active[i]);
|
||||
if (projectiles->active[i] == true) {
|
||||
if (projectiles->active[i]) {
|
||||
DrawCircle(projectiles->physics[i].position.x,
|
||||
projectiles->physics[i].position.y, projectiles->radius[i],
|
||||
projectiles->color[i]);
|
||||
|
||||
Reference in New Issue
Block a user