受歧视的工会和继承
我正在寻找为我的 F# 项目创建一个场景图,例如:
root
->player_bob
-->torch
->enemy_1
-->extravagant_hat
-->enemies_cat_jess
--->fleas
--->fur_ball
->loot
等等。
每个项目都需要保存一组游戏对象来代表它的子项目。
例如,敌人1的列表包含一只猫和一顶帽子,猫列表包含跳蚤和一个毛球
所以我计划让它们全部继承自一个类,该类包含一个描述对象子对象的集合。
现在回答我的问题: 我是否应该将子对象向下转换为 GameObject 并将它们存储在“GameObject”基类的列表中 或者创建一个有区别的联合,例如
type SceneObject =
|Character of Character //e.g player, enemy, cat
|Item of Item //e.g hat, torch, furball
并将对象存储为“SceneObjects”列表,以避免向上转换它们等带来的任何问题/开销。以及允许我描述对象未渲染和/或未渲染的特殊情况用于碰撞检测,例如:声音发射器、陷阱触发器等。
可区分联合+继承组合是我最初的想法;不过,由于我是 FP 的新手,我认为向专业人士询问解决此问题的最佳、实用的方法是明智的。
谢谢,
JD
I'm looking to create a scene-graph for my F# project somthing like:
root
->player_bob
-->torch
->enemy_1
-->extravagant_hat
-->enemies_cat_jess
--->fleas
--->fur_ball
->loot
etc,etc.
Each item needs to hold a collection of game objects to represent it's children.
e.g enemy1's list contains a cat and a hat and the cats list contains fleas and a fur ball
So I plan to make them all inherit from a class that contains a collection that describes that objects children.
Now to my question:
Should I down-cast child objects to GameObject's and store them in a list of the "GameObject" base class
OR create a discriminating union e.g
type SceneObject =
|Character of Character //e.g player, enemy, cat
|Item of Item //e.g hat, torch, furball
And store objects as a list of "SceneObjects" to avoid any problems/overheads with up-casting them, etc. As well as allowing me to describe special cases where the object isn't rendered and/or not used in collision detection e.g: sound emitters, trap triggers, etc.
The discriminated union + inheritance combo is my initial thought; though, as I'm new to FP, I thought it wise to ask the pro's for the best, functional, way of approaching this.
Thanks,
JD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以递归地使用可区分联合。
像这样使用它
You can use the discriminated union recursively.
And use it like this
Dario 建议的另一种选择是为游戏中的实际对象(例如物品和角色)声明一个单独的类型,并将其包装在另一个添加子对象列表的对象中。这看起来像这样:
两个选项都是正确的功能设计,但我更喜欢这个版本,因为它使某些类型的处理更容易。例如,如果你想遍历游戏树中的所有对象,你可以这样写:
在这个版本中,你根本不需要对
SceneObject
进行模式匹配,所以你不需要需要包含所有情况(例如Item
和Character
)。An alternative to what Dario suggest is to declare a separate type for actual objects in the game (such as items and characters) and wrap it in another object that adds a list of child objects. This would look like this:
Both of the options are correct functional design, but I would prefer this version, because it makes certain kinds of processing easier. For example, if you wanted to traverse all objects in the game tree, you can write something like:
In this version, you don't need to pattern match on
SceneObject
at all, so you don't need to include all the cases (such asItem
andCharacter
).