Skip to content

Commit

Permalink
Add the AI player
Browse files Browse the repository at this point in the history
  • Loading branch information
mel-mouk committed Jul 17, 2021
1 parent 6f99f4f commit 53a9019
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
17 changes: 8 additions & 9 deletions src/objects/entities/ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ void Ball::update(float timeElapsed) {
velocityY *= -1;
}

// Temporary handling bounce with the right wall
if (getRight() + velocityX >= _constraints.left + _constraints.width) {
_angle = (180 - _angle) % 360;
velocityX *= -1;
}

// Handle loss condition on the players side
if (getLeft() + velocityX <= _constraints.left) {
// Handle loss condition
if (getLeft() + velocityX <= _constraints.left || getRight() + velocityX >= _constraints.left + _constraints.width) {
isOut = true;
}

Expand All @@ -45,7 +39,12 @@ void Ball::collideWith(VisibleObject *target) {
float maxDiff = target->getBoundingRect().height;

float normalizedDiff = distDiff / maxDiff;
_angle = (int)(normalizedDiff * 90); // Between -45 and 45 degree

if (target->getPosition().x < Pang::SCREEN_WIDTH / 2) {
_angle = (int)(normalizedDiff * 90); // Between -45 and 45 degree
} else {
_angle = 180 - (int)(normalizedDiff * 90);
}
_speed += 100;
if (_speed > _maxSpeed) _speed = _maxSpeed;
}
18 changes: 18 additions & 0 deletions src/objects/entities/paddle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Paddle::Paddle(float constraintTop, float constraintBottom) : VisibleObject("../
}

void Paddle::handleInput(sf::Event *event) {
if (isAI) return;

if (event->type == sf::Event::KeyPressed) {
if (event->key.code == sf::Keyboard::Up) {
_direction = DIRECTION_UP;
Expand All @@ -19,6 +21,8 @@ void Paddle::handleInput(sf::Event *event) {
}

void Paddle::update(float timeElapsed) {
runAI();

float velocity = 0.0f;

if (_direction == DIRECTION_UP) {
Expand All @@ -35,4 +39,18 @@ void Paddle::update(float timeElapsed) {
} else if (pos.y + getBoundingRect().height > _constraintBottom) {
setPosition(pos.x, _constraintBottom - getBoundingRect().height);
}
}

void Paddle::runAI() {
if (!isAI) return;
Ball *ball = dynamic_cast<Ball*>(Pang::getState()->getObjectManager()->get("ball"));

float centerY = (getTop() + getBottom()) / 2;
if (ball->getBottom() > getBottom()) {
_direction = DIRECTION_DOWN;
} else if (ball->getTop() < getTop()) {
_direction = DIRECTION_UP;
} else {
_direction = DIRECTION_NONE;
}
}
5 changes: 5 additions & 0 deletions src/objects/entities/paddle.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#ifndef PANG_PADDLE_H
#define PANG_PADDLE_H

#include "../../pang.h"
#include "../visible-object.h"
#include "ball.h"

class Paddle: public VisibleObject {
public:
Paddle(float constraintTop, float constraintBottom);
void handleInput(sf::Event *event);
void update(float timeElapsed);
void runAI();

bool isAI;

private:
enum Direction { DIRECTION_NONE, DIRECTION_UP, DIRECTION_DOWN };
Expand Down
15 changes: 10 additions & 5 deletions src/pang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sf::RenderWindow Pang::_window;
sf::Clock Pang::_clock;
Pang::State Pang::_state = Uninitialized;
std::map<Pang::State, GameState *> Pang::_stateInstances;
GameState *Pang::_currentState;

void Pang::start() {
if (_state != Uninitialized) return;
Expand Down Expand Up @@ -38,27 +39,31 @@ void Pang::gameLoop() {
_window.clear(sf::Color(255, 255, 255));

// Because we don't want to change our state during a frame
GameState *currentState = _stateInstances[_state];
_currentState = _stateInstances[_state];

// Handle input
sf::Event event;
while (_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) { _state = Exiting; }
currentState->handleInput(&event);
_currentState->handleInput(&event);
}

// Update our entities
currentState->update(timeElapsed);
_currentState->update(timeElapsed);

// Draw our new entities
currentState->draw(&_window);
_currentState->draw(&_window);

_window.display();

currentState->endLoopLogic();
_currentState->endLoopLogic();
}
}

void Pang::setState(Pang::State s) {
_state = s;
}

GameState *Pang::getState() {
return _currentState;
}
2 changes: 2 additions & 0 deletions src/pang.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class Pang {

enum State { Uninitialized, Splashscreen, Menu, Playing, Exiting };
static void setState(State s);
static GameState *getState();

private:
static State _state;
static GameState *_currentState;
static std::map<State, GameState*> _stateInstances;
static sf::RenderWindow _window;
static sf::Clock _clock;
Expand Down
4 changes: 4 additions & 0 deletions src/states/game-state.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include "game-state.h"

GameState::~GameState() { }

VisibleObjectManager* GameState::getObjectManager() {
return &_visibleObjectManager;
}
2 changes: 2 additions & 0 deletions src/states/game-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class GameState {
virtual void draw(sf::RenderWindow *window) = 0;
virtual void endLoopLogic() {};

VisibleObjectManager* getObjectManager();

protected:
VisibleObjectManager _visibleObjectManager;
};
Expand Down
5 changes: 5 additions & 0 deletions src/states/playing-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ void PlayingState::init() {
player1->setPosition(150, 718);
_visibleObjectManager.add("player1", player1);

Paddle *player2 = new Paddle(field->getTop() + 10, field->getBottom() - 10);
player2->setPosition(1878, 718);
player2->isAI = true;
_visibleObjectManager.add("player2", player2);

sf::Rect<float> ballConstraint = sf::Rect(field->getLeft() + 10, field->getTop() + 10, field->getBoundingRect().width - 20, field->getBoundingRect().height - 20);
Ball *ball = new Ball(ballConstraint);
float centerX = field->getLeft() + field->getBoundingRect().width / 2;
Expand Down

0 comments on commit 53a9019

Please sign in to comment.