teleporter networking

This commit is contained in:
RandomityGuy 2024-06-27 00:11:40 +05:30
parent 2d5bdda9ab
commit 57873e6301
2 changed files with 69 additions and 47 deletions

View file

@ -85,6 +85,9 @@ class MPExitGameDlg extends GuiControl {
kickBtn.position = new Vector(108, 184); kickBtn.position = new Vector(108, 184);
kickBtn.extent = new Vector(45, 45); kickBtn.extent = new Vector(45, 45);
kickBtn.vertSizing = Top; kickBtn.vertSizing = Top;
kickBtn.pressedAction = (e) -> {
MarbleGame.canvas.pushDialog(new MPKickBanDlg());
}
dialogImg.addChild(kickBtn); dialogImg.addChild(kickBtn);
if (!Net.isHost) { if (!Net.isHost) {
kickBtn.disabled = true; kickBtn.disabled = true;

View file

@ -1,5 +1,6 @@
package triggers; package triggers;
import src.Marble;
import h3d.Vector; import h3d.Vector;
import src.ResourceLoader; import src.ResourceLoader;
import src.AudioManager; import src.AudioManager;
@ -8,11 +9,17 @@ import src.MarbleWorld;
import mis.MissionElement.MissionElementTrigger; import mis.MissionElement.MissionElementTrigger;
import src.Console; import src.Console;
@:publicFields
@:structInit
class TeleportationState {
var entryTime:Null<Float>;
var exitTime:Null<Float>;
}
class TeleportTrigger extends Trigger { class TeleportTrigger extends Trigger {
var delay:Float = 2; var delay:Float = 2;
var entryTime:Null<Float> = null; var marbleStates:Map<Marble, TeleportationState> = [];
var exitTime:Null<Float> = null;
public function new(element:MissionElementTrigger, level:MarbleWorld) { public function new(element:MissionElementTrigger, level:MarbleWorld) {
super(element, level); super(element, level);
@ -20,35 +27,48 @@ class TeleportTrigger extends Trigger {
this.delay = MisParser.parseNumber(element.delay) / 1000; this.delay = MisParser.parseNumber(element.delay) / 1000;
} }
function getState(marble:Marble) {
if (marbleStates.exists(marble))
return marbleStates.get(marble);
else {
marbleStates.set(marble, {entryTime: null, exitTime: null});
return marbleStates.get(marble);
}
}
override function onMarbleEnter(marble:src.Marble, time:src.TimeState) { override function onMarbleEnter(marble:src.Marble, time:src.TimeState) {
this.exitTime = null; var state = getState(marble);
state.exitTime = null;
marble.setCloaking(true, time); marble.setCloaking(true, time);
if (this.entryTime != null) if (state.entryTime != null)
return; return;
this.entryTime = time.currentAttemptTime; state.entryTime = time.currentAttemptTime;
this.level.displayAlert("Teleporter has been activated, please wait."); if (level.marble == marble && @:privateAccess !marble.isNetUpdate) {
AudioManager.playSound(ResourceLoader.getResource("data/sound/teleport.wav", ResourceLoader.getAudio, this.soundResources)); this.level.displayAlert("Teleporter has been activated, please wait.");
AudioManager.playSound(ResourceLoader.getResource("data/sound/teleport.wav", ResourceLoader.getAudio, this.soundResources));
}
} }
override function onMarbleLeave(marble:src.Marble, time:src.TimeState) { override function onMarbleLeave(marble:src.Marble, time:src.TimeState) {
this.exitTime = time.currentAttemptTime; var state = getState(marble);
state.exitTime = time.currentAttemptTime;
marble.setCloaking(false, time); marble.setCloaking(false, time);
} }
public override function update(timeState:src.TimeState) { public override function update(timeState:src.TimeState) {
if (this.entryTime == null) for (marble => state in marbleStates) {
return; if (state.entryTime == null)
continue;
if (state.exitTime != null && timeState.currentAttemptTime - state.exitTime > 0.05) {
state.entryTime = null;
state.exitTime = null;
continue;
}
if (timeState.currentAttemptTime - this.entryTime >= this.delay) { if (timeState.currentAttemptTime - state.entryTime >= this.delay) {
this.executeTeleport(); state.entryTime = null;
return; this.executeTeleport(marble);
} }
// There's a little delay after exiting before the teleporter gets cancelled
if (this.exitTime != null && timeState.currentAttemptTime - this.exitTime > 0.050) {
this.entryTime = null;
this.exitTime = null;
return;
} }
} }
@ -56,9 +76,7 @@ class TeleportTrigger extends Trigger {
ResourceLoader.load("sound/teleport.wav").entry.load(onFinish); ResourceLoader.load("sound/teleport.wav").entry.load(onFinish);
} }
function executeTeleport() { function executeTeleport(marble:Marble) {
this.entryTime = null;
function chooseNonNull(a:String, b:String) { function chooseNonNull(a:String, b:String) {
if (a != null) if (a != null)
return a; return a;
@ -88,45 +106,46 @@ class TeleportTrigger extends Trigger {
position = destination.vertices[0].add(new Vector(0, 0, 3)).add(pos); // destination.vertices[0].clone().add(new Vector(0, 0, 3)); position = destination.vertices[0].add(new Vector(0, 0, 3)).add(pos); // destination.vertices[0].clone().add(new Vector(0, 0, 3));
} }
position.w = 1; position.w = 1;
this.level.marble.prevPos.load(position); marble.prevPos.load(position);
this.level.marble.setPosition(position.x, position.y, position.z); marble.setPosition(position.x, position.y, position.z);
var ct = this.level.marble.collider.transform.clone(); var ct = marble.collider.transform.clone();
ct.setPosition(position); ct.setPosition(position);
this.level.marble.collider.setTransform(ct); marble.collider.setTransform(ct);
if (this.level.isRecording) { if (this.level.isRecording) {
this.level.replay.recordMarbleStateFlags(false, false, true, false); this.level.replay.recordMarbleStateFlags(false, false, true, false);
} }
if (!MisParser.parseBoolean(chooseNonNull(this.element.keepvelocity, destination.element.keepvelocity))) if (!MisParser.parseBoolean(chooseNonNull(this.element.keepvelocity, destination.element.keepvelocity)))
this.level.marble.velocity.set(0, 0, 0); marble.velocity.set(0, 0, 0);
if (MisParser.parseBoolean(chooseNonNull(this.element.inversevelocity, destination.element.inversevelocity))) if (MisParser.parseBoolean(chooseNonNull(this.element.inversevelocity, destination.element.inversevelocity)))
this.level.marble.velocity.scale(-1); marble.velocity.scale(-1);
if (!MisParser.parseBoolean(chooseNonNull(this.element.keepangular, destination.element.keepangular))) if (!MisParser.parseBoolean(chooseNonNull(this.element.keepangular, destination.element.keepangular)))
this.level.marble.omega.set(0, 0, 0); marble.omega.set(0, 0, 0);
Console.log('Teleport:'); Console.log('Teleport:');
Console.log('Marble Position: ${position.x} ${position.y} ${position.z}'); Console.log('Marble Position: ${position.x} ${position.y} ${position.z}');
Console.log('Marble Velocity: ${this.level.marble.velocity.x} ${this.level.marble.velocity.y} ${this.level.marble.velocity.z}'); Console.log('Marble Velocity: ${marble.velocity.x} ${marble.velocity.y} ${marble.velocity.z}');
Console.log('Marble Angular: ${this.level.marble.omega.x} ${this.level.marble.omega.y} ${this.level.marble.omega.z}'); Console.log('Marble Angular: ${marble.omega.x} ${marble.omega.y} ${marble.omega.z}');
// Determine camera orientation // Determine camera orientation
if (!MisParser.parseBoolean(chooseNonNull(this.element.keepcamera, destination.element.keepcamera))) { if (marble == level.marble) {
var yaw:Float; if (!MisParser.parseBoolean(chooseNonNull(this.element.keepcamera, destination.element.keepcamera))) {
if (this.element.camerayaw != null) var yaw:Float;
yaw = MisParser.parseNumber(this.element.camerayaw) * Math.PI / 180; if (this.element.camerayaw != null)
else if (destination.element.camerayaw != null) yaw = MisParser.parseNumber(this.element.camerayaw) * Math.PI / 180;
yaw = MisParser.parseNumber(destination.element.camerayaw) * Math.PI / 180; else if (destination.element.camerayaw != null)
else yaw = MisParser.parseNumber(destination.element.camerayaw) * Math.PI / 180;
yaw = 0; else
yaw = 0;
yaw = -yaw; // Need to flip it for some reason yaw = -yaw; // Need to flip it for some reason
this.level.marble.camera.CameraYaw = yaw + Math.PI / 2; marble.camera.CameraYaw = yaw + Math.PI / 2;
this.level.marble.camera.CameraPitch = 0.45; marble.camera.CameraPitch = 0.45;
this.level.marble.camera.nextCameraYaw = yaw + Math.PI / 2; marble.camera.nextCameraYaw = yaw + Math.PI / 2;
this.level.marble.camera.nextCameraPitch = 0.45; marble.camera.nextCameraPitch = 0.45;
}
AudioManager.playSound(ResourceLoader.getResource("data/sound/spawn.wav", ResourceLoader.getAudio, this.soundResources));
} }
AudioManager.playSound(ResourceLoader.getResource("data/sound/spawn.wav", ResourceLoader.getAudio, this.soundResources));
} }
} }