Skip to content

Commit

Permalink
🎱 polishing balls
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Apr 24, 2023
1 parent 6c64a92 commit 625b70e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
Binary file added assets/ball_medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/ball_medium.xcf
Binary file not shown.
34 changes: 19 additions & 15 deletions src/ball/ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,55 @@ Ball::Ball(int sWidth, int sHeight, int x, int y, int instanceId) {
screenWidth = sWidth;
screenHeight = sHeight;
ballPos = {(float)x, (float)y};
ballInitialPos = {(float)x, (float)y};
id = instanceId;
}

void Ball::Draw() {
DrawTextureRec(ball, ballRec, ballPos, WHITE);
}

void Ball::Move() {
void Ball::Move(float deltaTime) {
// Movement
ballPos.x += ballSpeed.x;
ballPos.y += ballSpeed.y;
ballPos.x += ballSpeed.x * deltaTime;
ballPos.y += ballSpeed.y * deltaTime;

// Key pressing
if(id == 0){
if(IsKeyDown(KEY_W)) {
ballSpeed.y -= 0.2f;
ballSpeed.y -= ballAcceleration;
} else if(IsKeyDown(KEY_S)) {
ballSpeed.y += 0.2f;
ballSpeed.y += ballAcceleration;
} else if(IsKeyDown(KEY_A)) {
ballSpeed.x -= 0.2f;
ballSpeed.x -= ballAcceleration;
} else if(IsKeyDown(KEY_D)) {
ballSpeed.x += 0.2f;
ballSpeed.x += ballAcceleration;
}
} else if(id == 1){
if(IsKeyDown(KEY_UP)) {
ballSpeed.y -= 0.2f;
ballSpeed.y -= ballAcceleration;
} else if(IsKeyDown(KEY_DOWN)) {
ballSpeed.y += 0.2f;
ballSpeed.y += ballAcceleration;
} else if(IsKeyDown(KEY_LEFT)) {
ballSpeed.x -= 0.2f;
ballSpeed.x -= ballAcceleration;
} else if(IsKeyDown(KEY_RIGHT)) {
ballSpeed.x += 0.2f;
ballSpeed.x += ballAcceleration;
}
}
}

void Ball::Collision() {
void Ball::Collision(Ball ball) {
// Collisions againt walls
float blockSpeed = 2.f;

if(GetY() >= 110.f && GetY() <= screenHeight - 125.f) {
canCollide = false;
} else { canCollide = true; }

if(GetX() >= screenWidth - 30 && canCollide) {
if(GetX() >= screenWidth - GetWidth() - GetWidth()/2.f && canCollide) {
SetSpeed(Vector2{GetSpeed().x - blockSpeed * GetSpeed().x, GetSpeed().y});
}
if(GetY() >= screenHeight - 30 && canCollide) {
} // change to Width if u have any error
if(GetY() >= screenHeight - GetHeight() - GetHeight()/2.f && canCollide) {
SetSpeed(Vector2{GetSpeed().x, GetSpeed().y - blockSpeed * GetSpeed().y});
}
if(GetY() <= 15 && canCollide) {
Expand All @@ -67,4 +69,6 @@ void Ball::Collision() {

if(ballSpeed.x > 0.f && IsKeyUp(KEY_A) && IsKeyUp(KEY_D)) { ballSpeed.x -= 0.01f * ballSpeed.x; }
if(ballSpeed.y > 0.f && IsKeyUp(KEY_W) && IsKeyUp(KEY_S)) { ballSpeed.y -= 0.01f * ballSpeed.y; }

// Collisons again ball
}
16 changes: 9 additions & 7 deletions src/ball/ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@ class Ball {
public:
Ball(int sWidth, int sHeight, int x, int y, int id);
void Draw();
void Move();
void Collision();

void Move(float deltaTime);
void Collision(Ball ball);

Vector2 GetInitialPosition();
Rectangle GetCollisionRec();
float GetWidth();
float GetHeight();
float GetX();
float GetY();

Vector2 GetSpeed();
void SetSpeed(Vector2 speed);
void SetPosition(Vector2 position);
void SetAcceleration(float acceleration);

private:
Texture2D ball = LoadTexture("assets/ball.png");
Texture2D ball = LoadTexture("assets/ball_medium.png");
int id = 0;
int screenWidth = 512;
int screenHeight = 512;
bool canCollide = true;
int ballWidth = ball.width;
int ballHeight = ball.height;
Vector2 ballInitialPos { 50, 50 };
Vector2 ballSpeed { 0.f, 0.f };
float ballAcceleration = 10.0f;
Vector2 ballPos { 50, 50 };
Rectangle ballRec { 0, 0, (float)ballWidth, (float)ballHeight };
Rectangle ballRec { 0, 0, GetWidth(), GetHeight() };
};
#endif
12 changes: 12 additions & 0 deletions src/ball/getter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ float Ball::GetY() {
return ballPos.y;
}

float Ball::GetWidth() {
return ball.width;
}

float Ball::GetHeight() {
return ball.height;
}

Vector2 Ball::GetSpeed() {
return ballSpeed;
}

Vector2 Ball::GetInitialPosition() {
return ballInitialPos;
}

Rectangle Ball::GetCollisionRec() {
return Rectangle{GetX(), GetY(), GetWidth(), GetHeight()};
}
4 changes: 4 additions & 0 deletions src/ball/setter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ void Ball::SetPosition(Vector2 position) {
void Ball::SetSpeed(Vector2 speed) {
ballSpeed = {speed.x, speed.y};
}

void Ball::SetAcceleration(float acceleration) {
ballAcceleration = acceleration;
}
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ int main() {

Map map{};
Ball ball_0{screenWidth, screenHeight, 50, 50, 0};
Ball ball_1{screenWidth, screenHeight, 250, 250, 1};
Ball ball_1{screenWidth, screenHeight, screenWidth - 75, screenHeight - 75, 1};

SetTargetFPS(60);

ClearBackground(BLACK);

while (!WindowShouldClose()) {

ball_0.Collision();
ball_1.Collision();
ball_0.Collision(ball_1);
ball_1.Collision(ball_0);

BeginDrawing();

map.Draw();
ball_0.Draw();
ball_0.Move();
ball_0.Move(GetFrameTime());

ball_1.Draw();
ball_1.Move();
ball_1.Move(GetFrameTime());

EndDrawing();
}
Expand Down

0 comments on commit 625b70e

Please sign in to comment.