debug output of game objects in Haskell/Yampa and HOOD

ε祈祈猫儿з 提交于 2019-12-10 19:38:09

问题


I'm stuck with generating debug output for my game objects using Haskell/Yampa (=Arrows) (with HOOD).

My engine basically runs a list of game objects which produce Output states (line, circle) which are then rendered.

data Output = Circle Position2 Double | Line Vector2

output :: [Output] -> IO ()
output oos = mapM render oos

render :: Output -> IO ()
render (Circle p r) = drawCircle p r
render (Line   vec) = drawLine (Point2 0 0) vec

The player object just moves to the right and is represented as a (positioned) circle.

playerObject :: SF () Output -- SF is an Arrow run by time
   p <- mover (Point2 0 0) -< (Vector2 10 0)
   returnA -< (Circle p 2.0)

mover is just a simple integrator (acceleration->velocity->position) where I want to observe the velocity and render it as debug output as an (unpositioned) Line.

mover :: Position2 -> SF Vector2 Position2
mover position0 = proc acceleration -> do
    velocity <- integral -< acceleration -- !! I want to observe velocity
    position <- (position0 .+^) ^<< integral -< velocity
    returnA -< position

How can I create additional graphical debug output for internal values of my game object functions?

What actually should happen is in output, first render the actual object (circle) but also render additional debug output (movement vector as line). Probably I can achieve this with HOOD but I'm still not fluent in Haskell and don't know how do adopt the HOOD tutorial for my case.


回答1:


I don't know HOOD but Debug.Trace is easy:

> import Debug.Trace
> mover position0 = proc acceleration -> do
> > velocity <- integral -< acceleration
> > position <- trace ("vel:" ++ show velocity ++ "\n") $
> > > > > > > > > > > (position0 .+^) ^<< integral -< velocity
> > returnA -< position

Note that it shouldn't be put on the line defining velocity.




回答2:


What you probably want to do is make mover more flexible, to support adding out-of-band debug information (the value of velocity) to be rendered. I don't think HOOD is relevant for your problem, since you already have the FRP framework for handling continuously changing values. Just arrange for velocity to be output.

Something like:

mover :: Position2 -> SF Vector2 (Position2, Vector2)
mover position0 = proc acceleration -> do
    velocity <- integral -< acceleration -- !! I want to observe velocity
    position <- (position0 .+^) ^<< integral -< velocity
    returnA -< (position, velocity)

playerObject :: SF () [Output]
   (p, v) <- mover (Point2 0 0) -< (Vector2 10 0)
   returnA -< [Circle p 2.0, Line v]


来源:https://stackoverflow.com/questions/3255334/debug-output-of-game-objects-in-haskell-yampa-and-hood

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!