简体   繁体   English

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

[英]How should I store and and use gun fire data in a game?

On this game I have 3 defense towers (the number is configurable) which fire a "bullet" every 3 seconds at 30km/h. 在此游戏中,我有3座防御塔(数量可配置),每3秒以30公里/小时的速度发射“子弹”。 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. 让我们假设我现在只需要存储它(我已经为3D点定义了一个结构)。

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. 可以正常工作且易于使用和实现的基本功能已绰绰有余。

PS: I didn't post this question on the game development website (despite the tag) because I think it fits better here. PS:我没有在游戏开发网站上发布此问题(尽管有标签),因为我认为这里更合适。

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. 给定您的游戏模型,我不会选择不允许删除O(1)的任何数据结构。 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. 编辑 对于稍微复杂的问题,STL类始终会复制放入其中的元素,因此在实践中,如果任何属性可能在对象的生命周期内发生变化,则需要在堆上分配结构并存储(智能?)指针给他们的收藏。

I'd probably use a std::vector or std::list . 我可能会使用std::vectorstd::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. 警告:如果您要为非常受限的平台(缓慢的CPU,很少的内存)进行编码,那么使用普通的固定大小的C数组可能是有意义的。 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. 从最容易编写的代码开始,然后在只有事实证明您负担不起抽象时才进行更改。

I guess you can start off with std::vector<BulletInfo> and see how it works from there. 我想您可以从std::vector<BulletInfo> ,然后从那里开始看看它是如何工作的。 It provides the array like interface but is dynamically re-sizable. 它提供了类似数组的接口,但是可以动态调整大小。

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. 稍微复杂一点的是,我想在外部经理中保留子弹列表,例如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. 但是对我来说,好处是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM