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