How should I have my game entities knowledgeable o

2019-04-03 01:15发布

问题:

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.

回答1:

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



回答2:

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



回答3:

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.



回答4:

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.