From b6ddb047a4617f16a1f8b7e98ebeab8aa4ea018e Mon Sep 17 00:00:00 2001 From: AndreFerreira Date: Tue, 20 Oct 2009 02:33:04 -0200 Subject: [PATCH] Arma na mao do player, com cotovelo!!! :D --- Makefile | 1 + game.cpp | 19 ++++++++++++ geometry.cpp | 15 ++++++++-- geometry.h | 13 +++++++++ player.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- player.h | 7 +++++ weapon.cpp | 5 ++++ weapon.h | 22 ++++++++++++++ 8 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 weapon.cpp create mode 100644 weapon.h diff --git a/Makefile b/Makefile index bb4cf4e..3613510 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ OBJS = \ mapa.o \ plataform.o \ controleteclado.o \ + weapon.o \ SRCS = $(OBJS,.o=.cpp) diff --git a/game.cpp b/game.cpp index 1556ab3..21f63b5 100644 --- a/game.cpp +++ b/game.cpp @@ -120,6 +120,24 @@ void Game::mainLoop() { geraMapa(); ControleTeclado c(*player); bool quit = false; + //demo temporario de arma, mais tarde criar com lua + Weapon shotgun; + Polygon shotgunsprite; + Linha cabo(-15,-15,-15,-15+10); + Linha cabo2(-15+1,-15+1,-15+1,-15+10+1); + Linha cano(-15,-15+10,-45,-15+10); + Linha cano2(-15+1,-15+10+1,-45+1,-15+10+1); + shotgunsprite.addLinha(cabo); + shotgunsprite.addLinha(cabo2); + shotgunsprite.addLinha(cano); + shotgunsprite.addLinha(cano2); + shotgun.setSprite(shotgunsprite); + Ponto r(-15,-15); + shotgun.setRightHand(r); + Ponto l(-30,-15+10); + shotgun.setLeftHand(l); + player->equip(&shotgun); + //fim demo while (!quit) { fps.start(); //player events @@ -136,4 +154,5 @@ void Game::mainLoop() { SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); } } + SDL_Quit(); } diff --git a/geometry.cpp b/geometry.cpp index 64eb6e8..c618b20 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -2,8 +2,6 @@ #include "math.h" #include -const double PI = 3.14159265358979323846; - Linha::Linha(double x1,double y1,double x2, double y2) { vertices[0].x = x1; vertices[0].y = y1; @@ -39,8 +37,15 @@ void Linha::desenha() { glEnd( ); } +void Polygon::desenha() { + int n = linhas.size(); + for (int i = 0; i < n; i++) { + linhas[i].desenha(); + } +} + double operator*(const Ponto &a, const Ponto &b) { - return a.x * b.y - b.x * a.y; + return a.x * b.y - b.x * a.y; } Ponto operator-(const Ponto &a, const Ponto &b) { @@ -57,6 +62,10 @@ Ponto operator+(const Ponto &a, const Ponto &b) { return ret; } +double distance(const Ponto &a,const Ponto &b) { + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); +} + void drawCircle(double radius, int lines) { double i2rad = PI/(lines/2.0); diff --git a/geometry.h b/geometry.h index ca20c2f..cb8b5ad 100644 --- a/geometry.h +++ b/geometry.h @@ -2,6 +2,9 @@ #define GEOMETRIA_H #include "SDL/SDL.h" #include "SDL/SDL_opengl.h" +#include + +const double PI = 3.14159265358979323846; struct Ponto { double x,y; @@ -9,6 +12,8 @@ struct Ponto { Ponto (double x1, double y1) {x = x1; y = y1;} }; +double distance(const Ponto &a,const Ponto &b); + double operator*(const Ponto &a, const Ponto &b); Ponto operator-(const Ponto &a, const Ponto &b); Ponto operator+(const Ponto &a, const Ponto &b); @@ -35,6 +40,14 @@ class Rect { Rect(Ponto a, Ponto b) {vertices[0] = a; vertices[1] = b; normaliza();} }; +class Polygon { + private: + std::vector linhas; + public: + void addLinha(Linha linha) {linhas.push_back(linha);} + void desenha(); +}; + void drawCircle(double radius, int lines); #endif diff --git a/player.cpp b/player.cpp index f951d20..c42278f 100644 --- a/player.cpp +++ b/player.cpp @@ -1,13 +1,20 @@ +#include #include "player.h" #include "gravity.h" Player::Player(Game* agame) { + weapon = NULL; setPosition(40,40); setSpeed(0,0); game = agame; game->gravityManager->subscribe(this); } +void Player::equip(Weapon* aweapon) { + //desequipar a anterior aqui mais tarde + weapon = aweapon; +} + Ponto Player::leftFeet() { Ponto pe(-10,0); return pe; @@ -19,13 +26,21 @@ Ponto Player::rightFeet() { } Ponto Player::leftArm() { - Ponto braco(-10,-30); - return braco; + if (weapon == NULL) { + Ponto braco(-10,-30); + return braco; + } + else + return weapon->getLeftHand(); } Ponto Player::rightArm() { - Ponto braco(10,-30); - return braco; + if (weapon == NULL) { + Ponto braco(10,-30); + return braco; + } + else + return weapon->getRightHand(); } Ponto Player::cintura() { @@ -38,6 +53,52 @@ Ponto Player::pescoco() { return neck; } +double areaTriangle(double a, double b, double c) { + if (a < b) std::swap(a,b); + if (a < c) std::swap(a,c); + if (b < c) std::swap(b,c); + //a >= b >= c + + return sqrt((a+(b+c))*(c-(a-b))*(c+(a-b))*(a+(b-c)))/4.0; +} + + +//retorna cotovelo em relacao ao ombro, usa matematica levemente pesada, cuidado ao mexer +Ponto Player::getCotovelo(Ponto ombro, Ponto hand) { + double a = tamanhoBraco(); + double b = tamanhoAntebraco(); + double d = distance(ombro,hand); + double area = areaTriangle(a,b,d); + double ylinha = 2.0*area/d; + double xlinha = sqrt(a*a-ylinha*ylinha); + double sinTheta = ylinha/a; + double cosTheta = xlinha/a; + Ponto ret; + ret.x = -(cosTheta*xlinha-sinTheta*ylinha); + ret.y = -(sinTheta*xlinha+cosTheta*ylinha); + return ret; +} + +//cuidado, distancia da arma nao pode exceder tamanhoBraco+tamanhoAntebraco +double Player::tamanhoBraco() { + return 20.0; +} + +double Player::tamanhoAntebraco() { + return 13.0; +} + +double Player::getAngle() { + Ponto gameaim(aim.x+game->camera.x,aim.y+game->camera.y); + Ponto gameneck(pescoco().x+getX(),pescoco().y+getY()); + double hyp = sqrt((gameaim.x-gameneck.x)*(gameaim.x-gameneck.x) + + (gameaim.y-gameneck.y)*(gameaim.y-gameneck.y)); + if (gameaim.y <= gameneck.y) + return -acos((-gameaim.x+gameneck.x)/hyp); + else + return acos((-gameaim.x+gameneck.x)/hyp); +} + void Player::desenha() { game->desenhaMira(aim); glPushMatrix(); @@ -57,13 +118,25 @@ void Player::desenha() { glVertex3f(rightfeet.x,rightfeet.y,0); glVertex3f(hips.x,hips.y,0); - - glVertex3f(leftarm.x,leftarm.y,0); - glVertex3f(neck.x,neck.y,0); - - glVertex3f(rightarm.x,rightarm.y,0); - glVertex3f(neck.x,neck.y,0); glEnd(); + Ponto rightelbow = getCotovelo(neck,rightarm+neck); + Ponto leftelbow = getCotovelo(neck,leftarm+neck); + glPushMatrix(); + glTranslatef(neck.x,neck.y,0); + glRotatef(getAngle()*180.0/PI,0,0,-1); + glBegin(GL_LINES); + glVertex3f(0,0,0); + glVertex3f(leftelbow.x,leftelbow.y,0); + glVertex3f(leftelbow.x,leftelbow.y,0); + glVertex3f(leftarm.x,leftarm.y,0); + + glVertex3f(0,0,0); + glVertex3f(rightelbow.x,rightelbow.y,0); + glVertex3f(rightelbow.x,rightelbow.y,0); + glVertex3f(rightarm.x,rightarm.y,0); + glEnd(); + if (weapon != NULL) weapon->desenha(); + glPopMatrix(); glPushMatrix(); glTranslatef(neck.x,neck.y-10,0); drawCircle(10,30); @@ -81,5 +154,5 @@ Linha Player::getBaseLine() { void Player::setAim(double x, double y) { aim.x = x; - aim.y = y; + aim.y = y; } diff --git a/player.h b/player.h index e9844a2..fb2ff99 100644 --- a/player.h +++ b/player.h @@ -4,6 +4,7 @@ #include "game.h" #include "geometry.h" #include "thing.h" +#include "weapon.h" class Game; @@ -17,10 +18,16 @@ class Player: public Thing { Ponto rightArm(); Ponto pescoco(); Ponto aim; + Weapon* weapon; + double tamanhoAntebraco(); + double tamanhoBraco(); + double getAngle(); + Ponto getCotovelo(Ponto ombro, Ponto hand); public: Player(Game* agame); void desenha(); void setAim(double x, double y); + void equip(Weapon* aweapon); virtual Linha getBaseLine(); }; diff --git a/weapon.cpp b/weapon.cpp new file mode 100644 index 0000000..ef4cb54 --- /dev/null +++ b/weapon.cpp @@ -0,0 +1,5 @@ +#include "weapon.h" + +void Weapon::desenha() { + sprite.desenha(); +} diff --git a/weapon.h b/weapon.h new file mode 100644 index 0000000..9873e9a --- /dev/null +++ b/weapon.h @@ -0,0 +1,22 @@ +#ifndef WEAPON_H +#define WEAPON_H + +#include "geometry.h" +#include "thing.h" + +class Weapon: public Thing { + private: + Polygon sprite; + Ponto leftHand; + Ponto rightHand; + public: + void setSprite(Polygon asprite) { sprite = asprite;} + void setLeftHand(Ponto hand) {leftHand = hand;} + void setRightHand(Ponto hand) {rightHand = hand;} + Ponto getLeftHand() {return leftHand;} + Ponto getRightHand(){return rightHand;} + void desenha(); +}; + + +#endif -- 2.11.4.GIT