RPG CLASSES//spells

Hello all,
For a text RPG would it be more efficient to make a class for all spells then link the spells to the job class.. Or to just have the spell function inside the job class itself.
Thanks in advance.

//quick example
class Spells {
public:
};

class MeleeSpells : Spells {
public:
};

// Or..

class Jobs {
public:
};

class Warrior : Job {
public:
//spells and their functions.
Well, in my opinion it would be easier to... well, not quite the former. It's known as the factory build pattern, and basically allows you to toss a basic descriptor of the class (e.g. the type of spell) and get as a result the class specific to that batch of spells. Since the factory returns a pointer of the base class, you don't have to be as specific- the actual character would only have to have an object of a pointer to class spell, not any of the derivations.

This should help: http://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns#Abstract_Factory
Very helpful, thanks for the point in the right direction!
I came up with this. Wondering if this is the way to go. Could I just point Firebolts->damage when I need to use it in the function to actually "hurt" the target?


#include <iostream>

class Spell {
public:
//Spell constructor
Spell() :
name("__SPELL NAME__"),
description("__SPELL DESC__"),
cost(1),
spellBaseDamage(1){
};
Spell(std::string spellName = "___SPELL NAME___",
std::string spellDescription = "___SPELL DESC___",
int manaCost = 1,
int baseDamage = 1) :

name(spellName),
description(spellDescription),
cost(manaCost),
spellDamage(baseDamage){
};

int cost;
int spellDamage;
int spellBaseDamage;
std::string name;
std::string description;

};

int main ()
{
Spell *Firebolt = new Spell("FireBolt", "A weak fire spell.", 10, 25);
std::cout << "SPELL NAME: " << Firebolt->name << "\n"
<< "SPELL DESCRIPTION: "<< Firebolt->description << "\n"
<< "COST: " << Firebolt->cost << "\t"
<< "SPELL DAMAGE: " << Firebolt->spellDamage << "\n";

delete Firebolt;




return 0;
}
Yep. That is exactly how it would work. If need-be, you could even pass to the function damage the stats of who is being targeted and who is using it, allowing for even more versatility.
I guess the next question would be how would I give my mage class access to fire bolt?
Would I just have the mage class point to the spells I want him to have?
Well, the mage class would include an object of class spell. Give that object any name you want, and then just use the arrow operand with that object's name in, say, an attack command.
How would I give the job class an object from spell class. Do I need to make spells a derived class of job?
No, no. You'd make it as if it were a variable within in the class. So you'd have the list of variables, followed by an object that points to an object of type spell.
Topic archived. No new replies allowed.