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 12, 2024
1 parent 12b8803 commit c8ff887
Show file tree
Hide file tree
Showing 4 changed files with 128 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-2022 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,80 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 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.Assert
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.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class GoToRandomPositionActionTest {
@JvmField
@Rule
val exception: ExpectedException = ExpectedException.none()

private var sprite: Sprite? = null
private var dummySprite: Sprite? = null
private var action: GoToRandomPositionAction? = null

@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

Assert.assertEquals(0f, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(0f, sprite!!.look.yInUserInterfaceDimensionUnit)

action!!.act(1f)

Assert.assertEquals(action!!.randomXPosition, sprite!!.look.xInUserInterfaceDimensionUnit)
Assert.assertEquals(action!!.randomYPosition, sprite!!.look.yInUserInterfaceDimensionUnit)
}

@Test
fun testNullActor() {
val factory = ActionFactory()
val action = factory.createGoToAction(null, dummySprite, BrickValues.GO_TO_RANDOM_POSITION)
exception.expect(NullPointerException::class.java)
action.act(1.0f)
}
}

0 comments on commit c8ff887

Please sign in to comment.