aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/goodgenerator/blocks/tileEntity/render/TileAntimatter.java
blob: 80b36ab01b9849235324cc2eb460d9b5acd4a98c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package goodgenerator.blocks.tileEntity.render;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;

import org.joml.AxisAngle4f;
import org.joml.Matrix4f;

import com.gtnewhorizon.structurelib.alignment.enumerable.Rotation;

public class TileAntimatter extends TileEntity {

    public boolean shouldRender = true;

    // Antimatter Core settings
    public static final float spikeR = 0.153f, spikeG = 0.435f, spikeB = 1f;
    public static final float coreR = 0.435f, coreG = 0.718f, coreB = 1f;
    public static final float maximalRadius = 7; // Includes core radius + spike length
    // Due to being partially managed by a global timer, rotationSpeedMultiplier shouldn't change
    // Otherwise it'll cause a snapping effect
    public final float rotationSpeedMultiplier = 1;

    public float coreScale = 1f;
    public float coreScaleSnapshot = 1f;
    public final float coreScaleTransitionTime = 2.5f;
    public float timeSnapshot;
    public float spikeFactor = .01f;

    // Protomatter Settings
    public static float protoSpiralMaxRadius = .5f;
    public static final float protoR = 0.2f, protoG = 0.2f, protoB = 0.2f;
    public float protomatterScale = .2f;
    public boolean protomatterRender = false;
    public float rotX = 0, rotY = 0, rotZ = 0;
    public float rotationAngle = 0;

    @Override
    public void writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);
        compound.setFloat("coreScale", coreScale);
        compound.setFloat("coreScaleSnapshot", coreScaleSnapshot);
        compound.setFloat("currentTime", timeSnapshot);
        compound.setFloat("spikeFactor", spikeFactor);
        compound.setBoolean("protomatterRender", protomatterRender);

        compound.setFloat("rotX", rotX);
        compound.setFloat("rotY", rotY);
        compound.setFloat("rotZ", rotZ);
        compound.setFloat("rotationAngle", rotationAngle);
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        coreScale = compound.getFloat("coreScale");
        coreScaleSnapshot = compound.getFloat("coreScaleSnapshot");
        timeSnapshot = compound.getFloat("currentTime");
        spikeFactor = compound.getFloat("spikeFactor");
        protomatterRender = compound.getBoolean("protomatterRender");

        rotX = compound.getFloat("rotX");
        rotY = compound.getFloat("rotY");
        rotZ = compound.getFloat("rotZ");
        rotationAngle = compound.getFloat("rotationAngle");
    }

    @Override
    public Packet getDescriptionPacket() {
        NBTTagCompound nbttagcompound = new NBTTagCompound();
        writeToNBT(nbttagcompound);
        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, nbttagcompound);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
        readFromNBT(pkt.func_148857_g());
        worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
    }

    @Override
    public AxisAlignedBB getRenderBoundingBox() {
        return AxisAlignedBB.getBoundingBox(
            xCoord - maximalRadius - 1,
            yCoord - maximalRadius - 1,
            zCoord - maximalRadius - 1,
            xCoord + maximalRadius + 1,
            yCoord + maximalRadius + 1,
            zCoord + maximalRadius + 1);
    }

    @Override
    public double getMaxRenderDistanceSquared() {
        return 65536;
    }

    public void setProtomatterRender(boolean flag) {
        if (protomatterRender == flag) return;
        protomatterRender = flag;
        worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    }

    public float getSpiralRadius(float normalization) {
        return Math.min(coreScale / 2.0f * normalization, protoSpiralMaxRadius);
    }

    public void setCoreSize(float diameter) {
        coreScaleSnapshot = coreScale;
        coreScale = diameter;
        timeSnapshot = this.getWorldObj()
            .getWorldInfo()
            .getWorldTotalTime();
        worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    }

    public void setRotationFields(ForgeDirection direction, Rotation rotation) {
        Matrix4f rotationMatrix = new Matrix4f().identity();

        float localAngle = switch (rotation) {
            case NORMAL -> 0;
            case CLOCKWISE -> 90;
            case COUNTER_CLOCKWISE -> -90;
            case UPSIDE_DOWN -> 180;
        };
        localAngle = (float) Math.toRadians(localAngle);
        rotationMatrix.rotate(localAngle, direction.offsetX, direction.offsetY, direction.offsetZ);

        float x = 0, y = 0;
        float angle = switch (direction) {
            case DOWN -> {
                x = 1;
                yield 90;
            }
            case UP -> {
                x = 1;
                yield -90;
            }
            case EAST, SOUTH -> {
                y = 1;
                yield 90;
            }
            case WEST, NORTH -> {
                y = 1;
                yield -90;
            }
            case UNKNOWN -> 0.0F;
        };
        angle = (float) Math.toRadians(angle);
        rotationMatrix.rotate(angle, x, y, 0);

        AxisAngle4f rotationVector = new AxisAngle4f();
        rotationMatrix.getRotation(rotationVector);

        rotationAngle = rotationVector.angle / (float) Math.PI * 180;
        rotX = rotationVector.x;
        rotY = rotationVector.y;
        rotZ = rotationVector.z;
        worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    }

}