A Player Class

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Gezmo P
Posts: 16
Joined: Sun Mar 28, 2004 10:01 am
Location: London, UK

A Player Class

Post by Gezmo P »

Hi all.

A quick (I hope!) question regarding a player class for a game:

I want to define a player as a collection of related data eg. a mesh, player name details etc.

I would also use this class to refer to a particular onscreen model eg to change its animation .

class Player
{
public:

Player(string pName);
~Player();
string getName();
bool setName(string pName);
bool setAnimatedMeshSceneNode(scene::IAnimatedMeshSceneNode* playerMesh);
scene::IAnimatedMeshSceneNode* ptrPlayerNode;

private:
string name;
scene::IBillboardSceneNode* myBillBoardNode;
};

AS you can see, I have placed a pointer to the animatedmeshscenenode in the public section, as there seemed to be a lot of accessor functions needed if it were placed in the private section. However, I am into my Object Oriented Principles( :wink: ) and realise this goes against the grain of encapsulation. Is there a simpler way to keep the pointer private, and yet allow accessor/mutators to reach it without having to hand code each one?

Many thanks

Gezmo
"DIIIIIIIIIIIIVVVVVVVVVVVVEEEEEEEEEEEEEEE"
stodge
Posts: 216
Joined: Fri Dec 05, 2003 5:57 pm

Post by stodge »

A Friend?
Guest

Post by Guest »

Not sure what you mean by "handcoding" everything, but look at it this way:

presumably you only want the functions/classes that understand about Irrlicht objects to access this data.

You could make all those functions/classes "friend"s (i.e. friend class X; in the class definition).
That gives them access to all private properties.
Nectar
Posts: 6
Joined: Thu Feb 10, 2005 8:02 pm

Post by Nectar »

im not entirely sure if this would work right for you, but i would suggest trying get() and set() functions. you could "get" the variable into a local variable in your scope, then, once you have done all you want to it, "set" it again back into the class
Kaeles-notlogged

Post by Kaeles-notlogged »

I have a player class made already, your welcome to use it, all of the functions work correctly... :-D

Code: Select all


/***************************************************************************/
//!class CPlayer
//!This class contains all the information for the player.
/***************************************************************************/

#ifndef CPLAYER_H_INCLUDED
#define CPLAYER_H_INCLUDED

#include <irrlicht.h>
#include "CEntity.h"

class CPlayer : public CEntity
    {
    private:
            irr::core::string<wchar_t> name;
            irr::core::vector3df playerPos;
            irr::core::vector3df playerRot;
            irr::scene::IAnimatedMeshSceneNode * playerNode;
           
    public:
            //!Constructor
            CPlayer(){};
            
            //!Destructor
            ~CPlayer(){};
                
            //! temporary, should be called in main game loop, will be called by 
            //! game.loop() when state system is done.
            virtual void update(){};
            
            void init();
                
            //accessor functions
            irr::core::string<wchar_t> getName()
                        {return name;};
            irr::core::vector3df getPlayerPos()
                        {return playerNode->getPosition();};
            irr::core::vector3df getPlayerRot()
                        {return playerNode->getRotation();};
                 
            //! returns a pointer to the sceneNode of this Player
            irr::scene::IAnimatedMeshSceneNode* getPlayerNode()
                        {return playerNode;};
                 
            //mutators
            void setname(irr::core::string<wchar_t> nametemp)
                        {name = nametemp;};
            void setPlayerNode(irr::scene::IAnimatedMeshSceneNode * tNode)
                        {playerNode = tNode;};

    };

#endif


//!****************************************************************************/
//!Implementation file for Class CPlayer
//!****************************************************************************/
#ifndef CPLAYER_CPP_INCLUDED
#define CPLAYER_CPP_INCLUDED
#include <irrlicht.h>
#include "CPlayer.h"

void CPlayer :: init()
{

setMesh(getSmgr()->getMesh("media/dwarf1.x"));
playerNode = getSmgr()->addAnimatedMeshSceneNode(getMesh());
    if (playerNode)
  		{
  		playerNode->setScale(irr::core::vector3df(20.0,20.0,20.0));
  		playerNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);
  		playerNode->setFrameLoop(0, 310);
  		playerNode->setAnimationSpeed(18);
  		//playerNode->setMaterialTexture(0,driver->getTexture("media/dwarf.jpg"));
		playerNode->setPosition(irr::core::vector3df(0.0f,0.0f,0.0f));
  		}


}

#endif




sure its not truely data abstraction, but im still a c++ newb
im working on a framework type thing (really more of an irrlicht extension)
that will utilze a singlton device(yea i know its in nx) and a memory manager for all the extension things (monster list, terrainmesh list, etc.)
kaeles-notlogged

Post by kaeles-notlogged »

oop.....
i forgot it was derived... thats what happens with too much coffee and not enough sleep
and its built more for rpg type thing...
so uh... delete what you dont need :-D

Code: Select all


/***********************************************************************/
//!Class: CEntity
//!base class for all "living" entitys in the game.
/***********************************************************************/
#ifndef CENTITY_H_INCLUDED
#define CENTITY_H_INCLUDED

#include <irrlicht.h>

class CEntity
    {
    private:
        int health;
        int armor;
        int energy;
        int damage;
        int chanceToHit;
        
        //!!name of the mesh assigned to entity
        irr::c8 meshname;
        irr::scene::IAnimatedMesh* mesh;
        //!!needed for mesh stuff
        irr::video::IVideoDriver* driver;
        irr::scene::ISceneManager* smgr;
        irr::gui::IGUIEnvironment* guienv;
        
    public:

        //!!accessor functions
        int getHealth()
                {return health;};
        int getArmor()
                {return armor;};
        int getEnergy()
                {return energy;};
        int getChanceToHit()
                {return chanceToHit;};
        int getDamage()
                {return damage;};
        irr::c8 getMeshName()
                {return meshname;};
        irr::scene::IAnimatedMesh* getMesh()
                {return mesh;};
        virtual irr::video::IVideoDriver* getDriver()
                {return driver;};
        virtual irr::scene::ISceneManager* getSmgr()
                {return smgr;};
        virtual irr::gui::IGUIEnvironment* getGUIEnv()
                {return guienv;};
        
        //!!mutator functions
        void setHealth(int tHealth)
                {health = tHealth;};
        void setArmor(int tArmor)
                {armor = tArmor;};
        void setEnergy(int tEnergy)
                {energy = tEnergy;};
        void setChanceToHit(int tChanceToHit)
                {chanceToHit = tChanceToHit;};
        void setDamage(int tDamage)
                {damage = tDamage;};
        void setMeshName(irr::c8 tMeshName)
                {meshname = tMeshName;};
        void setMesh(irr::scene::IAnimatedMesh* tMesh)
                {mesh = tMesh;};
        void setSmgr(irr::scene::ISceneManager* tSmgr)
                {smgr = tSmgr;};
        void setDriver(irr::video::IVideoDriver* tDriver)
                {driver = tDriver;};
        void setGUIEnv(irr::gui::IGUIEnvironment* tGuiEnv)
                {guienv = tGuiEnv;};
        
        //!!Called during Game.loop()
        virtual void update() = 0;
        
    };

#endif



LordNaikon
Posts: 164
Joined: Wed May 05, 2004 5:34 pm
Location: Germany Berlin
Contact:

Post by LordNaikon »

"thats what happens with too much coffee and not enough sleep" <-- those are the main sins of the programmers :P

i think you mean
"thats what happens with too much sleep and not enough coffee " <-- ahh holy c++ world :D
q|^.^|p beeing every time friendly to everyone
sys: 2500+Barton 512MB 6600GT WinXP
roninmagus
Posts: 91
Joined: Fri Oct 31, 2003 5:03 am

Post by roninmagus »

I may be misunderstanding your problem, but if all you want to do is change the animation on the scene node encapsulated inside the player class, then give the player class a GetPlayerNode() function which returns ptrPlayerNode...

Code: Select all

irr::scene::IAnimatedMeshSceneNode * Player::GetPlayerNode()
{
     return ptrPlayerNode;
}
then

Code: Select all

myPlayer->GetPlayerNode()->setFrameLoop(startFrame, endFrame);

// or you could do it this way:
irr::scene::IAnimatedMeshSceneNode * node = myPlayer->GetPlayerNode();
node->setFrameLoop(startFrame, endFrame);
daveandrews.org - A Christian Programmer's Weblog | Dusty Engine - A Task Engine for Irrlicht
Post Reply