bit of work on key binding for vehicles

If you notice i didn't create my own but am piggy backing every single
existing binding and sending it to the server. This way i can just pick
existing ones like forward and use it.
This commit is contained in:
Robert 2013-11-09 18:42:23 -05:00
parent 4e569bc1b5
commit 06006f32c0
6 changed files with 207 additions and 8 deletions

View file

@ -0,0 +1,65 @@
package dark.core.common;
import java.util.EnumSet;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;
import dark.core.network.PacketManagerKeyEvent;
/** This class handles keys already binded to the game so to avoid creating new key bindings
*
* @author DarkGuardsman */
public class PlayerKeyHandler implements ITickHandler
{
protected boolean[] keyDown;
protected boolean[] repeatings;
@Override
public final void tickStart(EnumSet<TickType> type, Object... tickData)
{
keyTick(type, false);
}
@Override
public final void tickEnd(EnumSet<TickType> type, Object... tickData)
{
keyTick(type, true);
}
private void keyTick(EnumSet<TickType> type, boolean tickEnd)
{
for (int i = 0; i < Minecraft.getMinecraft().gameSettings.keyBindings.length; i++)
{
KeyBinding keyBinding = Minecraft.getMinecraft().gameSettings.keyBindings[i];
int keyCode = keyBinding.keyCode;
boolean state = (keyCode < 0 ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode));
if (state)
{
PacketManagerKeyEvent.sendPacket(keyCode);
}
}
}
@Override
public EnumSet<TickType> ticks()
{
return EnumSet.of(TickType.CLIENT);
}
@Override
public String getLabel()
{
return "[CoreMachine]KeyBindingCatcher";
}
}

View file

@ -12,5 +12,5 @@ public interface IControlReceiver
* @param player - client player
* @param character - character code
* @param keycode - keyboard code */
public boolean keyTyped(EntityPlayer player, char character, int keycode);
public boolean keyTyped(EntityPlayer player, int keycode);
}

View file

@ -42,6 +42,7 @@ public class PacketHandler implements IPacketHandler, IPacketReceiver
{
registerManager(new PacketManagerTile());
registerManager(new PacketManagerEffects());
registerManager(PacketManagerKeyEvent.instance());
}
public static void registerManager(IPacketManager manager)

View file

@ -0,0 +1,87 @@
package dark.core.network;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import universalelectricity.prefab.network.IPacketReceiver;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import dark.core.common.DarkMain;
import dark.core.helpers.PacketDataWatcher;
import dark.core.interfaces.IControlReceiver;
public class PacketManagerKeyEvent implements IPacketManager
{
static int packetID = 0;
private static PacketManagerKeyEvent instance;
private List<IControlReceiver> receivers = new ArrayList<IControlReceiver>();
public static PacketManagerKeyEvent instance()
{
if (instance == null)
{
instance = new PacketManagerKeyEvent();
}
return instance;
}
public void register(IControlReceiver rec)
{
if (!this.receivers.contains(rec))
{
this.receivers.add(rec);
}
}
public void remove(IControlReceiver rec)
{
this.receivers.remove(rec);
}
@Override
public int getID()
{
return packetID;
}
@Override
public void setID(int maxID)
{
packetID = maxID;
}
@Override
public void handlePacket(INetworkManager network, Packet250CustomPayload packet, Player player, ByteArrayDataInput data)
{
try
{
int key = data.readInt();
for (IControlReceiver receiver : receivers)
{
receiver.keyTyped((EntityPlayer) player, key);
}
}
catch (Exception e)
{
System.out.println("[CoreMachine] Error reading packet at tile packet manager");
e.printStackTrace();
}
}
public static void sendPacket(int key)
{
PacketDispatcher.sendPacketToServer(PacketHandler.instance().getPacketWithID(DarkMain.getInstance().CHANNEL, PacketManagerKeyEvent.packetID, key));
}
}

View file

@ -14,6 +14,7 @@ import universalelectricity.core.UniversalElectricity;
import com.builtbroken.common.Triple;
import cpw.mods.fml.client.registry.KeyBindingRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
@ -22,6 +23,7 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;
import dark.core.common.ExternalModHandler;
import dark.core.common.PlayerKeyHandler;
import dark.core.prefab.fluids.FluidHelper;
import dark.core.prefab.tilenetwork.NetworkUpdateHandler;
import dark.core.registration.ModObjectRegistry;
@ -104,6 +106,7 @@ public abstract class ModPrefab
MinecraftForge.EVENT_BUS.register(new FluidHelper());
MinecraftForge.EVENT_BUS.register(SaveManager.instance());
TickRegistry.registerTickHandler(NetworkUpdateHandler.instance(), Side.SERVER);
TickRegistry.registerTickHandler(new PlayerKeyHandler(), Side.CLIENT);
UniversalElectricity.initiate();
Compatibility.initiate();
preInit = true;

View file

@ -1,10 +1,10 @@
package dark.core.prefab.vehicles;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import cpw.mods.fml.client.FMLClientHandler;
import dark.core.interfaces.IControlReceiver;
@ -24,6 +24,7 @@ public class EntityDrivable extends EntityAdvanced implements IControlReceiver
this.preventEntitySpawning = true;
this.ignoreFrustumCheck = true;
this.isImmuneToFire = true;
this.yOffset = 1.0f;
}
public EntityDrivable(World world, double xx, double yy, double zz)
@ -33,9 +34,37 @@ public class EntityDrivable extends EntityAdvanced implements IControlReceiver
}
@Override
public boolean keyTyped(EntityPlayer player, char character, int keycode)
public boolean keyTyped(EntityPlayer player, int keycode)
{
//TODO lay out WASD keys as well as R & F for auto move
if (this.ridingEntity != null && this.ridingEntity.equals(player))
{
//TODO add auto forward and backwards keys like those in WoT
if (keycode == Minecraft.getMinecraft().gameSettings.keyBindForward.keyCode)
{
this.accelerate(true);
}
if (keycode == Minecraft.getMinecraft().gameSettings.keyBindBack.keyCode)
{
this.accelerate(false);
}
if (keycode == Minecraft.getMinecraft().gameSettings.keyBindLeft.keyCode)
{
this.rotationYaw += 5;
}
if (keycode == Minecraft.getMinecraft().gameSettings.keyBindRight.keyCode)
{
this.rotationYaw -= 5;
}
//Power brakes
if (keycode == Minecraft.getMinecraft().gameSettings.keyBindJump.keyCode)
{
this.speed -= 2.f;
if (speed <= 0)
{
speed = 0;
}
}
}
return false;
}
@ -47,7 +76,7 @@ public class EntityDrivable extends EntityAdvanced implements IControlReceiver
//Changes the player's position based on the boats rotation
double deltaX = Math.cos(this.rotationYaw * Math.PI / 180.0D + 114.8) * -0.5D;
double deltaZ = Math.sin(this.rotationYaw * Math.PI / 180.0D + 114.8) * -0.5D;
this.riddenByEntity.setPosition(this.posX + deltaX, this.posY - 2 + this.riddenByEntity.getYOffset(), this.posZ + deltaZ);
this.riddenByEntity.setPosition(this.posX + deltaX, this.posY + this.riddenByEntity.getYOffset(), this.posZ + deltaZ);
}
}
@ -59,7 +88,6 @@ public class EntityDrivable extends EntityAdvanced implements IControlReceiver
if (this.worldObj.isRemote)
{
this.worldObj.spawnParticle("mobSpell", this.posX, this.posY, this.posZ, 0, 0, 0);
}
if (this.isCollidedHorizontally)
@ -67,7 +95,7 @@ public class EntityDrivable extends EntityAdvanced implements IControlReceiver
this.motionY = 0.1D;
}
this.applyFrication();
this.applyFriction();
this.motionX = -(this.speed * Math.cos((this.rotationYaw - 90F) * Math.PI / 180.0D));
this.motionZ = -(this.speed * Math.sin((this.rotationYaw - 90F) * Math.PI / 180.0D));
@ -83,9 +111,24 @@ public class EntityDrivable extends EntityAdvanced implements IControlReceiver
this.prevPosZ = this.posZ;
}
/** Increases the speed by a determined amount per tick the player holds the forward key down
*
* @param forward */
public void accelerate(boolean forward)
{
if (forward)
{
this.speed += 1;
}
else
{
this.speed -= 1;
}
}
/** By default this slows the vehicle down with a constant. However this can be used to apply
* advanced friction based on materials */
public void applyFrication()
public void applyFriction()
{
if (this.inWater)
{