-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cpp
157 lines (130 loc) · 2.74 KB
/
Enemy.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "Enemy.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include "Game.h"
Enemy::Enemy(void)
{
type = BALL_BAD;
radius = 0.38f;
travel_length = 0;
start_x = 0;
start_y = 0;
start_speed_x = 0;
start_speed_y = 0;
reflection_x = 1.0f;
reflection_y = 1.0f;
kollision_x = true;
turn_on_cliff = false;
}
Enemy::~Enemy(void)
{
}
void Enemy::doKollision(Ball& other){
if(other.getClass() == BALL_PLAYER){
if(other.getSpeedY() < getSpeedY()){
death();
if(other.getX() == x){
other.setX(x + 0.001f);
}
GLfloat betrag = sqrtf(powf(other.getSpeedX(), 2) + powf(other.getSpeedY(), 2));
GLfloat alpha = atanf((other.getY() - y)/(other.getX() - x));
if(other.getX() - x < 0){
alpha += M_PI;
}
if(other.getSpeedX() == 0){
other.setSpeedX(0.001f);
}
GLfloat beta = atanf(-other.getSpeedY()/other.getSpeedX());
if(other.getSpeedX() < 0){
beta += M_PI;
}
beta = alpha - (beta - alpha);
other.setSpeedX(cos(beta) * betrag);
other.setSpeedY(sin(beta) * betrag);
} else {
other.death();
}
}
}
void Enemy::setX(GLfloat x){
x_last = this->x;
this->x = x;
rotation -= 360.0f*(x-x_last)/(2.0f* (float)M_PI*radius);
if(rotation >360.0f){
rotation -= 360.0f;
}
if(travel_length != 0){
if(this->x > start_x + travel_length){
speed_x = abs(speed_x);
} else if(this->x < start_x){
speed_x = -abs(speed_x);
}
}
}
void Enemy::move(GLfloat time){
setX(x + (time*speed_x));
setY(y + (time*speed_y));
if(onFloor && x < onFloor_begin){
if(turn_on_cliff){
speed_x = abs(speed_x);
} else {
onFloor = false;
}
} else if(onFloor && x > onFloor_ende){
if(turn_on_cliff){
speed_x = -abs(speed_x);
} else {
onFloor = false;
}
}
}
void Enemy::reset(){
active = true;
x = start_x;
y = start_y;
speed_x = start_speed_x;
speed_y = start_speed_y;
setRotation(0);
}
void Enemy::setTravelLength(GLfloat travel_lenght){
this->travel_length = travel_lenght;
}
GLfloat Enemy::getTravelLength(){
return travel_length;
}
GLfloat Enemy::getStartX(){
return start_x;
}
GLfloat Enemy::getStartSpeedX(){
return start_speed_x;
}
GLfloat Enemy::getStartY(){
return start_y;
}
GLfloat Enemy::getStartSpeedY(){
return start_speed_y;
}
void Enemy::setStartX(GLfloat x){
this->start_x = x;
}
void Enemy::setStartSpeedX(GLfloat speed_x){
this->start_speed_x = speed_x;
}
void Enemy::setStartY(GLfloat y){
this->start_y = y;
}
void Enemy::setStartSpeedY(GLfloat speed_y){
this->start_speed_y = speed_y;
}
void Enemy::setKollisionXEnable(bool kollision_x){
this->kollision_x = kollision_x;
}
bool Enemy::isKolliosionXEnable(){
return kollision_x;
}
bool Enemy::turnsOnCliff(){
return turn_on_cliff;
}
void Enemy::setTurnOnCliff(bool turn_on_cliff){
this->turn_on_cliff = turn_on_cliff;
}