-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIControlSystem.cpp
87 lines (73 loc) · 1.92 KB
/
AIControlSystem.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* The AIControlSystem facilitates the control of an AI pong paddle
*
* Author: Skylar Payne
* Date: 8/22/2013
* File: AIControlSystem.h
**/
#include "AIControlSystem.h"
#include "Entity.h"
#include "IListener.h"
#include "PositionComponent.h"
#include <cstring>
/**
* @brief AIControlSystem::Update Moves ai paddles closer to the ball
* @param dt the time splice since last frame
*/
void AIControlSystem::Update(unsigned int dt)
{
_Accumulator += dt;
if(_Accumulator < 30000)
{
return;
}
_Accumulator -= 30000;
std::set<unsigned int>::iterator it;
for(it = _EntitiesToUpdate.begin(); it != _EntitiesToUpdate.end(); ++it)
{
float ease = 0.8f;
Entity* e = this->GetEntity(*it);
Entity* b = this->GetEntity(_BallID);
sf::Vector2f ballPos = b->GetComponent<PositionComponent>("Position")->GetPosition();
sf::Vector2f aiPos = e->GetComponent<PositionComponent>("Position")->GetPosition();
sf::Vector2f vel;
float yDis = ballPos.y - aiPos.y;
vel.y = yDis * ease;
if(yDis < -5)
{
vel.y = -5;
}
else if(yDis > 5)
{
vel.y = 5;
}
if(vel.y != 0.0f)
{
PushEntityMessage msg;
msg.ID = *it;
msg.newVelocity = vel;
Emit<PushEntityMessage>(msg);
}
}
}
/**
* @brief AIControlSystem::ValidateEntity inserts all ai paddles and takes note of the ball when created
* @param ID the ID of the potential ai paddle
* @return true if the entity is an AI, false otherwise
*/
bool AIControlSystem::ValidateEntity(unsigned int ID)
{
Entity* e = this->GetEntity(ID);
if(e)
{
if(strcmp(e->GetTag(), "Ball") == 0)
{
_BallID = ID;
}
else if(strcmp(e->GetTag(), "AI") == 0)
{
return true;
}
}
return false;
}