Spy*_*ros 4 ruby dsl ruby-on-rails
我读过Ruby非常适合特定领域的语言.在过去的几个月里,我一直在创建一个rpg类型的浏览器游戏.在某些时候,我希望用户能够完成任务并完成任务.任务可以是杀死x数量的怪物,杀死raid boss,也许收集一些物品等等.
整个过程听起来很有趣,容易出错.我也在想,为此创建一个DSL是个好主意.一种用简单语言描述任务的方法.但我对此没有多少经验.
你认为这是个好主意吗?如果是这样,你有什么建议/教程可以提出建议吗?
这是一个适合您的启动器:
module RPG
def quest
puts "starting your quest"
yield
end
def move direction
puts "moving to the #{direction.to_s}"
yield if block_given?
end
def door action
puts "#{action.to_s} door"
yield if block_given?
end
end
Run Code Online (Sandbox Code Playgroud)
游戏作者可以写下以下内容:
require 'rpg'
include RPG
quest do
move :left
move :right
door :open do
move :left
end
end
Run Code Online (Sandbox Code Playgroud)
运行收益率:
> ruby game.rb
starting your quest
moving to the left
moving to the right
opening door
moving to the left
Run Code Online (Sandbox Code Playgroud)
如果您正在设计DSL,那么您可能需要花些时间考虑您尝试将语言映射到的域.DSL很适合删除您为每项任务编写的重复样板,因此请专注于此.对于你的任务示例,你认为自己在任务之间需要什么常见的东西?显然,很多事情将取决于任务如何在"幕后"实施.
我可以想象一下这样的任务看起来像这样:
Qwest "Retrieve the Grail" do
given_by :pope
description "Some hethan dragon took my cup, go get it back!"
condition "Slay a dragon" do
dragon.is_dead?
end
condition "Grab the Grail" do
player.inventory.contains :grail
end
reward :phat_loot
end
Run Code Online (Sandbox Code Playgroud)
在这里,DSL可用于创建任务,为其命名,条件,奖励,并将其分配给任务给予者.
至于编写DSL,你会想要了解ruby中的元编程.我知道为什么_the_lucky_stiff已经写了一两篇关于它的文章,而尖锐的指南有一章关于它(第6章中的Dwemthy的数组).就个人而言,我总是很难理解为什么写这些东西.我最终购买了Metaprogramming Ruby,我发现它非常有用.
归档时间: |
|
查看次数: |
954 次 |
最近记录: |