Hey, I have a problem.
You see, for some reason, I am not able to shoot up or down. After a few hours looking I couldn't seem to find the error to this issue. So can anyone help me point out what is causing the bullets not go up nor down.
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <GL/glfw.h>
#include <vector>
#include "tools.h"
using namespace std;
class Bullet;
class Player{
public:
void init();
void draw();
void update();
void keyListener();
void mouseMotionListener();
void mouseClickListener();
void setVx(int Vx){intVx = Vx;}
void setVy(int Vy){intVy = Vy;}
void velocity();
void shoot(int intDir);
void nothing(){}
Rectangle getPlayer(){return rectPlayer;}
Point getMotion(){return ptMotion;}
Point getClick(){return ptClick;}
private:
int intX,intY,intVx,intVy,intWidth,intHeight;
bool blInitiated;
vector<Bullet> vectBullet;
Rectangle rectPlayer;
Point ptMotion,ptClick;
};
class Bullet{
public:
Bullet(int X,int Y);
~Bullet();
void draw();
void update();
void setVx(int Vx){intVx = Vx;}
void setVy(int Vy){intVy = Vy;}
void velocity();
Rectangle getBullet(){return rectBullet;}
private:
int intX,intY,intVx,intVy;
Rectangle rectBullet;
};
#endif
Player.cpp
#include "Player.h"
void Player::init(){
blInitiated = false;
intX = 0;
intY = 0;
intWidth = 50;
intHeight = 50;
rectPlayer = createRectangle(intX,intY,intWidth,intHeight);
blInitiated = true;
}
void Player::draw(){
glColor3f(0.0,0.0,255.0);
drawRectangle(rectPlayer.intX,rectPlayer.intY,rectPlayer.intWidth,rectPlayer.intHeight);
for(int i = 0;i < vectBullet.size();i++){
vectBullet.at(i).draw();
}
}
void Player::update(){
if(!blInitiated){init();}else{nothing();}
rectPlayer.intX = intX;
rectPlayer.intY = intY;
rectPlayer.intWidth = intWidth;
rectPlayer.intHeight = intHeight;
velocity();
for(int i = 0;i < vectBullet.size();i++){
vectBullet.at(i).update();
}
}
void Player::velocity(){
intX += intVx;
intY += intVy;
}
void Player::shoot(int intDir){
vectBullet.push_back(Bullet(intX + 23,intY + 23));
if(intDir == 1){vectBullet.back().setVy(-10);}
else if(intDir == 2){vectBullet.back().setVy(-10);}
if(intDir == 3){vectBullet.back().setVx(-10);}
else if(intDir == 4){vectBullet.back().setVx(+10);}
}
void Player::keyListener(){
if(glfwGetKey('A') == GLFW_PRESS){setVx(-3);}
else if(glfwGetKey('D') == GLFW_PRESS){setVx(+3);}
else{setVx(0);}
if(glfwGetKey('W') == GLFW_PRESS){setVy(-3);}
else if(glfwGetKey('S') == GLFW_PRESS){setVy(+3);}
else{setVy(0);}
if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS){shoot(1);}
else if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS){shoot(2);}
else if(glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS){shoot(3);}
else if(glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS){shoot(4);}
}
void Player::mouseMotionListener(){glfwGetMousePos(&ptMotion.intX,&ptMotion.intY);}
void Player::mouseClickListener(){
if(glfwGetMouseButton(GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS){
glfwGetMousePos(&ptClick.intX,&ptClick.intY);
}
}
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Bullet::Bullet(int X, int Y){
intX = X;
intY = Y;
rectBullet = createRectangle(intX,intY,4,4);
}
Bullet::~Bullet(){}
void Bullet::draw(){
glColor3f(0.0,0.0,255.0);
drawRectangle(rectBullet.intX,rectBullet.intY,rectBullet.intWidth,rectBullet.intHeight);
}
void Bullet::update(){
rectBullet.intX = intX;
rectBullet.intY = intY;
velocity();
}
void Bullet::velocity(){
intX += intVx;
intY += intVy;
}
It's just Vy(velocity of Y) won't set. I tried replacing "setVy" with "setVx" so I know the keyboard isn't the problem. It's something to do with setVy but I don't know what.
PS. I tested it out and I know the problem is somewhere in the "shoot()".