From 210967cfef4c20f258aec3daafee30be7881ecf0 Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 4 Jun 2018 18:32:03 -0400 Subject: [PATCH] Added syncable bullets --- .../content/bullets/TurretBullets.java | 4 +- .../mindustry/entities/bullet/Bullet.java | 41 +++++++++++++++++-- .../mindustry/entities/bullet/BulletType.java | 2 + .../mindustry/entities/effect/ItemDrop.java | 2 + .../mindustry/entities/traits/SyncTrait.java | 4 ++ .../anuke/mindustry/input/DesktopInput.java | 4 +- .../anuke/mindustry/input/InputHandler.java | 5 ++- 7 files changed, 52 insertions(+), 10 deletions(-) diff --git a/core/src/io/anuke/mindustry/content/bullets/TurretBullets.java b/core/src/io/anuke/mindustry/content/bullets/TurretBullets.java index 954d0ab770..c6cfccaed4 100644 --- a/core/src/io/anuke/mindustry/content/bullets/TurretBullets.java +++ b/core/src/io/anuke/mindustry/content/bullets/TurretBullets.java @@ -101,7 +101,7 @@ public class TurretBullets implements ContentList { @Override public void init(Bullet b) { - DamageArea.collideLine(b, b.team, hiteffect, b.x, b.y, b.angle(), length); + DamageArea.collideLine(b, b.getTeam(), hiteffect, b.x, b.y, b.angle(), length); } @Override @@ -168,7 +168,7 @@ public class TurretBullets implements ContentList { @Override public void init(Bullet b) { - Lightning.create(b.team, hiteffect, Palette.lancerLaser, damage, b.x, b.y, b.angle(), 30); + Lightning.create(b.getTeam(), hiteffect, Palette.lancerLaser, damage, b.x, b.y, b.angle(), 30); } }; } diff --git a/core/src/io/anuke/mindustry/entities/bullet/Bullet.java b/core/src/io/anuke/mindustry/entities/bullet/Bullet.java index 3c7ccf6019..a47ddcf2fe 100644 --- a/core/src/io/anuke/mindustry/entities/bullet/Bullet.java +++ b/core/src/io/anuke/mindustry/entities/bullet/Bullet.java @@ -3,24 +3,30 @@ package io.anuke.mindustry.entities.bullet; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Pools; import io.anuke.mindustry.entities.Unit; +import io.anuke.mindustry.entities.traits.SyncTrait; import io.anuke.mindustry.entities.traits.TeamTrait; import io.anuke.mindustry.game.Team; +import io.anuke.mindustry.net.Interpolator; import io.anuke.mindustry.world.Tile; import io.anuke.ucore.entities.EntityGroup; +import io.anuke.ucore.entities.impl.BulletEntity; import io.anuke.ucore.entities.trait.Entity; import io.anuke.ucore.entities.trait.SolidTrait; import io.anuke.ucore.entities.trait.VelocityTrait; -import io.anuke.ucore.entities.impl.BulletEntity; import io.anuke.ucore.util.Timer; +import java.nio.ByteBuffer; + import static io.anuke.mindustry.Vars.bulletGroup; import static io.anuke.mindustry.Vars.world; -public class Bullet extends BulletEntity implements TeamTrait{ +public class Bullet extends BulletEntity implements TeamTrait, SyncTrait{ private static Vector2 vector = new Vector2(); + private Interpolator interpolator = new Interpolator(); + private Team team; + public Timer timer = new Timer(3); - public Team team; public static Bullet create(BulletType type, TeamTrait owner, float x, float y, float angle){ return create(type, owner, owner.getTeam(), x, y, angle); @@ -46,12 +52,39 @@ public class Bullet extends BulletEntity implements TeamTrait{ return create(type, parent.owner, parent.team, x, y, angle); } - private Bullet(){} + /**Internal use only!*/ + public Bullet(){} public boolean collidesTiles(){ return true; //TODO make artillery and such not do this } + @Override + public boolean doSync(){ + return type.syncable; + } + + @Override + public Interpolator getInterpolator() { + return interpolator; + } + + @Override + public void write(ByteBuffer data) { + data.putFloat(x); + data.putFloat(y); + data.put((byte)team.ordinal()); + data.put((byte)type.id); + } + + @Override + public void read(ByteBuffer data, long time) { + x = data.getFloat(); + y = data.getFloat(); + team = Team.values()[data.get()]; + type = BulletType.getByID(data.get()); + } + @Override public Team getTeam() { return team; diff --git a/core/src/io/anuke/mindustry/entities/bullet/BulletType.java b/core/src/io/anuke/mindustry/entities/bullet/BulletType.java index 4702633b42..972139a050 100644 --- a/core/src/io/anuke/mindustry/entities/bullet/BulletType.java +++ b/core/src/io/anuke/mindustry/entities/bullet/BulletType.java @@ -22,6 +22,8 @@ public abstract class BulletType extends BaseBulletType{ public float statusIntensity = 0.5f; /**What fraction of armor is pierced, 0-1*/ public float armorPierce = 0f; + /**Whether to sync this bullet to clients.*/ + public boolean syncable; public BulletType(float speed, float damage){ this.id = lastid ++; diff --git a/core/src/io/anuke/mindustry/entities/effect/ItemDrop.java b/core/src/io/anuke/mindustry/entities/effect/ItemDrop.java index 15d7c3b9e1..75dbaabd81 100644 --- a/core/src/io/anuke/mindustry/entities/effect/ItemDrop.java +++ b/core/src/io/anuke/mindustry/entities/effect/ItemDrop.java @@ -158,11 +158,13 @@ public class ItemDrop extends SolidEntity implements SyncTrait, DrawTrait, Veloc public void write(ByteBuffer data) { data.putFloat(x); data.putFloat(y); + data.put((byte)item.id); } @Override public void read(ByteBuffer data, long time) { x = data.getFloat(); y = data.getFloat(); + item = Item.getByID(data.get()); } } diff --git a/core/src/io/anuke/mindustry/entities/traits/SyncTrait.java b/core/src/io/anuke/mindustry/entities/traits/SyncTrait.java index 21113f01d9..c16d955037 100644 --- a/core/src/io/anuke/mindustry/entities/traits/SyncTrait.java +++ b/core/src/io/anuke/mindustry/entities/traits/SyncTrait.java @@ -14,6 +14,10 @@ public interface SyncTrait extends Entity { return threads.isEnabled() && threads.getFPS() <= Gdx.graphics.getFramesPerSecond() / 2f; } + default boolean doSync(){ + return true; + } + default void setNet(float x, float y){ set(x, y); getInterpolator().target.set(x, y); diff --git a/core/src/io/anuke/mindustry/input/DesktopInput.java b/core/src/io/anuke/mindustry/input/DesktopInput.java index d3901a45af..284f818829 100644 --- a/core/src/io/anuke/mindustry/input/DesktopInput.java +++ b/core/src/io/anuke/mindustry/input/DesktopInput.java @@ -190,8 +190,8 @@ public class DesktopInput extends InputHandler{ mode = placing; } else { //only begin shooting if there's no cursor event - if(!tileTapped(cursor) && player.getPlaceQueue().size == 0 && !tryBeginMine(cursor) - && player.getMineTile() == null && !tryTapPlayer(worldx, worldy) && !droppingItem){ + if(!tileTapped(cursor) && player.getPlaceQueue().size == 0 && !tryTapPlayer(worldx, worldy) && !droppingItem && + !tryBeginMine(cursor) && player.getMineTile() == null){ shooting = true; } } diff --git a/core/src/io/anuke/mindustry/input/InputHandler.java b/core/src/io/anuke/mindustry/input/InputHandler.java index f2565a7475..bfa46788e8 100644 --- a/core/src/io/anuke/mindustry/input/InputHandler.java +++ b/core/src/io/anuke/mindustry/input/InputHandler.java @@ -26,7 +26,7 @@ import static io.anuke.mindustry.Vars.*; public abstract class InputHandler extends InputAdapter{ /**Used for dropping items.*/ - final float playerSelectRange = 16f; + final float playerSelectRange = mobile ? 17f : 11f; /**Maximum line length.*/ final int maxLength = 100; final Translator stackTrns = new Translator(); @@ -152,6 +152,7 @@ public abstract class InputHandler extends InputAdapter{ boolean canMine(Tile tile){ return tile.floor().drops != null && tile.floor().drops.item.hardness <= player.mech.drillPower + && player.inventory.canAcceptItem(tile.floor().drops.item) && tile.block() == Blocks.air && player.distanceTo(tile.worldx(), tile.worldy()) <= Player.mineDistance; } @@ -191,7 +192,7 @@ public abstract class InputHandler extends InputAdapter{ } public void tryDropItems(Tile tile, float x, float y){ - if(!droppingItem || !player.inventory.hasItem() || !tile.block().hasItems){ + if(!droppingItem || !player.inventory.hasItem() || !tile.block().hasItems || canTapPlayer(x, y)){ droppingItem = false; return; }