Reputation: 475
I want to create a kind of components about moving
geometrical units in some games. So, I am trying in this way:
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
}
}
}
thus, I want to call later as properties
let say of these GeometricalUnits in order to make them moving according special functions...
I would like to know if a better organization can be than mine which seems boring?!
My solution: this is my solution in general:
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"
}
}
this is how I am initiating pieces, in the same way I am moving them with another method that is calling functions based on the Movable Pieces Types
Upvotes: 0
Views: 113
Reputation: 11741
How about this ?
def Move(what:Movable, where:MoveFunction) : Movable
should take a Movable
(an immutable state object of what you want to move) and a higher-order function MoveFunction
where you want to move it (a function that can be applied to a Movable
. Each function should then take an existing state and return a new state by applying the higher order function (MoveFunction
) to the Movable
object.
Upvotes: 1