Mmh, i'm not sure what you're trying to get across L B.
What i'm trying to accomplish:
When the space key is hit, create a Laser, and pass it the ships position, and rotation, so it's placed in the right spot, then make it do what a laser does... fire.
That's easy enough to do, the issue is how to implement it. Since i can't just tell the game to add a laser, and have the laser delete itself when it's out of bounds.
One solution could be creating a global vector of lasers in my "game" class, but i really don't want to do that, since lasers aren't specific to the game, they're specific to the player. So i decided to try to put a vector of lasers inside my "player" class, to no avail. I'll give a quick snapshot of what i have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
//Laser.h/////////////////////////////////////
#include "Game.h"
class Laser
{
public:
Laser();
Laser(float x, float y, float rotation);
void Init();
void UpdateLaser();
bool IsOutOfBounds(sf::RenderWindow &Window);
private:
sf::Image Image;
sf::Sprite Sprite;
float vx;
float vy;
};
//Laser.cpp///////////////////////////////////
#include "Laser.h"
Laser::Laser(float x, float y, float rotation)
{
Init();
Sprite.SetRotation(rotation); //set the rotation to the ships rotation
vx -= sin(Game::DegreesToRadians(rotation)) * speed; //set x velocity
vy -= cos(Game::DegreesToRadians(rotation)) * speed; //set y velocity
Sprite.SetPosition(x + vx*2, y + vy*2); //set the position
}
void Laser::Init()
{
if(!Image.LoadFromFile(image_laser)) //load the image from the file "image_laser" (defined in stdafx.h)
Game::HandleError("Error loading laser image"); //if it fails, handle it.
Sprite.SetImage(Image); //otherwise set the sprites image to Image.
//Initialize variables
vx = 0;
vy = 0;
speed = 20;
}
void Laser::UpdateLaser(sf::RenderWindow &Window)
{
Sprite.Move(vx, vy); //translate the sprite
Window.Draw(Sprite); //draw the sprite
}
bool Laser::IsOutOfBounds(sf::RenderWindow &Window)
{
//if its out of bounds return true
sf::Vector2f pos = Sprite.GetPosition();
if(pos.x > Window.GetWidth() || pos.x < 0 || pos.y > Window.GetHeight() || pos.y < 0)
return true;
return false;
}
|
I need to figure a robust way to make multiple lasers, without creating a global vector in my game loop.
What i attempted was this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
//excerpt from player.h
private:
vector<Laser> lasers;
vector<Laser>::iterator itr;
//excerpt from player.cpp
void Player::Fire()
{
lasers.push_back(Laser(Sprite.GetPosition().x, Sprite.GetPosition().y, Sprite.GetRotation()));
}
void Player::CheckLaser()
{
for(itr = lasers.begin(); itr != lasers.end() ++itr)
{
//check each laser if its out of bounds....
//if it is do
lasers.erase(itr);
//otherwise update the laser
//i'm not sure how to go about that, would lasers[itr].UpdateLaser(); work?
}
}
|
Now i'm not sure if that would even hold up, I've got little experience with iterators.