mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-01-25 10:32:49 +00:00
electric train class
This commit is contained in:
parent
e6bb2233fa
commit
c58b456adb
@ -0,0 +1,68 @@
|
||||
package com.hbm.entity.train;
|
||||
|
||||
import com.hbm.items.ModItems;
|
||||
|
||||
import api.hbm.energy.IBatteryItem;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public abstract class EntityRailCarElectric extends EntityRailCarRidable {
|
||||
|
||||
public EntityRailCarElectric(World world) {
|
||||
super(world);
|
||||
}
|
||||
|
||||
public abstract int getMaxPower();
|
||||
public abstract int getPowerConsumption();
|
||||
|
||||
public boolean hasChargeSlot() { return false; }
|
||||
public int getChargeSlot() { return 0; }
|
||||
|
||||
@Override protected void entityInit() {
|
||||
this.dataWatcher.addObject(3, new Integer(0));
|
||||
}
|
||||
|
||||
@Override public boolean canAccelerate() {
|
||||
return this.getPower() >= this.getPowerConsumption();
|
||||
}
|
||||
|
||||
@Override public void consumeFuel() {
|
||||
this.setPower(this.getPower() - this.getPowerConsumption());
|
||||
}
|
||||
|
||||
public void setPower(int power) {
|
||||
this.dataWatcher.updateObject(3, power);
|
||||
}
|
||||
|
||||
public int getPower() {
|
||||
return this.dataWatcher.getWatchableObjectInt(3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate() {
|
||||
super.onUpdate();
|
||||
|
||||
if(!worldObj.isRemote) {
|
||||
|
||||
if(this.hasChargeSlot()) {
|
||||
ItemStack stack = this.getStackInSlot(this.getChargeSlot());
|
||||
|
||||
if(stack != null && stack.getItem() instanceof IBatteryItem) {
|
||||
IBatteryItem battery = (IBatteryItem) stack.getItem();
|
||||
int powerNeeded = this.getMaxPower() - this.getPower();
|
||||
long powerProvided = Math.min(battery.getDischargeRate(), battery.getCharge(stack));
|
||||
int powerTransfered = (int) Math.min(powerNeeded, powerProvided);
|
||||
|
||||
if(powerTransfered > 0) {
|
||||
battery.dischargeBattery(stack, powerTransfered);
|
||||
this.setPower(this.getPower() + powerTransfered);
|
||||
}
|
||||
} else if(stack != null) {
|
||||
if(stack.getItem() == ModItems.battery_creative || stack.getItem() == ModItems.fusion_core_infinite) {
|
||||
this.setPower(this.getMaxPower());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ import net.minecraft.world.World;
|
||||
|
||||
public abstract class EntityRailCarRidable extends EntityRailCarCargo {
|
||||
|
||||
public double engineSpeed;
|
||||
public SeatDummyEntity[] passengerSeats;
|
||||
|
||||
public EntityRailCarRidable(World world) {
|
||||
@ -20,6 +21,57 @@ public abstract class EntityRailCarRidable extends EntityRailCarCargo {
|
||||
this.passengerSeats = new SeatDummyEntity[this.getPassengerSeats().length];
|
||||
}
|
||||
|
||||
/** Returns the linear speed added per tick when using powered movement */
|
||||
public abstract double getPoweredAcceleration();
|
||||
/** A mulitplier used on the speed either is there is no player in the train or if the parking brake is active */
|
||||
public abstract double getPassivBrake();
|
||||
/** The parking brake can be toggled, assuming a player is present, otherwise it is implicitly ON */
|
||||
public abstract boolean shouldUseEngineBrake(EntityPlayer player);
|
||||
/** The max speed the engine can provide in both directions */
|
||||
public abstract double getMaxPoweredSpeed();
|
||||
/** Whether the engine is turned on */
|
||||
public abstract boolean canAccelerate();
|
||||
/** Called every tick if acceleration is successful */
|
||||
public void consumeFuel() { }
|
||||
|
||||
/** An additive to the engine's speed yielding the total speed, caused by uneven surfaces */
|
||||
public double getGravitySpeed() {
|
||||
return 0D;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getCurrentSpeed() { // in its current form, only call once per tick
|
||||
|
||||
if(this.riddenByEntity instanceof EntityPlayer) {
|
||||
|
||||
EntityPlayer player = (EntityPlayer) this.riddenByEntity;
|
||||
|
||||
if(this.canAccelerate()) {
|
||||
if(player.moveForward > 0) {
|
||||
engineSpeed += this.getPoweredAcceleration();
|
||||
} else if(player.moveForward < 0) {
|
||||
engineSpeed -= this.getPoweredAcceleration();
|
||||
} else {
|
||||
if(this.shouldUseEngineBrake(player)) {
|
||||
engineSpeed *= this.getPassivBrake();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(this.shouldUseEngineBrake(player)) {
|
||||
engineSpeed *= this.getPassivBrake();
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
engineSpeed *= this.getPassivBrake();
|
||||
}
|
||||
|
||||
double maxSpeed = this.getMaxPoweredSpeed();
|
||||
engineSpeed = MathHelper.clamp_double(engineSpeed, -maxSpeed, maxSpeed);
|
||||
|
||||
return engineSpeed + this.getGravitySpeed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean interactFirst(EntityPlayer player) {
|
||||
|
||||
|
||||
@ -19,12 +19,11 @@ import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.DamageSource;
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.Vec3;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class TrainCargoTram extends EntityRailCarRidable implements IGUIProvider {
|
||||
public class TrainCargoTram extends EntityRailCarElectric implements IGUIProvider {
|
||||
|
||||
/*
|
||||
*
|
||||
@ -43,35 +42,11 @@ public class TrainCargoTram extends EntityRailCarRidable implements IGUIProvider
|
||||
super(world);
|
||||
this.setSize(5F, 2F);
|
||||
}
|
||||
|
||||
public double speed = 0;
|
||||
public static final double maxSpeed = 0.5;
|
||||
public static final double acceleration = 0.01;
|
||||
public static final double deceleration = 0.95;
|
||||
|
||||
@Override
|
||||
public double getCurrentSpeed() { // in its current form, only call once per tick
|
||||
|
||||
if(this.riddenByEntity instanceof EntityPlayer) {
|
||||
|
||||
EntityPlayer player = (EntityPlayer) this.riddenByEntity;
|
||||
|
||||
if(player.moveForward > 0) {
|
||||
speed += acceleration;
|
||||
} else if(player.moveForward < 0) {
|
||||
speed -= acceleration;
|
||||
} else {
|
||||
speed *= deceleration;
|
||||
}
|
||||
|
||||
} else {
|
||||
speed *= deceleration;
|
||||
}
|
||||
|
||||
speed = MathHelper.clamp_double(speed, -maxSpeed, maxSpeed);
|
||||
|
||||
return speed;
|
||||
}
|
||||
@Override public double getPoweredAcceleration() { return 0.01; }
|
||||
@Override public double getPassivBrake() { return 0.95; }
|
||||
@Override public boolean shouldUseEngineBrake(EntityPlayer player) { return Math.abs(this.engineSpeed) < 0.1; }
|
||||
@Override public double getMaxPoweredSpeed() { return 0.5; }
|
||||
|
||||
@Override public TrackGauge getGauge() { return TrackGauge.STANDARD; }
|
||||
@Override public double getLengthSpan() { return 1.5; }
|
||||
@ -80,6 +55,11 @@ public class TrainCargoTram extends EntityRailCarRidable implements IGUIProvider
|
||||
@Override public int getSizeInventory() { return 29; }
|
||||
@Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.getEntityName() : "container.trainTram"; }
|
||||
|
||||
@Override public int getMaxPower() { return this.getPowerConsumption() * 100; }
|
||||
@Override public int getPowerConsumption() { return 50; }
|
||||
@Override public boolean hasChargeSlot() { return true; }
|
||||
@Override public int getChargeSlot() { return 28; }
|
||||
|
||||
@Override
|
||||
public DummyConfig[] getDummies() {
|
||||
return new DummyConfig[] {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user