I have wrote some code took me about an hour it is pretty basic and allows objects to be created in the game and you can relate to them by the ISceneNode id.
I have posted this because I want some feedback is this a good idea and am I on the right track?
Basically you register a game object and it will set the node id to the current object index. After that when ever you only have access to the scene node like it could be a ray or anything then you can relate to the game object using the id.
I would have a method called getGameObjById and you will pass it the SceneNode id and it will return a GameObject instance. Am I on the right path?
Code: Select all
class GameObjectManager;
class GameObject
{
public:
GameObject(scene::ISceneNode* node, int _objIndex, GameObjectManager* _gameObjManager, IrrlichtDevice* _device)
{
device = _device;
driver = device->getVideoDriver();
smgr = device->getSceneManager();
objNode = node;
gameObjManager = _gameObjManager;
objIndex = _objIndex;
}
int getObjIndex()
{
return objIndex;
}
scene::ISceneNode* getObjSceneNode()
{
return objNode;
}
private:
int objIndex;
scene::ISceneNode* objNode;
protected:
GameObjectManager* gameObjManager;
IrrlichtDevice *device;
video::IVideoDriver* driver;
scene::ISceneManager* smgr;
};
class GameObjectManager
{
public:
GameObjectManager(IrrlichtDevice *_device)
{
device = _device;
curObjIndex = 0;
}
GameObject* addGameObject(scene::ISceneNode* node)
{
GameObject* gObj = new GameObject(node, curObjIndex, this, device);
gameObjs.push_back(gObj);
curObjIndex++;
return gObj;
}
GameObject* addGameObject(GameObject* gameObj)
{
gameObjs.push_back(gameObj);
curObjIndex++;
}
int getCurObjIndex()
{
return curObjIndex;
}
private:
std::vector<GameObject*> gameObjs;
int curObjIndex;
IrrlichtDevice *device;
};
class Player : public GameObject
{
public:
Player(scene::ISceneNode* node, GameObjectManager* gameObjManager, irr::IrrlichtDevice* device) :
GameObject(node, gameObjManager->getCurObjIndex(), gameObjManager, device)
{
}
int health;
};
GameObjectManager* gameObjManager;
int main()
{
// ask user for driver
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
if (driverType==video::EDT_COUNT)
return 1;
// create device
IrrlichtDevice *device =
createDevice(driverType, core::dimension2d<u32>(640, 480), 16, false);
if (device == 0)
return 1; // could not create selected driver.
gameObjManager = new GameObjectManager(device);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
device->getFileSystem()->addFileArchive("../../media/map-20kdm2.pk3");
scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
scene::IMeshSceneNode* q3node = 0;
// The Quake mesh is pickable, but doesn't get highlighted.
if (q3levelmesh)
q3node = smgr->addOctreeSceneNode(q3levelmesh->getMesh(0), 0, IDFlag_IsPickable);
/*
So far so good, we've loaded the quake 3 level like in tutorial 2. Now,
here comes something different: We create a triangle selector. A
triangle selector is a class which can fetch the triangles from scene
nodes for doing different things with them, for example collision
detection. There are different triangle selectors, and all can be
created with the ISceneManager. In this example, we create an
OctreeTriangleSelector, which optimizes the triangle output a little
bit by reducing it like an octree. This is very useful for huge meshes
like quake 3 levels. After we created the triangle selector, we attach
it to the q3node. This is not necessary, but in this way, we do not
need to care for the selector, for example dropping it after we do not
need it anymore.
*/
scene::ITriangleSelector* selector = 0;
if (q3node)
{
q3node->setPosition(core::vector3df(-1350,-130,-1400));
selector = smgr->createOctreeTriangleSelector(
q3node->getMesh(), q3node, 128);
q3node->setTriangleSelector(selector);
// We're not done with this selector yet, so don't drop it.
}
// Set a jump speed of 3 units per second, which gives a fairly realistic jump
// when used with the gravity of (0, -10, 0) in the collision response animator.
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0, 100.0f, .2f, ID_IsNotPickable, 0, 0, true, 3.f);
camera->setPosition(core::vector3df(50,500,-60));
camera->setTarget(core::vector3df(-70,0,-60));
if (selector)
{
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,50,30),
core::vector3df(0,-0.1f,0), core::vector3df(0,30,0));
scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,50,30),
core::vector3df(0,-0.1f,0), core::vector3df(0,30,0));
selector->drop(); // As soon as we're done with the selector, drop it.
camera->addAnimator(anim);
anim->drop();
}
// Now I create three animated characters which we can pick, a dynamic light for
// lighting them, and a billboard for drawing where we found an intersection.
// First, let's get rid of the mouse cursor. We'll use a billboard to show
// what we're looking at.
device->getCursorControl()->setVisible(false);
/* Add 3 animated hominids, which we can pick using a ray-triangle intersection.
They all animate quite slowly, to make it easier to see that accurate triangle
selection is being performed. */
scene::IAnimatedMeshSceneNode* node = 0;
video::SMaterial material;
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/faerie.md2"),
0, 0);
Player* faerie = new Player(node, gameObjManager, device);
faerie->health = 100;
gameObjManager->addGameObject(node);
node->setPosition(core::vector3df(-90,-15,-140)); // Put its feet on the floor.
node->setScale(core::vector3df(0.6f)); // Make it appear realistically scaled
node->setMD2Animation(scene::EMAT_POINT);
node->setAnimationSpeed(20.f);
material.setTexture(0, driver->getTexture("../../media/faerie5.bmp"));
material.Lighting = true;
material.NormalizeNormals = true;
node->getMaterial(0) = material;
// Now create a triangle selector for it. The selector will know that it
// is associated with an animated node, and will update itself as necessary.
selector = smgr->createTriangleSelector(node);
node->setTriangleSelector(selector);
selector->drop(); // We're done with this selector, so drop it now.
// Remember which scene node is highlighted
scene::ISceneNode* highlightedSceneNode = 0;
scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
int lastFPS = -1;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, 0);
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Collision detection example - Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}
/*
**/