Change the front facing direction of a block programmatically.
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
8d152ff06a
commit
80873eec27
@ -4,10 +4,17 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Block {
|
||||
final static int CLOCKWISE = 1;
|
||||
final static int COUNTER_CLOCKWISE = -1;
|
||||
final static int SOUTH = 0;
|
||||
final static int WEST = 1;
|
||||
final static int NORTH = 2;
|
||||
final static int EAST = 3;
|
||||
Vector pos;
|
||||
Mesh block;
|
||||
FaceList neighbors;
|
||||
Block(Vector pos,Mesh block) {
|
||||
private FacingDirection facingDir;
|
||||
Block(Vector pos,Mesh block,FacingDirection facingDir) {
|
||||
this.neighbors=new FaceList();
|
||||
this.pos=pos;
|
||||
List<Triangle> newTris = new ArrayList<>();
|
||||
@ -17,6 +24,34 @@ public class Block {
|
||||
newTris.add(newT);
|
||||
}
|
||||
this.block=new Mesh(newTris);
|
||||
this.facingDir=facingDir;
|
||||
}
|
||||
private void updateFacingDirection(FacingDirection targetDirection) {
|
||||
while (facingDir!=targetDirection) {
|
||||
Texture t1 = block.triangles.get(0).tex;
|
||||
Texture t2 = block.triangles.get(1).tex;
|
||||
block.triangles.get(0).tex=block.triangles.get(2).tex;
|
||||
block.triangles.get(1).tex=block.triangles.get(3).tex;
|
||||
block.triangles.get(2).tex=block.triangles.get(4).tex;
|
||||
block.triangles.get(3).tex=block.triangles.get(5).tex;
|
||||
block.triangles.get(4).tex=block.triangles.get(6).tex;
|
||||
block.triangles.get(5).tex=block.triangles.get(7).tex;
|
||||
block.triangles.get(6).tex=t1;
|
||||
block.triangles.get(7).tex=t2;
|
||||
facingDir=facingDir.clockwise();
|
||||
}
|
||||
}
|
||||
public void setFacingDirection(FacingDirection direction) {
|
||||
updateFacingDirection(direction);
|
||||
}
|
||||
public FacingDirection getFacingDirection() {
|
||||
return facingDir;
|
||||
}
|
||||
public void rotateClockwise() {
|
||||
updateFacingDirection(facingDir.clockwise());
|
||||
}
|
||||
public void rotateCounterClockwise() {
|
||||
updateFacingDirection(facingDir.counterClockwise());
|
||||
}
|
||||
public void updateFaces() {
|
||||
if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y+1)+"_"+pos.z)) {
|
||||
|
16
src/sig/FacingDirection.java
Normal file
16
src/sig/FacingDirection.java
Normal file
@ -0,0 +1,16 @@
|
||||
package sig;
|
||||
|
||||
public enum FacingDirection {
|
||||
SOUTH,
|
||||
WEST,
|
||||
NORTH,
|
||||
EAST;
|
||||
|
||||
static FacingDirection[] orderList = new FacingDirection[]{SOUTH,WEST,NORTH,EAST};
|
||||
FacingDirection clockwise() {
|
||||
return orderList[(this.ordinal()+1)%orderList.length];
|
||||
}
|
||||
FacingDirection counterClockwise() {
|
||||
return orderList[Math.floorMod((this.ordinal()-1),orderList.length)];
|
||||
}
|
||||
}
|
@ -115,6 +115,9 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
|
||||
}break;
|
||||
}
|
||||
} else
|
||||
if (answer.e.getButton()==MouseEvent.BUTTON2) {
|
||||
answer.t.b.rotateClockwise();
|
||||
} else
|
||||
if (answer.e.getButton()==MouseEvent.BUTTON3) {
|
||||
removeBlock(answer.t.b.pos);
|
||||
}
|
||||
@ -123,7 +126,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
|
||||
}
|
||||
|
||||
public static void addBlock(Vector pos,BlockType type) {
|
||||
Block b = new Block(pos,new Cube(type));
|
||||
Block b = new Block(pos,new Cube(type),FacingDirection.SOUTH);
|
||||
blockGrid.put(pos.x+"_"+pos.y+"_"+pos.z,b);
|
||||
b.updateFaces();
|
||||
}
|
||||
@ -170,7 +173,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
|
||||
if (x%8>2&&x%8<6&&y>1&&y<4) {
|
||||
addBlock(new Vector(x,y,16),BlockType.GLASS);
|
||||
} else {
|
||||
addBlock(new Vector(x,y,16),BlockType.ICE);
|
||||
addBlock(new Vector(x,y,16),BlockType.FURNACE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user