Skip to content

Commit

Permalink
Add CycleTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Aug 1, 2023
1 parent 55f9549 commit 8bfd34f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import cn.zbx1425.mtrsteamloco.MainClient;
import cn.zbx1425.mtrsteamloco.render.scripting.eyecandy.EyeCandyScriptContext;
import cn.zbx1425.mtrsteamloco.render.scripting.train.TrainScriptContext;
import cn.zbx1425.mtrsteamloco.render.scripting.util.CycleTracker;
import cn.zbx1425.mtrsteamloco.render.scripting.util.GraphicsTexture;
import cn.zbx1425.mtrsteamloco.render.scripting.util.ScriptTimingUtil;
import cn.zbx1425.mtrsteamloco.render.scripting.util.TimingUtil;
import cn.zbx1425.mtrsteamloco.render.scripting.util.StateTracker;
import cn.zbx1425.sowcer.math.Matrices;
import cn.zbx1425.sowcer.math.Matrix4f;
Expand Down Expand Up @@ -43,8 +44,9 @@ public void load(Map<ResourceLocation, String> scripts) {
scope.put("Resources", scope, new NativeJavaClass(scope, ScriptResourceUtil.class));
scope.put("GraphicsTexture", scope, new NativeJavaClass(scope, GraphicsTexture.class));

scope.put("Timing", scope, new NativeJavaClass(scope, ScriptTimingUtil.class));
scope.put("Timing", scope, new NativeJavaClass(scope, TimingUtil.class));
scope.put("StateTracker", scope, new NativeJavaClass(scope, StateTracker.class));
scope.put("CycleTracker", scope, new NativeJavaClass(scope, CycleTracker.class));

scope.put("Color", scope, new NativeJavaClass(scope, Color.class));
scope.put("RawModel", scope, new NativeJavaClass(scope, RawModel.class));
Expand All @@ -68,10 +70,10 @@ public void load(Map<ResourceLocation, String> scripts) {
new NativeJavaClass(scope, classToLoadClass));
}
scope.put("CompoundTag", scope, new NativeJavaClass(scope, CompoundTag.class));
scope.put("installedMadParticle", scope, true);
scope.put("foundMadParticle", scope, true);
} catch (ClassNotFoundException ignored) {
Main.LOGGER.warn("MadParticle", ignored);
scope.put("installedMadParticle", scope, false);
scope.put("foundMadParticle", scope, false);
}

ScriptResourceUtil.scriptsToExecute = new ArrayList<>(scripts.entrySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.Optional;

@SuppressWarnings("unused")
public class ScriptResourceUtil {

protected static List<Map.Entry<ResourceLocation, String>> scriptsToExecute;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cn.zbx1425.mtrsteamloco.render.scripting.util;

@SuppressWarnings("unused")
public class CycleTracker {

private final String[] states;
private final float[] offsets;
private final float cycleDuration;

public CycleTracker(Object[] params) {
if (params.length % 2 != 0) throw new IllegalArgumentException();
float offset = 0;
states = new String[params.length / 2];
offsets = new float[params.length / 2];
for (int i = 0; i < params.length; i += 2) {
if (!(params[i] instanceof String)) throw new IllegalArgumentException();
if (!(params[i + 1] instanceof Float)) throw new IllegalArgumentException();
states[i / 2] = params[i].toString();
float elemDuration = Float.parseFloat(params[i + 1].toString());
offsets[i / 2] = offset;
offset += elemDuration;
}
cycleDuration = offset;
}

private String lastState;
private String currentState;
private float currentStateTime;
private int lastStateNum;
private boolean firstTimeCurrentState;

public void tick() {
float time = TimingUtil.elapsed() % cycleDuration;
int cycleNum = (int) (TimingUtil.elapsed() / cycleDuration);
for (int i = 0; i < offsets.length; i++) {
if (time >= offsets[i]) {
int stateNum = cycleNum * offsets.length + i;
currentState = states[i];
currentStateTime = offsets[i];
lastState = states[i == 0 ? offsets.length - 1 : i - 1];
if (lastStateNum != stateNum) {
firstTimeCurrentState = true;
lastStateNum = stateNum;
} else {
firstTimeCurrentState = false;
}
break;
}
}
}

public String stateNow() {
return currentState;
}

public String stateLast() {
return lastState;
}

public float stateNowDuration() {
return TimingUtil.elapsed() - currentStateTime;
}

public boolean stateNowFirst() {
return firstTimeCurrentState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.awt.image.*;
import java.util.UUID;

@SuppressWarnings("unused")
public class GraphicsTexture {

private final DynamicTexture dynamicTexture;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.zbx1425.mtrsteamloco.render.scripting.util;

@SuppressWarnings("unused")
public class StateTracker {

private String lastState;
Expand All @@ -11,7 +12,7 @@ public void setState(String value) {
if (value != null && !value.equals(currentState)) {
lastState = currentState;
currentState = value;
currentStateTime = ScriptTimingUtil.elapsed();
currentStateTime = TimingUtil.elapsed();
firstTimeCurrentState = true;
} else if (value != null) {
firstTimeCurrentState = false;
Expand All @@ -27,7 +28,7 @@ public String stateLast() {
}

public float stateNowDuration() {
return ScriptTimingUtil.elapsed() - currentStateTime;
return TimingUtil.elapsed() - currentStateTime;
}

public boolean stateNowFirst() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import mtr.MTRClient;

public class ScriptTimingUtil {
@SuppressWarnings("unused")
public class TimingUtil {

public static float elapsed() {
return MTRClient.getGameTick() / 20;
Expand Down

0 comments on commit 8bfd34f

Please sign in to comment.