From 8045c6afb86335b3951b24ae9525b40fdbb3d212 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 6 Jan 2018 12:22:05 -0500 Subject: [PATCH] Weapons are now displayed for players --- core/src/io/anuke/mindustry/core/NetClient.java | 17 +++++++++++++++++ core/src/io/anuke/mindustry/core/NetServer.java | 16 ++++++++++++++-- .../src/io/anuke/mindustry/entities/Player.java | 17 +++++++++++++++-- .../io/anuke/mindustry/input/DesktopInput.java | 2 ++ core/src/io/anuke/mindustry/net/Packets.java | 5 +++++ .../src/io/anuke/mindustry/net/Registrator.java | 1 + .../mindustry/ui/fragments/WeaponFragment.java | 2 ++ 7 files changed, 56 insertions(+), 4 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/NetClient.java b/core/src/io/anuke/mindustry/core/NetClient.java index 825dc8b620..4148519ba8 100644 --- a/core/src/io/anuke/mindustry/core/NetClient.java +++ b/core/src/io/anuke/mindustry/core/NetClient.java @@ -252,6 +252,15 @@ public class NetClient extends Module { GameState.set(State.menu); Gdx.app.postRunnable(() -> Vars.ui.showError("$text.server.kicked." + KickReason.values()[packet.reason].name())); }); + + Net.handle(WeaponSwitchPacket.class, packet -> { + Player player = Vars.control.playerGroup.getByID(packet.playerid); + + if(player == null) return; + + player.weaponLeft = (Weapon)Upgrade.getByID(packet.left); + player.weaponRight = (Weapon)Upgrade.getByID(packet.right); + }); } @Override @@ -265,6 +274,14 @@ public class NetClient extends Module { } } + public void handleWeaponSwitch(){ + WeaponSwitchPacket packet = new WeaponSwitchPacket(); + packet.left = Vars.player.weaponLeft.id; + packet.right = Vars.player.weaponRight.id; + packet.playerid = Vars.player.id; + Net.send(packet, SendMode.tcp); + } + public void handleUpgrade(Weapon weapon){ UpgradePacket packet = new UpgradePacket(); packet.id = weapon.id; diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index bc318c2bd4..d8400f1e92 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -147,8 +147,7 @@ public class NetServer extends Module{ Net.handleServer(ChatPacket.class, packet -> { Player player = connections.get(Net.getLastConnection()); - if(player == null) - return; //GHOSTS AAAA + if(player == null) return; //GHOSTS AAAA packet.name = player.name; Net.send(packet, SendMode.tcp); @@ -158,6 +157,19 @@ public class NetServer extends Module{ Weapon weapon = (Weapon)Upgrade.getByID(packet.id); Vars.control.removeItems(UpgradeRecipes.get(weapon)); }); + + Net.handleServer(WeaponSwitchPacket.class, packet -> { + Player player = connections.get(Net.getLastConnection()); + + if(player == null) return; + + packet.playerid = player.id; + + player.weaponLeft = (Weapon)Upgrade.getByID(packet.left); + player.weaponRight = (Weapon)Upgrade.getByID(packet.right); + + Net.sendExcept(player.clientid, packet, SendMode.tcp); + }); } public void handleBullet(BulletType type, Entity owner, float x, float y, float angle, short damage){ diff --git a/core/src/io/anuke/mindustry/entities/Player.java b/core/src/io/anuke/mindustry/entities/Player.java index 64bf481216..e022641311 100644 --- a/core/src/io/anuke/mindustry/entities/Player.java +++ b/core/src/io/anuke/mindustry/entities/Player.java @@ -88,15 +88,28 @@ public class Player extends DestructibleEntity implements Syncable{ } if((Vars.debug && (!Vars.showPlayer || !Vars.showUI)) || (isAndroid && isLocal)) return; + boolean snap = Vars.snapCamera && Settings.getBool("smoothcam") && Settings.getBool("pixelate") && isLocal; String part = isAndroid ? "ship" : "mech"; - if(Vars.snapCamera && Settings.getBool("smoothcam") && Settings.getBool("pixelate") && isLocal){ + if(snap){ Draw.rect(part + "-" + mech.name, (int)x, (int)y, angle-90); }else{ Draw.rect(part + "-" + mech.name, x, y, angle-90); } - + + if(!isAndroid) { + for (boolean b : new boolean[]{true, false}) { + Angles.translation(angle + Mathf.sign(b) * -50f, 3.5f); + String name = b ? weaponLeft.name : weaponRight.name; + float s = 5f; + if(snap){ + Draw.rect(name, (int)x + Angles.x(), (int)y + Angles.y(), s, s, angle- 90); + }else{ + Draw.rect(name, x + Angles.x(), y + Angles.y(), s, s, angle - 90); + } + } + } } @Override diff --git a/core/src/io/anuke/mindustry/input/DesktopInput.java b/core/src/io/anuke/mindustry/input/DesktopInput.java index bfeacb5768..2fb0f256f8 100644 --- a/core/src/io/anuke/mindustry/input/DesktopInput.java +++ b/core/src/io/anuke/mindustry/input/DesktopInput.java @@ -2,6 +2,7 @@ package io.anuke.mindustry.input; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.Vector2; +import io.anuke.mindustry.Vars; import io.anuke.mindustry.core.GameState; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.resource.Weapon; @@ -81,6 +82,7 @@ public class DesktopInput extends InputHandler{ for(int i = 1; i <= 6 && i <= control.getWeapons().size; i ++){ if(Inputs.keyTap("weapon_" + i)){ player.weaponLeft = player.weaponRight = control.getWeapons().get(i - 1); + Vars.netClient.handleWeaponSwitch(); ui.weaponfrag.update(); } } diff --git a/core/src/io/anuke/mindustry/net/Packets.java b/core/src/io/anuke/mindustry/net/Packets.java index c839caba21..683ae483d6 100644 --- a/core/src/io/anuke/mindustry/net/Packets.java +++ b/core/src/io/anuke/mindustry/net/Packets.java @@ -118,4 +118,9 @@ public class Packets { public static class UpgradePacket{ public byte id; //weapon ID only, currently } + + public static class WeaponSwitchPacket{ + public int playerid; + public byte left, right; + } } diff --git a/core/src/io/anuke/mindustry/net/Registrator.java b/core/src/io/anuke/mindustry/net/Registrator.java index 285adde428..baedb47bfe 100644 --- a/core/src/io/anuke/mindustry/net/Registrator.java +++ b/core/src/io/anuke/mindustry/net/Registrator.java @@ -35,6 +35,7 @@ public class Registrator { ChatPacket.class, KickPacket.class, UpgradePacket.class, + WeaponSwitchPacket.class, Class.class, byte[].class, diff --git a/core/src/io/anuke/mindustry/ui/fragments/WeaponFragment.java b/core/src/io/anuke/mindustry/ui/fragments/WeaponFragment.java index ae98372492..920490c6cf 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/WeaponFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/WeaponFragment.java @@ -1,5 +1,6 @@ package io.anuke.mindustry.ui.fragments; +import io.anuke.mindustry.Vars; import io.anuke.mindustry.core.GameState; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.resource.Weapon; @@ -48,6 +49,7 @@ public class WeaponFragment implements Fragment{ player.weaponLeft = weapon; } button.setChecked(true); + Vars.netClient.handleWeaponSwitch(); }); button.update(() -> button.setChecked(weapon == player.weaponLeft || weapon == player.weaponRight)); //TODO