Repository navigation
Expand file tree
/
Copy pathHandlePaddle.cpp
More file actions
126 lines (90 loc) · 3.18 KB
/
Copy pathHandlePaddle.cpp
File metadata and controls
126 lines (90 loc) · 3.18 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include "HandlePaddle.h"
#include "cPaddle.h"
#include "cBall.h"
#include "cBound.h"
#include "constants.h"
#include <AstuSuite2D.h>
#include <AstuECS.h>
using namespace astu::suite2d;
using namespace astu2d;
using namespace astu;
using namespace std;
const EntityFamily HandlePaddle::FAMILY = EntityFamily::Create<cPaddle>();
HandlePaddle::HandlePaddle(int updatePriority)
:BaseService("Paddle HandlerService")
,IteratingEntitySystem(FAMILY, updatePriority)
{
}
void HandlePaddle::OnStartup() {
auto& imps = ASTU_SERVICE(InputMappingService);
moveAxis = imps.BindAxis("Move");
}
void HandlePaddle::OnShutdown() {
auto& imps = ASTU_SERVICE(InputMappingService);
imps.RemoveAxisBinding(moveAxis);
moveAxis = nullptr;
}
void HandlePaddle::ProcessEntity(Entity &entity){
auto& pos = entity.GetComponent<CPose>();
auto& pad = entity.GetComponent<cPaddle>();
if( pad.currVel > 0 && moveAxis->GetValue() > 0
|| pad.currVel < 0 && moveAxis->GetValue() < 0){
pad.currVel += PADDLE_ACC*moveAxis->GetValue();
}
else{
pad.currVel += PADDLE_ACC*moveAxis->GetValue()* pad.controlMult;
}
//constrain the currVel between negative and positive max speed
pad.currVel = pad.currVel < -MAX_PADDLE_VEL? -MAX_PADDLE_VEL : pad.currVel;
pad.currVel = pad.currVel > MAX_PADDLE_VEL? MAX_PADDLE_VEL : pad.currVel;
if(pad.controlMult <= MAX_NOT_CONTROL_MULT){
pad.controlMult*= CONTROL_UP_MULT;
}
else{
pad.controlMult = 1;
}
//move the paddle
float currX = pos.transform.GetTranslationX();
//if the paddle hits the wall, calc the vel into the opposite direction and set the acc multiplier to the lower value
if(currX + pad.currVel > WORLD_PADDLE_WIDTH){
float antiX = pad.currVel -(WORLD_PADDLE_WIDTH - currX);
currX -= antiX;
pad.currVel = -pad.currVel;
pad.controlMult = MIN_NOT_CONTROL_MULT;
}
else if(currX + pad.currVel < -WORLD_PADDLE_WIDTH){
float antiX = pad.currVel -(-WORLD_PADDLE_WIDTH - currX);
currX -= antiX;
pad.currVel = -pad.currVel;
pad.controlMult = MIN_NOT_CONTROL_MULT;
}
else {
currX += pad.currVel;
}
pos.transform.SetTranslationX(currX);
//slow down the paddle over time and stop it if it becomes too slow
pad.currVel*=PADDLE_DAMPENING;
if(pad.currVel < MIN_PADDLE_VEL && pad.currVel > 0) {
pad.currVel = 0;
}
if(pad.currVel > -MIN_PADDLE_VEL && pad.currVel < 0){
pad.currVel = 0;
}
}
void HandlePaddle::HandleBallColl(astu::Entity& Ball, astu::Entity& Paddle){
auto& ballB = Ball.GetComponent<CBody>();
auto& pad = Paddle.GetComponent<cPaddle>();
Vector2f lvel = ballB.GetLinearVelocity();
lvel.x += pad.currVel * PADDLE_HIT_MULTIPLIER;
ballB.SetLinearVelocity(lvel);
}
bool HandlePaddle::OnCollision(astu::Entity& entityA, astu::Entity& entityB){
if(entityA.HasComponent<cBall>()&& entityB.HasComponent<cPaddle>()){
HandleBallColl(entityA, entityB);
}
if(entityB.HasComponent<cBall>()&& entityA.HasComponent<cPaddle>()){
HandleBallColl(entityB, entityA);
}
return 0;
}