-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaddle.cpp
More file actions
61 lines (58 loc) · 1.63 KB
/
Paddle.cpp
File metadata and controls
61 lines (58 loc) · 1.63 KB
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
/*
* File: Paddle.cpp
* Author: iMer
*
* Created on 21. März 2015, 17:10
*/
#include "Paddle.hpp"
#include "Engine/util/Random.hpp"
#include "Engine/util/misc.hpp"
#include "Engine/Scene.hpp"
#include "Engine/Game.hpp"
Paddle::Paddle(engine::Scene* scene): SpriteNode(scene), m_ai(false), m_rate(5.0) {
}
void Paddle::OnUpdate(sf::Time interval) {
if (m_ai) {
UpdateAI(interval);
} else {
auto pos = m_scene->GetGame()->GetMousePosition();
SetPosition(GetPosition().x, engine::util::minmax<float>(0, pos.y , m_scene->GetGame()->GetWindow()->getSize().y));
}
}
Paddle::~Paddle() {
}
void Paddle::UpdateAI(sf::Time interval) {
// shitty ai
auto pos = GetPosition();
auto balls = m_scene->GetChildByID("balls");
if (!balls) {
// hyperventilate
auto rand = engine::util::RandomFloat(-15,15);
SetPosition(pos.x, engine::util::minmax<float>(0, pos.y + rand(), m_scene->GetGame()->GetWindow()->getSize().y));
return;
}
// get closest ball
Node* closest = nullptr;
float dist = std::numeric_limits<float>::infinity();
for (auto ball : balls->GetChildren()) {
auto bpos = ball->GetGlobalPosition();
auto xvel = ball->GetBody()->GetLinearVelocity().x;
if (abs((pos.x - bpos.x)/xvel) < abs(dist)) {
if ((pos.x - bpos.x) < 0 && xvel < 0
|| (pos.x - bpos.x) > 0 && xvel > 0){
closest = ball;
dist = abs((pos.x - bpos.x)/xvel);
}
}
}
if (closest) {
SetPosition(pos.x, pos.y + engine::util::minmax(-m_rate, closest->GetPosition().y - pos.y, m_rate));
}
}
bool Paddle::initialize(Json::Value& root){
if (!engine::SpriteNode::initialize(root)){
return false;
}
m_ai = root.get("ai", false).asBool();
return true;
}