How should I know that my game entities are aware of the things around them?

Each type of enemy in my game is a different class, and instances are stored in array C. In the main game loop, update () is run for each instance of the enemy / element, and draw () is executed. Some of the update () commands require knowing where the main player is. What would be the best way to get this information in this instance? I donโ€™t think that a global variable is a smart way to do this, since the parameters of a multiplayer game can be added later.

This is just an example of a more serious problem, as things in the game should know about each other. How do enemies know that they are facing other enemies, for example.

+1
iphone architecture
Jul 12 '09 at 16:52
source share
4 answers

You need to build a structure with several branched tree types (not a simple binary tree). Nodes are places in the game. Each node can contain several pointers to simple objects / objects (depending on your programming language). When a player moves around the game, you move the pointer to your playerโ€™s position on the node tree representing the location. When you start the game, this tree-like structure is filled with things to pick up, monsters, etc. Random seed can also be used to scatter monsters around.

This helps the speed of the game, since you need to search for the current node and nodes one step from your current location / node. Any routines start the monsters moving forward or back, just moving the monster pointer to the next node or nodes. If a copper package is used, its pointer is destroyed from the room / node in which it is located.

Good luck.

+3
Jul 12 '09 at 17:08
source share

One way to make the search more efficient is to split the updated objects into a square tree. The tree will be split based on the location on the screen or in the game world that you customized. Thus, you can configure it so that only objects are updated. For example, if you do hit detection, you can completely ignore large groups.

Square trees

hope that helps

+3
Jul 12 '09 at 17:00
source share

A controller object?

You need to think carefully about using simple arrays, linear searches through large arrays can take a long time to look for things like collisions.

0
Jul 12 '09 at 16:55
source share

All objects in the game, including the player, must be in the same container. Your AI will scroll through them every frame and call some function that performs all the goals of each object.

Entities (for example, a player) have access to all other objects through the same container.

You will need to diligently understand the container to ensure that the entity has access to the container.

0
Jul 12 '09 at 17:11
source share



All Articles