Skip to content

Commit

Permalink
🌳 design pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Apr 24, 2023
1 parent 47d97fa commit 6c64a92
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 62 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CC=g++
BINARY=bin/Game
CFLAGS= -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
OBJECTS=src/main.o src/ball/ball.o src/map/map.o
CFILES=src/main.cpp src/ball/ball.cpp src/map/map.cpp
OBJECTS=src/main.o src/ball/ball.o src/ball/setter.o src/ball/getter.o src/map/map.o
CFILES=src/main.cpp src/ball/ball.cpp src/ball/setter.cpp src/ball/getter.cpp src/map/map.cpp

$(BINARY): $(OBJECTS)
mkdir -p bin/
Expand Down
100 changes: 45 additions & 55 deletions src/ball/ball.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "ball.h"

Ball::Ball(int sWidth, int sHeight) {
Ball::Ball(int sWidth, int sHeight, int x, int y, int instanceId) {
screenWidth = sWidth;
screenHeight = sHeight;
ballPos = {(float)x, (float)y};
id = instanceId;
}

void Ball::Draw() {
Expand All @@ -15,66 +17,54 @@ void Ball::Move() {
ballPos.y += ballSpeed.y;

// Key pressing
if(IsKeyDown(KEY_W)) {
ballSpeed.y -= 0.2f;
} else if(IsKeyDown(KEY_S)) {
ballSpeed.y += 0.2f;
} else if(IsKeyDown(KEY_A)) {
ballSpeed.x -= 0.2f;
} else if(IsKeyDown(KEY_D)) {
ballSpeed.x += 0.2f;
}
}

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

void Ball::Collision() {
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(id == 0){
if(IsKeyDown(KEY_W)) {
ballSpeed.y -= 0.2f;
} else if(IsKeyDown(KEY_S)) {
ballSpeed.y += 0.2f;
} else if(IsKeyDown(KEY_A)) {
ballSpeed.x -= 0.2f;
} else if(IsKeyDown(KEY_D)) {
ballSpeed.x += 0.2f;
}
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});
} else if(id == 1){
if(IsKeyDown(KEY_UP)) {
ballSpeed.y -= 0.2f;
} else if(IsKeyDown(KEY_DOWN)) {
ballSpeed.y += 0.2f;
} else if(IsKeyDown(KEY_LEFT)) {
ballSpeed.x -= 0.2f;
} else if(IsKeyDown(KEY_RIGHT)) {
ballSpeed.x += 0.2f;
}

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; }
}
}

void Ball::SetPosition(Vector2 position) {
ballPos = {position.x, position.y};
}
void Ball::Collision() {
float blockSpeed = 2.f;

float Ball::GetX() {
return ballPos.x;
}
if(GetY() >= 110.f && GetY() <= screenHeight - 125.f) {
canCollide = false;
} else { canCollide = true; }

float Ball::GetY() {
return ballPos.y;
}
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});
}

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

Vector2 Ball::GetInitialPosition() {
return ballInitialPos;
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; }
}
7 changes: 6 additions & 1 deletion src/ball/ball.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef BALL_H
#define BALL_H

#include "raylib.h"

class Ball {
public:
Ball(int sWidth, int sHeight);
Ball(int sWidth, int sHeight, int x, int y, int id);
void Draw();
void Move();
void Collision();
Expand All @@ -18,6 +21,7 @@ class Ball {

private:
Texture2D ball = LoadTexture("assets/ball.png");
int id = 0;
int screenWidth = 512;
int screenHeight = 512;
bool canCollide = true;
Expand All @@ -28,3 +32,4 @@ class Ball {
Vector2 ballPos { 50, 50 };
Rectangle ballRec { 0, 0, (float)ballWidth, (float)ballHeight };
};
#endif
17 changes: 17 additions & 0 deletions src/ball/getter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "ball.h"

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

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

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

Vector2 Ball::GetInitialPosition() {
return ballInitialPos;
}
9 changes: 9 additions & 0 deletions src/ball/setter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "ball.h"

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

void Ball::SetSpeed(Vector2 speed) {
ballSpeed = {speed.x, speed.y};
}
13 changes: 9 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ int main() {
InitWindow(screenWidth, screenHeight, "Game");

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

SetTargetFPS(60);

ClearBackground(BLACK);

while (!WindowShouldClose()) {

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

BeginDrawing();

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

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

EndDrawing();
}
Expand Down

0 comments on commit 6c64a92

Please sign in to comment.