5 #include "shotmanager.h"
6 #include "usereventtype.h"
9 Shooter::Shooter(Game
* agame
, Ponto pos
, Ponto speed
) {
11 setPosition(pos
.x
, pos
.y
);
12 setSpeed(speed
.x
, speed
.y
);
14 game
->gravityManager
->subscribe(this);
20 void Shooter::equip(Weapon
* aweapon
) {
21 //desequipar a anterior aqui mais tarde
25 static void allowFireFunc(void* param
) {
26 ((Shooter
*)param
)->allowFire();
29 static Uint32
allowFireCallback(Uint32 interval
, void *param
) {
31 SDL_UserEvent userevent
;
32 userevent
.type
= SDL_USEREVENT
;
33 userevent
.code
= FUNCTIONCALL
;
34 userevent
.data1
= (void*)allowFireFunc
;
35 userevent
.data2
= (void*)param
;
37 event
.type
= SDL_USEREVENT
;
38 event
.user
= userevent
;
40 SDL_PushEvent(&event
);
45 void Shooter::allowFire() {
51 void Shooter::fire() {
52 if (!canfire
|| weapon
== NULL
)
54 SDL_AddTimer(weapon
->fireRate
*game
->rate
,allowFireCallback
,this);
56 Ponto tiplinha
= weapon
->getTip();
57 double angle
= getAngle();
58 double cosAngle
= cos(angle
);
59 double sinAngle
= sin(angle
);
61 tip
.x
= (cosAngle
*tiplinha
.x
+sinAngle
*tiplinha
.y
);
62 tip
.y
= -(sinAngle
*tiplinha
.x
-cosAngle
*tiplinha
.y
);
63 tip
= tip
+ pescoco() + getPosition();
65 weapon
->fire(tip
,angle
);
69 Ponto
Shooter::leftFeet() {
74 Ponto
Shooter::rightFeet() {
79 Ponto
Shooter::leftArm() {
85 return weapon
->getLeftHand();
88 Ponto
Shooter::rightArm() {
94 return weapon
->getRightHand();
97 Ponto
Shooter::cintura() {
102 Ponto
Shooter::pescoco() {
107 double areaTriangle(double a
, double b
, double c
) {
108 if (a
< b
) std::swap(a
,b
);
109 if (a
< c
) std::swap(a
,c
);
110 if (b
< c
) std::swap(b
,c
);
113 return sqrt((a
+(b
+c
))*(c
-(a
-b
))*(c
+(a
-b
))*(a
+(b
-c
)))/4.0;
116 //retorna cotovelo em relacao ao ombro, usa matematica levemente pesada, cuidado ao mexer
117 Ponto
Shooter::getCotovelo(Ponto ombro
, Ponto hand
) {
118 double a
= tamanhoBraco();
119 double b
= tamanhoAntebraco();
120 double d
= distance(ombro
,hand
);
121 double area
= areaTriangle(a
,b
,d
);
122 double ylinha
= 2.0*area
/d
;
123 double xlinha
= sqrt(a
*a
-ylinha
*ylinha
);
124 double sinTheta
= (hand
.y
-ombro
.y
)/d
;
125 double cosTheta
= (hand
.x
-ombro
.x
)/d
;
127 ret
.x
= (cosTheta
*xlinha
-sinTheta
*ylinha
);
128 ret
.y
= (sinTheta
*xlinha
+cosTheta
*ylinha
);
132 //cuidado, distancia da arma nao pode exceder tamanhoBraco+tamanhoAntebraco
133 double Shooter::tamanhoBraco() {
137 double Shooter::tamanhoAntebraco() {
141 double Shooter::getAngle() {
142 Ponto gameneck
= pescoco() + getPosition();
143 double hyp
= sqrt((aim
.x
-gameneck
.x
)*(aim
.x
-gameneck
.x
) +
144 (aim
.y
-gameneck
.y
)*(aim
.y
-gameneck
.y
));
145 if (aim
.y
<= gameneck
.y
)
146 return -acos((-aim
.x
+gameneck
.x
)/hyp
);
148 return acos((-aim
.x
+gameneck
.x
)/hyp
);
151 void Shooter::desenha() {
153 glTranslatef(getX(),getY(),0);
154 Ponto leftfeet
= leftFeet();
155 Ponto rightfeet
= rightFeet();
156 Ponto hips
= cintura();
157 Ponto leftarm
= leftArm();
158 Ponto rightarm
= rightArm();
159 Ponto neck
= pescoco();
161 glVertex3f(hips
.x
,hips
.y
,0);
162 glVertex3f(neck
.x
,neck
.y
,0);
164 glVertex3f(leftfeet
.x
,leftfeet
.y
,0);
165 glVertex3f(hips
.x
,hips
.y
,0);
167 glVertex3f(rightfeet
.x
,rightfeet
.y
,0);
168 glVertex3f(hips
.x
,hips
.y
,0);
170 Ponto rightelbow
= getCotovelo(neck
,rightarm
+neck
);
171 Ponto leftelbow
= getCotovelo(neck
,leftarm
+neck
);
173 glTranslatef(neck
.x
,neck
.y
,0);
174 glRotatef(getAngle()*180.0/PI
,0,0,-1);
177 glVertex3f(leftelbow
.x
,leftelbow
.y
,-1);
178 glVertex3f(leftelbow
.x
,leftelbow
.y
,-1);
179 glVertex3f(leftarm
.x
,leftarm
.y
,-1);
182 glVertex3f(rightelbow
.x
,rightelbow
.y
,1);
183 glVertex3f(rightelbow
.x
,rightelbow
.y
,1);
184 glVertex3f(rightarm
.x
,rightarm
.y
,1);
186 if (weapon
!= NULL
) weapon
->desenha();
189 glTranslatef(neck
.x
,neck
.y
-10,0);
196 Linha
Shooter::getBaseLine() {
197 return Linha(leftFeet(), rightFeet());
200 Polygon
Shooter::getCollision() {
202 Linha
a(leftFeet(),rightFeet());
203 Linha
b(rightFeet(),cintura());
204 Linha
c(cintura(),rightArm());
205 Linha
d(rightArm(),pescoco());
206 Linha
e(pescoco(),leftArm());
207 Linha
f(leftArm(),cintura());
208 Linha
g(cintura(),leftFeet());
219 void Shooter::setAim(double x
, double y
) {