Skip to content

Commit

Permalink
CATROID-766 Evaluate “when touches actor or object” to false initially
Browse files Browse the repository at this point in the history
Evaluating “when …”  always to false at the start of a scene, allows an object to change 
its position before checking for a collision.

Since all objects are placed at coordinates (0, 0) initially,  when a scene is started or restarted, for the first 200 milliseconds, the condition “when …” is not checked and the value false is returned instead. After this time threshold has been completed, a scene is considered to be continued and hence, the condition at this point onwards is always evaluated to check for a collision.
  • Loading branch information
moemgva2910 committed May 17, 2022
1 parent ee88306 commit ef0ea40
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 4 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package org.catrobat.catroid.content;

import android.os.SystemClock;
import android.util.Log;

import org.catrobat.catroid.ProjectManager;
Expand All @@ -45,6 +46,13 @@ public class ConditionScriptTrigger {
static final int ALREADY_TRIGGERED = 1;
private static final String TAG = ConditionScriptTrigger.class.getSimpleName();

private boolean sceneFirstStart = false;
private boolean sceneRestarted = false;
private boolean sceneAlreadyStarted = false;

private long startTime = 0;
static final long EVALUATE_AND_TRIGGER_ACTIONS_THRESHOLD = 200;

@TriggerStatus
private int status = TRIGGER_NOW;
private final Formula formula;
Expand All @@ -56,7 +64,28 @@ public class ConditionScriptTrigger {
void evaluateAndTriggerActions(Sprite sprite) {
try {
Scope scope = new Scope(ProjectManager.getInstance().getCurrentProject(), sprite, null);
boolean conditionValue = formula.interpretBoolean(scope);
boolean conditionValue = false;

if (sceneFirstStart || sceneRestarted) {
if (startTime == 0) {
startTime = SystemClock.uptimeMillis();
} else {
long elapsedTime = SystemClock.uptimeMillis() - startTime;

if (elapsedTime >= EVALUATE_AND_TRIGGER_ACTIONS_THRESHOLD) {
if (sceneFirstStart) {
sceneFirstStart = false;
} else if (sceneRestarted) {
sceneRestarted = false;
}
startTime = 0;
conditionValue = formula.interpretBoolean(scope);
}
}
} else {
conditionValue = formula.interpretBoolean(scope);
}

if (conditionValue) {
triggerScript(sprite);
} else {
Expand All @@ -75,6 +104,19 @@ private void triggerScript(Sprite sprite) {
}
}

public void updateSceneFirstStart() {
sceneFirstStart = true;
sceneAlreadyStarted = true;
}

public void resetStartTimeIfSceneRestarted() {
if (sceneAlreadyStarted) {
startTime = 0;
sceneRestarted = true;
sceneFirstStart = false;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
48 changes: 46 additions & 2 deletions catroid/src/main/java/org/catrobat/catroid/content/Sprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
import org.catrobat.catroid.content.actions.ScriptSequenceAction;
import org.catrobat.catroid.content.bricks.Brick;
import org.catrobat.catroid.content.bricks.FormulaBrick;
import org.catrobat.catroid.content.bricks.IfLogicBeginBrick;
import org.catrobat.catroid.content.bricks.IfThenLogicBeginBrick;
import org.catrobat.catroid.content.bricks.PlaySoundBrick;
import org.catrobat.catroid.content.bricks.UserDefinedBrick;
import org.catrobat.catroid.content.bricks.WhenConditionBrick;
import org.catrobat.catroid.content.eventids.EventId;
import org.catrobat.catroid.embroidery.RunningStitch;
import org.catrobat.catroid.formulaeditor.Formula;
import org.catrobat.catroid.formulaeditor.FormulaElement;
import org.catrobat.catroid.formulaeditor.UserData;
import org.catrobat.catroid.formulaeditor.UserList;
import org.catrobat.catroid.formulaeditor.UserVariable;
Expand Down Expand Up @@ -364,12 +367,53 @@ public void initConditionScriptTriggers() {
for (Script script : scriptList) {
if (script instanceof WhenConditionScript) {
WhenConditionBrick conditionBrick = (WhenConditionBrick) script.getScriptBrick();
Formula condition = conditionBrick.getFormulaWithBrickField(Brick.BrickField.IF_CONDITION);
conditionScriptTriggers.add(new ConditionScriptTrigger(condition));
Formula formula = conditionBrick.getFormulaWithBrickField(Brick.BrickField.IF_CONDITION);
ConditionScriptTrigger conditionScriptTrigger = new ConditionScriptTrigger(formula);

if (waitBeforeCheckingCollision(formula)) {
conditionScriptTrigger.updateSceneFirstStart();
}
conditionScriptTriggers.add(conditionScriptTrigger);
}
}
}

public void resetConditionScriptTriggers() {
for (ConditionScriptTrigger conditionScriptTrigger : conditionScriptTriggers) {
conditionScriptTrigger.resetStartTimeIfSceneRestarted();
}
}

public void initIfConditionBrickTriggers() {
for (Script script : scriptList) {
if (!script.getBrickList().isEmpty()) {
Brick brick = script.getBrickList().get(0);

if (brick instanceof IfThenLogicBeginBrick) {
for (Formula formula : ((IfThenLogicBeginBrick) brick).getFormulas()) {
if (waitBeforeCheckingCollision(formula)) {
formula.sceneFirstStart(true);
}
}
} else if (brick instanceof IfLogicBeginBrick) {
for (Formula formula : ((IfLogicBeginBrick) brick).getFormulas()) {
if (waitBeforeCheckingCollision(formula)) {
formula.sceneFirstStart(true);
}
}
}
}
}
}

boolean waitBeforeCheckingCollision(Formula formula) {
boolean wait = false;
if (formula.getRoot() != null) {
wait = formula.getRoot().getElementType() == FormulaElement.ElementType.COLLISION_FORMULA;
}
return wait;
}

void evaluateConditionScriptTriggers() {
for (ConditionScriptTrigger conditionScriptTrigger : conditionScriptTriggers) {
conditionScriptTrigger.evaluateAndTriggerActions(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class Formula implements Serializable {

private transient InternFormula internFormula = null;

private boolean sceneFirstStart = false;

public Formula(FormulaElement formulaElement) {
formulaTree = formulaElement;
internFormula = new InternFormula(formulaTree.getInternTokenList());
Expand Down Expand Up @@ -132,7 +134,12 @@ public Integer interpretInteger(Scope scope) throws InterpretationException {

public Double interpretDouble(Scope scope) throws InterpretationException {
try {
return assertNotNaN(interpretDoubleInternal(scope));
if (sceneFirstStart) {
sceneFirstStart = false;
return 0.0;
} else {
return assertNotNaN(interpretDoubleInternal(scope));
}
} catch (ClassCastException | NumberFormatException exception) {
throw new InterpretationException("Couldn't interpret Formula.", exception);
}
Expand Down Expand Up @@ -255,4 +262,8 @@ private String toLocalizedString(boolean value, StringProvider stringProvider) {
public interface StringProvider {
String getTrueOrFalse(Boolean value);
}

public void sceneFirstStart(boolean sceneFirstStart) {
this.sceneFirstStart = sceneFirstStart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ public void create() {
physicsWorld = scene.resetPhysicsWorld();
sprites = new ArrayList<>(scene.getSpriteList());

resetConditionScriptTriggers();

embroideryPatternManager = new DSTPatternManager();
initActors(sprites);

Expand All @@ -208,6 +210,12 @@ public void create() {
axes = new Texture(Gdx.files.internal("stage/red_pixel.bmp"));
}

private void resetConditionScriptTriggers() {
for (Sprite sprite : sprites) {
sprite.resetConditionScriptTriggers();
}
}

public void setPaused(boolean paused) {
this.paused = paused;
}
Expand Down Expand Up @@ -538,6 +546,7 @@ public void render() {
for (Sprite sprite : sprites) {
sprite.initializeEventThreads(EventId.START);
sprite.initConditionScriptTriggers();
sprite.initIfConditionBrickTriggers();
if (!sprite.getLookList().isEmpty()) {
sprite.look.setLookData(sprite.getLookList().get(0));
}
Expand Down

0 comments on commit ef0ea40

Please sign in to comment.