5 #include "shotmanager.h"
7 Shooter::Shooter(Game
* agame
, Ponto pos
, Ponto speed
) {
9 setPosition(pos
.x
, pos
.y
);
10 setSpeed(speed
.x
, speed
.y
);
12 game
->gravityManager
->subscribe(this);
18 void Shooter::equip(Weapon
* aweapon
) {
19 //desequipar a anterior aqui mais tarde
23 static void allowFireFunc(void* param
) {
24 ((Shooter
*)param
)->allowFire();
27 static Uint32
allowFireCallback(Uint32 interval
, void *param
) {
29 SDL_UserEvent userevent
;
30 userevent
.type
= SDL_USEREVENT
;
32 userevent
.data1
= (void*)allowFireFunc
;
33 userevent
.data2
= (void*)param
;
35 event
.type
= SDL_USEREVENT
;
36 event
.user
= userevent
;
38 SDL_PushEvent(&event
);
43 void Shooter::allowFire() {
49 void Shooter::fire() {
50 if (!canfire
|| weapon
== NULL
)
52 SDL_AddTimer(weapon
->fireRate
,allowFireCallback
,this);
54 Ponto tiplinha
= weapon
->getTip();
55 double angle
= getAngle();
56 double cosAngle
= cos(angle
);
57 double sinAngle
= sin(angle
);
59 tip
.x
= (cosAngle
*tiplinha
.x
-sinAngle
*tiplinha
.y
);
60 tip
.y
= -(sinAngle
*tiplinha
.x
+cosAngle
*tiplinha
.y
);
61 tip
= tip
+ pescoco();
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
gameaim(aim
.x
+game
->camera
.x
,aim
.y
+game
->camera
.y
);
143 Ponto
gameneck(pescoco().x
+getX(),pescoco().y
+getY());
144 double hyp
= sqrt((gameaim
.x
-gameneck
.x
)*(gameaim
.x
-gameneck
.x
) +
145 (gameaim
.y
-gameneck
.y
)*(gameaim
.y
-gameneck
.y
));
146 if (gameaim
.y
<= gameneck
.y
)
147 return -acos((-gameaim
.x
+gameneck
.x
)/hyp
);
149 return acos((-gameaim
.x
+gameneck
.x
)/hyp
);
152 void Shooter::desenha() {
154 glTranslatef(getX(),getY(),0);
155 Ponto leftfeet
= leftFeet();
156 Ponto rightfeet
= rightFeet();
157 Ponto hips
= cintura();
158 Ponto leftarm
= leftArm();
159 Ponto rightarm
= rightArm();
160 Ponto neck
= pescoco();
162 glVertex3f(hips
.x
,hips
.y
,0);
163 glVertex3f(neck
.x
,neck
.y
,0);
165 glVertex3f(leftfeet
.x
,leftfeet
.y
,0);
166 glVertex3f(hips
.x
,hips
.y
,0);
168 glVertex3f(rightfeet
.x
,rightfeet
.y
,0);
169 glVertex3f(hips
.x
,hips
.y
,0);
171 Ponto rightelbow
= getCotovelo(neck
,rightarm
+neck
);
172 Ponto leftelbow
= getCotovelo(neck
,leftarm
+neck
);
174 glTranslatef(neck
.x
,neck
.y
,0);
175 glRotatef(getAngle()*180.0/PI
,0,0,-1);
178 glVertex3f(leftelbow
.x
,leftelbow
.y
,0);
179 glVertex3f(leftelbow
.x
,leftelbow
.y
,0);
180 glVertex3f(leftarm
.x
,leftarm
.y
,0);
183 glVertex3f(rightelbow
.x
,rightelbow
.y
,0);
184 glVertex3f(rightelbow
.x
,rightelbow
.y
,0);
185 glVertex3f(rightarm
.x
,rightarm
.y
,0);
187 if (weapon
!= NULL
) weapon
->desenha();
190 glTranslatef(neck
.x
,neck
.y
-10,0);
197 Linha
Shooter::getBaseLine() {
198 Ponto leftfeet
= leftFeet();
199 Ponto rightfeet
= rightFeet();
200 Linha
ret(leftfeet
.x
+getX(),leftfeet
.y
+getY(),rightfeet
.x
+getX(),rightfeet
.y
+getY());
204 void Shooter::setAim(double x
, double y
) {