Guys, I realize that I ask about too much stuff these days, but internship deadlines are coming and I need to send some games. It's a decent excuse.
My problem is that I can't come up with a good way to code my shoot() function in my Player class.( 3d fpshooter )
What I need is to somehow say: if the player has clicked on the left mouse button, check if the ray that goes out straight from his crosshair has intersected with an enemy object, and if it is, inform the monster who was hit that he needs to die in hell (or to decrease hp).
And the actual problem is that if I do it this way, I need access to all objects in every character that shoots, this means if I have 20 characters, every character needs to keep a pointer to all other characters so it can check if collision with any of the objects will return true... and it gets messed up because I have 20 pointers pointing to the same stuff.
What to do, bros? :huh:
( shoot() function can't take arguments, because it's called in a virtual void update() function )
#include "player.h"
Player::Player( glm::vec3 pos, Input *inputManager, std::vector<Model*> anims, std::vector<Shader*> shaders, std::vector<GameObject*> *gameObjects )
:Character( anims, shaders )
{
this->inputManager = inputManager;
objects = gameObjects;
mainCam.setCurrentPosition( pos );
}
void Player::update()
{
rememberPreviousState();
handleInput();
lookAround();
Move();
shoot();
}
void Player::shoot()
{
}
void Player::handleInput()
{
//Player movement
abilities[ int( Ability::MOVE_LEFT ) ] = inputManager->getKeyState( SDLK_a );
abilities[ int( Ability::MOVE_RIGHT ) ] = inputManager->getKeyState( SDLK_d );
abilities[ int( Ability::MOVE_FORWARD ) ] = inputManager->getKeyState( SDLK_w );
abilities[ int( Ability::MOVE_BACKWARDS ) ] = inputManager->getKeyState( SDLK_s );
if( inputManager->verticalAngle > F_PI/2.0f ) { inputManager->verticalAngle = F_PI/2.0f; }
if( inputManager->verticalAngle < -F_PI/2.0f ) { inputManager->verticalAngle = -F_PI/2.0f; }
mainCam.yawAngle = inputManager->horizontalAngle;
mainCam.pitchAngle = inputManager->verticalAngle;
}
void Player::lookAround()
{
mainCam.setDirectionRight( glm::vec3( cos( mainCam.yawAngle ), 0.0f, sin( mainCam.yawAngle ) ) );
mainCam.setDirection( glm::normalize( glm::vec3(
cos( mainCam.yawAngle - F_PI/2.0f )*cos( mainCam.pitchAngle ),
sin( -mainCam.pitchAngle ),
sin( mainCam.yawAngle - F_PI/2.0f )*cos( mainCam.pitchAngle ) ) ) );
moveHelperCam.setDirection( glm::vec3( cos( mainCam.yawAngle - F_PI/2.0f ), 0.0f, sin( mainCam.yawAngle - F_PI/2.0f ) ) );
mainCam.setDirectionUp( glm::cross( mainCam.getDirectionRight(), mainCam.getDirection() ) );
mainCam.setCurrentPitch( glm::angleAxis( mainCam.pitchAngle, glm::vec3( 1, 0, 0 ) ) );
mainCam.setCurrentYaw( glm::angleAxis( mainCam.yawAngle, glm::vec3( 0, 1, 0 ) ) );
}