IndustrialWires/src/main/java/malte0811/industrialWires/converter/MechPartFlywheel.java

173 lines
5.3 KiB
Java
Raw Normal View History

2017-10-04 17:05:04 +02:00
/*
* This file is part of Industrial Wires.
2018-02-28 21:06:33 +01:00
* Copyright (C) 2016-2018 malte0811
2017-10-04 17:05:04 +02:00
* Industrial Wires is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Industrial Wires is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
*/
package malte0811.industrialWires.converter;
import blusunrize.immersiveengineering.common.util.Utils;
import blusunrize.immersiveengineering.common.util.chickenbones.Matrix4;
2017-10-04 17:05:04 +02:00
import malte0811.industrialWires.IndustrialWires;
import malte0811.industrialWires.blocks.converter.MechanicalMBBlockType;
import malte0811.industrialWires.entities.EntityBrokenPart;
2017-10-04 17:05:04 +02:00
import malte0811.industrialWires.util.LocalSidedWorld;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.BakedQuadRetextured;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
2017-10-04 17:05:04 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.oredict.OreDictionary;
import java.util.List;
import java.util.stream.Collectors;
2017-10-04 17:05:04 +02:00
public class MechPartFlywheel extends MechMBPart {
private static final double RADIUS = 1.25;
private static final double THICKNESS = 1;
private static final double VOLUME = Math.PI*RADIUS*RADIUS*THICKNESS;
2017-10-04 17:05:04 +02:00
private Material material;
//A flywheel simply adds mass (lots of mass!), it doesn't actively change speeds/energy
@Override
public void createMEnergy(MechEnergy e) {}
2017-10-04 17:05:04 +02:00
@Override
public double requestMEnergy(MechEnergy e) {
2017-10-04 17:05:04 +02:00
return 0;
}
@Override
public void insertMEnergy(double added) {}
2017-10-04 17:05:04 +02:00
@Override
public double getInertia() {
return .5*material.density*VOLUME*RADIUS*RADIUS;
2017-10-04 17:05:04 +02:00
}
@Override
2018-03-23 16:27:32 +01:00
public double getMaxSpeed() {
return Math.sqrt(material.tensileStrength /material.density)/RADIUS;
2017-10-04 17:05:04 +02:00
}
@Override
public void writeToNBT(NBTTagCompound out) {
out.setInteger("material", material.ordinal());
}
@Override
public void readFromNBT(NBTTagCompound in) {
material = Material.values()[in.getInteger("material")];
2017-10-04 17:05:04 +02:00
}
@Override
public ResourceLocation getRotatingBaseModel() {
return new ResourceLocation(IndustrialWires.MODID, "block/mech_mb/flywheel.obj");
}
@Override
public List<BakedQuad> getRotatingQuads() {
List<BakedQuad> orig = super.getRotatingQuads();
TextureAtlasSprite newTex = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(material.blockTexture.toString());
return orig.stream().map((quad)->{
if (quad.getSprite().getIconName().contains("steel")) {
return new BakedQuadRetextured(quad, newTex);
} else {
return quad;
}
}).collect(Collectors.toList());
}
2017-10-04 17:05:04 +02:00
@Override
public boolean canForm(LocalSidedWorld w) {
BlockPos.PooledMutableBlockPos pos = BlockPos.PooledMutableBlockPos.retain(-1, 1, 0);
try {
material = null;
for (Material m:Material.values()) {
if (m.matchesBlock(w, pos, "block")) {
material = m;
break;
}
}
if (material==null) {
return false;
}
for (int x = -1;x<=1;x++) {
for (int y = -1;y<=1;y++) {
pos.setPos(x, y, 0);
if ((x!=0||y!=0)&&!material.matchesBlock(w, pos, "block")) {
return false;
}
}
}
pos.setPos(0, 0, 0);
return isValidDefaultCenter(w.getBlockState(pos));
2017-10-04 17:05:04 +02:00
} finally {
pos.release();
}
}
@Override
public void disassemble(boolean failed, MechEnergy energy) {
setDefaultShaft(BlockPos.ORIGIN);
IBlockState state = Blocks.AIR.getDefaultState();
if (!failed) {
for (ItemStack block: OreDictionary.getOres("block"+material.oreName())) {
if (block.getItem() instanceof ItemBlock) {
ItemBlock ib = (ItemBlock) block.getItem();
state = ib.getBlock().getStateFromMeta(block.getMetadata());
}
}
}
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if (x != 0 || y != 0) {
world.setBlockState(new BlockPos(x, y, 0), state);
}
}
}
if (failed) {
Matrix4 mat = new Matrix4();
mat.rotate(Utils.RAND.nextDouble(), 0, 0, 1);
Vec3d baseVec = new Vec3d(0, 1.5, 0);
for (int i = 0;i<8;i++) {
mat.rotate(Math.PI/4, 0, 0, 1);
Vec3d pos = mat.apply(baseVec);
EntityBrokenPart e = new EntityBrokenPart(world.getWorld(), material.blockTexture);
e.setPosition(pos.x, pos.y, -.5);
2018-03-23 16:27:32 +01:00
double speed = (energy.getSpeed()/ getMaxSpeed())/1.5;
e.motionX = pos.y*speed;
e.motionY = -pos.x*speed;
e.motionZ = (Utils.RAND.nextDouble()-.5)*speed/10;
world.spawnEntity(e);
e.breakBlocks(speed*speed*1.5*1.5);
}
}
}
2017-10-04 17:05:04 +02:00
@Override
public short getFormPattern() {
return 0b111_111_111;
}
@Override
public MechanicalMBBlockType getType() {
return MechanicalMBBlockType.FLYWHEEL;
}
}