Mapa agora feito em lua :D
[Projeto-PCG.git] / player.cpp
blob1faf12c736dd9cbf8f8dd9570b03042b6ca4a070
1 #include <math.h>
2 #include "player.h"
3 #include "gravity.h"
5 Player::Player(Game* agame) {
6 weapon = NULL;
7 setPosition(40,40);
8 setSpeed(0,0);
9 game = agame;
10 game->gravityManager->subscribe(this);
13 void Player::equip(Weapon* aweapon) {
14 //desequipar a anterior aqui mais tarde
15 weapon = aweapon;
18 Ponto Player::leftFeet() {
19 Ponto pe(-10,0);
20 return pe;
23 Ponto Player::rightFeet() {
24 Ponto pe(10,0);
25 return pe;
28 Ponto Player::leftArm() {
29 if (weapon == NULL) {
30 Ponto braco(-10,-30);
31 return braco;
33 else
34 return weapon->getLeftHand();
37 Ponto Player::rightArm() {
38 if (weapon == NULL) {
39 Ponto braco(10,-30);
40 return braco;
42 else
43 return weapon->getRightHand();
46 Ponto Player::cintura() {
47 Ponto c(0,-30);
48 return c;
51 Ponto Player::pescoco() {
52 Ponto neck(0,-60);
53 return neck;
56 double areaTriangle(double a, double b, double c) {
57 if (a < b) std::swap(a,b);
58 if (a < c) std::swap(a,c);
59 if (b < c) std::swap(b,c);
60 //a >= b >= c
62 return sqrt((a+(b+c))*(c-(a-b))*(c+(a-b))*(a+(b-c)))/4.0;
65 //retorna cotovelo em relacao ao ombro, usa matematica levemente pesada, cuidado ao mexer
66 Ponto Player::getCotovelo(Ponto ombro, Ponto hand) {
67 double a = tamanhoBraco();
68 double b = tamanhoAntebraco();
69 double d = distance(ombro,hand);
70 double area = areaTriangle(a,b,d);
71 double ylinha = 2.0*area/d;
72 double xlinha = sqrt(a*a-ylinha*ylinha);
73 double sinTheta = (hand.y-ombro.y)/d;
74 double cosTheta = (hand.x-ombro.x)/d;
75 Ponto ret;
76 ret.x = (cosTheta*xlinha-sinTheta*ylinha);
77 ret.y = (sinTheta*xlinha+cosTheta*ylinha);
78 return ret;
81 //cuidado, distancia da arma nao pode exceder tamanhoBraco+tamanhoAntebraco
82 double Player::tamanhoBraco() {
83 return 20.0;
86 double Player::tamanhoAntebraco() {
87 return 13.0;
90 double Player::getAngle() {
91 Ponto gameaim(aim.x+game->camera.x,aim.y+game->camera.y);
92 Ponto gameneck(pescoco().x+getX(),pescoco().y+getY());
93 double hyp = sqrt((gameaim.x-gameneck.x)*(gameaim.x-gameneck.x) +
94 (gameaim.y-gameneck.y)*(gameaim.y-gameneck.y));
95 if (gameaim.y <= gameneck.y)
96 return -acos((-gameaim.x+gameneck.x)/hyp);
97 else
98 return acos((-gameaim.x+gameneck.x)/hyp);
101 void Player::desenha() {
102 game->desenhaMira(aim);
103 glPushMatrix();
104 glTranslatef(getX(),getY(),0);
105 Ponto leftfeet = leftFeet();
106 Ponto rightfeet = rightFeet();
107 Ponto hips = cintura();
108 Ponto leftarm = leftArm();
109 Ponto rightarm = rightArm();
110 Ponto neck = pescoco();
111 glBegin(GL_LINES);
112 glVertex3f(hips.x,hips.y,0);
113 glVertex3f(neck.x,neck.y,0);
115 glVertex3f(leftfeet.x,leftfeet.y,0);
116 glVertex3f(hips.x,hips.y,0);
118 glVertex3f(rightfeet.x,rightfeet.y,0);
119 glVertex3f(hips.x,hips.y,0);
120 glEnd();
121 Ponto rightelbow = getCotovelo(neck,rightarm+neck);
122 Ponto leftelbow = getCotovelo(neck,leftarm+neck);
123 glPushMatrix();
124 glTranslatef(neck.x,neck.y,0);
125 glRotatef(getAngle()*180.0/PI,0,0,-1);
126 glBegin(GL_LINES);
127 glVertex3f(0,0,0);
128 glVertex3f(leftelbow.x,leftelbow.y,0);
129 glVertex3f(leftelbow.x,leftelbow.y,0);
130 glVertex3f(leftarm.x,leftarm.y,0);
132 glVertex3f(0,0,0);
133 glVertex3f(rightelbow.x,rightelbow.y,0);
134 glVertex3f(rightelbow.x,rightelbow.y,0);
135 glVertex3f(rightarm.x,rightarm.y,0);
136 glEnd();
137 if (weapon != NULL) weapon->desenha();
138 glPopMatrix();
139 glPushMatrix();
140 glTranslatef(neck.x,neck.y-10,0);
141 drawCircle(10,30);
142 glPopMatrix();
143 glPopMatrix();
147 Linha Player::getBaseLine() {
148 Ponto leftfeet = leftFeet();
149 Ponto rightfeet = rightFeet();
150 Linha ret(leftfeet.x+getX(),leftfeet.y+getY(),rightfeet.x+getX(),rightfeet.y+getY());
151 return ret;
154 void Player::setAim(double x, double y) {
155 aim.x = x;
156 aim.y = y;