开发者

List structure for c++ game

开发者 https://www.devze.com 2023-04-12 14:01 出处:网络
I\'m making a very very basic game in C++ to gain some experience in the language and I have hit a bit of a brick wall in terms of progress.

I'm making a very very basic game in C++ to gain some experience in the language and I have hit a bit of a brick wall in terms of progress.

My problem is, while I was designing a class for the player's weapons, I realized I required a list, as I will only have a certain number of weapons throughout the game.

So I ask you, if you were designing this, what implementation would you use for storing all of the weapons in a game? Why?

Here is my code so far for the weapons. as you can see I was just about to start defining all of them manually, starting with the "DoubleBlades"... (Edit* I forgot to note that players should be able to have more than one wepaon, and they can pick up more or drop some, so the list can grow and shrink)

#ifndef __WEAPON_H__
#define __WEAPON_H__

#include <string>

class Item
{
  public:
    Item(const std::string& name)
     : name(name){ }
    const std::string& getName(void) const { return name; }
    int getID(void) const { return this->itemID;}
  private:
   std::string name;
   int itemID;
};

class Weapon
 : public Item
{
  private:
   int damage;
  public:
   Weapon(const std::string& name)
    : Item(name) { }
   virtual int getDamage(void) const = 0;
};

class DoubleBlades
 : public Weapon
{
  public:
   DoubleBlades(int ammo)
    : Weapon("DoubleBlades") { }
   virtual int getDamage(void) const { return 12; }
};

#endif

Also if you spot any bad开发者_开发百科 habits I would really appreciate letting me know.


I would use the standard implementation (std::list<Item*>) because it's easy to use ("out of the box") and, out of the standard containers, it seems to be most suitable:

  • You probably need support for fast adding/deleting of weapons (so no std::vector or C arrays)
  • You probably don't need support for fast searching of the list for a specific item (so no std::map)

BTW you need to have a virtual destructor in the Item class (this is a general rule on base classes in c++).

Oh, and another minor problem - i think the Weapon class does not need the damage member variable - the class doesn't use it and it has private access, so the deriving classes cannot use it either.

You might have to use dynamic_cast in your implementation - a virtual environment such as yours will probably require "safe" casting of pointers from Item* to Weapon*.


On a very basic level, you may not necessarily need a data structure. For instance, if you know exactly how many weapons, etc. you need/may possibly have, you can (wastefully) allocate an array of size n and have certain spots in the array as a pointer to a weapon (if you currently have that weapon, else NULL). Then, simply cast appropriately based on weapon index. But this is a naive approach. Otherwise, refer to Mike's comment above on the original post.


If the list is going to vary in size, I'd use either an std::vector or and std::set. With both, you'll get to use all the nice stl functions and what not. If you use set, it will be quicker to sort the "weapon objects". The vector is more useful if you want to know the order in which a particualr object was added.

If they are going to have a fixed number of weapons you can still use a vector or a set, just make sure you pass the exact size you're going to use to the constructors. This will allow for some optimizations like allocating contiguous blocks of memory (which speeds up access times).


You might actually look at std::map, -- consider something like

std::map<std::string, Item*> 

This would allow you to access items by name, which can be nice syntactic sugar, and would allow you to quickly check for existence of an item using the count method.

0

精彩评论

暂无评论...
验证码 换一张
取 消