aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/tectech/thing/block/TileEntityForgeOfGods.java
blob: 49ab89de2b15a95930a682a73a87434b757f5cb4 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package tectech.thing.block;

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 com.gtnewhorizon.structurelib.alignment.enumerable.Rotation;

public class TileEntityForgeOfGods extends TileEntity {

    private float radius = 32;
    private float rotationSpeed = 10;
    private int ringCount = 1;
    private float colorR = .7f, colorG = .8f, colorB = 1f, gamma = 3f;
    private float rotAngle = 0, rotAxisX = 1, rotAxisY = 0, rotAxisZ = 0;
    private int rainbowR = 255, rainbowG = 0, rainbowB = 0;
    private int rainbowState = 0;
    private boolean rainbowMode = false;
    private int rainbowCycleSpeed = 1;

    private static final String NBT_TAG = "FOG:";
    private static final String ROTATION_SPEED_NBT_TAG = NBT_TAG + "ROTATION";
    private static final String SIZE_NBT_TAG = NBT_TAG + "RADIUS";
    private static final String RINGS_NBT_TAG = NBT_TAG + "RINGS";
    private static final String COLOR_RED_NBT_TAG = NBT_TAG + "COLOR_RED";
    private static final String COLOR_GREEN_NBT_TAG = NBT_TAG + "COLOR_GREEN";
    private static final String COLOR_BLUE_NBT_TAG = NBT_TAG + "COLOR_BLUE";
    private static final String COLOR_GAMMA_NBT_TAG = NBT_TAG + "COLOR_GAMMA";
    private static final String ROT_ANGLE_NBT_TAG = NBT_TAG + "ROT_ANGLE";
    private static final String ROT_AXIS_X_NBT_TAG = NBT_TAG + "ROT_AXIS_X";
    private static final String ROT_AXIS_Y_NBT_TAG = NBT_TAG + "ROT_AXIS_Y";
    private static final String ROT_AXIS_Z_NBT_TAG = NBT_TAG + "ROT_AXIS_Z";
    private static final String RAINBOW_MODE_NBT_TAG = NBT_TAG + "RAINBOW_MODE";
    private static final String RAINBOW_MODE_CYCLE_SPEED_NBT_TAG = NBT_TAG + "RAINBOW_MODE_CYCLE_SPEED";

    public static final float BACK_PLATE_DISTANCE = -121.5f, BACK_PLATE_RADIUS = 13f;

    @Override
    public AxisAlignedBB getRenderBoundingBox() {
        return INFINITE_EXTENT_AABB;
    }

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

    public void setStarRadius(float size) {
        this.radius = size;
    }

    public float getStarRadius() {
        return radius;
    }

    public float getRotationSpeed() {
        return rotationSpeed;
    }

    public void setRotationSpeed(float speed) {
        this.rotationSpeed = speed;
    }

    public float getColorR() {
        return rainbowMode ? rainbowR / 255f : colorR;
    }

    public float getColorG() {
        return rainbowMode ? rainbowG / 255f : colorG;
    }

    public float getColorB() {
        return rainbowMode ? rainbowB / 255f : colorB;
    }

    public float getGamma() {
        return gamma;
    }

    public void setColor(float r, float g, float b) {
        setColor(r, g, b, 1);
    }

    public void setColor(float r, float g, float b, float gamma) {
        colorR = r;
        colorG = g;
        colorB = b;
        this.gamma = gamma;
    }

    public void setRainbowMode(boolean state, int cycleSpeed) {
        this.rainbowMode = state;
        this.rainbowCycleSpeed = cycleSpeed;
    }

    public boolean getRainbowMode() {
        return rainbowMode;
    }

    public int getRingCount() {
        return ringCount;
    }

    public void setRingCount(int count) {
        if (ringCount < 1) return;
        ringCount = count;
    }

    public float getRotAngle() {
        return rotAngle;
    }

    public float getRotAxisX() {
        return rotAxisX;
    }

    public float getRotAxisY() {
        return rotAxisY;
    }

    public float getRotAxisZ() {
        return rotAxisZ;
    }

    public void setRenderRotation(Rotation rotation, ForgeDirection direction) {
        switch (direction) {
            case SOUTH -> rotAngle = 90;
            case NORTH -> rotAngle = 90;
            case WEST -> rotAngle = 0;
            case EAST -> rotAngle = 180;
            case UP -> rotAngle = -90;
            case DOWN -> rotAngle = -90;
        }
        rotAxisX = 0;
        rotAxisY = direction.offsetZ + direction.offsetX;
        rotAxisZ = direction.offsetY;

        updateToClient();
    }

    public float getLensDistance(int lensID) {
        return switch (lensID) {
            case 0 -> -61.5f;
            case 1 -> -54.5f;
            case 2 -> -44.5f;
            default -> throw new IllegalStateException("Unexpected value: " + lensID);
        };
    }

    public float getLenRadius(int lensID) {
        return switch (lensID) {
            case 0 -> 1.1f;
            case 1 -> 3.5f;
            case 2 -> 5f;
            default -> throw new IllegalStateException("Unexpected value: " + lensID);
        };
    }

    public float getStartAngle() {
        float x = -getLensDistance(getRingCount() - 1);
        float y = getLenRadius(getRingCount() - 1);
        float alpha = (float) Math.atan2(y, x);
        float beta = (float) Math.asin(radius / Math.sqrt(x * x + y * y));
        return alpha + ((float) Math.PI / 2 - beta);
    }

    public static float interpolate(float x0, float x1, float y0, float y1, float x) {
        return y0 + ((x - x0) * (y1 - y0)) / (x1 - x0);
    }

    public void incrementRainbowColors() {
        if (rainbowState == 0) {
            rainbowG += rainbowCycleSpeed;
            if (rainbowG >= 255) {
                rainbowG = 255;
                rainbowState = 1;
            }
        }
        if (rainbowState == 1) {
            rainbowR -= rainbowCycleSpeed;
            if (rainbowR <= 0) {
                rainbowR = 0;
                rainbowState = 2;
            }
        }
        if (rainbowState == 2) {
            rainbowB += rainbowCycleSpeed;
            if (rainbowB >= 255) {
                rainbowB = 255;
                rainbowState = 3;
            }
        }
        if (rainbowState == 3) {
            rainbowG -= rainbowCycleSpeed;
            if (rainbowG <= 0) {
                rainbowG = 0;
                rainbowState = 4;
            }
        }
        if (rainbowState == 4) {
            rainbowR += rainbowCycleSpeed;
            if (rainbowR >= 255) {
                rainbowR = 255;
                rainbowState = 5;
            }
        }
        if (rainbowState == 5) {
            rainbowB -= rainbowCycleSpeed;
            if (rainbowB <= 0) {
                rainbowB = 0;
                rainbowState = 0;
            }
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);
        compound.setFloat(ROTATION_SPEED_NBT_TAG, rotationSpeed);
        compound.setFloat(SIZE_NBT_TAG, radius);
        compound.setInteger(RINGS_NBT_TAG, ringCount);
        compound.setFloat(COLOR_RED_NBT_TAG, colorR);
        compound.setFloat(COLOR_GREEN_NBT_TAG, colorG);
        compound.setFloat(COLOR_BLUE_NBT_TAG, colorB);
        compound.setFloat(COLOR_GAMMA_NBT_TAG, gamma);
        compound.setFloat(ROT_ANGLE_NBT_TAG, rotAngle);
        compound.setFloat(ROT_AXIS_X_NBT_TAG, rotAxisX);
        compound.setFloat(ROT_AXIS_Y_NBT_TAG, rotAxisY);
        compound.setFloat(ROT_AXIS_Z_NBT_TAG, rotAxisZ);
        compound.setBoolean(RAINBOW_MODE_NBT_TAG, rainbowMode);
        compound.setInteger(RAINBOW_MODE_CYCLE_SPEED_NBT_TAG, rainbowCycleSpeed);
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        rotationSpeed = compound.getFloat(ROTATION_SPEED_NBT_TAG);
        radius = compound.getFloat(SIZE_NBT_TAG);

        ringCount = compound.getInteger(RINGS_NBT_TAG);
        if (ringCount < 1) ringCount = 1;

        colorR = compound.getFloat(COLOR_RED_NBT_TAG);
        colorG = compound.getFloat(COLOR_GREEN_NBT_TAG);
        colorB = compound.getFloat(COLOR_BLUE_NBT_TAG);
        gamma = compound.getFloat(COLOR_GAMMA_NBT_TAG);
        rotAngle = compound.getFloat(ROT_ANGLE_NBT_TAG);
        rotAxisX = compound.getFloat(ROT_AXIS_X_NBT_TAG);
        rotAxisY = compound.getFloat(ROT_AXIS_Y_NBT_TAG);
        rotAxisZ = compound.getFloat(ROT_AXIS_Z_NBT_TAG);
        rainbowMode = compound.getBoolean(RAINBOW_MODE_NBT_TAG);
        rainbowCycleSpeed = compound.getInteger(RAINBOW_MODE_CYCLE_SPEED_NBT_TAG);
    }

    @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());
    }

    public void updateToClient() {
        worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    }
}