我应该如何让我的游戏实体了解他们周围的事物? [英] How should I have my game entities knowledgeable of the things around them?

查看:22
本文介绍了我应该如何让我的游戏实体了解他们周围的事物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我游戏中的每种敌人类型都是不同的类,并且实例存储在 C 数组中.在主游戏循环中,为每个敌人/物品实例运行 update(),并运行 draw().一些 update() 命令需要知道主要玩家在哪里.将信息获取到实例的最佳途径是什么?我不认为使用全局变量是明智的做法,因为稍后可能会添加多人游戏选项.

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.

推荐答案

您需要构建一个多分支的树型结构(不是简单的二叉树).节点是游戏中的位置.每个节点可以包含多个简单的结构/对象指针(取决于您的编程语言).当玩家在游戏中移动时,您将玩家位置指针移动到表示位置的树节点.当您开始游戏时,这种树型结构中充满了要捡起的东西、怪物等.随机种子也可用于将怪物散布在周围.

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.

祝你好运

这篇关于我应该如何让我的游戏实体了解他们周围的事物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆