From ef5f30022ade0172a1708661152fd5396f2c5bf8 Mon Sep 17 00:00:00 2001 From: isaiaslafon Date: Fri, 4 Oct 2024 12:36:17 -0300 Subject: [PATCH 1/7] game with a mutable reference to center to update center when game starts --- src/wollok/game.wlk | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/wollok/game.wlk b/src/wollok/game.wlk index 40bb6c18..3d6cf496 100644 --- a/src/wollok/game.wlk +++ b/src/wollok/game.wlk @@ -4,7 +4,7 @@ import wollok.vm.runtime * Wollok Game main object */ object game { - + const center = new MutablePosition(x = self.xCenter(), y = self.yCenter()) /** Collection of visual objects in the game */ const visuals = [] /** Is Game running? */ @@ -219,6 +219,7 @@ object game { * Starts render the board in a new windows. */ method start() { + self.updateCenter() self.running(true) io.exceptionHandler({ exception => exception.printStackTrace() }) io.domainExceptionHandler({ exception => @@ -241,7 +242,30 @@ object game { /** * Returns the center board position (rounded down). */ - method center() = self.at(self.width().div(2), self.height().div(2)) + method center() = center + + //Subtask to use on Start. + method updateCenter(){ + self.updateCenterX() + self.updateCenterY() + } + + method updateCenterX(){ + center.x(self.xCenter()) + } + + method updateCenterY(){ + center.y(self.yCenter()) + } + + //To not duplicate in default and after defeault. + method xCenter() = self.midRoundDown(self.width()) + + //To not duplicate in default and after defeault. + method yCenter() = self.midRoundDown(self.height()) + + //To not duplicate div(2) on xCenter and yCenter. Can be remove and duplicated. + method midRoundDown(n) = n.div(2) /** * Sets game title. From 721dea736d9d646fbb2db1dfa8d710bf74e4d4ba Mon Sep 17 00:00:00 2001 From: isaiaslafon Date: Fri, 4 Oct 2024 21:27:29 -0300 Subject: [PATCH 2/7] alternative to center as a mutable position, start at 0,0 and collect every use, when game start then update the offset of each one in the collection and change to a center running strategy. Not modeled as a full state to not add a setter in game for center attribute. --- src/wollok/game.wlk | 111 +++++++++++++++++++++++++++++++++----------- 1 file changed, 84 insertions(+), 27 deletions(-) diff --git a/src/wollok/game.wlk b/src/wollok/game.wlk index 3d6cf496..d00f0d88 100644 --- a/src/wollok/game.wlk +++ b/src/wollok/game.wlk @@ -4,11 +4,12 @@ import wollok.vm.runtime * Wollok Game main object */ object game { - const center = new MutablePosition(x = self.xCenter(), y = self.yCenter()) /** Collection of visual objects in the game */ const visuals = [] /** Is Game running? */ var property running = false + + var center = new centerBeforeStart() /** * Allows to configure a visual component as "error reporter". * Then every error in game board will be reported by this visual component, @@ -219,14 +220,15 @@ object game { * Starts render the board in a new windows. */ method start() { - self.updateCenter() + center.updatePositions() + center = new CenterRunning() self.running(true) io.exceptionHandler({ exception => exception.printStackTrace() }) io.domainExceptionHandler({ exception => const reporter = if (errorReporter == null) exception.source() else errorReporter self.say(reporter, exception.message())}) } - + /** * Returns a position for given coordinates. */ @@ -240,33 +242,13 @@ object game { method origin() = self.at(0, 0) /** - * Returns the center board position (rounded down). + * Returns a center board mutable position (rounded down). + * (Before game start is 0,0 then offset to current Game Board Size). */ - method center() = center - - //Subtask to use on Start. - method updateCenter(){ - self.updateCenterX() - self.updateCenterY() - } - - method updateCenterX(){ - center.x(self.xCenter()) - } - - method updateCenterY(){ - center.y(self.yCenter()) + method center() { + return center.center() } - //To not duplicate in default and after defeault. - method xCenter() = self.midRoundDown(self.width()) - - //To not duplicate in default and after defeault. - method yCenter() = self.midRoundDown(self.height()) - - //To not duplicate div(2) on xCenter and yCenter. Can be remove and duplicated. - method midRoundDown(n) = n.div(2) - /** * Sets game title. */ @@ -350,6 +332,81 @@ object game { } +/** + * A class to get a mutable position in the center of the board when game is running. + * Is used by game object. + */ +class CenterRunning{ + + /** + * @private + */ + method center(){ + return new MutablePosition(x = self.xCenter(), y = self.yCenter()) + } + + /** + * @private + */ + method xCenter() = game.width().div(2) + + /** + * @private + */ + method yCenter() = game.height().div(2) + + //method midRoundDown(n) = n.div(2) + +} + +/** + * A class to get a mutable position in 0,0 until game start. + * The every requested mutable position get centered by the offset of the current Board Size. + * Don't have any other use. + */ +class CenterBeforeStar inherits CenterRunning{ + + /** + * @private + */ + const toCenterPositions = #{} + + /** + * @private + */ + override method center(){ + const toCenterLater = new MutablePosition(x = 0, y = 0) + toCenterPositions.add(toCenterLater) + + return toCenterLater + } + + /** + * @private + */ + method offsetPos(){ + return new Position(x = self.xCenter(), y = self.yCenter()) + } + + /** + * @private + */ + method centerPos(position, offsetPos){ + position.goRight(offsetPos.x()) + position.goUp( offsetPos.y()) + } + + /** + * @private + */ + method updatePositions(){ + const offsetPos = self.offsetPos() //To not redo the offset fot every position update. + toCenterPositions.forEach({ position => self.centerPos(position, offsetPos)}) + toCenterPositions.clear() //Can be omited if garbage collector take care after CenterBeforeStar instance is not referenced by game. + } + +} + class AbstractPosition { method x() From 020bf3746ee0d925a5f1ec6e6b2597dca029b70e Mon Sep 17 00:00:00 2001 From: isaiaslafon Date: Tue, 8 Oct 2024 06:02:05 -0300 Subject: [PATCH 3/7] #197 c inmutable center with offset wrapper. --- src/wollok/game.wlk | 201 ++++++++++++++++++++++++++++---------------- 1 file changed, 130 insertions(+), 71 deletions(-) diff --git a/src/wollok/game.wlk b/src/wollok/game.wlk index d00f0d88..c88b94db 100644 --- a/src/wollok/game.wlk +++ b/src/wollok/game.wlk @@ -8,8 +8,8 @@ object game { const visuals = [] /** Is Game running? */ var property running = false - - var center = new centerBeforeStart() + /** A center */ + var centerManager = new CenterBeforeRun() /** * Allows to configure a visual component as "error reporter". * Then every error in game board will be reported by this visual component, @@ -25,7 +25,7 @@ object game { self.height(5) self.cellSize(50) self.ground("ground.png") - } + } /** * Adds an object to the board for drawing it. @@ -94,10 +94,10 @@ object game { * Example: * game.whenCollideDo(pepita, { comida => pepita.comer(comida) }) */ - method whenCollideDo(visual, action) { + method whenCollideDo(visual, action) { io.addCollitionHandler(visual.identity(), { => self.colliders(visual).forEach({it => action.apply(it)}) - }) + }) } /** @@ -220,8 +220,9 @@ object game { * Starts render the board in a new windows. */ method start() { - center.updatePositions() - center = new CenterRunning() + centerManager.start() + centerManager = new CenterRunning() + self.running(true) io.exceptionHandler({ exception => exception.printStackTrace() }) io.domainExceptionHandler({ exception => @@ -246,7 +247,7 @@ object game { * (Before game start is 0,0 then offset to current Game Board Size). */ method center() { - return center.center() + return centerMaker.center() } /** @@ -332,79 +333,45 @@ object game { } -/** - * A class to get a mutable position in the center of the board when game is running. - * Is used by game object. - */ -class CenterRunning{ +class AbstractCenterManager{ - /** - * @private - */ - method center(){ - return new MutablePosition(x = self.xCenter(), y = self.yCenter()) - } - - /** - * @private - */ method xCenter() = game.width().div(2) - - /** - * @private - */ + method yCenter() = game.height().div(2) - - //method midRoundDown(n) = n.div(2) + + method center() + + method center(xOffset, yOffset) } -/** - * A class to get a mutable position in 0,0 until game start. - * The every requested mutable position get centered by the offset of the current Board Size. - * Don't have any other use. - */ -class CenterBeforeStar inherits CenterRunning{ +class CenterRunning inherits AbstractCenterManager{ - /** - * @private - */ - const toCenterPositions = #{} - - /** - * @private - */ - override method center(){ - const toCenterLater = new MutablePosition(x = 0, y = 0) - toCenterPositions.add(toCenterLater) + override method center() = new Position(x = self.xCenter(), y = self.yCenter()) - return toCenterLater - } - - /** - * @private - */ - method offsetPos(){ - return new Position(x = self.xCenter(), y = self.yCenter()) + method center(xOffset, yOffset) = new Position(x = self.xCenter() + xOffSet, y = self.yCenter() + yOffset) + +} + +class CenterBeforeRun inherits AbstractCenterManager{ + const centers = #{} + + method start(){ + centers.forEach(center => center.start()) + centers.clear() } - /** - * @private - */ - method centerPos(position, offsetPos){ - position.goRight(offsetPos.x()) - position.goUp( offsetPos.y()) - } - - /** - * @private - */ - method updatePositions(){ - const offsetPos = self.offsetPos() //To not redo the offset fot every position update. - toCenterPositions.forEach({ position => self.centerPos(position, offsetPos)}) - toCenterPositions.clear() //Can be omited if garbage collector take care after CenterBeforeStar instance is not referenced by game. - } + override method center(){ + const center = new CenterPosition(centerManager = self) + centers.add(center) + return center + } + override method center(xOffset, yOffset){ + const center = new CenterPosition(centerManager = self, xOffset = xOffset, yOffset = yOffset) + centers.add(center) + return center + } } class AbstractPosition { @@ -481,7 +448,99 @@ class AbstractPosition { * Returns a new position with its coordinates rounded */ method round() = self.createPosition(self.x().round(), self.y().round()) - +} + +class CenterPre{ + const centerManager + const xOffset = 0 + const yOffset = 0 + + method x() = centerManager.xCenter() + xOffset + + method y() = centerManager.yCenter() + yOffset + + /** + * Returns a new Position n steps right from this one. + */ + method right(n) = centerManager.center(xOffset = xOffset + n, yOffSet = yOffset) + + /** + * Returns a new Position n steps left from this one. + */ + method left(n) = centerManager.center(xOffset = xOffset - n, yOffSet = yOffset) + + /** + * Returns a new Position n steps up from this one. + */ + method up(n) = centerManager.center(xOffset = xOffset, yOffSet = yOffset + n) + + /** + * Returns a new Position, n steps down from this one. + */ + method down(n) = centerManager.center(xOffset = xOffset, yOffSet = yOffset - n) + + method clone() = centerManager.center(xOffset, yOffSet) + + method round() = centerManager.center(xOffset.round(), yOffset.round()) +} + +class CenterPosition inherits AbstractPosition{ + var position = new CenterPre() + + //To change coordinates to a fixed value. + 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. + */ + 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. + */ + 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. + */ + 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. + */ + 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. + */ + override method clone(){ + return position.clone() + } + + override method createPosition(_x, _y){ + return new Position(x = _x, y = _y) + } + + /** + * Returns a new position with its coordinates rounded if Running, or a center with offset rounded. + */ + method round() = position.round() } /** From 9311d4a89b62e8a3fa0ba1f793e46bb5c2985ed1 Mon Sep 17 00:00:00 2001 From: isaiaslafon Date: Tue, 8 Oct 2024 06:29:56 -0300 Subject: [PATCH 4/7] Fix #197 draft with immutable center position wrapper to update to a normal position when game runs. --- src/wollok/game.wlk | 100 +++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 30 deletions(-) diff --git a/src/wollok/game.wlk b/src/wollok/game.wlk index c88b94db..a71d485a 100644 --- a/src/wollok/game.wlk +++ b/src/wollok/game.wlk @@ -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". @@ -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. @@ -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)}) }) } @@ -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) @@ -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() @@ -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() } From 63d35d2b15169adc35eb3c606e9f7f338ef3798a Mon Sep 17 00:00:00 2001 From: isaiaslafon Date: Tue, 8 Oct 2024 17:43:45 -0300 Subject: [PATCH 5/7] spaces blank line added --- src/wollok/game.wlk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wollok/game.wlk b/src/wollok/game.wlk index a71d485a..f0943de8 100644 --- a/src/wollok/game.wlk +++ b/src/wollok/game.wlk @@ -4,6 +4,7 @@ import wollok.vm.runtime * Wollok Game main object */ object game { + /** Collection of visual objects in the game */ const visuals = [] /** Is Game running? */ @@ -51,7 +52,7 @@ object game { keyboard.down().onPressDo({ visual.position(visual.position().down(1)) }) keyboard.left().onPressDo({ visual.position(visual.position().left(1)) }) keyboard.right().onPressDo({ visual.position(visual.position().right(1)) }) -} + } /** * Removes an object from the board for stop drawing it. * From bb2781af068a1e727a1eeed81a59b57d6cc7dbdb Mon Sep 17 00:00:00 2001 From: isaiaslafon Date: Wed, 22 Jan 2025 12:28:14 -0300 Subject: [PATCH 6/7] Refactor without state/strategy, fixed round method CenterOffset. Added xCenter yCenter game methods. --- src/wollok/game.wlk | 267 ++++++++++++++------------------------------ 1 file changed, 85 insertions(+), 182 deletions(-) diff --git a/src/wollok/game.wlk b/src/wollok/game.wlk index f0943de8..a54a7d77 100644 --- a/src/wollok/game.wlk +++ b/src/wollok/game.wlk @@ -9,8 +9,7 @@ object game { const visuals = [] /** Is Game running? */ var property running = false - /** 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". * Then every error in game board will be reported by this visual component, @@ -220,9 +219,6 @@ object game { * Starts render the board in a new windows. */ method start() { - centerManager.start() - centerManager = new CenterRunning() - self.running(true) io.exceptionHandler({ exception => exception.printStackTrace() }) io.domainExceptionHandler({ exception => @@ -242,14 +238,28 @@ object game { */ method origin() = self.at(0, 0) + /** + * Returns the center X coordinate. + */ + method xCenter(){ //To not duplicade code, could be duplicated. Is also used by CenterOffset, could be implemented there and duplicated in center method. + return self.width().div(2) + } + + /** + * Returns the center Y coordinate. + */ + method yCenter(){ //To not duplicade code, could be duplicated. Is also used by CenterOffset. + return self.height().div(2) + } + /** * Returns a center board mutable position (rounded down). * (Before game start is 0,0 then offset to current Game Board Size). */ method center() { - return centerMaker.center() + return if (running) { new Position(x = self.xCenter(), y = self.yCenter()) } else { new CenterOffset() } } - + /** * Sets game title. */ @@ -333,75 +343,6 @@ 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) - return center - } -} - class AbstractPosition { method x() @@ -478,112 +419,6 @@ class AbstractPosition { method round() = self.createPosition(self.x().round(), self.y().round()) } -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 - - /** - * @private - */ - method right(n) = centerManager.center(xOffset = xOffset + n, yOffSet = yOffset) - - /** - * @private - */ - method left(n) = centerManager.center(xOffset = xOffset - n, yOffSet = yOffset) - - /** - * @private - */ - method up(n) = centerManager.center(xOffset = xOffset, yOffSet = yOffset + n) - - /** - * @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() - - /** - * @private - */ - method start(){ - position = new Position(x = position.x(), y = position.y()) - } - - override method x() = position.x() - - override method y() = position.y() - - /** - * 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, 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, 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, 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, or a lazy center with offset while idle. - */ - override method clone(){ - return position.clone() - } - - override method createPosition(_x, _y){ - return new Position(x = _x, y = _y) - } - - /** - * Returns a new position with its coordinates rounded if Running, or a lazy center with rounded offset while idle. - */ - method round() = position.round() -} - /** * Represents a position in a two-dimensional gameboard. * It is an immutable object since Wollok 1.8.0 @@ -629,6 +464,74 @@ class MutablePosition inherits AbstractPosition { } +class CenterOffset inherits AbstractPosition{ + + /** x and y offset values to save transformations when game Runs. */ + const xOffset = 0 + const yOffset = 0 + + /** x of the current center adding the transformation offset in x*/ + override method x() = game.xCenter() + xOffset + + /** y of the current center adding the transformation offset in y*/ + override method y() = game.yCenter() + yOffset + + /** + * Returns a new Position n steps right from this one while Running, or a lazy center with offset while idle. + */ + override method right(n) = if (game.running()) { new Position(x = self.x() + n, y = self.y()) } else { new CenterOffset(xOffset = xOffset + n, yOffset = yOffset) } + + /** + * Returns a new Position n steps left from this one while Running, or a lazy center with offset while idle. + */ + override method left(n) = if (game.running()) { new Position(x = self.x() - n, y = self.y()) } else { new CenterOffset(xOffset = xOffset - n, yOffset = yOffset) } + + /** + * Returns a new Position n steps up from this one while Running, or a lazy center with offset while idle. + */ + override method up(n) = if (game.running()) { new Position(x = self.x(), y = self.y() + n) } else { new CenterOffset(xOffset = xOffset, yOffset = yOffset + n)} + + /** + * Returns a new Position, n steps down from this one while Running, or a lazy center with offset while idle. + */ + override method down(n) = if (game.running()) { new Position(x = self.x(), y = self.y() - n) } else { new CenterOffset(xOffset = xOffset, yOffset = yOffset - n) } + + /** + * Returns a new Position is the game is Running with the same coordinates, or a lazy center with offset while idle. + */ + override method clone() = if (game.running()) { new Position(x = self.x(), y = self.y()) } else { new CenterOffset(xOffset = xOffset, yOffset = yOffset) } + + override method createPosition(_x, _y) = new Position(x = _x, y = _y) + + //ISSUE! rounding can't change center, so it have to calculate in the added offset with current center.a() + /* ISSUE EXAMPLE: + xCenter 5.5 and xOffset 3.2 = 8.7, rounded is 9. So 3.2 = 3.5. + + xCenter 5.5 and xOffset 3.6 = 9.1, rounded is 9. So 3.2 = 3.5. + + xCenter 5.5 and xOffset -5 = 0.5, rounded is 1 or 0... so = -5 = -4.5 or -5.5. + + x and y center are always >= 0, xOffset and yOffset can be any number. + */ + + /** + * Returns a new position with its coordinates rounded if Running, or a lazy center with rounded offset while idle. + */ + override method round(){ + //Para no duplicar código, s epodría duplicar. + const xRound = self.x().round() + const yRound = self.y().round() + + return if(game.running()){ new Position(x = xRound, y = yRound) } + else { new CenterOffset(xOffset = xRound - game.xCenter(), yOffset = yRound - game.yCenter()) } + + /* + //Version que duplica. + return if(game.running()){ new Position(x = self.x().round() , y = self.y().round()) } + else { new CenterOffset(xOffset = self.x().round() - game.xCenter(), yOffset = self.y().round() - game.yCenter()) } + */ + } +} /** * Keyboard object handles all keys movements. There is a method for each key. From b19290ca1c10a5c2099473f03a4f32679deff742 Mon Sep 17 00:00:00 2001 From: isaiaslafon Date: Wed, 22 Jan 2025 17:14:39 -0300 Subject: [PATCH 7/7] Fix #197 c center immutable offset wrapper resolve all issues update simplified --- src/wollok/game.wlk | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/wollok/game.wlk b/src/wollok/game.wlk index a54a7d77..6425adab 100644 --- a/src/wollok/game.wlk +++ b/src/wollok/game.wlk @@ -503,15 +503,14 @@ class CenterOffset inherits AbstractPosition{ override method createPosition(_x, _y) = new Position(x = _x, y = _y) - //ISSUE! rounding can't change center, so it have to calculate in the added offset with current center.a() - /* ISSUE EXAMPLE: - xCenter 5.5 and xOffset 3.2 = 8.7, rounded is 9. So 3.2 = 3.5. - - xCenter 5.5 and xOffset 3.6 = 9.1, rounded is 9. So 3.2 = 3.5. - - xCenter 5.5 and xOffset -5 = 0.5, rounded is 1 or 0... so = -5 = -4.5 or -5.5. - - x and y center are always >= 0, xOffset and yOffset can be any number. + + /* + ISSUE! rounding can't change the center, so it have to calculate in the added offset with current center.x() and y(). + EXAMPLE: + -xCenter 5.5 and xOffset 3.2 = 8.7, rounded is 9. So 3.2 = 3.5. + -xCenter 5.5 and xOffset 3.6 = 9.1, rounded is 9. So 3.2 = 3.5. + -xCenter 5.5 and xOffset -5 = 0.5, rounded is 1 or 0... so = -5 = -4.5 or -5.5. + X and Y center are always >= 0, xOffset and yOffset can be any number. */ /**