diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java b/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java deleted file mode 100644 index 3dc31399967..00000000000 --- a/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team - * () - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * An additional term exception under section 7 of the GNU Affero - * General Public License, version 3, is available at - * http://developer.catrobat.org/license_additional_term - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package org.catrobat.catroid.content.actions; - -import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction; - -import org.catrobat.catroid.common.ScreenValues; -import org.catrobat.catroid.content.Sprite; - -public class GoToRandomPositionAction extends TemporalAction { - - private Sprite sprite; - private float randomXPosition; - private float randomYPosition; - - @Override - protected void update(float percent) { - randomXPosition = (float) Math.random() - * (ScreenValues.currentScreenResolution.getWidth() + 1) - (ScreenValues.currentScreenResolution.getWidth() / 2); - randomYPosition = (float) Math.random() - * (ScreenValues.currentScreenResolution.getHeight() + 1) - (ScreenValues.currentScreenResolution.getHeight() / 2); - - sprite.look.setPositionInUserInterfaceDimensionUnit(randomXPosition, randomYPosition); - } - - public void setSprite(Sprite sprite) { - this.sprite = sprite; - } - - public float getRandomXPosition() { - return this.randomXPosition; - } - - public float getRandomYPosition() { - return this.randomYPosition; - } -} diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.kt b/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.kt new file mode 100644 index 00000000000..6645e8daffa --- /dev/null +++ b/catroid/src/main/java/org/catrobat/catroid/content/actions/GoToRandomPositionAction.kt @@ -0,0 +1,48 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2024 The Catrobat Team + * () + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * An additional term exception under section 7 of the GNU Affero + * General Public License, version 3, is available at + * http://developer.catrobat.org/license_additional_term + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.catrobat.catroid.content.actions + +import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction +import org.catrobat.catroid.common.ScreenValues +import org.catrobat.catroid.content.Sprite + +class GoToRandomPositionAction : TemporalAction() { + private var sprite: Sprite? = null + var randomXPosition: Float = 0f + private set + var randomYPosition: Float = 0f + private set + + override fun update(percent: Float) { + randomXPosition = Math.random() + .toFloat() * (ScreenValues.currentScreenResolution.width + 1) - (ScreenValues.currentScreenResolution.width / 2) + randomYPosition = Math.random() + .toFloat() * (ScreenValues.currentScreenResolution.height + 1) - (ScreenValues.currentScreenResolution.height / 2) + + sprite!!.look.setPositionInUserInterfaceDimensionUnit(randomXPosition, randomYPosition) + } + + fun setSprite(sprite: Sprite?) { + this.sprite = sprite + } +} diff --git a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java deleted file mode 100644 index 7d391e08fac..00000000000 --- a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team - * () - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * An additional term exception under section 7 of the GNU Affero - * General Public License, version 3, is available at - * http://developer.catrobat.org/license_additional_term - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package org.catrobat.catroid.test.content.actions; - -import com.badlogic.gdx.scenes.scene2d.Action; - -import org.catrobat.catroid.common.BrickValues; -import org.catrobat.catroid.content.ActionFactory; -import org.catrobat.catroid.content.Sprite; -import org.catrobat.catroid.content.actions.GoToRandomPositionAction; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import static junit.framework.Assert.assertEquals; - -@RunWith(JUnit4.class) -public class GoToRandomPositionActionTest { - @Rule - public final ExpectedException exception = ExpectedException.none(); - - private Sprite sprite; - private Sprite dummySprite; - private GoToRandomPositionAction action; - - @Before - public void setUp() throws Exception { - sprite = new Sprite("testSprite"); - dummySprite = new Sprite("dummySprite"); - action = (GoToRandomPositionAction) sprite.getActionFactory().createGoToAction( - sprite, dummySprite, BrickValues.GO_TO_RANDOM_POSITION); - } - - @Test - public void testGoToOtherSpriteAction() { - sprite.look.setXInUserInterfaceDimensionUnit(0f); - sprite.look.setYInUserInterfaceDimensionUnit(0f); - - assertEquals(0f, sprite.look.getXInUserInterfaceDimensionUnit()); - assertEquals(0f, sprite.look.getYInUserInterfaceDimensionUnit()); - - action.act(1f); - - assertEquals(action.getRandomXPosition(), sprite.look.getXInUserInterfaceDimensionUnit()); - assertEquals(action.getRandomYPosition(), sprite.look.getYInUserInterfaceDimensionUnit()); - } - - @Test - public void testNullActor() { - ActionFactory factory = new ActionFactory(); - Action action = factory.createGoToAction(null, dummySprite, BrickValues.GO_TO_RANDOM_POSITION); - exception.expect(NullPointerException.class); - action.act(1.0f); - } -} diff --git a/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.kt b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.kt new file mode 100644 index 00000000000..3ddf1b9f632 --- /dev/null +++ b/catroid/src/test/java/org/catrobat/catroid/test/content/actions/GoToRandomPositionActionTest.kt @@ -0,0 +1,77 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2024 The Catrobat Team + * () + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * An additional term exception under section 7 of the GNU Affero + * General Public License, version 3, is available at + * http://developer.catrobat.org/license_additional_term + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.catrobat.catroid.test.content.actions + +import junit.framework.TestCase.assertEquals +import org.catrobat.catroid.common.BrickValues +import org.catrobat.catroid.common.ScreenValues +import org.catrobat.catroid.content.ActionFactory +import org.catrobat.catroid.content.Sprite +import org.catrobat.catroid.content.actions.GoToRandomPositionAction +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +class GoToRandomPositionActionTest { + + private lateinit var sprite: Sprite + private lateinit var dummySprite: Sprite + private lateinit var action: GoToRandomPositionAction + + @Before + @Throws(Exception::class) + fun setUp() { + sprite = Sprite("testSprite") + dummySprite = Sprite("dummySprite") + ScreenValues.setToDefaultScreenSize() + action = sprite.actionFactory.createGoToAction( + sprite, dummySprite, BrickValues.GO_TO_RANDOM_POSITION + ) as GoToRandomPositionAction + } + + @Test + fun testGoToOtherSpriteAction() { + sprite.look.xInUserInterfaceDimensionUnit = 0f + sprite.look.yInUserInterfaceDimensionUnit = 0f + + assertEquals(0f, sprite.look.xInUserInterfaceDimensionUnit) + assertEquals(0f, sprite.look.yInUserInterfaceDimensionUnit) + + action.act(1f) + + assertEquals(action.randomXPosition, sprite.look.xInUserInterfaceDimensionUnit) + assertEquals(action.randomYPosition, sprite.look.yInUserInterfaceDimensionUnit) + } + + @Test + fun testNullActor() { + val factory = ActionFactory() + Assert.assertThrows(NullPointerException::class.java) { + val action = factory.createGoToAction(null, dummySprite, BrickValues.GO_TO_RANDOM_POSITION) + action.act(1.0f) + } + } +}