我想在某些游戏中创建一种关于moving
几何单位的组件。所以,我正在尝试这样:
trait Move
case class MoveHorizontal(val velocityMvH: Int) extends Move
case class MoveVertical(val velocityMvV: Int) extends Move
case class MoveRandomly() extends Move
case class MoveDiagonally(val velocityMvDx: Int = 0, val velocityMvDy: Int = 0) extends Move
case class MoveDiagonal() extends Move
case class MoveReverseX() extends Move
case class MoveReverseY() extends Move
class Moving(gu: GeometricalUnit, mv: () => Unit) extends Move {
val velocity = 20
var xVelocity: Int = velocity
var yVelocity: Int = -velocity
if (gu.isMovable == true) {
mv match {
case MoveHorizontal(mvH) => gu.dim.x += mvH
case MoveVertical(mvV) => gu.dim.y += mvV
case MoveRandomly() => moveRandomly()
case MoveDiagonally(mvDx, mvDy) => moveDiagonally(mvDx, mvDy)
case MoveDiagonal() => moveDiagonally()
case MoveReverseX() => xVelocity *= -1
case MoveReverseY() => yVelocity *= -1
}
}
}
因此,我想稍后调用properties
来说明这些GeometricalUnits,以便让它们按照特殊功能移动...
我想知道一个更好的组织是否比我的看起来更无聊?!
我的解决方案: 这是我的解决方案:
def initiatePieces(s: Movable, x: Int, y: Int) = {
nrInitiations += 1
s.dimM.x = x
s.dimM.y = y
s match {
case _: BBall => randomMvmBB()
case _: WBall => randomMvmWB()
case _: PBall => randomMvmPB()
case _ => "nothing"
}
}
这就是我启动碎片的方式,就像我使用另一种基于Movable Pieces Types调用函数的方法移动它们一样
答案 0 :(得分:1)
这个怎么样?
def Move(what:Movable, where:MoveFunction) : Movable
应该使用Movable
(您想要移动的不可变状态对象)和要移动它的高阶函数MoveFunction
(可以应用于{的函数{1}}。然后,每个函数应该采用现有状态,并通过将更高阶函数(Movable
)应用于MoveFunction
对象来返回新状态。