之前写小游戏(非专业,纯玩)都是习惯面向对象。大概感觉是这样
class Object()
#states: [position, health, direction...]
#methods: [move, serializer]
class Player(Object)
#player_methods: [attack...]
class Boss(Object)
#boss_methods: [...]
p1, p2 = new Player(), new Player()
boss = new Boss()
p1.move(..)
p2.move(..)
boss.attack(..)
Renderer.render(p1, p2, boss)
学了几天 elixir 后发现函数式也挺顺手,但是不知道有没有实际用函数式开发游戏的。
global_state<Immutable> {
player1_pos: ..
player1_health: ..
player2_pos: ..
player2_health: ..
boss_pos: ..
boss_health: ..
...
}
fn move: state -> new_state
fn move_player: () -> state -> new_state
fn move_player_by_num: num -> () -> state -> new_state
fn move_boss: state -> new_state
fn boss_attack: state -> new_state
global_state
|> move_player_by_num(1)
|> move_player_by_num(2)
|> move_boss
|> bos_attack
|> render
感觉除了内存占用大一些没其他坏处,有有经验的分享下么?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.