Projectile.h
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
sf::Image BulletImage;
static sf::Sprite Bullet;
bool BulletState;
public:
void SetDirection(sf::Sprite);
void SetSpeed(float);
void Destroy(void);
bool IsDestroyed(const Projectile& b);
};
#endif
Projectile.cpp
#include "Projectile.h"
#include <SFML/Graphics.hpp>
sf::Sprite Projectile::Bullet;
void Projectile::SetDirection(sf::Sprite Reference)
{
float TargetRotation = Reference.GetRotation();
Bullet.SetRotation(static_cast<float>(fmod(TargetRotation, 360)));
float BulletRotation = Bullet.GetRotation();
if (BulletRotation < 0)
Bullet.SetRotation(BulletRotation + 360.f);
}
void Projectile::SetSpeed(float SpeedMultiplier)
{
BulletImage.LoadFromFile("bullet.png");
Bullet.SetImage(BulletImage);
float BulletX = Bullet.GetPosition().x;
float BulletY = Bullet.GetPosition().y;
Bullet.SetX(BulletX * SpeedMultiplier);
Bullet.SetY(BulletY * SpeedMultiplier);
}
void Projectile::Destroy()
{
BulletState = true;
}
bool Projectile::IsDestroyed(const Projectile& b)
{
if(BulletState)
{
return true;
}
else
{
return false;
}
}
main.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Projectile.h"
#define PI 3.1415926535
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha");
sf::Image PlayerImage;
sf::Image Background;
Background.LoadFromFile("background.png");
PlayerImage.LoadFromFile("player.png");
sf::Sprite Player(PlayerImage);
sf::Sprite BackgroundS(Background);
sf::Sprite* follow = &Player;
Player.SetPosition(400,300);
Projectile Bullet;
Bullet.BulletImage.LoadFromFile("bullet.png");
Bullet.Bullet.SetImage(Bullet.BulletImage);
std::vector <Projectile> BulletClip;
sf::View Camera(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f));
sf::Vector2f PPos = Player.GetPosition();
sf::Music InGameMusic;
InGameMusic.OpenFromFile("in_game.ogg");
bool shotFired = false;
std::vector<Projectile*> AllBullets;
while(Window.IsOpened())
{
sf::Event Event;
while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();
}
Bullet.SetDirection(Player);
Bullet.SetSpeed(2);
Window.Clear();
float ElapsedTime = Window.GetFrameTime();
sf::Vector2f MousePos = Window.ConvertCoords(Window.GetInput().GetMouseX(), Window.GetInput().GetMouseY());
if (Window.GetInput().IsKeyDown(sf::Key::W)) Player.Move(0, -425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::S)) Player.Move(0, 425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::A)) Player.Move(-425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::D)) Player.Move(425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::N)) InGameMusic.Play();
if (Window.GetInput().IsKeyDown(sf::Key::M)) InGameMusic.Stop();
if (Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) shotFired = true;
if (shotFired)
{
BulletClip.push_back(Bullet);
Bullet.BulletState = false;
}
for (std::vector<Projectile>::iterator Itr = BulletClip.begin(), End = BulletClip.end(); Itr != End; )
{
if (Bullet.IsDestroyed(*Itr))
{
Itr = BulletClip.erase(Itr);
}
else
{
Window.Draw(Bullet.Bullet);
++Itr;
}
}
Player.SetCenter(Player.GetSize() / 2.f);
Player.SetRotation(((-1* 360 / PI *(atan2(static_cast<double>(Player.GetPosition().y - MousePos.y), static_cast<double>(Player.GetPosition().x - MousePos.x))))/2)+90);
Camera.SetCenter(follow->GetPosition());
Window.SetView(Camera);
Window.Draw(BackgroundS);
Window.Draw(Player);
Window.Display();
InGameMusic.SetLoop(true);
}
}
Whenever I click mouse1 the game gets really laggy but nothing appears.
And how would I create some text that tells me how many bullets were fired? Since only sf::string is drawable.