Skip to content

Commit

Permalink
Fix #197 draft with immutable center position wrapper to update to a …
Browse files Browse the repository at this point in the history
…normal position when game runs.
  • Loading branch information
isaiaslafon committed Oct 8, 2024
1 parent 020bf37 commit 9311d4a
Showing 1 changed file with 70 additions and 30 deletions.
100 changes: 70 additions & 30 deletions src/wollok/game.wlk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object game {
const visuals = []
/** Is Game running? */
var property running = false
/** A center */
/** A center manager to delegate calculate object default positionas center until game run. */
var centerManager = new CenterBeforeRun()
/**
* Allows to configure a visual component as "error reporter".
Expand Down Expand Up @@ -84,7 +84,6 @@ object game {
io.addEventHandler(event, action)
}


/**
* Adds a block that will be executed while the given object collides with other.
* Two objects collide when are in the same position.
Expand All @@ -96,7 +95,7 @@ object game {
*/
method whenCollideDo(visual, action) {
io.addCollitionHandler(visual.identity(), { =>
self.colliders(visual).forEach({it => action.apply(it)})
self.colliders(visual).forEach({it => action.apply(it)})
})
}

Expand Down Expand Up @@ -334,39 +333,67 @@ object game {
}

class AbstractCenterManager{


/**
* @private
*/
method xCenter() = game.width().div(2)


/**
* @private
*/
method yCenter() = game.height().div(2)


/**
* @private
*/
method center()


/**
* @private
*/
method center(xOffset, yOffset)

}

class CenterRunning inherits AbstractCenterManager{

/**
* @private
*/
override method center() = new Position(x = self.xCenter(), y = self.yCenter())

/**
* @private
*/
method center(xOffset, yOffset) = new Position(x = self.xCenter() + xOffSet, y = self.yCenter() + yOffset)

}

class CenterBeforeRun inherits AbstractCenterManager{
/** Collection of centers to update to a fixed x y values when game Run. */
const centers = #{}

/**
* @private
*/
method start(){
centers.forEach(center => center.start())
centers.clear()
}

/**
* @private
*/
override method center(){
const center = new CenterPosition(centerManager = self)
centers.add(center)
return center
}

/**
* @private
*/
override method center(xOffset, yOffset){
const center = new CenterPosition(centerManager = self, xOffset = xOffset, yOffset = yOffset)
centers.add(center)
Expand Down Expand Up @@ -451,83 +478,96 @@ class AbstractPosition {
}

class CenterPre{
/** to update and ask new Positions in ther center while game is Running or not. */
const centerManager
/** x and y offset values to save transformations when game Runs. */
const xOffset = 0
const yOffset = 0

/**
* @private
*/
method x() = centerManager.xCenter() + xOffset

/**
* @private
*/
method y() = centerManager.yCenter() + yOffset

/**
* Returns a new Position n steps right from this one.
*/
/**
* @private
*/
method right(n) = centerManager.center(xOffset = xOffset + n, yOffSet = yOffset)

/**
* Returns a new Position n steps left from this one.
*/
/**
* @private
*/
method left(n) = centerManager.center(xOffset = xOffset - n, yOffSet = yOffset)

/**
* Returns a new Position n steps up from this one.
*/
/**
* @private
*/
method up(n) = centerManager.center(xOffset = xOffset, yOffSet = yOffset + n)

/**
* Returns a new Position, n steps down from this one.
*/
/**
* @private
*/
method down(n) = centerManager.center(xOffset = xOffset, yOffSet = yOffset - n)


/**
* @private
*/
method clone() = centerManager.center(xOffset, yOffSet)

/**
* @private
*/
method round() = centerManager.center(xOffset.round(), yOffset.round())
}

class CenterPosition inherits AbstractPosition{
var position = new CenterPre()

//To change coordinates to a fixed value.
/**
* @private
*/
method start(){
position = new Position(x = position.x(), y = position.y())
}

//Post calculate x until game start, the is like an unmutable Position.
override method x() = position.x()

//Post calculate x until game start, the is like an unmutable Position.
override method y() = position.y()

/**
* Returns a new Position n steps right from this one while Running, a lazy center with offset while idle.
* Returns a new Position n steps right from this one while Running, or a lazy center with offset while idle.
*/
override method right(n) {
return position.right(n)
}

/**
* Returns a new Position n steps left from this one while Running, a lazy center with offset while idle.
* Returns a new Position n steps left from this one while Running, or a lazy center with offset while idle.
*/
override method left(n) {
return position.left(n)
}

/**
* Returns a new Position n steps up from this one while Running, a lazy center with offset while idle.
* Returns a new Position n steps up from this one while Running, or a lazy center with offset while idle.
*/
override method up(n) {
return position.up(n)
}

/**
* Returns a new Position, n steps down from this one while Running, a lazy center with offset while idle.
* Returns a new Position, n steps down from this one while Running, or a lazy center with offset while idle.
*/
override method down(n) {
return position.down(n)
}

/**
* Returns a new Position is the game is Running with the same coordinates, a new lazy center with offset while idle.
* Returns a new Position is the game is Running with the same coordinates, or a lazy center with offset while idle.
*/
override method clone(){
return position.clone()
Expand All @@ -538,7 +578,7 @@ class CenterPosition inherits AbstractPosition{
}

/**
* Returns a new position with its coordinates rounded if Running, or a center with offset rounded.
* Returns a new position with its coordinates rounded if Running, or a lazy center with rounded offset while idle.
*/
method round() = position.round()
}
Expand Down

0 comments on commit 9311d4a

Please sign in to comment.