views:

272

answers:

4

Every enemy type in my game is a different class, and the instances are stored in a C array. In the main game loop update() is run for each enemy/item instance, and draw() is run. Some of the update() commands require knowledge of where the main player is. What would be the best route to get that information to the instance? I don't think a global variable is the smart way to do it, since multiplayer options may be added later.

This is only an example of the bigger problem, which is how things in the game are supposed to know about each other. How would enemies know they're colliding with other enemies for example.

A: 

A Controller entity?

You need to think carefully about using plain arrays, linear searches through large arrays can be very time consuming to search for things like collisions.

Cade Roux
I guess I don't know any better. What should I be using instead?
Jameson
An array is fine for storage of the entities. For collision, you can have another structure (a world grid) where every cell lists every entity inside it. That way you don't have to test every entity against every other entity.
Nosredna
It depends ;-) How many entities are you talking about, what operations are you going to do and is there a way to partition the space in some way so that it's easy to determine which entities you need to check. Regardless, you can always start with plain arrays and then refactor to another mechanism later as long as you have methods which form an abstraction layer e.g.: bool entity.IsCollision() {for each entity e in this.controller.entities { if (e != this } return false ; } This can be refactored to not be a linear search.
Cade Roux
+3  A: 

One way to make the search a bit more efficient is to split the entities being updated into a quad tree. The tree would get divided based on location on the screen or in the game world you have setup. This way you can set it up so only near by entities get updated. So for example if you're doing hit detection you can totally ignore large groups.

quad trees

hope that helps

Ori Cohen
+3  A: 

Your need to build a multi-branched tree type structure (not simple binary tree). The nodes are the locations in the game. Each node can contain multiple simple stuctures/objects pointers (depending on your programming language). As the player moves around the game you move your player position pointer to the tree node representing the location. When you start the game this tree type stucture is populated with things to pick up, monsters etc. A random seed can also be use to scatter monsters around.

This helps the speed of the game as you only have to search current node and nodes one step away from your current location/node. Any routines triggered that monsters advance or retreat just move the monsters pointer to the next node or nodes. If a med pack is used then its pointer is destroyed from the room/node that it is in.

Good luck

A: 

All entities in the game, including the player, should be in the same container. Your AI will loop through them every frame and call some function that executes all the goals of every entity.

Entities (like the player) have access to all other entities through the same container.

You'll have to get clever about the container to make the entity access to the container efficient, however.

Hans Malherbe