Skip to content

Commit

Permalink
Add gate lights
Browse files Browse the repository at this point in the history
  • Loading branch information
CSX8600 committed Apr 20, 2023
1 parent 1827bae commit 0b60947
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class CrossingGateGateGui extends GuiScreen {
private GuiTextField upperRotation;
private GuiTextField lowerRotation;
private GuiTextField delay;
private GuiTextField lightStartOffset;

private CrossingGateGateTileEntity te;
public CrossingGateGateGui(CrossingGateGateTileEntity te)
Expand All @@ -40,6 +41,9 @@ public void initGui() {

delay = new GuiTextField(COMP_IDS.DELAY, fontRenderer, horizontalCenter - 50, verticalCenter + 20, 100, 20);
delay.setText(String.valueOf(te.getDelay()));

lightStartOffset = new GuiTextField(COMP_IDS.LIGHT_START_OFFSET, fontRenderer, horizontalCenter - 50, verticalCenter + 50, 100, 20);
lightStartOffset.setText(String.valueOf(te.getLightStartOffset()));
}

@Override
Expand All @@ -59,6 +63,9 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {

fontRenderer.drawString("Activation Delay:", delay.x - fontRenderer.getStringWidth("Activation Delay:") - 10, delay.y + (delay.height / 4), 0xFFFFFF);
delay.drawTextBox();

fontRenderer.drawString("Light Start Offset:", lightStartOffset.x - fontRenderer.getStringWidth("Light Start Offset:") - 10, lightStartOffset.y + (lightStartOffset.height / 4), 0xFFFFFF);
lightStartOffset.drawTextBox();
}

@Override
Expand All @@ -77,6 +84,7 @@ protected void keyTyped(char typedChar, int keyCode) throws IOException {
upperRotation.textboxKeyTyped(typedChar, keyCode);
lowerRotation.textboxKeyTyped(typedChar, keyCode);
delay.textboxKeyTyped(typedChar, keyCode);
lightStartOffset.textboxKeyTyped(typedChar, keyCode);
}

if (keyCode == Keyboard.KEY_MINUS)
Expand All @@ -92,6 +100,7 @@ protected void keyTyped(char typedChar, int keyCode) throws IOException {
keyTypedDecimal(upperRotation, typedChar, keyCode);
keyTypedDecimal(lowerRotation, typedChar, keyCode);
keyTypedDecimal(delay, typedChar, keyCode);
keyTypedDecimal(lightStartOffset, typedChar, keyCode);
}
}

Expand All @@ -110,6 +119,7 @@ protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOEx
upperRotation.mouseClicked(mouseX, mouseY, mouseButton);
lowerRotation.mouseClicked(mouseX, mouseY, mouseButton);
delay.mouseClicked(mouseX, mouseY, mouseButton);
lightStartOffset.mouseClicked(mouseX, mouseY, mouseButton);
}

@Override
Expand All @@ -118,6 +128,7 @@ public void onGuiClosed() {
te.setUpperRotationLimit(Float.parseFloat(upperRotation.getText()));
te.setLowerRotationLimit(Float.parseFloat(lowerRotation.getText()));
te.setDelay(Float.parseFloat(delay.getText()));
te.setLightStartOffset(Float.parseFloat(lightStartOffset.getText()));
te.performClientToServerSync();
}

Expand All @@ -127,5 +138,6 @@ public static class COMP_IDS
public static final int UPPER_ROTATION = 2;
public static final int LOWER_ROTATION = 3;
public static final int DELAY = 4;
public static final int LIGHT_START_OFFSET = 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

import com.clussmanproductions.trafficcontrol.ModBlocks;
import com.clussmanproductions.trafficcontrol.ModItems;
import com.clussmanproductions.trafficcontrol.signs.SignRepository;
import com.clussmanproductions.trafficcontrol.ModTrafficControl;

import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
Expand Down Expand Up @@ -51,4 +57,22 @@ public void init(FMLInitializationEvent e) {
entityClassRendererKey = new KeyBinding("key.entityclassrenderer.toggle", Keyboard.KEY_RBRACKET, "key.trafficcontrol.category");
ClientRegistry.registerKeyBinding(entityClassRendererKey);
}

@SubscribeEvent
public static void bakeModels(ModelBakeEvent e)
{
bakeModel(e, new ModelResourceLocation(ModTrafficControl.MODID + ":crossing_gate_light", "normal"));
bakeModel(e, new ModelResourceLocation(ModTrafficControl.MODID + ":crossing_gate_light", "on=true"));
}

private static void bakeModel(ModelBakeEvent e, ModelResourceLocation location)
{
try {
IModel model = ModelLoaderRegistry.getModel(location);
IBakedModel bakedModel = model.bake(model.getDefaultState(), DefaultVertexFormats.BLOCK, ModelLoader.defaultTextureGetter());
e.getModelRegistry().putObject(location, bakedModel);
} catch (Exception e1) {
ModTrafficControl.logger.error("An error occurred while baking a custom model", e1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.clussmanproductions.trafficcontrol.ModSounds;
import com.clussmanproductions.trafficcontrol.ModTrafficControl;
import com.clussmanproductions.trafficcontrol.blocks.BlockCrossingGateGate;
import com.clussmanproductions.trafficcontrol.blocks.BlockLampBase;
import com.clussmanproductions.trafficcontrol.blocks.BlockLampBase.EnumState;
import com.clussmanproductions.trafficcontrol.util.ILoopableSoundTileEntity;
import com.clussmanproductions.trafficcontrol.util.LoopableTileEntitySound;
import com.clussmanproductions.trafficcontrol.util.NBTUtils;
Expand All @@ -21,21 +23,25 @@ public class CrossingGateGateTileEntity extends SyncableTileEntity implements IT
private float gateRotation = -60;
private float gateDelay = 0;
private EnumStatuses status = EnumStatuses.Open;
private BlockLampBase.EnumState flashState = EnumState.Off;
private boolean soundPlaying = false;
private float crossingGateLength = 4;
private float upperRotationLimit = 60;
private float lowerRotationLimit = 0;
private float delay = 4;
private float lightStartOffset = 1;

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setFloat("gateRotation", gateRotation);
compound.setFloat("gateDelay", gateDelay);
compound.setInteger("status", getCodeFromEnum(status));
compound.setInteger("flashState", flashState.getID());
compound.setFloat("length", crossingGateLength);
compound.setFloat("upperRotation", upperRotationLimit);
compound.setFloat("lowerRotation", lowerRotationLimit);
compound.setFloat("delay", delay);
compound.setFloat("lightStartOffset", lightStartOffset);
return super.writeToNBT(compound);
}

Expand All @@ -45,10 +51,12 @@ public void readFromNBT(NBTTagCompound compound) {
gateRotation = compound.getFloat("gateRotation");
gateDelay = compound.getFloat("gateDelay");
status = getStatusFromCode(compound.getInteger("status"));
flashState = BlockLampBase.EnumState.getStateByID(compound.getInteger("flashState"));
crossingGateLength = NBTUtils.getFloatOrDefault(compound, "length", 4);
upperRotationLimit = NBTUtils.getFloatOrDefault(compound, "upperRotation", 60);
lowerRotationLimit = NBTUtils.getFloatOrDefault(compound, "lowerRotation", 0);
delay = NBTUtils.getFloatOrDefault(compound, "delay", 4);
lightStartOffset = NBTUtils.getFloatOrDefault(compound, "lightStartOffset", 1);
}

public float getFacingRotation()
Expand Down Expand Up @@ -200,10 +208,12 @@ public NBTTagCompound getUpdateTag() {
nbt.setFloat("gateRotation", gateRotation);
nbt.setFloat("gateDelay", gateDelay);
nbt.setInteger("status", getCodeFromEnum(status));
nbt.setInteger("flashState", flashState.getID());
nbt.setFloat("length", crossingGateLength);
nbt.setFloat("upperRotation", upperRotationLimit);
nbt.setFloat("lowerRotation", lowerRotationLimit);
nbt.setFloat("delay", delay);
nbt.setFloat("lightStartOffset", lightStartOffset);

return nbt;
}
Expand All @@ -213,10 +223,12 @@ public void handleUpdateTag(NBTTagCompound tag) {
gateRotation = tag.getFloat("gateRotation");
gateDelay = tag.getFloat("gateDelay");
status = getStatusFromCode(tag.getInteger("status"));
flashState = BlockLampBase.EnumState.getStateByID(tag.getInteger("flashState"));
crossingGateLength = tag.getFloat("length");
upperRotationLimit = tag.getFloat("upperRotation");
lowerRotationLimit = tag.getFloat("lowerRotation");
delay = tag.getFloat("delay");
lightStartOffset = tag.getFloat("lightStartOffset");
}

@SideOnly(Side.CLIENT)
Expand Down Expand Up @@ -327,6 +339,38 @@ public void setDelay(float delay) {
}
}

public BlockLampBase.EnumState getFlashState()
{
return flashState;
}

public void setFlashState(BlockLampBase.EnumState state)
{
boolean shouldMarkDirty = state != flashState;
flashState = state;

if (shouldMarkDirty)
{
sendUpdates(true);
}
}

public float getLightStartOffset()
{
return lightStartOffset;
}

public void setLightStartOffset(float lightStartOffset)
{
boolean shouldMarkDirty = lightStartOffset != this.lightStartOffset;
this.lightStartOffset = lightStartOffset;

if (shouldMarkDirty)
{
sendUpdates(true);
}
}

@Override
public void onChunkUnload() {
soundPlaying = false;
Expand All @@ -344,6 +388,7 @@ public NBTTagCompound getClientToServerUpdateTag() {
tag.setFloat("upperRotation", upperRotationLimit);
tag.setFloat("lowerRotation", lowerRotationLimit);
tag.setFloat("delay", delay);
tag.setFloat("lightStartOffset", lightStartOffset);
return tag;
}

Expand All @@ -353,6 +398,7 @@ public void handleClientToServerUpdateTag(NBTTagCompound compound) {
setUpperRotationLimit(compound.getFloat("upperRotation"));
setLowerRotationLimit(compound.getFloat("lowerRotation"));
setDelay(compound.getFloat("delay"));
setLightStartOffset(compound.getFloat("lightStartOffset"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,7 @@ private void notifyLamps()
for(BlockPos lampLocation : crossingLampLocations)
{
try
{
// IBlockState lampState = world.getBlockState(lampLocation);
// world.setBlockState(lampLocation, lampState.withProperty(BlockLampBase.STATE, state));

{
CrossingLampsTileEntity te = (CrossingLampsTileEntity)world.getTileEntity(lampLocation);
te.setState(state);

Expand All @@ -351,6 +348,21 @@ private void notifyLamps()
}
}

for(BlockPos gateLocation : crossingGateLocations)
{
try
{
CrossingGateGateTileEntity te = (CrossingGateGateTileEntity)world.getTileEntity(gateLocation);
te.setFlashState(state);

world.notifyBlockUpdate(gateLocation, world.getBlockState(gateLocation), world.getBlockState(gateLocation), 3);
}
catch(Exception ex)
{
// Do nothing - there's a separate process to unpair gates
}
}

for(BlockPos positionToRemove : positionsToRemove)
{
crossingLampLocations.remove(positionToRemove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import org.lwjgl.opengl.GL11;

import com.clussmanproductions.trafficcontrol.ModTrafficControl;
import com.clussmanproductions.trafficcontrol.blocks.BlockLampBase;
import com.clussmanproductions.trafficcontrol.blocks.BlockLampBase.EnumState;
import com.clussmanproductions.trafficcontrol.tileentity.CrossingGateGateTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.render.TESRHelper.Box;
import com.clussmanproductions.trafficcontrol.tileentity.render.TESRHelper.TextureInfo;
import com.clussmanproductions.trafficcontrol.tileentity.render.TESRHelper.TextureInfoCollection;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.ResourceLocation;
import scala.Tuple3;

public class RendererCrossingGateGate extends TileEntitySpecialRenderer<CrossingGateGateTileEntity> {
@Override
Expand All @@ -28,7 +34,7 @@ public void render(CrossingGateGateTileEntity te, double x, double y, double z,

GlStateManager.scale(.0625, .0625, .0625);

GlStateManager.translate(3, 0, 0);
GlStateManager.translate(3, 2, 0);

GlStateManager.rotate(te.getGateRotation(), 0, 0, 1);

Expand All @@ -39,9 +45,14 @@ public void render(CrossingGateGateTileEntity te, double x, double y, double z,
tes.draw();
builder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
renderGateVerticies(builder, te.getCrossingGateLength());

tes.draw();

if (te.getCrossingGateLength() - te.getLightStartOffset() >= 2)
{
renderGateLights(builder, te.getCrossingGateLength(), te.getFlashState(), te.getLightStartOffset());
}


GlStateManager.popMatrix();
}

Expand All @@ -56,7 +67,7 @@ private void renderWeightVerticies(BufferBuilder builder)
new TextureInfo(generic, 0, 0, 8, 1),
new TextureInfo(generic, 0, 0, 8, 1),
new TextureInfo(generic, 0, 0, 8, 1));
Box box = new Box(-5.5, -7.5, 4, 1, 2, -8, collection);
Box box = new Box(-7.5, -9.5, 4, 1, 2, -8, collection);
box.render(builder, (tex) -> bindTexture(tex));

// Rotator Arm Support
Expand All @@ -67,10 +78,10 @@ private void renderWeightVerticies(BufferBuilder builder)
new TextureInfo(generic, 4, 4, 5, 5),
new TextureInfo(generic, 4, 4, 5, 9),
new TextureInfo(generic, 4, 4, 5, 9));
box = new Box(-4.5, -7, 4, 5, 1, -1, collection);
box = new Box(-6.5, -9.5, 4, 7, 2, -1, collection);
box.render(builder, (tex) -> bindTexture(tex));

box = new Box(-4.5, -7, -3, 5, 1, -1, collection);
box = new Box(-6.5, -9.5, -3, 7, 2, -1, collection);
box.render(builder, (tex) -> bindTexture(tex));

// Rotator Connector
Expand All @@ -82,10 +93,10 @@ private void renderWeightVerticies(BufferBuilder builder)
new TextureInfo(generic, 5, 6, 6, 9),
new TextureInfo(generic, 2, 3, 3, 6));

box = new Box(-2.5, -6, 4, 3, 7, -1, collection);
box = new Box(-2.5, -7.5, 4, 3, 8.5, -1, collection);
box.render(builder, (tex) -> bindTexture(tex));

box = new Box(-2.5, -6, -3, 3, 7, -1, collection);
box = new Box(-2.5, -7.5, -3, 3, 8.5, -1, collection);
box.render(builder, (tex) -> bindTexture(tex));

// Weight connector
Expand Down Expand Up @@ -113,7 +124,41 @@ private void renderGateVerticies(BufferBuilder builder, float crossingGateLength
new TextureInfo(gate, 0, 1, 16, 1.7),
new TextureInfo(gate, 0, 2, 3, 4),
new TextureInfo(gate, 0, 2, 3, 4));
Box gateBox = new Box(-(crossingGateLength * 16) - 11, -7.5, 0.5, (crossingGateLength * 16) + 5.5, 2, -1, collection);
Box gateBox = new Box( -(crossingGateLength * 16) - 13, -9.5, 0.5,
(crossingGateLength * 16) + 5.5, 2, -1, collection);
gateBox.render(builder, (tex) -> bindTexture(tex));
}

private final ModelResourceLocation lightOffLocation = new ModelResourceLocation(ModTrafficControl.MODID + ":crossing_gate_light", "normal");
private final ModelResourceLocation lightOnLocation = new ModelResourceLocation(ModTrafficControl.MODID + ":crossing_gate_light", "on=true");
private void renderGateLights(BufferBuilder builder, float crossingGateLength, BlockLampBase.EnumState flashState, float lightStartOffset)
{
IBakedModel modelOff = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelManager().getModel(lightOffLocation);
IBakedModel modelOn = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelManager().getModel(lightOnLocation);
bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);

// First
GlStateManager.disableLighting();
GlStateManager.translate(-20.5 - (lightStartOffset * 16), -7.5, -8);
GlStateManager.scale(1 / .0625, 1 / .0625, 1 / .0625);
IBakedModel model = flashState == EnumState.Flash2 ? modelOn : modelOff;
Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer().renderModelBrightnessColor(model, 1, 1, 1, 1);
GlStateManager.scale(0.0625, 0.0625, 0.0625);

// Middle
GlStateManager.translate(-(crossingGateLength - lightStartOffset) * 16 / 2 + 1, 0, 0);
GlStateManager.scale(1 / .0625, 1 / .0625, 1 / .0625);
model = flashState == EnumState.Flash1 ? modelOn : modelOff;
Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer().renderModelBrightnessColor(model, 1, 1, 1, 1);
GlStateManager.scale(0.0625, 0.0625, 0.0625);

// End
GlStateManager.translate(-(crossingGateLength - lightStartOffset) * 16 / 2 + 1, 0, 0);
GlStateManager.scale(1 / .0625, 1 / .0625, 1 / .0625);
model = flashState == EnumState.Off ? modelOff : modelOn;
Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer().renderModelBrightnessColor(model, 1, 1, 1, 1);
GlStateManager.scale(0.0625, 0.0625, 0.0625);

GlStateManager.enableLighting();
}
}
Loading

0 comments on commit 0b60947

Please sign in to comment.