From 1558783b7d3b51cb075d196b1d76ee96b7a2c656 Mon Sep 17 00:00:00 2001 From: Anuken Date: Tue, 29 Oct 2024 10:08:04 -0400 Subject: [PATCH] Persistent player spectating on desktop --- core/src/mindustry/input/DesktopInput.java | 9 +++++++-- core/src/mindustry/input/InputHandler.java | 10 ++++++++++ core/src/mindustry/input/MobileInput.java | 7 ++++++- .../src/mindustry/ui/fragments/PlayerListFragment.java | 6 +----- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/src/mindustry/input/DesktopInput.java b/core/src/mindustry/input/DesktopInput.java index 2fabd1b23e..0563b27754 100644 --- a/core/src/mindustry/input/DesktopInput.java +++ b/core/src/mindustry/input/DesktopInput.java @@ -236,15 +236,18 @@ public class DesktopInput extends InputHandler{ if(!detached){ panning = false; } + spectating = null; } if(input.keyDown(Binding.pan)){ panCam = true; panning = true; + spectating = null; } if((Math.abs(Core.input.axis(Binding.move_x)) > 0 || Math.abs(Core.input.axis(Binding.move_y)) > 0 || input.keyDown(Binding.mouse_move))){ panning = false; + spectating = null; } } @@ -258,11 +261,13 @@ public class DesktopInput extends InputHandler{ } Core.camera.position.add(Tmp.v1.setZero().add(Core.input.axis(Binding.move_x), Core.input.axis(Binding.move_y)).nor().scl(camSpeed)); - }else if(!player.dead() && !panning){ + }else if((!player.dead() || spectating != null) && !panning){ //TODO do not pan Team corePanTeam = state.won ? state.rules.waveTeam : player.team(); Position coreTarget = state.gameOver && !state.rules.pvp && corePanTeam.data().lastCore != null ? corePanTeam.data().lastCore : null; - Core.camera.position.lerpDelta(coreTarget != null ? coreTarget : player, Core.settings.getBool("smoothcamera") ? 0.08f : 1f); + Position panTarget = coreTarget != null ? coreTarget : spectating != null ? spectating : player; + + Core.camera.position.lerpDelta(panTarget, Core.settings.getBool("smoothcamera") ? 0.08f : 1f); } if(panCam){ diff --git a/core/src/mindustry/input/InputHandler.java b/core/src/mindustry/input/InputHandler.java index d4331e3ff5..9c1a90919e 100644 --- a/core/src/mindustry/input/InputHandler.java +++ b/core/src/mindustry/input/InputHandler.java @@ -100,6 +100,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ public Seq selectPlans = new Seq<>(BuildPlan.class); public Queue lastPlans = new Queue<>(); public @Nullable Unit lastUnit; + public @Nullable Unit spectating; //for RTS controls public Seq selectedUnits = new Seq<>(); @@ -801,7 +802,16 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ return !selectPlans.isEmpty(); } + public void spectate(Unit unit){ + spectating = unit; + camera.position.set(unit); + } + public void update(){ + if(spectating != null && (!spectating.isValid() || spectating.team != player.team())){ + spectating = null; + } + if(logicCutscene && !renderer.isCutscene()){ Core.camera.position.lerpDelta(logicCamPan, logicCamSpeed); }else{ diff --git a/core/src/mindustry/input/MobileInput.java b/core/src/mindustry/input/MobileInput.java index f4cb6aaf4f..24a74521fd 100644 --- a/core/src/mindustry/input/MobileInput.java +++ b/core/src/mindustry/input/MobileInput.java @@ -779,7 +779,11 @@ public class MobileInput extends InputHandler implements GestureListener{ if(!Core.settings.getBool("keyboard") && !locked && !scene.hasKeyboard()){ //move camera around float camSpeed = 6f; - Core.camera.position.add(Tmp.v1.setZero().add(Core.input.axis(Binding.move_x), Core.input.axis(Binding.move_y)).nor().scl(Time.delta * camSpeed)); + Vec2 delta = Tmp.v1.setZero().add(Core.input.axis(Binding.move_x), Core.input.axis(Binding.move_y)).nor().scl(Time.delta * camSpeed); + Core.camera.position.add(delta); + if(!delta.isZero()){ + spectating = null; + } } if(Core.settings.getBool("keyboard")){ @@ -940,6 +944,7 @@ public class MobileInput extends InputHandler implements GestureListener{ //pan player Core.camera.position.x -= deltaX; Core.camera.position.y -= deltaY; + spectating = null; } camera.position.clamp(-camera.width/4f, -camera.height/4f, world.unitWidth() + camera.width/4f, world.unitHeight() + camera.height/4f); diff --git a/core/src/mindustry/ui/fragments/PlayerListFragment.java b/core/src/mindustry/ui/fragments/PlayerListFragment.java index 9286aab195..58d82ae32d 100644 --- a/core/src/mindustry/ui/fragments/PlayerListFragment.java +++ b/core/src/mindustry/ui/fragments/PlayerListFragment.java @@ -13,7 +13,6 @@ import arc.util.*; import mindustry.game.*; import mindustry.gen.*; import mindustry.graphics.*; -import mindustry.input.*; import mindustry.net.*; import mindustry.net.Packets.*; import mindustry.ui.*; @@ -127,11 +126,8 @@ public class PlayerListFragment{ iconTable.tapped(() -> { if(!user.dead() && clickable){ - Core.camera.position.set(user.unit()); + control.input.spectate(user.unit()); ui.showInfoFade(Core.bundle.format("viewplayer", user.name), 1f); - if(control.input instanceof DesktopInput input){ - input.panning = true; - } } });