Skip to content

Commit

Permalink
CATROID-1071 GoToRandomPositionAction refactor
Browse files Browse the repository at this point in the history
Refactor GoToRandomPositionAction.java and GoToRandomPositionActionTest.java to Kotlin
  • Loading branch information
erikcoxx98 committed Nov 27, 2024
1 parent 11ad848 commit fe06d15
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 137 deletions.

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)
}
}
}

0 comments on commit fe06d15

Please sign in to comment.