Adding a weapon
From IrrWizard
Introduction
This short tutorial will show you how to add a weapon.
Note: By selecting the 'Full Framework' option, a weapon manager will already added.
Adding a weapon
To add a weapon to the camera, we must put the code in the CGamePlayState class. The weapon is common to all game levels, and because all of our game levels inherit from this class, they will all benefit from the code.
The code
Create a new function called AddFPSWeapon, remember to add the declaration in the header file before hand.
//! Adds a weapon model to the camera
void CGamePlayState::AddFPSWeapon(CGameManager* pManager)
{
// load FPS weapon to Camera
irr::scene::IAnimatedMesh* m_pWeaponMesh;
irr::scene::IAnimatedMeshSceneNode* m_pWeaponNode;
m_pWeaponMesh = pManager->getDevice()->getSceneManager()->getMesh("media/gun.md2");
m_pWeaponNode = pManager->getDevice()->getSceneManager()->addAnimatedMeshSceneNode(m_pWeaponMesh, pManager->getSceneManager()->getActiveCamera() , -1);
m_pWeaponNode->setMaterialFlag(video::EMF_LIGHTING, true);
m_pWeaponNode->setMD2Animation(scene::EMAT_STAND);
m_pWeaponNode->setMaterialTexture(0, pManager->getDevice()->
getVideoDriver()->getTexture( "media/gun.bmp"));
m_pWeaponNode->setScale(core::vector3df(1,1,1));
m_pWeaponNode->setPosition(core::vector3df(10,-20, 0));
m_pWeaponNode->setRotation(core::vector3df(10,180,-65));
m_pWeaponNode->setParent(pManager->getSceneManager()->getActiveCamera());
}
Now we can add a call to the new function in the Game level CGameStateLevel01
//! Initialisation, generic level.
void CGameStateLevel01::Init(CGameManager* pManager)
{
// perform generic initialisation from parent
CGamePlayState::Init(pManager);
CGamePlayState::loadMap(pManager, "20kdm3.bsp");
CGamePlayState::addFPSWeapon(pManager);
}
And that's about it. Compile and run. If you don't see the weapon at first, play around with the last 3 settings, setScale, setPosition, and setRotation. The weapon may be directly above or behind you, thus rendering it invisible to the camera.
