diff --git a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/Position.xtend b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/Position.xtend index f63c286970..c291f553c0 100644 --- a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/Position.xtend +++ b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/Position.xtend @@ -49,9 +49,9 @@ abstract class Position { def abstract int getX() def abstract int getY() - def getXinPixels() { x * Gameboard.CELLZISE } + def getXinPixels() { x * Gameboard.instance.cellsize } - def getYinPixels() { y * Gameboard.CELLZISE } + def getYinPixels() { y * Gameboard.instance.cellsize } override public int hashCode() { val prime = 31 diff --git a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/VisualComponent.xtend b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/VisualComponent.xtend index 56e9624290..1604f1a620 100644 --- a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/VisualComponent.xtend +++ b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/VisualComponent.xtend @@ -105,8 +105,8 @@ class VisualComponent { val yMouse = Gdx.input.y val bottomX = position.xinPixels val bottomY = Gameboard.getInstance().pixelHeight - position.yinPixels - val topX = bottomX + Gameboard.CELLZISE - val topY = bottomY - Gameboard.CELLZISE + val topX = bottomX + Gameboard.instance.cellsize + val topY = bottomY - Gameboard.instance.cellsize return (xMouse > bottomX && xMouse < topX) && (yMouse < bottomY && yMouse > topY) } diff --git a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Cell.xtend b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Cell.xtend index 8080dfa2ee..d05774a85e 100644 --- a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Cell.xtend +++ b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Cell.xtend @@ -12,7 +12,7 @@ class Cell { new(Position position, Image image) { this.position = position - this.image = image => [ size = new CellSize(Gameboard.CELLZISE) ] + this.image = image => [ size = new CellSize(Gameboard.instance.cellsize) ] } def draw(Window window) { diff --git a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Gameboard.xtend b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Gameboard.xtend index 7fbcaee662..27681fe51b 100644 --- a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Gameboard.xtend +++ b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/Gameboard.xtend @@ -22,8 +22,7 @@ import static org.uqbar.project.wollok.sdk.WollokSDK.* @Accessors class Gameboard { - public static Gameboard instance - public static final int CELLZISE = 50 + static Gameboard instance val Logger log = Logger.getLogger(this.class) @@ -32,6 +31,7 @@ class Gameboard { String boardGround int height int width + @Accessors int cellsize = 50 Background background List components = newArrayList List listeners = newArrayList @@ -41,10 +41,14 @@ class Gameboard { def static getInstance() { if (instance === null) { - instance = new Gameboard() + resetInstance } instance } + + def static resetInstance() { + instance = new Gameboard() + } new() { title = "Wollok Game" @@ -102,9 +106,9 @@ class Gameboard { null } - def pixelHeight() { height * CELLZISE } + def pixelHeight() { height * cellsize } - def pixelWidth() { width * CELLZISE } + def pixelWidth() { width * cellsize } def clear() { components.clear() diff --git a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/GameboardInputProcessor.xtend b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/GameboardInputProcessor.xtend index 74aa6c8eb5..2f261d0661 100644 --- a/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/GameboardInputProcessor.xtend +++ b/org.uqbar.project.wollok.game/src/org/uqbar/project/wollok/game/gameboard/GameboardInputProcessor.xtend @@ -26,7 +26,7 @@ class GameboardInputProcessor implements InputProcessor { override boolean touchDown(int x, int y, int pointer, int button) { val inverseY = Gameboard.getInstance().pixelHeight() - y - val position = new WGPosition(x / Gameboard.CELLZISE, inverseY / Gameboard.CELLZISE ) + val position = new WGPosition(x / Gameboard.instance.cellsize, inverseY / Gameboard.instance.cellsize ) val Iterable lista = Gameboard.getInstance.getComponentsInPosition(position) diff --git a/org.uqbar.project.wollok.lib/src/wollok/game.wlk b/org.uqbar.project.wollok.lib/src/wollok/game.wlk index 613d910f59..40a345f22c 100644 --- a/org.uqbar.project.wollok.lib/src/wollok/game.wlk +++ b/org.uqbar.project.wollok.lib/src/wollok/game.wlk @@ -217,6 +217,20 @@ object game { */ method ground(image) native + /** + * Sets cells size. + */ + method cellSize(size){ + if(size<=0) + throw new Exception(message = "Cell size can't be 0 or lower") + self.doCellSize(size) + } + + /** + * @private + */ + method doCellSize(size) native + /** * Sets full background image. */ diff --git a/org.uqbar.project.wollok.lib/src/wollok/game/GameObject.xtend b/org.uqbar.project.wollok.lib/src/wollok/game/GameObject.xtend index eef1e25750..48ad9c482c 100644 --- a/org.uqbar.project.wollok.lib/src/wollok/game/GameObject.xtend +++ b/org.uqbar.project.wollok.lib/src/wollok/game/GameObject.xtend @@ -207,4 +207,6 @@ class GameObject { def ground(String image) { board.ground = image } def boardGround(String image) { board.boardGround = image } + def doCellSize(WollokObject size) { board.cellsize = size.coerceToInteger } + } diff --git a/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/sdk/GameTest.xtend b/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/sdk/GameTest.xtend index bec46b7e0b..b8282fb197 100644 --- a/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/sdk/GameTest.xtend +++ b/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/sdk/GameTest.xtend @@ -4,206 +4,235 @@ import org.junit.Before import org.junit.Test import org.uqbar.project.wollok.game.gameboard.Gameboard import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase +import org.junit.After class GameTest extends AbstractWollokInterpreterTestCase { - + var gameboard = Gameboard.getInstance - + @Before def void init() { gameboard.clear } + @After + def void finish() { + Gameboard.resetInstance + } + @Test def void canInstanceNewPosition() { ''' - assert.equals(«position(1,2)», game.at(1,2)) + assert.equals(«position(1,2)», game.at(1,2)) '''.test } - + @Test def void originShouldReturnOriginCoordinatePosition() { ''' - assert.equals(«position(0,0)», game.origin()) + assert.equals(«position(0,0)», game.origin()) '''.test } - + @Test def void centerShouldReturnCenteredCoordinatePosition() { ''' - game.width(2) - game.height(5) - assert.equals(«position(1,2)», game.center()) + game.width(2) + game.height(5) + assert.equals(«position(1,2)», game.center()) '''.test } - + @Test def void shouldReturnVisualColliders() { ''' - «position(0,0)».drawElement(myVisual) - 2.times{ i => «position(0,0)».drawElement(new Visual()) } - «position(0,1)».drawElement(new Visual()) - - assert.equals(2, game.colliders(myVisual).size()) + «position(0,0)».drawElement(myVisual) + 2.times{ i => «position(0,0)».drawElement(new Visual()) } + «position(0,1)».drawElement(new Visual()) + + assert.equals(2, game.colliders(myVisual).size()) '''.gameTest } - + @Test def void shouldReturnUniqueCollider() { ''' - «position(0,0)».drawElement(myVisual) - const otherVisual = new Visual() - «position(0,0)».drawElement(otherVisual) - - assert.equals(otherVisual, game.uniqueCollider(myVisual)) + «position(0,0)».drawElement(myVisual) + const otherVisual = new Visual() + «position(0,0)».drawElement(otherVisual) + + assert.equals(otherVisual, game.uniqueCollider(myVisual)) '''.gameTest } - + @Test def void movingCharacterShouldSetObjectPosition() { ''' - game.addVisualCharacter(pepita) + game.addVisualCharacter(pepita) '''.gameTest gameboard.components.head.up ''' - assert.equals(1, game.at(0,1).allElements().size()) + assert.equals(1, game.at(0,1).allElements().size()) '''.test } - + @Test def void addSameObjectToGameShouldFail() { ''' - «position(0,0)».drawElement(myVisual) - assert.throwsExceptionWithMessage("myVisual[] is already in the game.", { «position(1,1)».drawElement(myVisual) }) + «position(0,0)».drawElement(myVisual) + assert.throwsExceptionWithMessage("myVisual[] is already in the game.", { «position(1,1)».drawElement(myVisual) }) '''.gameTest } - + @Test def void addVisualUsingNullMustFail() { ''' - assert.throwsExceptionWithMessage("Operation addVisual doesn't support null parameters", { => game.addVisual(null) }) + assert.throwsExceptionWithMessage("Operation addVisual doesn't support null parameters", { => game.addVisual(null) }) '''.test } @Test def void addVisualInUsingNullMustFail() { ''' - assert.throwsExceptionWithMessage("Operation addVisualIn doesn't support null parameters", { => game.addVisualIn(null, null) }) + assert.throwsExceptionWithMessage("Operation addVisualIn doesn't support null parameters", { => game.addVisualIn(null, null) }) '''.test } @Test def void collidersUsingNullMustFail() { ''' - assert.throwsExceptionWithMessage("Operation colliders doesn't support null parameters", { => game.colliders(null) }) + assert.throwsExceptionWithMessage("Operation colliders doesn't support null parameters", { => game.colliders(null) }) '''.test } @Test def void onTickUsingNullMustFail() { ''' - assert.throwsExceptionWithMessage("Operation onTick doesn't support null parameters", { => game.onTick(null, null, null) }) + assert.throwsExceptionWithMessage("Operation onTick doesn't support null parameters", { => game.onTick(null, null, null) }) '''.test } @Test def void whenKeyPressedDoUsingNullMustFail() { ''' - assert.throwsExceptionWithMessage("Operation whenKeyPressedDo doesn't support null parameters", { => game.whenKeyPressedDo(null, null) }) + assert.throwsExceptionWithMessage("Operation whenKeyPressedDo doesn't support null parameters", { => game.whenKeyPressedDo(null, null) }) '''.test } @Test def void whenAddingAnObjectAndCheckingIfItIsInTheBoardItReturnsTrue() { ''' - import wollok.game.* - - object myVisual { } - - program a { - game.addVisualIn(myVisual, game.at(0, 0)) - assert.that(game.hasVisual(myVisual)) - } + import wollok.game.* + + object myVisual { } + + program a { + game.addVisualIn(myVisual, game.at(0, 0)) + assert.that(game.hasVisual(myVisual)) + } '''.interpretPropagatingErrors } @Test def void whenAddingAnObjectAndCheckingIfOtherIsInTheBoardItReturnsFalse() { ''' - import wollok.game.* - - object myVisual { } - object myVisual2 { } - - program a { - game.addVisualIn(myVisual, game.at(0, 0)) - assert.notThat(game.hasVisual(myVisual2)) - } + import wollok.game.* + + object myVisual { } + object myVisual2 { } + + program a { + game.addVisualIn(myVisual, game.at(0, 0)) + assert.notThat(game.hasVisual(myVisual2)) + } '''.interpretPropagatingErrors } @Test def void whenNoObjectIsAddedAndGetAllVisualsItReturnsNoElement() { ''' - import wollok.game.* - - program a { - assert.equals(0, game.allVisuals().size()) - } + import wollok.game.* + + program a { + assert.equals(0, game.allVisuals().size()) + } '''.interpretPropagatingErrors } @Test def void whenAddingAnObjectAndGettingAllVisualsItReturnsOneElement() { ''' - import wollok.game.* - - object myVisual { } - - program a { - game.addVisualIn(myVisual, game.at(0, 0)) - assert.equals(1, game.allVisuals().size()) - assert.equals(myVisual, game.allVisuals().get(0)) - } + import wollok.game.* + + object myVisual { } + + program a { + game.addVisualIn(myVisual, game.at(0, 0)) + assert.equals(1, game.allVisuals().size()) + assert.equals(myVisual, game.allVisuals().get(0)) + } '''.interpretPropagatingErrors } @Test def void whenAddingSomeObjectAndGettingAllVisualsItReturnsAllTheAddedElements() { ''' - import wollok.game.* - - object myVisual { } - object myVisual2 { } - object myVisual3 { } - - program a { - game.addVisualIn(myVisual, game.at(0, 0)) - game.addVisualIn(myVisual2, game.at(0, 0)) - game.addVisualIn(myVisual3, game.at(0, 0)) - assert.equals(3, game.allVisuals().size()) - assert.equals([myVisual, myVisual2, myVisual3].asSet(), game.allVisuals().asSet()) - } + import wollok.game.* + + object myVisual { } + object myVisual2 { } + object myVisual3 { } + + program a { + game.addVisualIn(myVisual, game.at(0, 0)) + game.addVisualIn(myVisual2, game.at(0, 0)) + game.addVisualIn(myVisual3, game.at(0, 0)) + assert.equals(3, game.allVisuals().size()) + assert.equals([myVisual, myVisual2, myVisual3].asSet(), game.allVisuals().asSet()) + } '''.interpretPropagatingErrors } + + @Test + def void cellSize_works_correctly() { + ''' + game.cellSize(10) + '''.gameTest + assertEquals(10, gameboard.cellsize) + } + @Test + def void cellSize_throws_exception_limit(){ + ''' + assert.throwsExceptionWithMessage("Cell size can't be 0 or lower", {=>game.cellSize(0)}) + + '''.gameTest + } + + @Test + def void cellSize_throws_exception(){ + ''' + assert.throwsExceptionWithMessage("Cell size can't be 0 or lower", {=>game.cellSize(-10)}) + '''.gameTest + } + def gameTest(CharSequence test) { ''' - import wollok.game.* - - object myVisual { } - class Visual { } - - object pepita { - var property position = game.origin() - } - - program a { - «test» - } + import wollok.game.* + + object myVisual { } + class Visual { } + + object pepita { + var property position = game.origin() + } + + program a { + «test» + } '''.interpretPropagatingErrors } - + private def position(int x, int y) { '''new Position(x = «x», y = «y»)''' } diff --git a/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/typesystem/CoreInferenceTestCase.xtend b/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/typesystem/CoreInferenceTestCase.xtend index 6665733655..3da5fbadf9 100644 --- a/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/typesystem/CoreInferenceTestCase.xtend +++ b/org.uqbar.project.wollok.tests/src/org/uqbar/project/wollok/tests/typesystem/CoreInferenceTestCase.xtend @@ -12,11 +12,6 @@ import static org.uqbar.project.wollok.sdk.WollokSDK.* import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.* import static extension org.uqbar.project.wollok.model.WollokModelExtensions.* -/** - * Test cases for type inference related to the console object. - * - * @author npasserini - */ class CoreInferenceTestCase extends AbstractWollokTypeSystemTestCase { @Parameters(name="{index}: {0}")