-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIPaddle.cpp
61 lines (46 loc) · 1.25 KB
/
AIPaddle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "StdAfx.h"
#include "AIPaddle.h"
#include "Game.h"
#include "GameBall.h"
AIPaddle::AIPaddle() :
_velocity(0),
_maxVelocity(600.0f)
{
Load("images/paddle.png");
assert(IsLoaded());
GetSprite().setOrigin(GetSprite().getLocalBounds().width /2, GetSprite().getLocalBounds().height / 2);
}
AIPaddle::~AIPaddle()
{
}
void AIPaddle::Draw(sf::RenderWindow & rw)
{
VisibleGameObject::Draw(rw);
}
float AIPaddle::GetVelocity() const
{
return _velocity;
}
void AIPaddle::Update(float elapsedTime)
{
const GameBall* gameBall = static_cast<GameBall*>
(Game::GetGameObjectManager().Get("Ball"));
sf::Vector2f ballPosition = gameBall->GetPosition();
if(GetPosition().x -20 < ballPosition.x)
_velocity += 15.0f;
else if(GetPosition().x +20 > ballPosition.x)
_velocity -= 10.0f;
else
_velocity = 0.0f;
if(_velocity > _maxVelocity)
_velocity = _maxVelocity;
if(_velocity < -_maxVelocity)
_velocity = -_maxVelocity;
sf::Vector2f pos = this->GetPosition();
if(pos.x <= GetSprite().getLocalBounds().width/2
|| pos.x >= (Game::SCREEN_WIDTH - GetSprite().getLocalBounds().width/2))
{
_velocity = -_velocity; // Bounce by current velocity in opposite direction
}
GetSprite().move(_velocity * elapsedTime, 0);
}