函数式编程适不适合游戏开发

查看 4|回复 0
作者:cj323   
之前写小游戏(非专业,纯玩)都是习惯面向对象。大概感觉是这样
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[I] {
  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
感觉除了内存占用大一些没其他坏处,有有经验的分享下么?
您需要登录后才可以回帖 登录 | 立即注册

返回顶部