166
General / Re: Slow performance with A*
« on: June 26, 2015, 07:18:29 pm »
Thanks for taking the time to help me with this. I worry that I might just be trying something too complicated for my current skillset. I'll probably keep at this for a few more days, but if I can't get it working, I'll move onto something simpler.
You can try something easier to debug instead of something simpler.
Example, select an arbitrary position (X,Y) , and the apply path finding when X time had passed.
Example:
extern const int TILESIZE = 32 ;
extern const int MAPSIZE = 11;
#include <SFML/Graphics.hpp>
#include "Tile.h"
#include "Layer.h"
#include "CollisionLayer.h"
#include "Cursor.h"
#include "EventHandler.h"
#include "Unit.h"
#include "Units.h"
#include "Path.h"
#define NOT !
#define AND &&
// Auxiliary MACROS.
int main()
{
sf::RenderWindow window( sf::VideoMode( 640 , 480 ) , "Joyous Day!" ) ;
sf::Clock myTimer ;
Layer testLayer( MAPSIZE , MAPSIZE ) ;
Cursor testCursor ;
auto testObj = testLayer.getTileVector() ;
Path testPath( testObj[9][9] , testObj[1][2] ) ; //This is the creation of the path object, it takes in an origin and a destination tile.
std::vector<Tile> path = testPath.findPath( testLayer ) ; //The findPath function does most of the work. It works for the example tiles
bool timeCompleted = false ;
int timeToReach = 3 ; // 3 seconds.
float Xpos ; // = insert a number
float Ypos ; // = insert a number
double _TILESIZE = 1/TILESIZE ; // To use multiplication.
// Main loop.
while ( window.isOpen() )
{
// Events section.
sf::Event event ;
while ( window.pollEvent( event ) )
{
if ( event.type == sf::Event::Closed )
window.close() ;
}
// Path finding section.
if ( NOT timeCompleted AND myTimer.getElapsedTime().asSeconds() >= timeToReach ) // Only will be executed ONCE (is easier to debug)
{
timeCompleted = true ; // Set false to ensure that this block won't be execute anymore.
testCursor.shape.move( TILESIZE , 0 ) ; // I don't know what happens here...
auto myTileVector = testLayer.getTileVector() ;
testPath.setPath( myTileVector[5][5] , myTileVector[ Xpos * _TILESIZE ][ Ypos * _TILESIZE ] ) ;
path = testPath.findPath( testLayer ) ;
}
// Draw section.
window.clear() ;
for ( auto &pathTile : path )
{
MyShape = pathTile.getShape() ;
MyShape.setFillColor( sf::Color( 255 , 0 , 0 , 128 ) ) ;
window.draw( MyShape ) ;
}
testLayer.drawLayer( window ) ;
testCursor.drawCursor( window ) ;
window.display() ;
} // end of MAIN LOOP
return 0 ; // End of process.
}
extern const int MAPSIZE = 11;
#include <SFML/Graphics.hpp>
#include "Tile.h"
#include "Layer.h"
#include "CollisionLayer.h"
#include "Cursor.h"
#include "EventHandler.h"
#include "Unit.h"
#include "Units.h"
#include "Path.h"
#define NOT !
#define AND &&
// Auxiliary MACROS.
int main()
{
sf::RenderWindow window( sf::VideoMode( 640 , 480 ) , "Joyous Day!" ) ;
sf::Clock myTimer ;
Layer testLayer( MAPSIZE , MAPSIZE ) ;
Cursor testCursor ;
auto testObj = testLayer.getTileVector() ;
Path testPath( testObj[9][9] , testObj[1][2] ) ; //This is the creation of the path object, it takes in an origin and a destination tile.
std::vector<Tile> path = testPath.findPath( testLayer ) ; //The findPath function does most of the work. It works for the example tiles
bool timeCompleted = false ;
int timeToReach = 3 ; // 3 seconds.
float Xpos ; // = insert a number
float Ypos ; // = insert a number
double _TILESIZE = 1/TILESIZE ; // To use multiplication.
// Main loop.
while ( window.isOpen() )
{
// Events section.
sf::Event event ;
while ( window.pollEvent( event ) )
{
if ( event.type == sf::Event::Closed )
window.close() ;
}
// Path finding section.
if ( NOT timeCompleted AND myTimer.getElapsedTime().asSeconds() >= timeToReach ) // Only will be executed ONCE (is easier to debug)
{
timeCompleted = true ; // Set false to ensure that this block won't be execute anymore.
testCursor.shape.move( TILESIZE , 0 ) ; // I don't know what happens here...
auto myTileVector = testLayer.getTileVector() ;
testPath.setPath( myTileVector[5][5] , myTileVector[ Xpos * _TILESIZE ][ Ypos * _TILESIZE ] ) ;
path = testPath.findPath( testLayer ) ;
}
// Draw section.
window.clear() ;
for ( auto &pathTile : path )
{
MyShape = pathTile.getShape() ;
MyShape.setFillColor( sf::Color( 255 , 0 , 0 , 128 ) ) ;
window.draw( MyShape ) ;
}
testLayer.drawLayer( window ) ;
testCursor.drawCursor( window ) ;
window.display() ;
} // end of MAIN LOOP
return 0 ; // End of process.
}