In my code I create 3 shots after the user presses ENTER. There are delays int NEXT_SHOT_IN_CHAIN_DELAY
between each other. When the ENTER is pressed I create three objects of class Shot
and add them in the vector std::vector <Shot*> shots
. The class Shot
changes the inside statement from: 0 to 2
WAIT_FOR_SHOT_START -> BULLET_LEFT_WEAPON -> BULLET_IN_ATTACK_ZONE
According to the current time from the start of the program, when I create an exemplar of the class shot
- the number of the bullet and the start time to leave the weapon (variable startShotTime
) has normal values.
Console output:
DEBUG: Bullet by creation: 0; Start shot time: 3833; End shot time: 4583 and statement: 0; Actual time: 4583
DEBUG: Bullet by creation: 1; Start shot time: 4163; End shot time: 4913 and statement: 0; Actual time: 4913
DEBUG: Bullet by creation: 2; Start shot time: 4493; End shot time: 5243 and statement: 0; Actual time: 5243
On every frame I update all the shots. And in the updating function my logger tells me that the bulletNumber and startShotTime have already new (strange) values.
Console output:
DEBUG: Bullet 258744461 Start shot time: 575665160
DEBUG: Bullet 575663496 Start shot time: 3744914323
DEBUG: Bullet 575663688 Start shot time: 575663624
What can change the values of my private variables?
Class Bullet is only the graphic demonstration of the bullets on the screen. It does'n affect the Shot class
.
If it's important: I use VisualStudio on Windows 10 and SDL2 library.
Here is my code, BulletController.h
:
#pragma once
#include <vector>
#include "Timer.h"
#include "Logger.h"
#include <vector>
#include "FightersController.h"
#include "Bullet.h"
#include "Random.h"
#include "Shot.h"
#include "EventsListener.h"
class BulletController{
public:
BulletController(FightersController* fightersController, Bullet *bullets, int bulletsNumber);
~ BulletController();
void update(long deltaTime);
private:
int bulletsNumber;
const int BULLETS_PART_TO_BE_RENDERED_IN_PERCENTS = 60;
Bullet* bullets;
FightersController* fightersController = nullptr;
const int NOTHING_TO_DELETE =-1;
std::vector <Shot*> shots;
int shotNumberToBeDeleted=NOTHING_TO_DELETE ;
void makeExplosion(int numberOfBullet);
void updateShooting();
void startToShotIfPossible();
void updateActualBullets();
void setVisibilityForBullets();
const int bulletsInLine = 3;
const int NEXT_SHOT_IN_CHAIN_DELAY = 330;
bool shotAlreadyPressed;
//Mutable
Random* mutRandomizer;
};
BulletController.cpp
:
#include "BulletController.h"
BulletController::BulletController(FightersController* fightersController, Bullet *bullets, int bulletsNumber){
this->bullets = bullets;
this->bulletsNumber = bulletsNumber;
this->fightersController = fightersController;
mutRandomizer = new Random();
}
BulletController::~BulletController(){
delete mutRandomizer;
delete bullets;
}
void BulletController::update(long deltaTime){
updateActualBullets();
updateShooting();
}
void BulletController::updateShooting() {
std::vector <Command>* commands = EventsListener::getInstance()->getCommands();
bool shotCommandFounded = false;
if (commands->size() > 0) {
for (auto i = (commands->begin()); i != commands->end(); ++i) {
Command command = (Command)*i; //This copies the command
if (command.getType() == command_constants::SHOT) {
Logger::debug("Player pressed shot");
if (!shotAlreadyPressed) {
shotAlreadyPressed = true;
startToShotIfPossible();
}
shotCommandFounded = true;
}
}
}
if (shotAlreadyPressed) {
if (!shotCommandFounded) {
shotAlreadyPressed = false;
}
}
}
void BulletController::updateActualBullets() {
if (shots.size() > 0) {
int toBeDeleted = -1;
int itterator = 0;
for (auto i : shots) {
i->update();
}
for (auto i : shots) {
if (i->isInHitZone()) {
makeExplosion(itterator);
toBeDeleted = itterator;
}
itterator++;
}
if (toBeDeleted >= 0) {
shots.erase(shots.begin() + toBeDeleted);
}
}
}
void BulletController::startToShotIfPossible() {
Shot firstShot(0);
shots.push_back(&firstShot);
Shot secondShot(NEXT_SHOT_IN_CHAIN_DELAY);
shots.push_back(&secondShot);
Shot thirdShot(NEXT_SHOT_IN_CHAIN_DELAY*2);
shots.push_back(&thirdShot);
setVisibilityForBullets();
Logger::debug("Shot started");
}
void BulletController::setVisibilityForBullets() {
int count = 0;
for (int i = 0; i < bulletsNumber; i++) {
int randomValue = mutRandomizer->nextInt(100);
if (randomValue <= BULLETS_PART_TO_BE_RENDERED_IN_PERCENTS) {
bullets[i].activateForTime(1, 1000);
count++;
}
}
Logger::debug(std::to_string(count) + " bullets will be visible by this shot");
}
void BulletController::makeExplosion(int numberOfBullet){
shotNumberToBeDeleted = numberOfBullet;
fightersController->handleAttack();
}
Shot.h
:
#pragma once
#include <SDL.h>
#include "Logger.h"
#include <string>
class Shot
{
public:
Shot(int timeBeforeStart);
~Shot();
void update();
static const int WAIT_FOR_SHOT_START = 0;
static const int BULLET_LEFT_WEAPON = 1;
static const int BULLET_IN_ATTACK_ZONE = 2;
bool isInHitZone();
private:
const int SHOT_TIME = 750;
int statement;
unsigned long int startShotTime;
unsigned long int endShotTime;
int bulletNumber;
};
Shot.cpp
:
#include "Shot.h"
static int bulletCount = 0;
Shot::Shot(int timeBeforeStart) {
bulletNumber = bulletCount;
bulletCount++;
startShotTime = SDL_GetTicks() + timeBeforeStart;
endShotTime = SDL_GetTicks() + timeBeforeStart + SHOT_TIME;
statement = WAIT_FOR_SHOT_START;
Logger::debug("Bullet by creation: " + std::to_string(bulletNumber) + "; Start shot time: " + std::to_string(startShotTime) + "; End shot time: " + std::to_string(endShotTime) + " and statement: " + std::to_string(statement) + "; Actual time: " + std::to_string(endShotTime));
}
Shot::~Shot() {
}
void Shot::update() {
Logger::debug("Bullet " + std::to_string(bulletNumber) + " Start shot time: " + std::to_string(startShotTime));
if (statement == WAIT_FOR_SHOT_START) {
if (SDL_GetTicks() >= startShotTime) {
statement == BULLET_LEFT_WEAPON;
Logger::debug("Bullet " + std::to_string(bulletNumber) + " left the weapon at " + std::to_string(SDL_GetTicks()));
}
}
else if (statement == BULLET_LEFT_WEAPON) {
if (SDL_GetTicks() >= endShotTime) {
statement == BULLET_IN_ATTACK_ZONE;
Logger::debug("Bullet " + std::to_string(bulletNumber) + " is in hit zone at " + std::to_string(SDL_GetTicks()));
}
}
}
bool Shot::isInHitZone() {
if (statement == BULLET_IN_ATTACK_ZONE) return true;
else return false;
}