Opa, tinha me esquecido de colocar o plataform
[Projeto-PCG.git] / player.cpp
blobc42278fd67af5adff8820731c0599a27d6be8e1c
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;
66 //retorna cotovelo em relacao ao ombro, usa matematica levemente pesada, cuidado ao mexer
67 Ponto Player::getCotovelo(Ponto ombro, Ponto hand) {
68 double a = tamanhoBraco();
69 double b = tamanhoAntebraco();
70 double d = distance(ombro,hand);
71 double area = areaTriangle(a,b,d);
72 double ylinha = 2.0*area/d;
73 double xlinha = sqrt(a*a-ylinha*ylinha);
74 double sinTheta = ylinha/a;
75 double cosTheta = xlinha/a;
76 Ponto ret;
77 ret.x = -(cosTheta*xlinha-sinTheta*ylinha);
78 ret.y = -(sinTheta*xlinha+cosTheta*ylinha);
79 return ret;
82 //cuidado, distancia da arma nao pode exceder tamanhoBraco+tamanhoAntebraco
83 double Player::tamanhoBraco() {
84 return 20.0;
87 double Player::tamanhoAntebraco() {
88 return 13.0;
91 double Player::getAngle() {
92 Ponto gameaim(aim.x+game->camera.x,aim.y+game->camera.y);
93 Ponto gameneck(pescoco().x+getX(),pescoco().y+getY());
94 double hyp = sqrt((gameaim.x-gameneck.x)*(gameaim.x-gameneck.x) +
95 (gameaim.y-gameneck.y)*(gameaim.y-gameneck.y));
96 if (gameaim.y <= gameneck.y)
97 return -acos((-gameaim.x+gameneck.x)/hyp);
98 else
99 return acos((-gameaim.x+gameneck.x)/hyp);
102 void Player::desenha() {
103 game->desenhaMira(aim);
104 glPushMatrix();
105 glTranslatef(getX(),getY(),0);
106 Ponto leftfeet = leftFeet();
107 Ponto rightfeet = rightFeet();
108 Ponto hips = cintura();
109 Ponto leftarm = leftArm();
110 Ponto rightarm = rightArm();
111 Ponto neck = pescoco();
112 glBegin(GL_LINES);
113 glVertex3f(hips.x,hips.y,0);
114 glVertex3f(neck.x,neck.y,0);
116 glVertex3f(leftfeet.x,leftfeet.y,0);
117 glVertex3f(hips.x,hips.y,0);
119 glVertex3f(rightfeet.x,rightfeet.y,0);
120 glVertex3f(hips.x,hips.y,0);
121 glEnd();
122 Ponto rightelbow = getCotovelo(neck,rightarm+neck);
123 Ponto leftelbow = getCotovelo(neck,leftarm+neck);
124 glPushMatrix();
125 glTranslatef(neck.x,neck.y,0);
126 glRotatef(getAngle()*180.0/PI,0,0,-1);
127 glBegin(GL_LINES);
128 glVertex3f(0,0,0);
129 glVertex3f(leftelbow.x,leftelbow.y,0);
130 glVertex3f(leftelbow.x,leftelbow.y,0);
131 glVertex3f(leftarm.x,leftarm.y,0);
133 glVertex3f(0,0,0);
134 glVertex3f(rightelbow.x,rightelbow.y,0);
135 glVertex3f(rightelbow.x,rightelbow.y,0);
136 glVertex3f(rightarm.x,rightarm.y,0);
137 glEnd();
138 if (weapon != NULL) weapon->desenha();
139 glPopMatrix();
140 glPushMatrix();
141 glTranslatef(neck.x,neck.y-10,0);
142 drawCircle(10,30);
143 glPopMatrix();
144 glPopMatrix();
148 Linha Player::getBaseLine() {
149 Ponto leftfeet = leftFeet();
150 Ponto rightfeet = rightFeet();
151 Linha ret(leftfeet.x+getX(),leftfeet.y+getY(),rightfeet.x+getX(),rightfeet.y+getY());
152 return ret;
155 void Player::setAim(double x, double y) {
156 aim.x = x;
157 aim.y = y;