pushbutton

This commit is contained in:
RandomityGuy 2023-02-14 13:40:53 +05:30
parent c56d382c3a
commit 7fbf8f26af
3 changed files with 87 additions and 1 deletions

View file

@ -1,5 +1,6 @@
package src;
import shapes.PushButton;
#if js
import gui.MainMenuGui;
#else
@ -473,6 +474,7 @@ class MarbleWorld extends Scheduler {
if (full) {
var tidx = 0;
var lidx = 0;
var pidx = 0;
for (dtss in this.dtsObjects) {
if (dtss is Trapdoor) {
var trapdoor:Trapdoor = cast dtss;
@ -496,6 +498,15 @@ class MarbleWorld extends Scheduler {
}
lidx++;
}
if (dtss is PushButton) {
var pushbutton:PushButton = cast dtss;
if (!this.isWatching) {
this.replay.recordPushButtonState(pushbutton.lastContactTime - this.timeState.timeSinceLoad);
} else {
pushbutton.lastContactTime = this.replay.getPushButtonState(pidx) + this.timeState.timeSinceLoad;
}
pidx++;
}
}
}
@ -763,6 +774,8 @@ class MarbleWorld extends Scheduler {
shape = new Tornado();
else if (dataBlockLowerCase == "trapdoor")
shape = new Trapdoor();
else if (dataBlockLowerCase == "pushbutton")
shape = new PushButton();
else if (dataBlockLowerCase == "oilslick")
shape = new Oilslick();
else if (dataBlockLowerCase == "arrow" || StringTools.startsWith(dataBlockLowerCase, "sign"))
@ -880,6 +893,8 @@ class MarbleWorld extends Scheduler {
shape = new Tornado();
else if (dataBlockLowerCase == "trapdoor")
shape = new Trapdoor();
else if (dataBlockLowerCase == "pushbutton")
shape = new PushButton();
else if (dataBlockLowerCase == "oilslick")
shape = new Oilslick();
else if (dataBlockLowerCase == "arrow" || StringTools.startsWith(dataBlockLowerCase, "sign"))

View file

@ -201,6 +201,7 @@ class ReplayInitialState {
var trapdoorLastDirections:Array<Int> = [];
var trapdoorLastCompletions:Array<Float> = [];
var landMineDisappearTimes:Array<Float> = [];
var pushButtonContactTimes:Array<Float> = [];
public function new() {}
@ -219,6 +220,10 @@ class ReplayInitialState {
for (time in this.landMineDisappearTimes) {
bw.writeFloat(time);
}
bw.writeInt16(this.pushButtonContactTimes.length);
for (time in this.pushButtonContactTimes) {
bw.writeFloat(time);
}
}
public function read(br:BytesReader) {
@ -236,6 +241,10 @@ class ReplayInitialState {
for (i in 0...landMineCount) {
this.landMineDisappearTimes.push(br.readFloat());
}
var pushButtonCount = br.readInt16();
for (i in 0...pushButtonCount) {
this.pushButtonContactTimes.push(br.readFloat());
}
}
}
@ -252,7 +261,7 @@ class Replay {
var currentPlaybackFrameIdx:Int;
var currentPlaybackTime:Float;
var version:Int = 5;
var version:Int = 6;
var readFullEntry:FileEntry;
public function new(mission:String) {
@ -328,6 +337,10 @@ class Replay {
initialState.landMineDisappearTimes.push(disappearTime);
}
public function recordPushButtonState(lastContactTime:Float) {
initialState.pushButtonContactTimes.push(lastContactTime);
}
public function getTrapdoorState(idx:Int) {
return {
lastContactTime: initialState.trapdoorLastContactTimes[idx],
@ -340,6 +353,10 @@ class Replay {
return initialState.landMineDisappearTimes[idx];
}
public function getPushButtonState(idx:Int) {
return initialState.pushButtonContactTimes[idx];
}
public function clear() {
this.frames = [];
currentRecordFrame = null;

54
src/shapes/PushButton.hx Normal file
View file

@ -0,0 +1,54 @@
package shapes;
import hxd.snd.effect.Spatialization;
import src.TimeState;
import collision.CollisionInfo;
import src.Util;
import src.DtsObject;
import h3d.Vector;
import src.ForceObject;
import src.ResourceLoader;
import src.AudioManager;
import src.MarbleWorld;
class PushButton extends DtsObject {
var lastContactTime = -1e8;
public function new() {
super();
this.dtsPath = "data/shapes/buttons/pushbutton.dts";
this.isCollideable = true;
this.isTSStatic = false;
this.identifier = "PushButton";
this.hasNonVisualSequences = true;
this.enableCollideCallbacks = true;
}
public override function update(timeState:TimeState) {
var currentCompletion = this.getCurrentCompletion(timeState);
// Override the keyframe
this.sequenceKeyframeOverride.set(this.dts.sequences[0], currentCompletion * (this.dts.sequences[0].numKeyFrames - 1));
super.update(timeState);
}
function getCurrentCompletion(timeState:TimeState) {
var elapsed = timeState.timeSinceLoad - this.lastContactTime;
var completion = Util.clamp(elapsed / this.dts.sequences[0].duration, 0, 1);
if (elapsed > 5)
completion = Util.clamp(1 - (elapsed - 5) / this.dts.sequences[0].duration, 0, 1);
return completion;
}
override function onMarbleContact(time:TimeState, ?contact:CollisionInfo) {
super.onMarbleContact(time, contact);
if (time.timeSinceLoad - this.lastContactTime <= 0)
return; // The trapdoor is queued to open, so don't do anything.
var currentCompletion = this.getCurrentCompletion(time);
if (currentCompletion == 0)
this.lastContactTime = time.timeSinceLoad;
// this.level.replay.recordMarbleContact(this);
}
}