NPC class. Easily create new NPCs.

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Quinnx
Posts: 9
Joined: Thu Dec 16, 2010 4:17 am

NPC class. Easily create new NPCs.

Post by Quinnx »

For a nice bases as to make your npcs.

The code is unfinished...I stopped working on this a while ago. But I'll complete here soon.

By the way, you can create your own items, and races as well. Just as easily in the same way.

Header files:

stdafx.h:

Code: Select all

#include "iostream"

#include "Irrlicht.h"
NPC.h:

Code: Select all

#include "stdafx.h"
#include "Races.h"
#include "ItemTypes.h"

using namespace irr;
using namespace core;
using namespace io;
using namespace video;
using namespace scene;
using namespace std;


class NPC// : public ISceneNode
{
	private:

		IAnimatedMesh* MSkeleton;
		IAnimatedMesh* MHead;
		IAnimatedMesh* MUpperBody;
		IAnimatedMesh* MHands; // could go even further to support fingers and toes
		IAnimatedMesh* MLowerBody;
		IAnimatedMesh* MFoot;

		IAnimatedMeshSceneNode* SSkeleton;
		IAnimatedMeshSceneNode* SHead;
		IAnimatedMeshSceneNode* SUpperBody;
		IAnimatedMeshSceneNode* SHands;
		IAnimatedMeshSceneNode* SLowerBody;
		IAnimatedMeshSceneNode* SFoot;



	public:
		eRaceType Race;

		//char Name;

		int Health;
		int Magic;
		int Fatigue;
		int Weight;
		int Encumberance;
		float scale;

		// Attributes
		int Strength;
		int Intelligence;
		int WillPower;
		int Agility;
		int Speed;
		int Endurance;
		int Luck;


		NPC(eRaceType race, int health, int magic, int fatigue, int weight, int encumberance,
			int strength, int intelligence, int willpower, int agility, int speed, int endurance, int luck);
		eRaceType getRace(void);
		void addInstance();
		void setRace(eRaceType race);
		void equipItem(eItemType item);

		void setHealth(int i);
		void setMagic(int i);
		void setFatigue(int i);
		void setWeight(int i);
		void setEncumberance(int i);

		int getHealth(void);
		int getMagic(void);
		int getFatigue(void);
		int getWeight(void);
		int getEncumberance(void);

		void setStrength(int i);
		void setIntelligence(int i);
		void setWillPower(int i);
		void setAgility(int i);
		void setSpeed(int i);
		void setEndurance(int i);
		void setLuck(int i);

		int getStrength(void);
		int getIntelligence(void);
		int getWillPower(void);
		int getAgility(void);
		int getSpeed(void);
		int getEndurance(void);
		int getLuck(void);
};

/*eRaceType NPC::getRace()
{
	return Race;
};*/

NPC::NPC(eRaceType race, int health, int magic, int fatigue, int weight, int encumberance,
			int strength, int intelligence, int willpower, int agility, int speed, int endurance, int luck)
{
	//MSkeleton = smgr->getMesh("Data/Meshes/Character/Skeleton.x");
	//MHead = ;
	//MUpperBody = ;
	//MHands = ;
	//MLowerBody = ;
	//Mfoot = ;

	Race = race;
	Health = health;
	Magic = magic;
	Fatigue = fatigue;
	Weight = weight;
	Encumberance = encumberance;

	Strength = strength;
	Intelligence = intelligence;
	WillPower = willpower;
	Agility = agility;
	Speed = speed;
	Endurance = endurance;
	Luck = luck;
};

// Set main stats

void NPC::setHealth(int i)
{
	Health = i;
};

void NPC::setMagic(int i)
{
	Magic = i;
};

void NPC::setFatigue(int i)
{
	Fatigue = i;
};

void NPC::setWeight(int i)
{
	Weight = i;
};

void NPC::setEncumberance(int i)
{
	Encumberance = i;
};

// get main stats

int NPC::getHealth(void)
{
	return Health;
};

int NPC::getMagic()
{
	return Magic;
};

int NPC::getFatigue(void)
{
	return Fatigue;
};

int NPC::getWeight(void)
{
	return Weight;
};

int NPC::getEncumberance(void)
{
	return Encumberance;
};

// set attributes

void NPC::setStrength(int i)
{
	Strength = i;
};

void NPC::setIntelligence(int i)
{
	Intelligence = i;
};

void NPC::setWillPower(int i)
{
	WillPower = i;
};

void NPC::setAgility(int i)
{
	Agility = i;
};

void NPC::setSpeed(int i)
{
	Speed = i;
};

void NPC::setEndurance(int i)
{
	Endurance = i;
};

void NPC::setLuck(int i)
{
	Luck = i;
};

// get attributes

int NPC::getStrength(void)
{
	return Strength;
};

int NPC::getIntelligence(void)
{
	return Intelligence;
};

int NPC::getWillPower(void)
{
	return WillPower;
};

int NPC::getAgility(void)
{
	return Agility;
};

int NPC::getSpeed(void)
{
	return Speed;
};

int NPC::getEndurance(void)
{
	return Endurance;
};

int NPC::getLuck(void)
{
	return Luck;
};

eRaceType NPC::getRace(void)
{
	return Race;
};

void NPC::addInstance()
{
	//ISceneNode* n =
		//smgr->addSceneNode("IAnimatedMeshSceneNode",0);

	//SHead = ;
	//SUpperBody = ;
	//SHands = ;
	//SLowerBody = ;
	//Sfoot = ;
};

void NPC::setRace(eRaceType race)
{
	Race = race;
};

void NPC::equipItem(eItemType item)
{

};
Races.h:

Code: Select all

#include "stdafx.h"

using namespace std;

enum eRaceType
{
	Human = 0,
	Orc  = 1,
	High_Elf = 2,
	Dark_Elf = 3,
	Wood_Elf = 4,
	Beast = 5
};
ItemTypes.h:

Code: Select all

#include "stdafx.h"

using namespace std;

enum eItemType
{
	Armor = 0,
	Clothing  = 1,
	Weapon = 2,
	Ingredient = 3,
};
Finally to create your npcs, you can store them in header files..resource files...save them in your own way...ect.

Main_Content.h:

Code: Select all

#include "stdafx.h"

using namespace irr;
using namespace core;
using namespace io;
using namespace video;
using namespace scene;
using namespace gui;
using namespace std;

// NPC's. Race, Health, Magic, Fatiuge, Weight, Encumberance,
//		Strength, Intelligence, WillPower, Agility, Speed, Endurance, Luck
NPC* Player = new NPC(Human, 100, 50, 200, 0, 150,
		15, 15, 15 , 15, 15, 15, 15);
NPC* TestNPC = new NPC(Human, 25, 50, 150, 0, 100,
	5, 5, 5 , 5, 5, 5, 5);
NPC* BobTheBeast = new NPC(Beast, 200, 50, 150, 0, 100,
	5, 5, 5 , 5, 5, 5, 5);
Burns
Posts: 26
Joined: Fri Jul 16, 2010 12:10 am
Contact:

RE:

Post by Burns »

good...job...
grumpymonkey
Posts: 222
Joined: Mon Jan 19, 2009 10:03 pm
Location: Miami, Florida
Contact:

Post by grumpymonkey »

its really simple, might be useful for some beginners looking to make an rpg. It is very specific though so idk if it would be helpful for most
Image
vatomalo
Posts: 2
Joined: Thu Mar 24, 2011 11:06 am
Location: NO

Post by vatomalo »

hei i was wondering if anyone can help me to access a member function of the non class type player*
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Post by REDDemon »

would be nice that someone creates a npc class completely data driven.
so that parameters are not fixed but created from a xml file.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

Nice.
But your NPC contains all the info, so if you need to spawn a player, you use this class too? Player has a lot of common properties. I would separate at least model, animations and other graphical info.

An example of class hierarchy:

Code: Select all

public class Actor
{
    protected Actor() {} // so we cannot just instantiate this class; inheritance is the only way to use it

    // model, animation, textures,
    // race and attributes info
    // other common props
};

public class NPCActor : public Actor
{
    // waypoint info
    // loot info
    // social group info (like wolf defend attacked nearby wolfs, because they are pack)
    // aggressiveness info (like friendly, neutral, aggressive)
    // AI controller info
    // other NPC-only related props
};

public class PlayerActor : public Actor
{
    // inventory info
    // skills, crafts, achievements,... info
    // team info (if multiplayer)
    // other player-only related props
};

public class BossActor : public NPCActor
{
    // special scripts info
    // special abilities info
    // other boss-only related props
};
cobra
Posts: 371
Joined: Fri Jan 23, 2009 2:56 am
Location: United States
Contact:

Post by cobra »

vatomalo wrote:hei i was wondering if anyone can help me to access a member function of the non class type player*

You're using a pointer called "player," so you need to use player->someMemberFunction() rather than player.someMemberFunction().
Josiah Hartzell
Image
vatomalo
Posts: 2
Joined: Thu Mar 24, 2011 11:06 am
Location: NO

Post by vatomalo »

Thank you very much, but i actually figured it out on my own yesterday, right after asking. Its wierd asking actually helps myself figure it out, since ive had this problem for quite some time. Yes im a noob, but thanks again!!!!
Post Reply