Skip to content

Commit 9cb7443

Browse files
committed
Dynamic field projection sizes (no cross axis check)
1 parent 898e69d commit 9cb7443

File tree

3 files changed

+140
-42
lines changed

3 files changed

+140
-42
lines changed

src/main/java/com/robotgryphon/compactcrafting/blocks/tiles/FieldProjectorTile.java

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import net.minecraft.util.math.vector.Vector3i;
2424
import net.minecraftforge.items.ItemHandlerHelper;
2525

26-
import java.util.List;
27-
import java.util.Optional;
26+
import java.util.*;
2827
import java.util.stream.Collectors;
28+
import java.util.stream.Stream;
2929

3030
public class FieldProjectorTile extends TileEntity implements ITickableTileEntity {
3131

@@ -53,35 +53,58 @@ public Optional<BlockPos> getCenter() {
5353
}
5454
}
5555

56+
public Optional<FieldProjectionSize> getFieldSize() {
57+
BlockState bs = this.getBlockState();
58+
Direction facing = bs.get(FieldProjectorBlock.FACING);
59+
60+
Optional<FieldProjectionSize> matchedSize = Arrays
61+
.stream(FieldProjectionSize.values())
62+
.sorted(Comparator.comparingInt(FieldProjectionSize::getOffset))
63+
.filter(size -> {
64+
// check block exists in offset position
65+
BlockPos offsetInDirection = pos.offset(facing, size.getOffset() + 1);
66+
BlockState stateInPos = world.getBlockState(offsetInDirection);
67+
68+
if (stateInPos.getBlock() instanceof FieldProjectorBlock) {
69+
// We have a field projector
70+
Direction otherFpDirection = stateInPos.get(FieldProjectorBlock.FACING);
71+
if (otherFpDirection == facing.getOpposite())
72+
return true;
73+
}
74+
75+
return false;
76+
})
77+
.findFirst();
78+
79+
return matchedSize;
80+
}
81+
5682
/**
5783
* Gets the location of the opposite field projector.
5884
*
5985
* @return
6086
*/
6187
public Optional<BlockPos> getOppositeProjector() {
62-
BlockState bs = this.getBlockState();
63-
Direction facing = bs.get(FieldProjectorBlock.FACING);
64-
65-
for (int i = 1; i <= 13; i++) {
66-
// check block exists in offset position
67-
BlockPos offsetInDirection = pos.offset(facing, i);
68-
BlockState stateInPos = world.getBlockState(offsetInDirection);
88+
Optional<FieldProjectionSize> size = getFieldSize();
89+
if(size.isPresent()) {
90+
int offset = size.get().getOffset() + 1;
91+
BlockPos opposite = getPos().offset(getFacing(), offset);
6992

70-
if (stateInPos.getBlock() instanceof FieldProjectorBlock) {
71-
// We have a field projector
72-
Direction otherFpDirection = stateInPos.get(FieldProjectorBlock.FACING);
73-
if (otherFpDirection == facing.getOpposite())
74-
return Optional.of(offsetInDirection);
75-
}
93+
return Optional.of(opposite);
7694
}
7795

7896
return Optional.empty();
7997
}
8098

81-
public Direction getProjectorSide() {
99+
public Direction getFacing() {
82100
BlockState bs = this.getBlockState();
83101
Direction facing = bs.get(FieldProjectorBlock.FACING);
84102

103+
return facing;
104+
}
105+
106+
public Direction getProjectorSide() {
107+
Direction facing = getFacing();
85108
return facing.getOpposite();
86109
}
87110

@@ -92,30 +115,25 @@ public boolean isMainProjector() {
92115
return side == Direction.NORTH;
93116
}
94117

95-
public FieldProjectionSize getProjectionSize() {
96-
// TODO: Make this different depending on the spacing of the field projectors
97-
return FieldProjectionSize.MEDIUM;
98-
}
99-
100118
@Override
101119
public void tick() {
102120
if (!isMainProjector())
103121
return;
104122

105123
Optional<AxisAlignedBB> fieldBounds = getFieldBounds();
106-
if(fieldBounds.isPresent()) {
124+
if (fieldBounds.isPresent()) {
107125
// TODO: Check crafting state of projection field, update that state instead of doing manual work here
108126
boolean hasCatalyst = hasCatalystInBounds(fieldBounds.get(), Items.ENDER_PEARL);
109127

110-
if(hasCatalyst) {
128+
if (hasCatalyst) {
111129
List<ItemEntity> enderPearls = getCatalystsInField(fieldBounds.get(), Items.ENDER_PEARL);
112130

113131
// We dropped an ender pearl in - are there any blocks in the field?
114132
// If so, we'll need to check the recipes -- but for now we just use it to
115133
// not delete the item if there's nothing in here
116-
if(CraftingHelper.hasBlocksInField(world, fieldBounds.get())) {
117-
CraftingHelper.consumeCatalystItem(enderPearls.get(0), 1);
134+
if (CraftingHelper.hasBlocksInField(world, fieldBounds.get())) {
118135
CraftingHelper.deleteCraftingBlocks(world, fieldBounds.get());
136+
CraftingHelper.consumeCatalystItem(enderPearls.get(0), 1);
119137
}
120138
}
121139
}
@@ -129,10 +147,15 @@ private Optional<AxisAlignedBB> getFieldBounds() {
129147
return Optional.empty();
130148

131149
BlockPos centerPos = center.get();
132-
FieldProjectionSize size = getProjectionSize();
133-
AxisAlignedBB bounds = new AxisAlignedBB(centerPos).grow(size.getSize());
150+
Optional<FieldProjectionSize> size = this.getFieldSize();
151+
if(size.isPresent()) {
152+
AxisAlignedBB bounds = new AxisAlignedBB(centerPos).grow(size.get().getSize());
134153

135-
return Optional.of(bounds);
154+
return Optional.of(bounds);
155+
}
156+
157+
// No valid field found
158+
return Optional.empty();
136159
}
137160

138161
private List<ItemEntity> getCatalystsInField(AxisAlignedBB fieldBounds, Item itemFilter) {
@@ -167,7 +190,7 @@ private int collectItems(List<ItemEntity> itemsInRange) {
167190
@Override
168191
public AxisAlignedBB getRenderBoundingBox() {
169192
Optional<AxisAlignedBB> field = this.getFieldBounds();
170-
if(field.isPresent()) {
193+
if (field.isPresent()) {
171194
return field.get().grow(10);
172195
}
173196

src/main/java/com/robotgryphon/compactcrafting/client/render/FieldProjectorRenderer.java

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@
88
import com.robotgryphon.compactcrafting.core.FieldProjectionSize;
99
import net.minecraft.block.BlockState;
1010
import net.minecraft.client.Minecraft;
11-
import net.minecraft.client.renderer.*;
11+
import net.minecraft.client.renderer.Atlases;
12+
import net.minecraft.client.renderer.BlockRendererDispatcher;
13+
import net.minecraft.client.renderer.IRenderTypeBuffer;
14+
import net.minecraft.client.renderer.RenderType;
1215
import net.minecraft.client.renderer.model.IBakedModel;
1316
import net.minecraft.client.renderer.model.ModelManager;
1417
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
1518
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
1619
import net.minecraft.util.Direction;
1720
import net.minecraft.util.math.AxisAlignedBB;
1821
import net.minecraft.util.math.BlockPos;
22+
import net.minecraft.util.math.vector.Quaternion;
1923
import net.minecraft.util.math.vector.Vector3d;
2024
import net.minecraft.util.math.vector.Vector3f;
25+
import net.minecraft.util.math.vector.Vector3i;
2126
import net.minecraftforge.client.model.ModelDataManager;
2227
import net.minecraftforge.client.model.data.IModelData;
2328

2429
import java.awt.*;
2530
import java.util.Optional;
2631

27-
import static net.minecraft.client.renderer.RenderType.makeType;
28-
2932
public class FieldProjectorRenderer extends TileEntityRenderer<FieldProjectorTile> {
3033

3134
enum RotationSpeed {
@@ -45,6 +48,8 @@ public int getSpeed() {
4548
}
4649

4750
private IBakedModel bakedModelCached;
51+
52+
private Color colorProjectionArc = new Color(255, 106, 0, 200);
4853
private final Color colorProjectionCube = new Color(255, 106, 0, 100);
4954
private final Color colorScanLine = new Color(255, 106, 0, 200);
5055

@@ -56,16 +61,18 @@ public FieldProjectorRenderer(TileEntityRendererDispatcher rendererDispatcherIn)
5661
public void render(FieldProjectorTile tile, float partialTicks, MatrixStack matrixStack, IRenderTypeBuffer buffers, int combinedLightIn, int combinedOverlayIn) {
5762
renderDish(tile, matrixStack, buffers, combinedLightIn, combinedOverlayIn);
5863

59-
Optional<BlockPos> c = tile.getCenter();
60-
if (c.isPresent()) {
61-
BlockPos center = c.get();
62-
FieldProjectionSize fieldProjectionSize = tile.getProjectionSize();
63-
int fieldSize = fieldProjectionSize.getSize();
64+
Optional<FieldProjectionSize> fieldProjectionSize = tile.getFieldSize();
65+
if (fieldProjectionSize.isPresent()) {
66+
BlockPos center = tile.getCenter().get();
67+
int fieldSize = fieldProjectionSize.get().getSize();
6468

6569
AxisAlignedBB cube = new AxisAlignedBB(center).grow(fieldSize);
6670

6771
renderFaces(tile, matrixStack, buffers, cube, 0);
6872

73+
// TODO - WIP ARC CODE
74+
// drawProjectorArcs(tile, matrixStack, buffers, cube, fieldSize);
75+
6976
if (tile.isMainProjector()) {
7077
drawScanLines(tile, matrixStack, buffers, cube, fieldSize);
7178
renderProjectionCube(tile, matrixStack, buffers, cube, fieldSize);
@@ -270,7 +277,63 @@ private void renderProjectionCube(FieldProjectorTile tile, MatrixStack mx, IRend
270277
* Handles drawing the projection arcs that connect the projector blocks to the main projection
271278
* in the center of the crafting area.
272279
*/
273-
private void drawProjectorArcs() {
280+
private void drawProjectorArcs(FieldProjectorTile tile, MatrixStack mx, IRenderTypeBuffer buffers, AxisAlignedBB cube, int cubeSize) {
281+
282+
IVertexBuilder builder = buffers.getBuffer(RenderTypesExtensions.getLines());
283+
284+
double zAngle = ((Math.sin(Math.toDegrees(RenderTickCounter.renderTicks) / -5000) + 1.0d) / 2) * (cube.getYSize());
285+
double scanHeight = (cube.minY + zAngle);
286+
287+
Vector3d centerPos = cube.getCenter();
288+
289+
Direction facing = tile.getBlockState().get(FieldProjectorBlock.FACING);
290+
Quaternion rotation = facing.getRotation();
291+
Vector3i identity = facing.getDirectionVec();
292+
293+
mx.push();
294+
295+
mx.translate(.5, .5, .5);
296+
297+
mx.rotate(rotation);
298+
299+
// 0, 0, 0 is now the edge of the projector's space
300+
addColoredVertex(builder, mx, colorProjectionArc, new Vector3f(0f, 0f, 0f));
301+
302+
// Now translate to center of projection field
303+
addColoredVertex(builder, mx, colorProjectionArc, new Vector3f((float) -scanHeight, 3, 0));
304+
305+
mx.pop();
306+
307+
// mx.push();
308+
//
309+
// translateRendererToCube(tile, mx, cube, cubeSize);
310+
//
311+
// Vector3f LEFT_ENDPOINT = null;
312+
// Vector3f RIGHT_ENDPOINT = null;
313+
//
314+
// switch(facing) {
315+
// case NORTH:
316+
// LEFT_ENDPOINT = new Vector3f((float) cube.minX, (float) scanHeight, (float) cube.maxZ);
317+
// break;
318+
//
319+
// case SOUTH:
320+
// LEFT_ENDPOINT = new Vector3f((float) cube.maxX, (float) scanHeight, (float) cube.minZ);
321+
// break;
322+
//
323+
// case WEST:
324+
// LEFT_ENDPOINT = new Vector3f((float) cube.maxX, (float) scanHeight, (float) cube.maxZ);
325+
// break;
326+
//
327+
// case EAST:
328+
// LEFT_ENDPOINT = new Vector3f((float) cube.minX, (float) scanHeight, (float) cube.minZ);
329+
// break;
330+
// }
331+
//
332+
// if(LEFT_ENDPOINT != null)
333+
// addColoredVertex(builder, mx, colorProjectionArc, LEFT_ENDPOINT);
334+
//
335+
// mx.pop();
336+
274337
// // Render projection planes
275338
// double zAngle = ((Math.sin(Math.toDegrees(RenderTickCounter.renderTicks) / -5000) + 1.0d) / 2) * (cube.maxY - cube.minY);
276339
// double y3 = y1 + zAngle;

src/main/java/com/robotgryphon/compactcrafting/core/FieldProjectionSize.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,37 @@ public enum FieldProjectionSize {
44
/**
55
* 3x3x3 Crafting Field Size (Magnitude 1)
66
*/
7-
SMALL(1, "small"),
7+
SMALL(1, 7, "small"),
88

99
/**
1010
* 5x5x5 Crafting Field Size (Magnitude 2)
1111
*/
12-
MEDIUM(2, "medium"),
12+
MEDIUM(2, 11, "medium"),
1313

1414
/**
1515
* 7x7x7 Crafting Field Size (Magnitude 3)
1616
*/
17-
LARGE(3, "large");
17+
LARGE(3, 15, "large");
1818

1919
private int size;
20+
21+
/**
22+
* Number of blocks away from the opposite projector (exclusive).
23+
*/
24+
private int offset;
25+
2026
private String name;
21-
FieldProjectionSize(int size, String name) {
27+
28+
FieldProjectionSize(int size, int offset, String name) {
2229
this.size = size;
30+
this.offset = offset;
2331
this.name = name;
2432
}
2533

34+
public int getOffset() {
35+
return this.offset;
36+
}
37+
2638
public int getSize() {
2739
return this.size;
2840
}

0 commit comments

Comments
 (0)