Completely rework the kart mechanics

This commit is contained in:
Sam 2018-09-12 03:49:00 +01:00 committed by Alexander Meech
parent 5b9f5e1678
commit e716c9b2ea
4 changed files with 203 additions and 30 deletions

View File

@ -36,6 +36,7 @@ import mineplex.core.game.nano.NanoFavourite;
import mineplex.core.incognito.IncognitoManager; import mineplex.core.incognito.IncognitoManager;
import mineplex.core.incognito.events.IncognitoStatusChangeEvent; import mineplex.core.incognito.events.IncognitoStatusChangeEvent;
import mineplex.core.newnpc.NewNPCManager; import mineplex.core.newnpc.NewNPCManager;
import mineplex.core.packethandler.PacketHandler;
import mineplex.core.serverConfig.ServerConfiguration; import mineplex.core.serverConfig.ServerConfiguration;
import mineplex.core.stats.StatsManager; import mineplex.core.stats.StatsManager;
import mineplex.core.status.ServerStatusManager; import mineplex.core.status.ServerStatusManager;
@ -113,6 +114,9 @@ public class NanoManager extends MiniPlugin implements IRelation
// AntiCheat // AntiCheat
private final AntiHack _antiHack; private final AntiHack _antiHack;
// Packet
private final PacketHandler _packetHandler;
// NPC // NPC
private final NewNPCManager _npcManager; private final NewNPCManager _npcManager;
@ -189,6 +193,8 @@ public class NanoManager extends MiniPlugin implements IRelation
_antiHack = require(AntiHack.class); _antiHack = require(AntiHack.class);
_packetHandler = require(PacketHandler.class);
_npcManager = require(NewNPCManager.class); _npcManager = require(NewNPCManager.class);
_damageManager = require(DamageManager.class); _damageManager = require(DamageManager.class);
@ -412,6 +418,11 @@ public class NanoManager extends MiniPlugin implements IRelation
return _antiHack; return _antiHack;
} }
public PacketHandler getPacketHandler()
{
return _packetHandler;
}
public NewNPCManager getNpcManager() public NewNPCManager getNpcManager()
{ {
return _npcManager; return _npcManager;

View File

@ -1,11 +1,14 @@
package mineplex.game.nano.game.games.minekart; package mineplex.game.nano.game.games.minekart;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep; import org.bukkit.entity.Sheep;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import mineplex.core.common.util.UtilEnt; import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilTime;
import mineplex.game.nano.game.games.minekart.KartController.DriftDirection;
public class Kart public class Kart
{ {
@ -16,23 +19,32 @@ public class Kart
private int _lap, _lapCheckpoint, _lapKeyCheckpoint; private int _lap, _lapCheckpoint, _lapKeyCheckpoint;
private Vector _velocity; 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; _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.vegetate(_vehicle, true);
UtilEnt.ghost(_vehicle, true, false); UtilEnt.ghost(_vehicle, true, false);
UtilEnt.setFakeHead(_vehicle, true); UtilEnt.setFakeHead(_vehicle, true);
_vehicle.setPassenger(driver);
_lap = 1; _lap = 1;
_velocity = new Vector(); _velocity = new Vector();
_yaw = location.getYaw();
_vehicle.setPassenger(driver);
} }
public void remove() public void remove()
@ -90,13 +102,82 @@ public class Kart
return _velocity; 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) public void setCrashed(boolean crashed)
{ {
_crashed = crashed; _crashed = crashed;
_driftDirection = null;
_driftLast = 0;
if (crashed)
{
_crashedAt = System.currentTimeMillis();
}
} }
public boolean isCrashed() public boolean isCrashed()
{ {
return _crashed; return _crashed;
} }
public long getCrashedAt()
{
return _crashedAt;
}
} }

View File

@ -4,12 +4,24 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import mineplex.core.common.util.UtilAlg;
public class KartController public class KartController
{ {
public enum DriftDirection
{
LEFT, RIGHT
}
void applyAirDrag(Kart kart) 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) if (velocity.length() < 0.05)
{ {
@ -21,9 +33,11 @@ public class KartController
{ {
Location location = kart.getVehicle().getLocation().add(0, 0.5, 0); 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) if (location.getBlock().getType() != Material.AIR)
{ {
kart.getVelocity().multiply(-0.5); kart.getVelocity().multiply(-1);
kart.getVelocity().setY(0.5); kart.getVelocity().setY(0.5);
kart.setCrashed(true); kart.setCrashed(true);
} }
@ -37,21 +51,43 @@ public class KartController
} }
Vector velocity = kart.getVelocity(); 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)
{ {
velocity = player; double velocityLength = kart.getVelocity().length();
if (velocity.lengthSquared() == 0) if (velocityLength < 0.1)
{ {
return; return;
} }
kart.setVelocity(velocity); float velocityInverse = (float) (2 - velocityLength);
float rotation = 3 * kart.getSidewaysInput() * velocityInverse;
kart.setYaw(kart.getYaw() - rotation);
} }
velocity.add(player); 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) void applyTopSpeed(Kart kart)
@ -62,12 +98,15 @@ public class KartController
} }
Vector velocity = kart.getVelocity(); 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() kart.setVelocity(velocity.clone()
.setY(0) .setY(0)
.normalize()); .normalize()
.multiply(max));
} }
} }
} }

View File

@ -6,17 +6,20 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.minecraft.server.v1_8_R3.PacketPlayInSteerVehicle;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.UtilAlg; import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilEnt; import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilMath; 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.UpdateType;
import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.nano.NanoManager; 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.PlayerGameApplyEvent;
import mineplex.game.nano.game.event.PlayerGameRespawnEvent; import mineplex.game.nano.game.event.PlayerGameRespawnEvent;
import mineplex.game.nano.game.event.PlayerStateChangeEvent; 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; private int _spawnIndex;
@ -52,6 +56,8 @@ public class MineKart extends SoloGame
_teamComponent.setAdjustSpawnYaw(false); _teamComponent.setAdjustSpawnYaw(false);
_damageComponent.setDamage(false); _damageComponent.setDamage(false);
manager.getPacketHandler().addPacketHandler(this, PacketPlayInSteerVehicle.class);
} }
@Override @Override
@ -97,6 +103,41 @@ public class MineKart extends SoloGame
public void disable() public void disable()
{ {
_checkpoints.clear(); _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 @EventHandler
@ -118,11 +159,9 @@ public class MineKart extends SoloGame
{ {
_worldComponent.setCreatureAllowOverride(true); _worldComponent.setCreatureAllowOverride(true);
Kart kart = new Kart(this, player); Kart kart = new Kart(player);
_karts.put(player, kart); _karts.put(player, kart);
player.getInventory().addItem(new ItemStack(Material.STONE_SWORD));
_worldComponent.setCreatureAllowOverride(false); _worldComponent.setCreatureAllowOverride(false);
}, 1); }, 1);
} }
@ -173,7 +212,7 @@ public class MineKart extends SoloGame
if (kart.isCrashed()) if (kart.isCrashed())
{ {
if (UtilEnt.isGrounded(kart.getVehicle())) if (UtilTime.elapsed(kart.getCrashedAt(), 1000) && UtilEnt.isGrounded(kart.getVehicle()))
{ {
kart.setCrashed(false); kart.setCrashed(false);
player.sendMessage("setCrashed(false)"); player.sendMessage("setCrashed(false)");
@ -190,9 +229,11 @@ public class MineKart extends SoloGame
_controller.collideBlock(kart); _controller.collideBlock(kart);
// Accelerate // Accelerate
if (canControl && player.isBlocking()) if (canControl)
{ {
_controller.turn(kart);
_controller.accelerate(kart); _controller.accelerate(kart);
_controller.drift(kart);
_controller.applyTopSpeed(kart); _controller.applyTopSpeed(kart);
} }
@ -201,10 +242,11 @@ public class MineKart extends SoloGame
location.add(kart.getVelocity()); location.add(kart.getVelocity());
location.setDirection(kart.getVelocity()); location.setDirection(kart.getVelocity());
UtilEnt.setPosition(vehicle, location); 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)); 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());
}); });
} }