我正在编写一个基于文本的游戏,我希望用户能够操纵任何项目。
我希望用户能够说,例如,“我把一瓶油扔向怪物。” 现在,当这种情况发生时,[ throw ]将是一个 Action ,[一瓶油]是一个具有[包含油]属性的库存元素,除非进一步指定,否则怪物将是房间中最近的怪物。
代码需要分析所有这些事物的各种属性,然后选择导致这些事物交互的操作。
当我编写游戏故事代码时,它将如下所示
*room.darkroom.1 "wake up" [...] everything appears blurry, the voice sounds strange as if echoing down a long tunnel. From what you can see you can tell [you are in a dark room] your vision blurs from white to dark. [pc is inside very dark room. Inside this room everything is hidden in darkness. The northern wall has a locked thick wooden door. the southern wall has a barrel full of oil containing a bottle of oil. In the eastern side of the room are 2 medium sized crates one crate contains flint and rusty dagger. In the western side of the room a [small window] */room.darkroom.1
脚本将获取所有这些单词并将属性应用于它们,随机化某些未声明的元素。因此,如果您随后说“椅子”,它将转到椅子属性并随机分配未声明的属性。椅子的一项属性是物质。如果没有说明它是一把橡木椅子,那么脚本应该从任何 Material 属性中选择并分配它们。这把新椅子将选取一些属性并保存起来,以供将来在包含所有属性的数据库中引用。
另一个属性是一个 Action 。一把椅子不一定有 Action ……但它可能会坏。脚本需要知道椅子是否易碎。但我认为它可以嵌套到另一个属性中,例如 [toughness]。
许多东西都会有许多相同的属性:椅子、床头柜、 table 在很多方面都是相似的。但根据所采取的行动,它们也会产生不同的结果。
例如,当“ splinter ”包含椅腿和废料时,我该如何指定?椅子腿和废料都需要继承被毁坏的椅子的属性,例如橡木/易燃/易碎。显然,如前所述,如果橡木是具有该值(value)的部分,那么它可能不必继承易燃或易碎的特性。
我希望能够轻松访问存储属性的数组,并且该数组可能包含影响事物行为和描述的脚本。
我开始只是创建一个将循环遍历的键控数组...但现在我认为我应该利用 JavaScript 的基于对象的元素。
这是我开始做的事情,但我立即停止了:
var language = {
// key currentmodifyers possible modifyers action/condition description of item
item: ";medium,material;;its a thing thats not a person",
chair: "item,chair_Legs;;pile_of.@scraps.collection.chairlegx3/broken,@[chair ]scraps/destroyed; and someone could sit on it. ",
chair_leg:"item,blunt,light,"
}
我有太多的定义要写,所以开始写所有的定义,然后当我发现最好的方法时必须重新做一遍,这将是一种耻辱。
我正在研究用 JavaScript 构建对象的各种方法,但希望有经验的人提供最好的方法。
了解我的问题的复杂性,存储和应用属性的最佳方法是什么?
最佳答案
您的问题实际上有两个好的想法:
But now I am thinking that I should utilize the object based element of JavaScript.
你肯定需要使用对象而不是数组,OOP思想可以直接应用到你的描述中。 有些对象由较小的部分组成(OOP 中的“组合”),并且一些继承其他对象的属性(“继承”)。
因此,您可以拥有一些基 Item
类,它定义所有对象的基本操作和属性(就像它们具有不同的属性并且可以执行某些操作)。
然后您可以添加诸如StaticItem
(将是椅子、床头柜等的底座)和LiveBeeing
(怪物等)之类的对象。
现在您可以实际创建主席
(伪代码):
StaticItem extends Item
this.material = oak // oak is an object of OakMaterial
// is this static item flamable?
this.isFlamable = function() {
// we "redirect" flamability detection to the material
return this.material.flamable();
}
Chair extends Static
// break this chair!
this.break = function() {
// when it is broken we return a HeapOfCrap, it is a
// Static subclass which contains some sub-items
// In this case the heap contains 4 chair legs of the
// same material as the chair
return HeapOfCrap([
new ChairLeg(this.material), new ChairLeg(this.material),
new ChairLeg(this.material), new ChairLeg(this.material)
])
}
等等,您需要仔细规划对象结构,并且有一个很好的领域可以应用 design patterns 中的一些内容。 .
上面的例子只是我立即想到的,而实际的设计需要更多的时间和思考。
您问题中的第二个好想法是关于大量的工作:
I have so many definitions to write so it would be a shame to start writing it all and then have to do it over when I discover the best way.
您绝对会选择使用对象或数组,还是其他任何东西 - 不要试图立即描述您的所有游戏。
选择一些基本元素,例如“房间”、“门”、“椅子”、“怪物”、“玩家”,并尝试仅使用这些元素编写最小的游戏。
尝试结构以找到好的设计,一旦确定它是好的 - 继续向游戏添加更多元素。
关于javascript - 基于 javascript 文本的游戏和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35440314/