开发者

Having an instanced, derived class put a pointer to itself into an array?

Okay so here's a r开发者_运维百科eal mess of a question - I don't even entirely know what to search for.

I asked a question here, related to a game's entity handling system: Initiating a derived class with specific variable values

So far, that's working out great for me, but for one thing. I want to have mobs not just collide with eachother, but interact.

How can I look up a specific instance of the derived class, by coordinates?

For example, find the baseObject:Enemy() located at 22,22 and get the value of "nType" from within it

What comes to mind is putting some kind of pointer to an instance in an array, and moving it when said entity moves... but how do I make a derived class add a pointer to itself to an array? and how do I then pull something from that instance's variables?

Whew. Hope this makes sense.


Well, I think a solution to this problem is to use delegates/event passing.

Let's say you have a mob, as someone said earlier you could have a "mob watcher" object. The idea is that when a GameEntity is part of a mob, that entity subscribes to the "mob watcher". If that entity leaves the mob, it will let the "mob watcher" know about it (unsubscribe).

So, when you need to know, who's composing a mob, you could just ask the mob watcher for the "mob list", if you need to search by Entity position you could then work yourself through the list of entities composing the mob, and find the one in the "position of interest".

If your mobs are gigantic you could add some kind of spatial hash feature to your mob watcher, so you could easily filter and ask for "guys in the mob which are located in gameGrid[10][13]".

If you use event passing, it's quite cool because if when you want to forward messages between the mob, sending an event to the mob watcher could be used to then forward the aforementioned event to the subscribed entities.

If you use delegates, it works in a similar way.

Observer Pattern Delegate Pattern


Well, the specific question you're asking is easy to answer: the pseudo-variable this is available in all member functions; it points to the current object.

The "looking up by coordinates" part is trickier; you might consider using an octree structure to organize your objects (Google is your friend there.)


You will need to make a new object whose job is to keep track of where the "mobs" are. According to your question, this new object will need to contain at least a std::vector which contains a pointer to each "mob" in the world, and also a member function like:

mob* findEnemy( int x, int y );

Unfortunately, what you asked for in your question is probably not what you want. You probably want to get all of the "mobs" near a point:

std::vector<mob*> findEnemiesNearPoint( int x, int y );

...but returning a std::vector can be very slow, so you probably want an array instead, but the array must be sized carefully, and now we're far far beyond the amount of information provided in your question so far.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜