OgreAStarExample.cpp

 

//==========================================================================//
// Ogre A* Example - December 2009                                          //
//                                                                          //
// Author: Daniel Soltyka                                                   //
// E-Mail: dsoltyka [at] gmail [dot] com                                    //
//                                                                          //
// This example demonstrates the A* pathfinding algorithm in a 3D           //
// environment.                                                             //
//                                                                          //
// Modified A* routines based off of A* Algorithm Implementation using STL  //
// by Justin Heyes-Jones.                                                   //
//                                                                          //
// Original Routines can be found at http://www.heyes-jones.com/astar.html  //
//==========================================================================//

#include "OgreAStarExample.h"

OgreAStarExample::OgreAStarExample()
{
	// create world map
	mMap = new WorldMap();
}

// Create our scene
void OgreAStarExample::createScene()
{
	// set default lighting
	mSceneMgr->setAmbientLight(ColourValue(1.0f, 1.0f, 1.0f));

	mEntity = mSceneMgr->createEntity("Robot", "robot.mesh");
	mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("RobotNode", Vector3(0.0f , 0.0f, 0.0f));
	mNode->attachObject(mEntity);

	Entity* ent;
	SceneNode* node;

	// create ground
	Plane plane(Vector3::UNIT_Y,0);

	MeshManager::getSingleton().createPlane("ground",
		ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
		2560,2560,20,20,true,1,5,5,Vector3::UNIT_Z);

	ent = mSceneMgr->createEntity("GroundEntity", "ground");
	node = mSceneMgr->getRootSceneNode()->createChildSceneNode("GroundNode", Vector3(640.0f, 0.0f, 640.0f));
	node->attachObject(ent);
	ent->setMaterialName("Examples/BumpyMetal");

	std::stringstream ss;

	// create walls by iterating through our world map and placing cube objects appropriately
	for (int y = 0; y < mMap->GetHeight(); y++)
	{
		for (int x = 0; x < mMap->GetWidth(); x++)
		{
			if (mMap->GetTile(x,y) == 9)
			{
				// generate entity name using x,y coords
				ss << x;
				string temp_x = ss.str();
				ss << y;
				string temp_y = ss.str();

				string entityName = "Cube" + temp_x + "x" + temp_y;

				ent = mSceneMgr->createEntity( entityName, "cube.mesh");
				node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CubeNode" + temp_x + "x" + temp_y, Vector3((x * 64.0f), 50.0f, (y * 64.0f)));
				node->attachObject(ent);
				ent->setMaterialName("Examples/Rockwall");
				node->setScale(0.64f, 1.0f, 0.64f);
			}
		}
	}

	// create start and end markers
	ent = mSceneMgr->createEntity("Start", "cube.mesh");
	node = mSceneMgr->getRootSceneNode()->createChildSceneNode("StartNode", Vector3(0.0f, 0.0f, 0.0f));
	node->attachObject(ent);
	ent->setMaterialName("Examples/RustySteel");
	node->setScale(0.64f, 1.0f, 0.64f);

	ent = mSceneMgr->createEntity("End", "cube.mesh");
	node = mSceneMgr->getRootSceneNode()->createChildSceneNode("EndNode", Vector3(0.0f, 0.0f, 0.0f));
	node->attachObject(ent);
	ent->setMaterialName("Examples/RustySteel");
	node->setScale(0.64f, 1.0f, 0.64f);

	// set the camera to look out at our handiwork
	mCamera->setPosition(90.0f, 1280.0f, 535.0f);
	mCamera->setAutoTracking(true,mNode);

}

// Create our frame listener
void OgreAStarExample::createFrameListener()
{
	mFrameListener= new DefaultFrameListener(mWindow, mCamera, mSceneMgr, mNode, mEntity, mMap, mPathFinder);
	mFrameListener->showDebugOverlay(true);
	mRoot->addFrameListener(mFrameListener);
}