summaryrefslogtreecommitdiff
path: root/ihl/enviroment/SpotlightTileEntity.java
blob: d8e55df4c4c7702534767f6751c5a254de1a70e4 (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
package ihl.enviroment;

import ic2.api.network.INetworkTileEntityEventListener;
import ic2.core.IC2;
import ihl.ClientProxy;
import ihl.IHLMod;
import ihl.IHLModInfo;
import ihl.handpump.XYZ;
import ihl.utils.IHLUtils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.util.ForgeDirection;

public class SpotlightTileEntity extends LightBulbTileEntity implements INetworkTileEntityEventListener {
	public float directionX = 0f;
	public float directionY = -1f;
	public float directionZ = 0f;
	public float prevDirectionX = 0f;
	public float prevDirectionY = -1f;
	public float prevDirectionZ = 0f;
	public float rotationPitch = 0f;
	public float rotationYaw = 0f;
	public float prevRotationPitch = 0f;
	public float prevRotationYaw = 0f;
	boolean needLightTargetUpdate = false;

	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound) {
		super.readFromNBT(nbttagcompound);
		directionX = nbttagcompound.getFloat("directionX");
		directionY = nbttagcompound.getFloat("directionY");
		directionZ = nbttagcompound.getFloat("directionZ");
		this.rotationPitch = this.getVectorPitchAngle(directionX,directionY,directionZ);
		this.rotationYaw = this.getVectorYawAngle(directionX,directionY,directionZ);
		needLightTargetUpdate = true;
	}

	@Override
	public void writeToNBT(NBTTagCompound nbttagcompound) {
		super.writeToNBT(nbttagcompound);
		nbttagcompound.setDouble("directionX", directionX);
		nbttagcompound.setDouble("directionY", directionY);
		nbttagcompound.setDouble("directionZ", directionZ);
	}

	@Override
	public List<String> getNetworkedFields() {
		List<String> list = super.getNetworkedFields();
		list.add("needLightTargetUpdate");
		list.add("rotationPitch");
		list.add("rotationYaw");
		list.add("directionX");
		list.add("directionY");
		list.add("directionZ");
		return list;
	}

	@Override
	public void updateEntity() {
		super.updateEntity();
		if (this.worldObj.isRemote) {
			if(this.directionX!=this.prevDirectionX || 
					this.directionY!=this.prevDirectionY ||
					this.directionZ!=this.prevDirectionZ){
				updateLightState();
				this.prevDirectionX=this.directionX;
				this.prevDirectionY=this.directionY;
				this.prevDirectionZ=this.directionZ;
			}
		} else if (needLightTargetUpdate) {
			this.updateLightState();
			needLightTargetUpdate = false;
		}
		if (!this.worldObj.isRemote) {
			if (this.prevRotationPitch != this.rotationPitch) {
				IC2.network.get().updateTileEntityField(this, "rotationPitch");
				this.prevRotationPitch = this.rotationPitch;
			}
			if (this.prevRotationYaw != this.rotationYaw) {
				IC2.network.get().updateTileEntityField(this, "rotationYaw");
				this.prevRotationYaw = this.rotationYaw;
			}
		}
	}

	@Override
	public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
		return IHLUtils.getThisModItemStack("spotlight");
	}

	public void setDirectionVector(EntityLivingBase player) {
		ForgeDirection dir = ForgeDirection.getOrientation(this.getFacing());
		Vec3 lookVec = player.getLookVec();
		if (lookVec.xCoord * dir.offsetX + lookVec.yCoord * dir.offsetY + lookVec.zCoord * dir.offsetZ < 0) {
			double x = player.posX - this.xCoord - 0.5D;
			double y = player.posY + player.getEyeHeight() - this.yCoord - 0.5D;
			double z = player.posZ - this.zCoord - 0.5D;
			double d = Math.sqrt(x * x + y * y + z * z);
			directionX = (float) (x / d);
			directionY = (float) (y / d);
			directionZ = (float) (z / d);
		} else {
			Vec3 plook = player.getLookVec();
			directionX = (float) (plook.xCoord);
			directionY = (float) (plook.yCoord);
			directionZ = (float) (plook.zCoord);
		}
		IC2.network.get().updateTileEntityField(this, "directionX");
		IC2.network.get().updateTileEntityField(this, "directionY");
		IC2.network.get().updateTileEntityField(this, "directionZ");
		this.rotationPitch = this.getVectorPitchAngle(directionX,directionY,directionZ);
		this.rotationYaw = this.getVectorYawAngle(directionX,directionY,directionZ);
		this.needLightTargetUpdate = true;
		IC2.network.get().initiateTileEntityEvent(this, 0, true);
	}

	private float getVectorPitchAngle(float x, float y, float z) {
		switch (this.getFacing()) {
		case 0:
			return (float) -(Math.abs(Math.asin(z)) + Math.abs(Math.asin(x)));
		case 1:
			return (float) (Math.abs(Math.asin(z)) + Math.abs(Math.asin(x)));
		case 2:
			return (float) (Math.abs(Math.asin(y)) + Math.abs(Math.asin(x)));
		case 3:
			return (float) (Math.abs(Math.asin(y)) + Math.abs(Math.asin(x)));
		case 4:
			return (float) (Math.abs(Math.asin(y)) + Math.abs(Math.asin(z)));
		case 5:
			return (float) (Math.abs(Math.asin(y)) + Math.abs(Math.asin(z)));
		default:
			return (float) (-Math.asin(y));
		}
	}

	@SideOnly(value = Side.CLIENT)
	@Override
	protected LightSource createLightSource(int red, int green, int blue) {
		return ((ClientProxy) IHLMod.proxy).getLightHandler().calculateLightSource(worldObj, xCoord, yCoord, zCoord,
				256, red, green, blue, new double[] { this.xCoord + 0.5f, this.yCoord + 0.5f, this.zCoord + 0.5f,
						this.directionX, this.directionY, this.directionZ, 0.8d });
	}

	private float getVectorYawAngle(float x, float y, float z) {
		switch (this.getFacing()) {
		case 0:
			if (z >= 0) {
				return (float) (Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2);
			} else {
				return (float) (-Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2);
			}
		case 1:
			if (z >= 0) {
				return (float) (Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2);
			} else {
				return (float) (-Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2);
			}
		case 2:
			if (y >= 0) {
				return (float) (-Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2);
			} else {
				return (float) (Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2);
			}
		case 3:
			if (y >= 0) {
				return (float) (-Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2);
			} else {
				return (float) (Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2);
			}
		case 4:
			if (y >= 0) {
				return (float) (-Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2);
			} else {
				return (float) (Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2);
			}
		case 5:
			if (y >= 0) {
				return (float) (-Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2);
			} else {
				return (float) (Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2);
			}
		default:
			return 0f;
		}
	}

	@Override
	public void onNetworkEvent(int event) {
		this.worldObj.playSound(xCoord + 0.5d, yCoord + 0.5d, zCoord + 0.5d, IHLModInfo.MODID + ":spotlightRotating",
				10F, 1f, true);
	}
}

/*
 * -Y DOWN(0, -1, 0) 0 /* +Y UP(0, 1, 0) 1 /* -Z NORTH(0, 0, -1) 2 /* +Z
 * SOUTH(0, 0, 1) 3 /* -X WEST(-1, 0, 0), 4 /* +X EAST(1, 0, 0), 5
 * //VALID_DIRECTIONS = {DOWN, UP, NORTH, SOUTH, WEST, EAST};
 */