Skip to content
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,117 @@
/*
* 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.conditional

import android.util.Log
import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction
import org.catrobat.catroid.content.Scope
import org.catrobat.catroid.formulaeditor.Formula
import org.catrobat.catroid.formulaeditor.InterpretationException

open class GlideToAction : TemporalAction() {
private var startXValue = 0f
private var startYValue = 0f
private var currentXValue = 0f
private var currentYValue = 0f
private var endX: Formula? = null
private var endY: Formula? = null
var duration: Formula? = null
lateinit var scope: Scope


var durationValue = 0f
private var endXValue = 0f
private var endYValue = 0f

private var velocityXValue = 0f
private var velocityYValue = 0f

private var restart = false

override fun begin() {

val durationInterpretation: Float = interpretFormula(duration, scope, "durationInterpretation")
val endXInterpretation : Float = interpretFormula(endX,scope, "endXInterpretation")
val endYInterpretation : Float = interpretFormula(endY,scope, "endYInterpretation")

if (!restart) {
super.setDuration(durationInterpretation)
durationValue = durationInterpretation
endXValue = endXInterpretation
endYValue = endYInterpretation
}
restart = false

startXValue = scope.sprite.look.xInUserInterfaceDimensionUnit
startYValue = scope.sprite.look.yInUserInterfaceDimensionUnit
currentXValue = startXValue
currentYValue = startYValue
if (startXValue == endXInterpretation && startYValue == endYInterpretation) {
super.finish()
}
if (velocityXValue == 0f && velocityYValue == 0f && durationValue != 0f) {
velocityXValue = (endXValue - startXValue) / durationValue
velocityYValue = (endYValue - startYValue) / durationValue
}

scope.sprite.glidingVelocityX = velocityXValue
scope.sprite.glidingVelocityY = velocityYValue
scope.sprite.isGliding = true
}

override fun update(percent: Float) {
val deltaX = scope.sprite.look.xInUserInterfaceDimensionUnit - currentXValue
val deltaY = scope.sprite.look.yInUserInterfaceDimensionUnit - currentYValue
if ((kotlin.math.abs(deltaX) > 0.1f) || (kotlin.math.abs(deltaY) > 0.1f)) {
restart = true
val remainingDuration = getDuration() - time
setDuration(remainingDuration)
durationValue = remainingDuration
restart()
} else {
currentXValue = startXValue + (endXValue - startXValue) * percent
currentYValue = startYValue + (endYValue - startYValue) * percent
scope.sprite.look.setPositionInUserInterfaceDimensionUnit(currentXValue, currentYValue)
}
}

override fun end() {
scope.sprite.isGliding = false
scope.sprite.glidingVelocityX = 0f
scope.sprite.glidingVelocityY = 0f
}

fun setPosition(x: Formula?, y: Formula?) {
endX = x
endY = y
}

private fun interpretFormula(formula: Formula?, scope: Scope, name: String): Float {
return try {
formula?.interpretFloat(scope) ?: 0f
} catch (interpretationException: InterpretationException) {
Log.d(javaClass.simpleName, "Formula interpretation failed for [$name].", interpretationException)
0f
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ class ActionPhysicsFactory : ActionFactory() {
}

override fun createGlideToAction(sprite: Sprite, sequence: SequenceAction, x: Formula?, y: Formula?, duration: Formula?): Action {

val action = Actions.action(GlideToPhysicsAction::class.java)
action.setPosition(x, y)
action.setDuration(duration)
action.duration = duration
val scope = Scope(ProjectManager.getInstance().currentProject, sprite, sequence)
action.setScope(scope)
action.scope = scope
action.setPhysicsLook(sprite.look as PhysicsLook)
return action
}
Expand Down
Loading