Skip to content

Commit

Permalink
🌹 collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Apr 24, 2023
1 parent 8aad1fa commit 9650763
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Game is a simple C++ game built using Raylib. It is a two-players game where the

To install the game, follow these steps:

Clone the repository to your local machine.<br>
Clone the repository to your local machine or download the latest game version from the [releases page](https://github.com/FelipeFTN/Game/releases).<br>
Open the project in your preferred C++ IDE.<br>
Make sure you have the Raylib library installed on your machine.<br>
Build and run the game.<br>
Expand Down
51 changes: 51 additions & 0 deletions src/ball/ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,54 @@ void Ball::Move() {
ballSpeed.x += 0.2f;
}
}

void Ball::SetSpeed(Vector2 speed) {
ballSpeed = {speed.x, speed.y};
}

void Ball::Collision() {
bool canCollide = true;
float blockSpeed = 2.f;

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

if(GetX() >= screenWidth - 30 && canCollide) {
SetSpeed(Vector2{GetSpeed().x - blockSpeed * GetSpeed().x, GetSpeed().y});
}
if(GetY() >= screenHeight - 30 && canCollide) {
SetSpeed(Vector2{GetSpeed().x, GetSpeed().y - blockSpeed * GetSpeed().y});
}
if(GetY() <= 15 && canCollide) {
SetSpeed(Vector2{GetSpeed().x, GetSpeed().y - blockSpeed * GetSpeed().y});
}
if(GetX() <= 15 && canCollide) {
SetSpeed(Vector2{GetSpeed().x - blockSpeed * GetSpeed().x, GetSpeed().y});
}

if(GetX() > screenWidth || GetX() < -20) {
SetPosition(GetInitialPosition());
SetSpeed(Vector2{0.f, 0.f});
}
}

void Ball::SetPosition(Vector2 position) {
ballPos = {position.x, position.y};
}

float Ball::GetX() {
return ballPos.x;
}

float Ball::GetY() {
return ballPos.y;
}

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

Vector2 Ball::GetInitialPosition() {
return ballInitialPos;
}
11 changes: 11 additions & 0 deletions src/ball/ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ class Ball {
Ball(int sWidth, int sHeight);
void Draw();
void Move();
void Collision();


Vector2 GetInitialPosition();
float GetX();
float GetY();

Vector2 GetSpeed();
void SetSpeed(Vector2 speed);
void SetPosition(Vector2 position);

private:
Texture2D ball = LoadTexture("assets/ball.png");
int screenWidth = 512;
int screenHeight = 512;
int ballWidth = ball.width;
int ballHeight = ball.height;
Vector2 ballInitialPos { 50, 50 };
Vector2 ballSpeed { 0.f, 0.f };
Vector2 ballPos { 50, 50 };
Rectangle ballRec { 0, 0, (float)ballWidth, (float)ballHeight };
Expand Down
16 changes: 11 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#include "raylib.h"
#include "ball/ball.h"
#include "map/map.h"
#include "raylib.h"
#include <string>
#include <iostream>

int main() {
const int screenWidth = 512;
const int screenHeight = 512;

InitWindow(screenWidth, screenHeight, "Game");

Map map{ };
Ball ball{ screenWidth, screenHeight };
Map map{};
Ball ball{screenWidth, screenHeight};

SetTargetFPS(60);

while(!WindowShouldClose()) {
ClearBackground(BLACK);

while (!WindowShouldClose()) {

ClearBackground(BLACK);
ball.Collision();

BeginDrawing();

Expand All @@ -30,3 +34,5 @@ int main() {

return 0;
}
/* DEBUGGING TEST */
/* DrawText(std::to_string(ball.GetX()).c_str(), 190, 200, 20, LIGHTGRAY); */

0 comments on commit 9650763

Please sign in to comment.