我应该如何在游戏中存储和使用枪支射击数据?

发布于 2024-11-05 18:58:52 字数 363 浏览 1 评论 0原文

在这个游戏中,我有 3 个防御塔(数量可配置),每 3 秒以 30 公里/小时的速度发射一颗“子弹”。这些防御塔有雷达,只有当玩家位于塔雷达下方时它们才会开始射击。那不是问题。

我的问题是如何存储枪支射击的数据。我不确定每颗子弹到底需要什么数据,但我想到的当然是子弹的位置。假设我现在只需要存储它(我已经为 3D 点定义了一个结构)。

我是否应该尝试计算出游戏在特定点可以拥有的最大子弹并声明一个具有该大小的数组?我应该使用链表吗?或者也许是别的什么?

我真的不知道该怎么做。我不需要任何花哨或复杂的东西。一些基本的、可以工作的、易于使用和实施的东西就足够了。

PS:我没有在游戏开发网站上发布这个问题(尽管有标签),因为我认为它更适合放在这里。

On this game I have 3 defense towers (the number is configurable) which fire a "bullet" every 3 seconds at 30km/h. These defense towers have a radar and they only start firing when the player is under the tower radar. That's not the issue.

My question is how to store the data for the gun fire. I'm not sure exactly what data do I need for each bullet, but one that comes to mind is the position of the bullet of course. Let's assume that I only need to store that (I already have a struct defined for a 3D point) for now.

Should I try to figure it out the maximum bullets the game can have at a particular point and declare an array with that size? Should I use a linked-list? Or maybe something else?

I really have no idea how to do this. I don't need anything fancy or complex. Something basic that just works and it's easy to use and implement is more than enough.

P.S: I didn't post this question on the game development website (despite the tag) because I think it fits better here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清旖 2024-11-12 18:58:52

一般来说,固定长度数组不是一个好主意。

考虑到你的游戏模型,我不会选择任何不允许 O(1) 删除的数据结构。无论如何,这排除了普通数组,并且可能建议使用链表。然而,应该通过使用具有正确属性的通用容器类来抽象出底层细节。

至于您应该存储的内容

  1. 位置(如您提到的)
  2. 速度
  3. 伤害系数(您的枪可以升级,不是吗?)
  4. 最大射程(同上)

编辑 对于稍微复杂的问题,STL 类总是获取放入其中的元素的副本,因此在实践中,如果任何属性可能在对象的生命周期内发生变化,您将需要在堆上分配结构并在集合中存储(智能?)指向它们的指针。

Generally, fixed length arrays aren't a good idea.

Given your game model, I wouldn't go for any data structure that doesn't allow O(1) removal. That rules out plain arrays anyway, and might suggest a linked list. However the underlying details should be abstracted out by using a generic container class with the right attributes.

As for what you should store:

  1. Position (as you mentioned)
  2. Velocity
  3. Damage factor (your guns are upgradeable, aren't they?)
  4. Maximum range (ditto)

EDIT To slightly complicated matters the STL classes always take copies of the elements put in them, so in practise if any of the attributes might change over the object's lifetime you'll need to allocate your structures on the heap and store (smart?) pointers to them in the collection.

格子衫的從容 2024-11-12 18:58:52

我可能会使用 std::vectorstd::list。什么都是最简单的。

警告:如果您正在为一个非常受限的平台(CPU 速度慢、内存少)进行编码,那么使用普通的固定大小 C 数组可能是有意义的。但现在这种情况不太可能发生。从最容易编码的内容开始,当且仅当事实证明您无法承担抽象时才更改它。

I'd probably use a std::vector or std::list. Whatever's easiest.

Caveat: If you are coding for a very constrained platform (slow CPU, little memory), then it might make sense to use a plain-old fixed-size C array. But that's very unlikely these days. Start with whatever is easiest to code, and change it later if and only if it turns out you can't afford the abstractions.

瞄了个咪的 2024-11-12 18:58:52

我想您可以从 std::vector 开始,然后看看它是如何工作的。它提供类似数组的接口,但可以动态调整大小。

I guess you can start off with std::vector<BulletInfo> and see how it works from there. It provides the array like interface but is dynamically re-sizable.

最偏执的依靠 2024-11-12 18:58:52

在这种情况下,我更喜欢使用稍微复杂的方法来管理项目符号。由于屏幕上可能出现的子弹数量与塔的数量直接相关,因此我会在每个塔类中保留一个小的固定长度的子弹数组。每当一座塔发射子弹时,它都会搜索其阵列,找到未使用的子弹,用新的位置/速度设置子弹并将其标记为活动状态。

稍微复杂的部分是我喜欢在外部管理器中保留第二个项目符号列表,例如 BulletManager。当每个塔创建时,塔会将其所有项目符号添加到中央管理器。然后中央管理器可以负责更新项目符号。

我喜欢这种方法,因为它可以轻松地让我管理与项目符号相关的内存限制,只需调整“活动塔的数量”数量,所有项目符号都会为您创建。您不需要动态分配子弹,因为它们都是汇集在一起​​的,并且您不只有一个中央池,在添加/删除塔时需要不断更改其大小。

它确实涉及轻微的移动开销,因为有一个带有指针列表的中央管理器。并且您需要小心,始终从中央管理器处移除被摧毁的塔中的任何子弹。但对我来说,这些好处是值得的。

In instances like this I prefer a slightly more complex method to managing bullets. Since the number of bullets possible on screen is directly related to the number of towers I would keep a small fixed length array of bullets inside each tower class. Whenever a tower goes to fire a bullet it would search through its array, find an un-used bullet, setup the bullet with a new position/velocity and mark it active.

The slightly more complex part is I like to keep a second list of bullets in an outside manager, say a BulletManager. When each tower is created the tower would add all its bullets to the central manager. Then the central manager can be in charge of updating the bullets.

I like this method because it easily allows me to manage memory constrains related to bullets, just tweak the 'number of active towers' number and all of the bullets are created for you. You don't need to allocate bullets on the fly because they are all pooled, and you don't have just one central pool that you constantly need to change the size of as you add/remove towers.

It does involve slightly move overhead because there is a central manager with a list of pointers. And you need to be careful to always remove any bullets from a destroyed tower from the central manager. But for me the benefits are worth it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文