Firstly, I'm a noob, now that that's out of the way, I'm confident I can make what I'm trying to make which is an overhead rpg type game similar to Final Fantasy 3, the problem is that the code will be ridiculously inefficient.
My first question is how do I make multiple instances of the missile in the most efficient manner possible? Secondly, as you know if you've played an overhead rpg like FF3, the sprite changes when moving in different directions, what is the most efficient way of doing this in your opinion? Please use this code as an example, pretending Sieg is the main character. Lastly, what's the best way to do collision detection? What I did in this piece of crap game works for it but I imagine it would get very tedious to do with a sprite in an open world with multiple objects like trees and such around.
#include <SFML/Graphics.hpp>
#include <windows.h>
#include <SFML/Audio.hpp>
#include <iostream>
int main()
{
// Load the music from an OggVorbis file
sf::Music Zepplin;
if (!Zepplin.OpenFromFile("music.ogg"))
return EXIT_FAILURE;
// Play it
Zepplin.Play();
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 30), "Flibbidy");
// Load Background /////////////////////////////////////////
sf:: Image BackgroundFile;
if (!BackgroundFile.LoadFromFile("background.png"))
return EXIT_FAILURE;
sf::Sprite Background(BackgroundFile);
// Load the sprite image from a file
sf::Image SiegFile;
if (!SiegFile.LoadFromFile("floob.png"))
return EXIT_FAILURE;
// Create the sprite
sf::Sprite Sieg(SiegFile);
// Change its properties
Sieg.SetColor(sf::Color(0, 255, 255));
Sieg.SetPosition(200.f, 100.f);
Sieg.SetScale(2.f, 2.f);
Sieg.SetCenter(26, 18);
sf::Image MissileFile;
if(!MissileFile.LoadFromFile("missile.png"))
return EXIT_FAILURE;
sf::Sprite Missile(MissileFile);
App.Draw(Missile);
Missile.SetPosition(500, 600);
Missile.SetCenter(53, 37);
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
int Random = sf::Randomizer::Random(0, 600);
// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left)) Sieg.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) Sieg.Move( 100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) Sieg.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) Sieg.Move(0, 100 * ElapsedTime);
// Rotate the sprite
if (App.GetInput().IsKeyDown(sf::Key::Add)) Sieg.Rotate(- 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Subtract)) Sieg.Rotate(+ 100 * ElapsedTime); // Clear screen
App.Clear();
int PosX = Sieg.GetPosition().x;
int PosY = Sieg.GetPosition().y;
int MissilePosX = Missile.GetPosition().x;
int MissilePosY = Missile.GetPosition().y;
Missile.Move(-500 * ElapsedTime, 0);
if (PosX - MissilePosX < 10)
if ((MissilePosX - PosX) < 20 && (PosY - MissilePosY) < 20 && (MissilePosY - PosY) < 20)
{
Sieg.FlipX(true);
}
else
{
Sieg.FlipX(false);
}
// Display spritein our window
App.Draw(Background);
App.Draw(Sieg);
App.Draw(Missile);
if (MissilePosX < 50)
{
App.Draw(Missile);
Missile.SetPosition(800, Random);
}
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
// I have no idea how to use this class, I copied and pasted it from a tutorial on this //site and messed around with it to the best of my ability to get it to not interfere with //the debugging/compiling of the program, I have no idea how it ties in with sprite //management though and can't get it to do anything productive.
class Missile2
{
public :
// What is this "const::string& Missileflub" what purpose does it serve?
static bool Init(const std::string& Missileflub)
{
return MissileFile2.LoadFromFile("missile.png");
}
Missile2()
{
MissileCopy.SetImage(MissileFile2); // every sprite uses the same unique image
}
private :
static sf::Image MissileFile2; // shared by every instance
sf::Sprite MissileCopy; // one per instance
};
[/cpp]