【发布时间】:2012-06-13 15:12:31
【问题描述】:
我有这些数据类型:
data PointPlus = PointPlus
{ coords :: Point
, velocity :: Vector
} deriving (Eq)
data BodyGeo = BodyGeo
{ pointPlus :: PointPlus
, size :: Point
} deriving (Eq)
data Body = Body
{ geo :: BodyGeo
, pict :: Color
} deriving (Eq)
它是我游戏中角色、敌人、物体等的基本数据类型(嗯,我现在只有两个矩形作为玩家和地面:p)。
当一个键时,字符通过更改其velocity 向右、向左或跳跃。通过将velocity 添加到coords 来完成移动。目前是这样写的:
move (PointPlus (x, y) (xi, yi)) = PointPlus (x + xi, y + yi) (xi, yi)
我只是使用Body 的PointPlus 部分,而不是整个Body,否则会是:
move (Body (BodyGeo (PointPlus (x, y) (xi, yi)) wh) col) = (Body (BodyGeo (PointPlus (x + xi, y + yi) (xi, yi)) wh) col)
move 的第一个版本更好吗?无论如何,如果move 只更改PointPlus,则必须有另一个函数在新的Body 中调用它。我解释一下:有一个函数update 被调用来更新游戏状态;它传递当前的游戏状态,现在是单个Body,并返回更新后的Body。
update (Body (BodyGeo (PointPlus xy (xi, yi)) wh) pict) = (Body (BodyGeo (move (PointPlus xy (xi, yi))) wh) pict)
这让我发痒。除了PointPlus,Body 中的所有内容都保持不变。有没有办法手动避免这种完全的“重建”?喜欢在:
update body = backInBody $ move $ pointPlus body
当然不必定义backInBody。
【问题讨论】: