Skip to content

Commit

Permalink
Persistent player spectating on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuken committed Oct 29, 2024
1 parent 81282ed commit 1558783
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
9 changes: 7 additions & 2 deletions core/src/mindustry/input/DesktopInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,18 @@ public void update(){
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;
}
}

Expand All @@ -258,11 +261,13 @@ public void update(){
}

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){
Expand Down
10 changes: 10 additions & 0 deletions core/src/mindustry/input/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{
public Seq<BuildPlan> selectPlans = new Seq<>(BuildPlan.class);
public Queue<BuildPlan> lastPlans = new Queue<>();
public @Nullable Unit lastUnit;
public @Nullable Unit spectating;

//for RTS controls
public Seq<Unit> selectedUnits = new Seq<>();
Expand Down Expand Up @@ -801,7 +802,16 @@ public boolean isUsingSchematic(){
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{
Expand Down
7 changes: 6 additions & 1 deletion core/src/mindustry/input/MobileInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,11 @@ public void update(){
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")){
Expand Down Expand Up @@ -940,6 +944,7 @@ public boolean pan(float x, float y, float deltaX, float deltaY){
//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);
Expand Down
6 changes: 1 addition & 5 deletions core/src/mindustry/ui/fragments/PlayerListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.input.*;
import mindustry.net.*;
import mindustry.net.Packets.*;
import mindustry.ui.*;
Expand Down Expand Up @@ -127,11 +126,8 @@ public void draw(){

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;
}
}
});

Expand Down

0 comments on commit 1558783

Please sign in to comment.