From e716c9b2ea1ea63046011b1669dd2c1c2babeff1 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 12 Sep 2018 03:49:00 +0100 Subject: [PATCH] Completely rework the kart mechanics --- .../src/mineplex/game/nano/NanoManager.java | 11 +++ .../game/nano/game/games/minekart/Kart.java | 91 ++++++++++++++++++- .../game/games/minekart/KartController.java | 69 +++++++++++--- .../nano/game/games/minekart/MineKart.java | 62 +++++++++++-- 4 files changed, 203 insertions(+), 30 deletions(-) diff --git a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/NanoManager.java b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/NanoManager.java index 1ea4805a3..85eaeb65f 100644 --- a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/NanoManager.java +++ b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/NanoManager.java @@ -36,6 +36,7 @@ import mineplex.core.game.nano.NanoFavourite; import mineplex.core.incognito.IncognitoManager; import mineplex.core.incognito.events.IncognitoStatusChangeEvent; import mineplex.core.newnpc.NewNPCManager; +import mineplex.core.packethandler.PacketHandler; import mineplex.core.serverConfig.ServerConfiguration; import mineplex.core.stats.StatsManager; import mineplex.core.status.ServerStatusManager; @@ -113,6 +114,9 @@ public class NanoManager extends MiniPlugin implements IRelation // AntiCheat private final AntiHack _antiHack; + // Packet + private final PacketHandler _packetHandler; + // NPC private final NewNPCManager _npcManager; @@ -189,6 +193,8 @@ public class NanoManager extends MiniPlugin implements IRelation _antiHack = require(AntiHack.class); + _packetHandler = require(PacketHandler.class); + _npcManager = require(NewNPCManager.class); _damageManager = require(DamageManager.class); @@ -412,6 +418,11 @@ public class NanoManager extends MiniPlugin implements IRelation return _antiHack; } + public PacketHandler getPacketHandler() + { + return _packetHandler; + } + public NewNPCManager getNpcManager() { return _npcManager; diff --git a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/Kart.java b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/Kart.java index 8ec6184df..446b4fdbf 100644 --- a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/Kart.java +++ b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/Kart.java @@ -1,11 +1,14 @@ package mineplex.game.nano.game.games.minekart; +import org.bukkit.Location; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.entity.Sheep; import org.bukkit.util.Vector; import mineplex.core.common.util.UtilEnt; +import mineplex.core.common.util.UtilTime; +import mineplex.game.nano.game.games.minekart.KartController.DriftDirection; public class Kart { @@ -16,23 +19,32 @@ public class Kart private int _lap, _lapCheckpoint, _lapKeyCheckpoint; private Vector _velocity; - private boolean _crashed; + private float _frontWaysInput, _sidewaysInput; + private float _yaw; + private DriftDirection _driftDirection; + private float _driftPower; + private long _driftLast; - Kart(MineKart game, Player driver) + private boolean _crashed; + private long _crashedAt; + + Kart(Player driver) { _driver = driver; - _vehicle = driver.getWorld().spawn(driver.getLocation(), Sheep.class); + Location location = driver.getLocation(); + _vehicle = driver.getWorld().spawn(location, Sheep.class); UtilEnt.vegetate(_vehicle, true); UtilEnt.ghost(_vehicle, true, false); UtilEnt.setFakeHead(_vehicle, true); + _vehicle.setPassenger(driver); + _lap = 1; _velocity = new Vector(); - - _vehicle.setPassenger(driver); + _yaw = location.getYaw(); } public void remove() @@ -90,13 +102,82 @@ public class Kart return _velocity; } + public void setInput(float frontWaysInput, float sidewaysInput) + { + _frontWaysInput = frontWaysInput; + _sidewaysInput = sidewaysInput; + } + + public float getFrontWaysInput() + { + return _frontWaysInput; + } + + public float getSidewaysInput() + { + return _sidewaysInput; + } + + public void setYaw(float yaw) + { + _yaw = yaw; + } + + public float getYaw() + { + return _yaw; + } + + public void setDriftDirection(DriftDirection driftDirection) + { + _driftDirection = driftDirection; + } + + public void setDriftPower(float driftPower) + { + _driftPower = Math.min(0.999F, driftPower); + } + + public DriftDirection getDriftDirection() + { + return _driftDirection; + } + + public float getDriftPower() + { + return _driftPower; + } + + public void setDriftLast() + { + _driftLast = System.currentTimeMillis(); + _driftPower = 0; + } + + public boolean isBoosting() + { + return !UtilTime.elapsed(_driftLast, 1000); + } + public void setCrashed(boolean crashed) { _crashed = crashed; + _driftDirection = null; + _driftLast = 0; + + if (crashed) + { + _crashedAt = System.currentTimeMillis(); + } } public boolean isCrashed() { return _crashed; } + + public long getCrashedAt() + { + return _crashedAt; + } } diff --git a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/KartController.java b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/KartController.java index cf62f4e12..12532accf 100644 --- a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/KartController.java +++ b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/KartController.java @@ -4,12 +4,24 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.util.Vector; +import mineplex.core.common.util.UtilAlg; + public class KartController { + public enum DriftDirection + { + LEFT, RIGHT + } + void applyAirDrag(Kart kart) { - Vector velocity = kart.getVelocity().multiply(0.96); + if (kart.getFrontWaysInput() != 0) + { + return; + } + + Vector velocity = kart.getVelocity().multiply(0.95); if (velocity.length() < 0.05) { @@ -21,9 +33,11 @@ public class KartController { Location location = kart.getVehicle().getLocation().add(0, 0.5, 0); + kart.getDriver().sendMessage("y=" + kart.getVehicle().getLocation().getY() % 1); + if (location.getBlock().getType() != Material.AIR) { - kart.getVelocity().multiply(-0.5); + kart.getVelocity().multiply(-1); kart.getVelocity().setY(0.5); kart.setCrashed(true); } @@ -37,21 +51,43 @@ public class KartController } Vector velocity = kart.getVelocity(); - Vector player = kart.getDriver().getLocation().getDirection().setY(0).multiply(0.04); + Vector facing = UtilAlg.getTrajectory(kart.getYaw(), 0).multiply(0.03 * kart.getFrontWaysInput()); + velocity.add(facing); + } - if (velocity.lengthSquared() == 0) + void turn(Kart kart) + { + double velocityLength = kart.getVelocity().length(); + + if (velocityLength < 0.1) { - velocity = player; - - if (velocity.lengthSquared() == 0) - { - return; - } - - kart.setVelocity(velocity); + return; } - velocity.add(player); + float velocityInverse = (float) (2 - velocityLength); + float rotation = 3 * kart.getSidewaysInput() * velocityInverse; + + kart.setYaw(kart.getYaw() - rotation); + } + + void drift(Kart kart) + { + if (kart.getDriftDirection() != null && kart.getVelocity().lengthSquared() > 0) + { + float power = 0.05F * (kart.getDriftDirection() == DriftDirection.LEFT ? kart.getSidewaysInput() : -kart.getSidewaysInput()); + + kart.setDriftPower(kart.getDriftPower() + power); + } + else if (kart.getDriftPower() > 0.2) + { + Vector facing = UtilAlg.getTrajectory(kart.getYaw(), 0).multiply(kart.getVelocity().length() + (kart.getDriftPower() / 2)); + kart.setVelocity(facing); + kart.setDriftLast(); + } + else + { + kart.setDriftPower(0); + } } void applyTopSpeed(Kart kart) @@ -62,12 +98,15 @@ public class KartController } Vector velocity = kart.getVelocity(); + double length = velocity.length(); + double max = kart.isBoosting() ? 1.5 : 0.7; - if (velocity.length() > 1) + if (length > max) { kart.setVelocity(velocity.clone() .setY(0) - .normalize()); + .normalize() + .multiply(max)); } } } diff --git a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/MineKart.java b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/MineKart.java index 3f27807e0..d605900e7 100644 --- a/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/MineKart.java +++ b/Plugins/Mineplex.Game.Nano/src/mineplex/game/nano/game/games/minekart/MineKart.java @@ -6,17 +6,20 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import net.minecraft.server.v1_8_R3.PacketPlayInSteerVehicle; + import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.inventory.ItemStack; import mineplex.core.common.util.UtilAlg; import mineplex.core.common.util.UtilEnt; import mineplex.core.common.util.UtilMath; +import mineplex.core.common.util.UtilTime; +import mineplex.core.packethandler.IPacketHandler; +import mineplex.core.packethandler.PacketInfo; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.game.nano.NanoManager; @@ -25,8 +28,9 @@ import mineplex.game.nano.game.SoloGame; import mineplex.game.nano.game.event.PlayerGameApplyEvent; import mineplex.game.nano.game.event.PlayerGameRespawnEvent; import mineplex.game.nano.game.event.PlayerStateChangeEvent; +import mineplex.game.nano.game.games.minekart.KartController.DriftDirection; -public class MineKart extends SoloGame +public class MineKart extends SoloGame implements IPacketHandler { private int _spawnIndex; @@ -52,6 +56,8 @@ public class MineKart extends SoloGame _teamComponent.setAdjustSpawnYaw(false); _damageComponent.setDamage(false); + + manager.getPacketHandler().addPacketHandler(this, PacketPlayInSteerVehicle.class); } @Override @@ -97,6 +103,41 @@ public class MineKart extends SoloGame public void disable() { _checkpoints.clear(); + _manager.getPacketHandler().removePacketHandler(this); + } + + @Override + public void handle(PacketInfo packetInfo) + { + Kart kart = _karts.get(packetInfo.getPlayer()); + + if (kart != null) + { + PacketPlayInSteerVehicle packet = (PacketPlayInSteerVehicle) packetInfo.getPacket(); + + // Dismounting + if (packet.d()) + { + packetInfo.setCancelled(true); + return; + } + + // a() - Right-Left, b() - Backwards-Forwards + kart.setInput(packet.b(), packet.a()); + + // Holding Space + if (packet.c()) + { + if (packet.a() != 0 && kart.getDriftDirection() == null) + { + kart.setDriftDirection(packet.a() > 0 ? DriftDirection.LEFT : DriftDirection.RIGHT); + } + } + else + { + kart.setDriftDirection(null); + } + } } @EventHandler @@ -118,11 +159,9 @@ public class MineKart extends SoloGame { _worldComponent.setCreatureAllowOverride(true); - Kart kart = new Kart(this, player); + Kart kart = new Kart(player); _karts.put(player, kart); - player.getInventory().addItem(new ItemStack(Material.STONE_SWORD)); - _worldComponent.setCreatureAllowOverride(false); }, 1); } @@ -173,7 +212,7 @@ public class MineKart extends SoloGame if (kart.isCrashed()) { - if (UtilEnt.isGrounded(kart.getVehicle())) + if (UtilTime.elapsed(kart.getCrashedAt(), 1000) && UtilEnt.isGrounded(kart.getVehicle())) { kart.setCrashed(false); player.sendMessage("setCrashed(false)"); @@ -190,9 +229,11 @@ public class MineKart extends SoloGame _controller.collideBlock(kart); // Accelerate - if (canControl && player.isBlocking()) + if (canControl) { + _controller.turn(kart); _controller.accelerate(kart); + _controller.drift(kart); _controller.applyTopSpeed(kart); } @@ -201,10 +242,11 @@ public class MineKart extends SoloGame location.add(kart.getVelocity()); location.setDirection(kart.getVelocity()); UtilEnt.setPosition(vehicle, location); - UtilEnt.CreatureLook(vehicle, location.getYaw()); + UtilEnt.CreatureLook(vehicle, kart.getYaw()); location.getWorld().playSound(location, Sound.PIG_IDLE, (float) (.1 + velocityLength / 2), (float) (.5 + velocityLength)); - player.setExp((float) velocityLength); + player.setLevel((int) (velocityLength * 100)); + player.setExp(kart.getDriftPower()); }); }