feat: add multiple birds
This commit is contained in:
+7
-5
@@ -152,12 +152,14 @@ typedef struct
|
||||
float rotation;
|
||||
} Player;
|
||||
|
||||
#define MAX_BIRDS 100
|
||||
typedef struct {
|
||||
Sprite sprite;
|
||||
Animation animation;
|
||||
Physics physics;
|
||||
bool arrived;
|
||||
Vector2 destPos;
|
||||
int count;
|
||||
Sprite sprite[MAX_BIRDS];
|
||||
Animation animation[MAX_BIRDS];
|
||||
Physics physics[MAX_BIRDS];
|
||||
bool arrived[MAX_BIRDS];
|
||||
Vector2 destPos[MAX_BIRDS];
|
||||
} Birds;
|
||||
|
||||
typedef struct {
|
||||
|
||||
+60
-50
@@ -7,72 +7,82 @@ const TextureAsset birdsTextureAssets[BIRDS_TEX_COUNT] = {
|
||||
};
|
||||
|
||||
Birds CreateBirds(void){
|
||||
Birds birds = {.count = MAX_BIRDS};
|
||||
TextureAsset flyingAsset = birdsTextureAssets[FLYING];
|
||||
Texture2D texture = LoadTexture(flyingAsset.path);
|
||||
float frameWidth = flyingAsset.frameWidth;
|
||||
float rotation = flyingAsset.rotation;
|
||||
|
||||
Sprite birdSprite = {
|
||||
.texture = texture,
|
||||
.frameSize = {frameWidth, frameWidth},
|
||||
.sourceRec = {0.0f, 0.0f, frameWidth, frameWidth},
|
||||
.destRec = {GetScreenWidth()/2, 0.0f, frameWidth, frameWidth},
|
||||
.origin = {frameWidth / 2, frameWidth / 2},
|
||||
.rotation = rotation,
|
||||
.color = WHITE
|
||||
};
|
||||
|
||||
Animation birdsAnimation = {
|
||||
.state = 1,
|
||||
.currentFrame = 0,
|
||||
.maxFrame = 2,
|
||||
.framesCounter = 0,
|
||||
.framesSpeed = 2.0f,
|
||||
.animTimer = 0.0f
|
||||
};
|
||||
for (int i = 0; i < birds.count; i++)
|
||||
{
|
||||
birds.sprite[i] = (Sprite){
|
||||
.texture = texture,
|
||||
.frameSize = {frameWidth, frameWidth},
|
||||
.sourceRec = {0.0f, 0.0f, frameWidth, frameWidth},
|
||||
.destRec = {GetScreenWidth()/2, 0.0f, frameWidth, frameWidth},
|
||||
.origin = {frameWidth / 2, frameWidth / 2},
|
||||
.rotation = rotation,
|
||||
.color = WHITE
|
||||
};
|
||||
|
||||
Rectangle cameraRec = GetCameraRectangle();
|
||||
printf("cameraRec: %f, %f, %f, %f", cameraRec.x, cameraRec.y, cameraRec.width, cameraRec.height);
|
||||
birds.animation[i] = (Animation){
|
||||
.state = 1,
|
||||
.currentFrame = 0,
|
||||
.maxFrame = 2,
|
||||
.framesCounter = 0,
|
||||
.framesSpeed = 2.0f,
|
||||
.animTimer = 0.0f
|
||||
};
|
||||
|
||||
Physics birdsPhysics = {
|
||||
.position = GetPositionOutsideRectangle(cameraRec, 1, 100),
|
||||
.direction = (Vector2){0.0f, 0.0f},
|
||||
.speed = 5.0f
|
||||
};
|
||||
Rectangle cameraRec = GetCameraRectangle();
|
||||
|
||||
return (Birds){
|
||||
.sprite = birdSprite,
|
||||
.animation = birdsAnimation,
|
||||
.physics = birdsPhysics,
|
||||
.arrived = true,
|
||||
.destPos = {0.0f, 0.0f}
|
||||
};
|
||||
|
||||
birds.physics[i] = (Physics){
|
||||
.position = GetPositionOutsideRectangle(cameraRec, 1, 100),
|
||||
.direction = (Vector2){0.0f, 0.0f},
|
||||
.speed = 5.0f
|
||||
};
|
||||
|
||||
birds.arrived[i] = true;
|
||||
birds.destPos[i] = (Vector2){0.0f, 0.0f};
|
||||
|
||||
}
|
||||
|
||||
return birds;
|
||||
}
|
||||
|
||||
void UpdateBirds(Birds *birds){
|
||||
Camera2D *camera = GetCamera();
|
||||
Rectangle cameraRec = GetCameraRectangle();
|
||||
if(birds->arrived)
|
||||
|
||||
for (int i = 0; i < birds->count; i++)
|
||||
{
|
||||
birds->destPos = GetPositionOutsideRectangle(cameraRec, 1, 100);
|
||||
birds->arrived = false;
|
||||
if(birds->arrived[i])
|
||||
{
|
||||
birds->destPos[i] = GetPositionOutsideRectangle(cameraRec, 1, 100);
|
||||
birds->arrived[i] = false;
|
||||
}
|
||||
if(CheckCollisionPointCircle(birds->physics[i].position, birds->destPos[i], 10.0F))
|
||||
{
|
||||
birds->arrived[i] = true;
|
||||
}
|
||||
Vector2 dir = GetDirectionBetweenTwoVectors(birds->physics[i].position, birds->destPos[i]);
|
||||
UpdatePhysics(&birds->physics[i], dir);
|
||||
printf("birdsPos: %f, %f", birds->physics[i].position.x, birds->physics[i].position.y);
|
||||
printf("cameraTarget: %f, %f",camera->target.x, camera->target.y );
|
||||
UpdateSpriteDestRec(&birds->sprite[i], &birds->physics[i].position);
|
||||
float rotation = Vector2ToRotation(dir);
|
||||
UpdateSpriteRotation(&birds->sprite[i], rotation + 45.0f);
|
||||
UpdateAnimation(&birds->animation[i], GetFrameTime());
|
||||
UpdateSpriteSourceRec(&birds->sprite[i], &(Vector2){birds->sprite[i].frameSize.x * birds->animation[i].currentFrame, birds->sprite[i].frameSize.y});
|
||||
}
|
||||
if(CheckCollisionPointCircle(birds->physics.position, birds->destPos, 10.0F))
|
||||
{
|
||||
birds->arrived = true;
|
||||
}
|
||||
Vector2 dir = GetDirectionBetweenTwoVectors(birds->physics.position, birds->destPos);
|
||||
UpdatePhysics(&birds->physics, dir);
|
||||
printf("birdsPos: %f, %f", birds->physics.position.x, birds->physics.position.y);
|
||||
printf("cameraTarget: %f, %f",camera->target.x, camera->target.y );
|
||||
UpdateSpriteDestRec(&birds->sprite, &birds->physics.position);
|
||||
float rotation = Vector2ToRotation(dir);
|
||||
UpdateSpriteRotation(&birds->sprite, rotation + 45.0f);
|
||||
UpdateAnimation(&birds->animation, GetFrameTime());
|
||||
UpdateSpriteSourceRec(&birds->sprite, &(Vector2){birds->sprite.frameSize.x * birds->animation.currentFrame, birds->sprite.frameSize.y});
|
||||
}
|
||||
|
||||
void RenderBirds(Birds *birds){
|
||||
RenderSprite(&birds->sprite);
|
||||
|
||||
for (int i = 0; i < birds->count; i++)
|
||||
{
|
||||
RenderSprite(&birds->sprite[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user