Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CATROID-1071 GoToRandomPositionAction refactor #5052

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* 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 <http://www.gnu.org/licenses/>.
*/
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)
}
}
}