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
  • Loading branch information
moemgva2910 committed Jan 18, 2023
1 parent 15b1d13 commit 30dca1d
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 8 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.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2018 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -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
50 changes: 47 additions & 3 deletions catroid/src/main/java/org/catrobat/catroid/content/Sprite.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2023 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down 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 @@ -370,12 +373,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
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2023 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down 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 @@ -136,7 +138,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 @@ -259,4 +266,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
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2023 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down 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 30dca1d

Please sign in to comment.