-
Notifications
You must be signed in to change notification settings - Fork 497
/
BuildingItem.java
executable file
·200 lines (151 loc) · 4.91 KB
/
BuildingItem.java
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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.builders;
import java.util.Date;
import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.core.Position;
import buildcraft.core.blueprints.BuildingSlot;
import buildcraft.core.blueprints.IBuilder;
import buildcraft.core.network.NetworkData;
public class BuildingItem implements IBuilder {
@NetworkData
public Position origin, destination;
@NetworkData
public LinkedList <ItemStack> stacksToBuild = new LinkedList<ItemStack>();
public LinkedList <StackAtPosition> stacksToDisplay = new LinkedList<StackAtPosition>();
public class StackAtPosition {
public Position pos;
public ItemStack stack;
public boolean display;
}
public Position posDisplay = new Position();
public boolean isDone = false;
long previousUpdate;
double lifetime = 0;
double lifetimeDisplay = 0;
double maxLifetime = 0;
private boolean initialized = false;
double vx, vy, vz;
double maxHeight;
public BuildingSlot slotToBuild;
public IBuilderContext context;
public double receivedProgress = 0;
public void initialize () {
if (!initialized) {
double dx = destination.x - origin.x;
double dy = destination.y - origin.y;
double dz = destination.z - origin.z;
double size = Math.sqrt(dx * dx + dy * dy + dz * dz);
maxLifetime = size * 5.0;
maxHeight = (5.0 + (destination.y - origin.y) / 2.0);
// the below computation is an approximation of the distance to
// travel for the object. It really follows a sinus, but we compute
// the size of a triangle for simplification.
Position middle = new Position();
middle.x = (destination.x + origin.x) / 2;
middle.y = (destination.y + origin.y) / 2;
middle.z = (destination.z + origin.z) / 2;
Position top = new Position ();
top.x = middle.x;
top.y = middle.y + maxHeight;
top.z = middle.z;
Position originToTop = new Position ();
originToTop.x = top.x - origin.x;
originToTop.y = top.y - origin.y;
originToTop.z = top.z - origin.z;
Position destinationToTop = new Position ();
destinationToTop.x = destination.x - origin.x;
destinationToTop.y = destination.y - origin.y;
destinationToTop.z = destination.z - origin.z;
Position distance = new Position();
double d1 = Math.sqrt(originToTop.x * originToTop.x + originToTop.y
* originToTop.y + originToTop.z * originToTop.z);
double d2 = Math.sqrt(destinationToTop.x * destinationToTop.x + destinationToTop.y
* destinationToTop.y + destinationToTop.z * destinationToTop.z);
d1 = d1 / size * maxLifetime;
d2 = d2 / size * maxLifetime;
maxLifetime = d1 + d2;
vx = dx / maxLifetime;
vy = dy / maxLifetime;
vz = dz / maxLifetime;
for (ItemStack s : stacksToBuild) {
StackAtPosition sPos = new StackAtPosition();
sPos.stack = s;
stacksToDisplay.add(sPos);
}
if (stacksToDisplay.size() == 0) {
StackAtPosition sPos = new StackAtPosition();
sPos.stack = new ItemStack(BuildCraftBuilders.buildToolBlock);
stacksToDisplay.add(sPos);
}
initialized = true;
}
}
public Position getDisplayPosition (double time) {
Position result = new Position ();
result.x = origin.x + vx * time;
result.y = origin.y + vy * time + Math.sin(time / maxLifetime * Math.PI) * maxHeight;
result.z = origin.z + vz * time;
return result;
}
public void update () {
if (isDone) {
return;
}
initialize();
lifetime++;
if (lifetime > maxLifetime + stacksToBuild.size() - 1) {
isDone = true;
build ();
}
lifetimeDisplay = lifetime;
previousUpdate = new Date ().getTime();
if (slotToBuild != null && lifetime > maxLifetime) {
slotToBuild.writeCompleted(context, (lifetime - maxLifetime)
/ stacksToBuild.size());
}
}
public void displayUpdate () {
initialize();
double tickDuration = 1000.0 / 20.0;
long currentUpdate = new Date ().getTime();
double timeSpan = currentUpdate - previousUpdate;
previousUpdate = currentUpdate;
double displayPortion = timeSpan / tickDuration;
if (lifetimeDisplay - lifetime <= 1.0) {
lifetimeDisplay += 1.0 * displayPortion;
}
}
private void build() {
if (slotToBuild != null) {
slotToBuild.writeToWorld(context);
}
}
public LinkedList <StackAtPosition> getStacks () {
int d = 0;
for (StackAtPosition s : stacksToDisplay) {
double stackLife = lifetimeDisplay - d;
if (stackLife <= maxLifetime && stackLife > 0) {
s.pos = getDisplayPosition(stackLife);
s.display = true;
} else {
s.display = false;
}
d++;
}
return stacksToDisplay;
}
@Override
public boolean isDone() {
return isDone;
}
}