Skip to content

Commit

Permalink
Obstacles move at random speeds
Browse files Browse the repository at this point in the history
  • Loading branch information
aeffggbh committed Nov 20, 2024
1 parent 2c69acb commit bff0452
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
32 changes: 21 additions & 11 deletions src/Objects/Obstacle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace Obstacle
BottomLeaves
};

enum Parts
{
Top,
Bottom
};

Texture2D tree;
Sprite::Sprite trunk;
Sprite::Sprite topLeaves;
Expand All @@ -26,6 +32,9 @@ namespace Obstacle
static float minObstacleHeight;
static float obstacleSpace = 200.0f;

static int minSpeed = 200;
static int maxSpeed = 600;

static void ManageObstacle(Obstacle& obstacle);
static void MoveObstacle(Obstacle& enemyToMove);
static void KeepObstacleOnScreen(Obstacle& enemyToKeepOnScreen);
Expand All @@ -36,7 +45,7 @@ namespace Obstacle
tree = LoadTexture("res/sprites/tree.png");
}

void Init(Obstacle& obstacle, float originX, float speed, Color color)
void Init(Obstacle& obstacle, float originX, int dir, Color color)
{
InitTextures();

Expand All @@ -54,8 +63,8 @@ namespace Obstacle
else
obstacle.finishX = static_cast<float>(GetScreenWidth());

obstacle.hSpeed = speed;

obstacle.dir = dir;
SetObstacle(obstacle);
}

Expand Down Expand Up @@ -108,6 +117,7 @@ namespace Obstacle

void SetObstacle(Obstacle& obstacleToSet)
{
obstacleToSet.hSpeed = static_cast<float>(GetRandomValue(minSpeed, maxSpeed) * obstacleToSet.dir);
obstacleToSet.movesVertical = GetRandomValue(0, 1);

float dividedObstacleHeight = static_cast<float>(GetRandomValue(static_cast<int>(minObstacleHeight), static_cast<int>(maxObstacleHeight)));
Expand All @@ -134,7 +144,7 @@ namespace Obstacle
obstacleToSet.parts[i].spriteParts[j].destination.x = obstacleToSet.parts[i].collisionShape.x;
}

if (i == 0)
if (i == Top)
{
//top
obstacleToSet.parts[i].collisionShape.height = dividedObstacleHeight;
Expand Down Expand Up @@ -211,16 +221,16 @@ namespace Obstacle

enemyToMove.offSetY += enemyToMove.vSpeed * GetFrameTime();

enemyToMove.parts[0].spriteParts[TopLeaves].destination.y = enemyToMove.parts[0].collisionShape.y + enemyToMove.parts[0].collisionShape.height;
enemyToMove.parts[1].spriteParts[BottomLeaves].destination.y = enemyToMove.parts[1].collisionShape.y - enemyToMove.parts[1].spriteParts[BottomLeaves].destination.height;
enemyToMove.parts[Top].spriteParts[TopLeaves].destination.y = enemyToMove.parts[Top].collisionShape.y + enemyToMove.parts[Top].collisionShape.height;
enemyToMove.parts[Bottom].spriteParts[BottomLeaves].destination.y = enemyToMove.parts[Bottom].collisionShape.y - enemyToMove.parts[Bottom].spriteParts[BottomLeaves].destination.height;


enemyToMove.parts[0].collisionShape.height = enemyToMove.offSetY;
enemyToMove.parts[1].collisionShape.y = enemyToMove.offSetY + obstacleSpace;
enemyToMove.parts[1].collisionShape.height = GetScreenHeight() - enemyToMove.parts[1].collisionShape.y;
enemyToMove.parts[Top].collisionShape.height = enemyToMove.offSetY;
enemyToMove.parts[Bottom].collisionShape.y = enemyToMove.offSetY + obstacleSpace;
enemyToMove.parts[Bottom].collisionShape.height = GetScreenHeight() - enemyToMove.parts[1].collisionShape.y;

enemyToMove.parts[0].trunkRepetitions = static_cast<int>(enemyToMove.parts[0].collisionShape.height) / trunk.texture.height + 1;
enemyToMove.parts[1].trunkRepetitions = static_cast<int>(enemyToMove.parts[1].collisionShape.height) / trunk.texture.height + 1;
enemyToMove.parts[Top].trunkRepetitions = static_cast<int>(enemyToMove.parts[Top].collisionShape.height) / trunk.texture.height + 1;
enemyToMove.parts[Bottom].trunkRepetitions = static_cast<int>(enemyToMove.parts[1].collisionShape.height) / trunk.texture.height + 1;
}

}
Expand Down
3 changes: 2 additions & 1 deletion src/Objects/Obstacle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Obstacle
float originX;
float offSetY;
float finishX;
int dir;
bool addedScore;
bool movesVertical;
Color color;
Expand All @@ -33,7 +34,7 @@ namespace Obstacle

void Load();

void Init(Obstacle& obstacle, float originX, float speed, Color color);
void Init(Obstacle& obstacle, float originX, int dir, Color color);
void Update(Obstacle& obstacle);
void Draw(Obstacle obstacle);
void SetObstacle(Obstacle& obstacle);
Expand Down
4 changes: 2 additions & 2 deletions src/Scenes/Gameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ namespace Gameplay
PlayerNS::Init(player1, Player::player1Color, KEY_SPACE, Player::player1Num, static_cast<float>(GetScreenWidth() / 4));
PlayerNS::Init(player2, Player::player2Color, KEY_UP, Player::player2Num, static_cast<float>(GetScreenWidth() - GetScreenWidth() /4));

ObstacleNS::Init(obstacle, static_cast<float>(GetScreenWidth()), 500.0f, WHITE);
ObstacleNS::Init(obstacle2, 0, -400.0f, ColorManager::GetColor(ColorManager::Red));
ObstacleNS::Init(obstacle, static_cast<float>(GetScreenWidth()), 1, WHITE);
ObstacleNS::Init(obstacle2, 0, -1, ColorManager::GetColor(ColorManager::Red));
}

void Load()
Expand Down

0 comments on commit bff0452

Please sign in to comment.