Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector data linking

Tags:

c++

I have a class Monsters and when a instance is created it should link each monster with there weapon. ex. Gryphon monster should have gryphon attack 1 and gryphon attack 2 , of course the name of the attack is TBD, but for now well use gryphon attack 1 and 2.

Currenly I have this.

    #include <vector>

typedef enum {Living, Dead, Nature} Race;
typedef enum {Gryphon, Oracle, Mercenary,Templar,
              Satyr,Fallin Angel,ArcAngel,Satan,Grimreaper,
              Unbaptized Babies,Boggart,Succubus,Meat Wagon,
              Djinns,Manticore,Water Nymph,Plant Nymph,
              Mother Nature, Cannibal Tribesmen,Wyvern,
              Vegetable Lamb, Ent, Lava Worm, Alpha Dragon
              } MonsterType;
typedef enum {gryphon1,Oracle1, Mercenary1,Templar1,
              Satyr1,Fallin Angel1,ArcAngel1,Satan1,Grimreaper1,
              Unbaptized Babies1,Boggart1,Succubus1,Meat Wagon1,
              Djinns1,Manticore1,Water Nymph1,Plant Nymph1,
              Mother Nature1, Cannibal Tribesmen1,Wyvern1,
              Vegetable Lamb1, Ent1, Lava Worm1,Alpha Dragon1,
              Gryphon2, Oracle2, Mercenary2,Templar2,
              Satyr2,Fallin Angel2,ArcAngel2,Satan2,Grimreaper2,
              Unbaptized Babies2,Boggart2,Succubus2,Meat Wagon2,
              Djinns2,Manticore2,Water Nymph2,Plant Nymph2,
              Mother Nature2, Cannibal Tribesmen2,Wyvern2,
              Vegetable Lamb2, Ent2, Lava Worm2, Alpha Dragon2
              } Weapon;

Class Monsters{

protected:
    MonsterType type;
    Race race;
    std::vector<Weapon> weapon_list;
public:
     bool flying;
     bool lava;
     bool water;
     int life;
     int karmaCost;
     int move;
     int crit;
     int defMagic;
     int defNonMagic;
     bool isDead;
     bool canMove;
     bool canAttack;
     bool onFlag;
     int nextTurn;


};

I am not sure about the vector , nor if its needed it was just some experiments i was messing with.. But what is the best way to link the weapon to the monster? Also note each weapon has values that goes along with it, So

gryphon attack 1 {
  int range = 10
  int ticks = 5
  bool magical = false
  int power = 23
  bool heals = false 
}  


gryphon attack 2 {
  int range = 5
  int ticks = 7
  bool magical = true
  int power = 29
  bool heals = true 
} 

the actual values are read in from an ini or network, so not worried about the actual values yet, but i need to know i can add the values gryphon->weapon1->range = 5

I am still very new to this so if something seems very wrong please tell me.

like image 571
Glen Morse Avatar asked Jun 10 '13 06:06

Glen Morse


People also ask

What is an example of vector data?

Vector data is represented as a collection of simple geometric objects such as points, lines, polygons, arcs, circles, etc. For example, a city may be represented by a point, a road may be represented by a collection of lines, and a state may be represented as a polygon.

What are the 3 feature types of vector data?

Vector data is split into three types: point, line (or arc), and polygon data.

What is a vector of data?

Vector data is a geographic data type where data is stored as a collection of points, lines, or polygons along with attribute data. Individual points recorded as coordinate pairs, which represent a physical position in the world, make up vector data at its most basic level.

How are vectors connected to other lines in GIS?

Lines connect vertices Vector lines connect each vertex with paths. Basically, you're connecting the dots in a set order and it becomes a vector line with each dot representing a vertex. Lines usually represent features that are linear in nature. For example, maps show rivers, roads, and pipelines as vector lines.


3 Answers

Speaking from experience: the approach you have chosen will lead to many problems in the future. I know I am not exactly answering your question, but I am doing it only to save you some trouble. Forgive me and/ord disregard this below if you want to do it your way.

Do not create specialized classes for each of your monsters or characters. Create a single, abstract, sort of compound class with many properties which describe various aspects of that game object. Sort of like that:

// simplified class declaration, not a C++ code
class GameActor {
  ActorVisualization visualization;

  vector<InventoryItems> inventory;

  ActorStatistics stats;

  vector<ActorEffects> appliedEffects;
}

Such abstract object will be used for all Actors in your game, including player characters.

The next step is to use the visitor pattern for all things that can happen to this actor.

// continued
class GameActor {
  bool applies(Visitor& visitor);

  void accept(Visitor& visitor) {
    if (applies(visitor)) {
      visitor.visit(this);
    }
  }
}

class Visitor {
  void visit(GameActor& actor);
}

Extend your GameActor to suit your needs if required. Whenever you are adding new functionaliy try to use the already implemented visitor mechanism. Create a new property of the GameActor only if necessary.

Samples of visitor? It could be written differently, but I hope it clarifies how things should get done.

class DamageInflictedVisitor {
  int amount;
  damageType_t dmgType;

  void visit(GameActor& actor) {
    double res = actor.getStats().getResistances().getResistanceForType(dmgType);
    int finalAmount = amount * (1-res);
    actor.getStats().inflictDamage(finalAmount);
  }
}

class ActorAliveVisitor {
  void visit(GameActor& actor) {
    if (actor.getStats().hp <= 0) {
      if (actor.getType() == AT_MONSTER) {
        // remove from the game, create a new ExperienceGained visitor applicable for players, etc.
      } else if (actor.getType() == AT_PLAYER) {
        // notify the game that the given player is dead
      }
    }
  }
}

By using such simple visitors you have very good code readability, you know what each visitor does simply by looking at it's name.

like image 162
Dariusz Avatar answered Oct 22 '22 16:10

Dariusz


Try to make a hierarchical structure for your monsters instead of a big typelist. For example baseclass Monster which just has position/orientation and a race. then you make a derived class LivingMonster, which has health added for example, and a class LivingArmedMonster which has the weapon. this will make sure you wont get a bloated class and makes it easier to add monsters later that use other functions without blowing up the big monster-class.

as for your weapon: the list is a great idea, only thing I would add is maybe use a pointer since then you can have different weapons (that are derived from base class: weapon) without changing the list. also it will make it easier to exchange weapons between monster (you have a weapon storage which creates all the weapons) then you can drop and pick up the weapon, so you just move the pointer from 1 monster's weapon vector to the other. this is way less intense than copying the full object

like image 34
Giel Avatar answered Oct 22 '22 16:10

Giel


Class Weapon {
  int range;
  int ticks;
  bool magical;
  int power;
  bool heals;
  public Weapon(int range, ......){
      this->range = range;
      ...
  }
};

Class Monster{
protected:
    MonsterType type;
    Race race;
    std::vector<Weapon> weapon_list;
public:
     int life;
     int karmaCost;
     ...
     void addWeapon(Weapon w){
         weapon_list.push_back(w);
     }
};

Class FlyingMonster : public Monster{
    public:
    int flightSpeed;
}


Class MonsterFactory{
   static FlyingMonster *CreateGryphon(){
      FlyingMonster *gryphon = new FlyingMonster();
      gryphon.addWeapon(WeaponFactory::CreateGryphonAttack1());
      gryphon.addWeapon(WeaponFactory::CreateGryphonAttack2());
      return gryphon;
   }
};

Class WeaponFactory{
   static Weapon* CreateGryphonAttack1(){
      Weapon* w = new Weapon(gryphonAttack1BaseRange);
      return w;
   }
};

FlyingMonster* tom = MonsterFactory::CreateGryphon();
tom->weapon_list[0].range = 50;
like image 37
Lefteris E Avatar answered Oct 22 '22 16:10

Lefteris E