forked from imoital/CastleBlast
/
Projectile.cpp
158 lines (135 loc) · 3.53 KB
/
Projectile.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
158
#include "Projectile.h"
#include "GameManager.h"
#include "Wind.h"
namespace CastleBlast {
Projectile::Projectile(GameManager* gm) : Collidable(0.7, 0.7, 0.7)
{
_size = cg::Vector3d(5, 15, 3);
_position = cg::Vector3d(10.0, 10.0, 10.0);
_debug = false;
_start = false;
_front.set(2,0,0);
_up.set(0,2,0);
_right.set(0,0,2);
_force = 0;
_gameManager=gm;
_wind = (Wind*) _gameManager->get("WIND");
_windForce = _wind->getWind();
}
Projectile::~Projectile(void)
{}
void Projectile::init()
{
_isOtherPlayer = false;
}
void Projectile::draw()
{
cg::Vector3d min = _position - _size;
cg::Vector3d max = _position + _size;
GLfloat mat_emission[] = {0.0f,0.0f,0.0f,1.0f};
GLfloat mat_ambient[] = {0.25f,0.25f,0.25f,1.0f};
GLfloat mat_diffuse[] = {0.4f,0.4f,0.4f,1.0f};
GLfloat mat_specular[] = {0.774f,0.774f,0.774f,1.0f};
GLfloat mat_shininess[] = {76.8f};
glPushMatrix();
{
glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
if (_start && _debug)
drawBoundingBox();
glTranslated(_position[0], _position[1], _position[2]);
if (_start) {
if(_debug)
debugDrawAxis();
glutSolidSphere(0.7, 16, 16);
}
}
glPopMatrix();
}
void Projectile::debugDrawAxis()
{
if (_debug){
glDisable(GL_LIGHTING);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINES);
{
glVertex3d(0.0,0.0,0.0);
glVertex3d(_front[0],_front[1],_front[2]);
}
glEnd();
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINES);
{
glVertex3d(0.0,0.0,0.0);
glVertex3d(_up[0],_up[1],_up[2]);
}
glEnd();
glColor3f(0.0f,0.0f,1.0f);
glBegin(GL_LINES);
{
glVertex3d(0.0,0.0,0.0);
glVertex3d(_right[0],_right[1],_right[2]);
}
glEnd();
glEnable(GL_LIGHTING);
}
}
void Projectile::debugToggle()
{
_debug = !_debug;
}
void Projectile::update(cg::Vector3d position, cg::Vector3d velocity, double force, unsigned long elapsed_millis)
{
_force = force;
//_wind->setWind();
//std::cout << _wind->getWind() << std::endl;
//std::cout << _windForce << std::endl;
if (!_start){
_position = position;
_position[1] = _position[1]+3;
_velocity = velocity;
_velocity[1] = _velocity[1]+0.5;
_velocity = _velocity*_force;
}
else {
double time = (elapsed_millis / 500.0);
_velocity[1] += -G*time;
_velocity[0] += _windForce[0]*time;
//_velocity[2] += _windForce[2]*time;
_position[1] += _velocity[1] * time - (G*time*time)/2;
_position[0] += _velocity[0]*time + _windForce[0]*time*time/2;
_position[2] += _velocity[2]*time; //+ _windForce[2]*time*time/2;
if (notify(_position)) {
std::cout << _windForce << std::endl;
_start = false;
_gameManager->changePlayer();
_windForce = _wind->setWind();
}
}
}
void Projectile::start()
{
_start = true;
}
bool Projectile::isCollision(CastleBlast::Collidable *obj)
{
Collidable::boundaries otherBoundaries = obj->getBoundaries();
if ((_boundes.x_min >= otherBoundaries.x_min) &&
(_boundes.x_max <= otherBoundaries.x_max) &&
(_boundes.y_min >= otherBoundaries.y_min) &&
(_boundes.y_max <= otherBoundaries.y_max) &&
(_boundes.z_min >= otherBoundaries.z_min) &&
(_boundes.z_max <= otherBoundaries.z_max)) {
_start = false;
return true;
}
else return false;
}
bool Projectile::hasStart()
{
return _start;
}
}