Here, this is the entire thing now
I hope its not too confusing
sorry about this =(
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
float DecimalCos(float Num){
return (cos(Num*(3.14/180)))*(180/3.14);
}
float DecimalSin(float Num){
return (sin(Num*(3.14/180)))*(180/3.14);
}
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(1200, 600, 32), "SFML Graphics");
int MouseX,MouseY;
// Load the sprite image from a file
sf::Image Image;
sf::Image turret;
sf::Image Missiletastic;
sf::Image Kursor;
float MissileSpriteRotation;
if (!Image.LoadFromFile("Tank.png"))
return EXIT_FAILURE;
if (!turret.LoadFromFile("Turret.png"))
return EXIT_FAILURE;
if (!Missiletastic.LoadFromFile("Missile.png"))
return EXIT_FAILURE;
if (!Kursor.LoadFromFile("Cursor.png"))
return EXIT_FAILURE;
// Create the sprite
sf::Sprite PlayerTank(Image);
sf::Sprite PlayerTurret(turret);
App.ShowMouseCursor(0);
// Change its properties
PlayerTank.SetPosition(200.f, 100.f);
PlayerTank.SetCenter(25.f,27.f);
PlayerTurret.SetCenter(25.f,25.f);
float PlayerX;
float PlayerY;
float PlayerRotation;
float SystemTime = App.GetFrameTime();
sf::Event Mouse;
sf::Sprite MissileSprite(Missiletastic);
MissileSprite.SetCenter(5.f,8.f);
sf::Sprite Cursor(Kursor);
Cursor.SetCenter(25.f,25.f);
sf::Event Event;
// Start game loop
while (App.IsOpened())
{
// Process events
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
MouseY = Event.MouseMove.Y;
MouseX = Event.MouseMove.X;
//Moves the turret along with the tank and moves the cursor
PlayerTurret.SetX(PlayerTank.GetPosition().x);
PlayerTurret.SetY(PlayerTank.GetPosition().y);
Cursor.SetX(MouseX);
Cursor.SetY(MouseY);
// Moves the tank
if (App.GetInput().IsKeyDown(sf::Key::D)) PlayerTank.Rotate(-100.0 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::A)) PlayerTank.Rotate( 100.0 * ElapsedTime );
if (App.GetInput().IsKeyDown(sf::Key::W)){
PlayerRotation = -1* PlayerTank.GetRotation() + 180 + 90;
PlayerTank.Move(( DecimalCos(PlayerRotation)) * 5 * ElapsedTime, (DecimalSin(PlayerRotation)) * 5 * ElapsedTime);
}
if (App.GetInput().IsKeyDown(sf::Key::S)){
PlayerRotation = -1* PlayerTank.GetRotation() + 180 + 90;
PlayerTank.Move(( DecimalCos(PlayerRotation)) * 5 * ElapsedTime * -1, (DecimalSin(PlayerRotation)) * 5 * ElapsedTime * -1);
}
//fires the missile
if (App.GetInput().IsKeyDown(sf::Key::Space) ){//and SystemTime + .0001 < App.GetFrameTime()
MissileSprite.SetX(PlayerTurret.GetPosition().x);
MissileSprite.SetY(PlayerTurret.GetPosition().y);
MissileSprite.Rotate(PlayerTurret.GetRotation());
SystemTime = App.GetFrameTime();
}
MissileSpriteRotation = -1* MissileSprite.GetRotation() + 180 + 90;
MissileSprite.Move(( DecimalCos(MissileSpriteRotation)) * 10 * ElapsedTime, (DecimalSin(MissileSpriteRotation)) * 10 * ElapsedTime);
//Sets the players turret rotation
PlayerTurret.SetRotation (-1*static_cast<float>(atan2(static_cast<double>(MouseY - PlayerTurret.GetPosition().y),static_cast<double>(MouseX - PlayerTurret.GetPosition().x))* 180 / 3.14) - 90);
// Clear screen
App.Clear();
// Display sprite in our window
App.Draw(MissileSprite);
App.Draw(PlayerTank);
App.Draw(PlayerTurret);
App.Draw(Cursor);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}