Jump to content

Recommended Posts

Posted

I have wrote a car entity class witch works perfectly fine except from some bugs. For example the collision sometimes just disappears and when i drove into a wall and then dismount the player dismounts at the wall and not at the entity itself. Why does it does that?

CODE:

 

 

package de.ItsAMysterious.mods.reallifemod.core.entities.cars;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.inventory.IInventory;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.util.AxisAlignedBB;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

 

import org.lwjgl.input.Keyboard;

import org.lwjgl.util.vector.Vector3f;

 

import de.ItsAMysterious.mods.reallifemod.RealLifeMod;

import de.ItsAMysterious.mods.reallifemod.api.entity.EntitySeat;

import de.ItsAMysterious.mods.reallifemod.api.util.LittleFunctions;

import de.ItsAMysterious.mods.reallifemod.client.ClientProxy;

import de.ItsAMysterious.mods.reallifemod.core.gui.GuiVehicleModification;

import de.ItsAMysterious.mods.reallifemod.core.streets.entitys.EntityVehicle;

 

public class EntityJeep extends EntityVehicle implements IInventory {

 

//Constants

private static final int inventorysize = 6;

 

//Arrays

private ItemStack jeepItems[] = new ItemStack[inventorysize];

public EntitySeat[] seats = new EntitySeat[5];

 

public EntityJeep(World world) {

super(world);

this.setSize(3, 1.0F);

this.setSpeedMinMax(-0.25, 0.75);

//setSeatNumber(5);

this.color=new Vector3f(2,0,0);

}

 

public EntityJeep(World world, double x, double y, double z,

float rotationYaw, double width, double length) {

super(world);

this.setSize(3, 1.0F);

this.setSpeedMinMax(-0.25, 0.75);

//this.setSeatNumber(5);

setLocationAndAngles(x, y, z, rotationYaw, 0);

}

 

private void setSeatPositions(Vector3f[]positions) {

for(int i=0;i<seats.length-1;i++){

if(i<positions.length-1){

seats.offsetX=positions.x;

seats.offsetY=positions.y;

seats.offsetZ=positions.z;

 

seats.posX=posX+positions.x;

seats.posY=posY+positions.y;

seats.posZ=posZ+positions.z;

}

}

}

 

public void setSeatNumber(int number){

seats = new EntitySeat[number];

buildupSeats();

}

 

private void buildupSeats() {

if(!worldObj.isRemote){

for(int i = 0; i<seats.length-1;i++){

seats = new EntitySeat(worldObj);

}

this.setSeatPositions(new Vector3f[]{new Vector3f(0.5f,0.5f,1),new Vector3f(1.5f,0.5f,1),new Vector3f(2.5f,0.5f,1),new Vector3f(0.5f,0.5f,2.5f),new Vector3f(2.5f,0.5f,2.5f)});

 

for(EntitySeat s:seats){

if(s!=null)

worldObj.spawnEntityInWorld(s);

}

}

}

 

public AxisAlignedBB getCollisionBox(Entity entity) {

return AxisAlignedBB.getBoundingBox(-1, 0, 0, 1, 1, 1);

}

 

public AxisAlignedBB getBoundingBox() {

return AxisAlignedBB.getBoundingBox(-1, 0, 0, 1, 1, 7);

}

 

public boolean canBeCollidedWith(){

return !this.isDead;

}

 

public void onUpdate() {

super.onUpdate();

if (this.getTimeSinceHit() > 0) {

this.setTimeSinceHit(this.getTimeSinceHit() - 1);

}

 

if (this.getDamageTaken() > 0.0F) {

this.setDamageTaken(this.getDamageTaken() - 1.0F);

}

 

this.prevPosX = this.posX;

this.prevPosY = this.posY;

this.prevPosZ = this.posZ;

this.boundingBox.calculateXOffset(this.getBoundingBox(), this.motionX);

this.boundingBox.calculateYOffset(this.getBoundingBox(), this.motionY);

this.boundingBox.calculateZOffset(this.getBoundingBox(), this.motionZ);

 

if (this.riddenByEntity != null

&& this.riddenByEntity instanceof EntityLivingBase) {

this.checkInput();

this.move();

 

if (Keyboard.isKeyDown(Keyboard.KEY_M)) {

Minecraft.getMinecraft().displayGuiScreen(new GuiVehicleModification(this));

}

 

}

 

if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)) {

if (this.worldObj.getClosestPlayer(posX, posY, posZ, 5.0F) != null) {

EntityPlayer entity = (EntityPlayer) this.worldObj

.getClosestPlayer(posX, posY, posZ, 5.0F);

this.interactFirst(entity);

//this.riddenByEntity = (EntityPlayer) entity;

this.playSound("reallifemod:door_close", 1.0F, 1.0F);

}

}

 

for(int i = 0; i<seats.length-1;i++){

if(seats!=null){

double r = Math.sqrt(LittleFunctions.square(seats.offsetX)+LittleFunctions.square(seats.offsetY)+LittleFunctions.square(seats.offsetZ));

double sx,sy,sz;

double phi = seats.offsetZ/r;//sin phi!!

double lambda = rotationPitch;

sx = r*Math.cos(phi)*Math.cos(lambda);

sy = r*Math.sin(lambda)*Math.cos(phi);

sz = r*Math.sin(phi);

this.seats.setPosition(posX-sx, posY-sy, posZ-sz);

}

}

}

 

@Override

public void setVelocity(double p_70016_1_, double p_70016_3_,

double p_70016_5_) {

this.velocityX = (float) (this.motionX = p_70016_1_);

this.velocityY = (float) (this.motionY = p_70016_3_);

this.velocityZ = (float) (this.motionZ = p_70016_5_);

}

 

public void move() {

double R = 0;

double k = 0;

if (this.deltasteer != 0) {

R = 5 / deltasteer;

k = 1 / R;

}

this.wheelrotation += this.currentspeed * 20;

double angleVelocity = this.currentspeed * k;

if (currentspeed < 0.5) {

currentspeed += acceleration;

}

this.rotationYaw += angleVelocity;

this.motionX = Math.sin(rotationYaw * Math.PI / 180) * currentspeed;

this.motionZ = Math.cos(Math.toRadians(rotationYaw)) * currentspeed;

this.moveEntity(motionX, motionY, motionZ);

}

 

public void setPositionAndRotation2(double p_70056_1_, double p_70056_3_,

double p_70056_5_, float p_70056_7_, float p_70056_8_,

int p_70056_9_) {

this.vehicleX = p_70056_1_;

this.vehicleY = p_70056_3_;

this.vehicleZ = p_70056_5_;

this.vehicleYaw = (double) p_70056_7_;

this.vehiclePitch = (double) p_70056_8_;

this.motionX = this.velocityX;

this.motionY = this.velocityY;

this.motionZ = this.velocityZ;

}

 

public boolean hasDriver() {

if(seats[0]!=null){

return seats[0].riddenByEntity!=null;

}else

return false;

}

 

@Override

public int getSizeInventory() {

return inventorysize;

}

 

@Override

public ItemStack getStackInSlot(int slotID) {

if(slotID<=5){

return jeepItems[slotID];

}else

return null;

}

 

public ItemStack decrStackSize(int slot, int amount) {

ItemStack stack = getStackInSlot(slot);

if (stack != null) {

if (stack.stackSize > amount) {

stack = stack.splitStack(amount);

if (stack.stackSize == 0) {

setInventorySlotContents(slot, null);

}

} else {

setInventorySlotContents(slot, null);

}

 

this.onInventoryChanged();

}

return stack;

}

 

public ItemStack getStackInSlotOnClosing(int slot) {

ItemStack stack = getStackInSlot(slot);

if (stack != null)

setInventorySlotContents(slot, null);

return stack;

}

 

public void onInventoryChanged() {

for (int i = 0; i < jeepItems.length; ++i) {

if (this.getStackInSlot(i) != null

&& this.getStackInSlot(i).stackSize == 0)

this.setInventorySlotContents(i, null);

}

}

 

public void setInventorySlotContents(int slot, ItemStack stack) {

if(slot>=0&&slot<inventorysize)

this.jeepItems[slot] = stack;

if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {

stack.stackSize = this.getInventoryStackLimit();

}

markDirty();

}

 

@Override

public String getInventoryName() {

return "Jeep Trunk";

}

 

@Override

public boolean hasCustomInventoryName() {

return true;

}

 

@Override

public int getInventoryStackLimit() {

return 64;

}

 

@Override

public void markDirty() {

}

 

@Override

public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {

return true;

}

 

@Override

public void openInventory() {

}

 

@Override

public void closeInventory() {

}

 

 

 

@Override

public boolean isItemValidForSlot(int slotID, ItemStack stack) {

return slotID < jeepItems.length;

}

 

@Override

public boolean interactFirst(EntityPlayer player) {

super.interactFirst(player);

if(!worldObj.isRemote){

seats=new EntitySeat[5];

EntitySeat s = new EntitySeat(worldObj, 0.5f,0,1);

if(this.seats[0]==null){

seats[0] = s;

worldObj.spawnEntityInWorld(seats[0]);

}

seats[0].interactFirst(player);

}

/*if(seats.get(0)!=null){

seats.get(0).mountEntity(this);

player.mountEntity(seats.get(0));

}*/

return true;

//return true;

}

 

private EntitySeat getNextEmptySeat(EntityPlayer player) {

for (int i = 0; i < seats.length-1; i++) {

EntitySeat seat = seats;

if (seat.riddenByEntity == null) {

if(seat.worldObj.isRemote)

player.mountEntity(seat);

return seat;

} else {

if (i == seats.length-1) {

player.addChatMessage(new ChatComponentText(

"This car is full!!"));

}

}

}

return null;

}

 

public void applyEntityCollision(Entity p_70108_1_)

    {

super.applyEntityCollision(p_70108_1_);

        if (p_70108_1_.riddenByEntity != this && p_70108_1_.ridingEntity != this)

        {

            double d0 = p_70108_1_.posX - this.posX;

            double d1 = p_70108_1_.posZ - this.posZ;

            double d2 = MathHelper.abs_max(d0, d1);

 

            if (d2 >= 0.009999999776482582D)

            {

                d2 = (double)MathHelper.sqrt_double(d2);

                d0 /= d2;

                d1 /= d2;

                double d3 = 1.0D / d2;

 

                if (d3 > 1.0D)

                {

                    d3 = 1.0D;

                }

 

                d0 *= d3;

                d1 *= d3;

                d0 *= 0.05000000074505806D;

                d1 *= 0.05000000074505806D;

                d0 *= (double)(1.0F - this.entityCollisionReduction);

                d1 *= (double)(1.0F - this.entityCollisionReduction);

                this.addVelocity(0, 0.0D, 0);

                p_70108_1_.addVelocity(0, 0.0D, 0);

            }

        }

    }

 

public void checkInput() {

super.checkInput();

if (Keyboard.isKeyDown(Keyboard.KEY_W)) {

acceleration = 0.025;

} else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {

acceleration = -0.025;

} else {

acceleration = 0;

}

 

if (Keyboard.isKeyDown(Keyboard.KEY_D)) {

if (deltasteer > -45) {

this.deltasteer--;

}

 

} else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {

if (this.deltasteer < 45) {

this.deltasteer++;

}

} else {

if (this.deltasteer < 0) {

this.deltasteer++;

} else if (deltasteer > 0) {

this.deltasteer--;

}

}

 

if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {

brake();

}

 

if (Keyboard.isKeyDown(Keyboard.KEY_H)) {

worldObj.playSound(posX, posY, posZ, "reallifemod:horn", 10.5f,

1.0f, true);

}

 

if(ClientProxy.Key_CarInv.isPressed()){

EntityPlayer player =(EntityPlayer) riddenByEntity;

if(riddenByEntity!=null){

player.openGui(RealLifeMod.instance, this.getEntityId(), worldObj,(int) posX,(int) posY,(int) posZ);

}

}

 

}

 

protected void brake() {

if (currentspeed < 0) {

currentspeed += 0.2;

} else

currentspeed = 0;

if (currentspeed > 0.1) {

currentspeed -= 0.2;

} else

currentspeed = 0;

currentspeed = 0;

}

 

public void updateSeatPositions() {

//seatMotionX=sin()

 

}

 

public void setDamageTaken(float p_70266_1_) {

this.dataWatcher.updateObject(19, Float.valueOf(p_70266_1_));

}

 

public float getDamageTaken() {

return this.dataWatcher.getWatchableObjectFloat(19);

}

 

/**

* Sets the time to count down from since the last time entity was hit.

*/

public void setTimeSinceHit(int p_70265_1_) {

this.dataWatcher.updateObject(17, Integer.valueOf(p_70265_1_));

}

 

/**

* Gets the time since the last hit.

*/

public int getTimeSinceHit() {

return this.dataWatcher.getWatchableObjectInt(17);

}

 

/**

* Sets the forward direction of the entity.

*/

public void setForwardDirection(int p_70269_1_) {

this.dataWatcher.updateObject(18, Integer.valueOf(p_70269_1_));

}

 

/**

* Gets the forward direction of the entity.

*/

public int getForwardDirection() {

return this.dataWatcher.getWatchableObjectInt(18);

}

 

@Override

protected void entityInit() {

this.dataWatcher.addObject(17, new Integer(0));

this.dataWatcher.addObject(18, new Integer(1));

this.dataWatcher.addObject(19, new Float(0.0F));

}

 

@Override

protected void readEntityFromNBT(NBTTagCompound comp) {

super.readEntityFromNBT(comp);

super.color = new Vector3f(comp.getFloat("colorRed"),comp.getFloat("colorGreen"),comp.getFloat("colorGreen"));

}

 

@Override

protected void writeEntityToNBT(NBTTagCompound compound) {

compound.setFloat("colorRed", this.color.x);

compound.setFloat("colorGreen", this.color.y);

compound.setFloat("colorBlue", this.color.z);

super.writeEntityToNBT(compound);

 

}

 

@Override

public void updateRiderPosition() {

double R=0;

double k = 0;

if (this.deltasteer != 0) {

R = 5 / deltasteer;

k = 1 / R;

}

double z = Math.cos((this.rotationYaw + 16) * Math.PI / 180.0) * 2.2;// *5.0990195135927848300282241090228D;

double y = posY + Math.tan(rotationPitch * Math.PI / 180) * 3;

double x = Math.sin((this.rotationYaw + 16) * Math.PI / 180) * 2.2;

this.riddenByEntity.setPosition(this.posX + x, this.posY + 2, this.posZ

+ z);

this.riddenByEntity.rotationYaw -= this.currentspeed * k;

EntityPlayer p = (EntityPlayer) this.riddenByEntity;

p.rotationYawHead -= this.currentspeed * k;

 

}

 

}

 

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.