diff --git a/src/sig/Block.java b/src/sig/Block.java index 8272cb5..de61213 100644 --- a/src/sig/Block.java +++ b/src/sig/Block.java @@ -111,6 +111,16 @@ public class Block { if (b.block instanceof Staircase && block instanceof Staircase) { neighbors.DOWN=false; b.neighbors.UP=true; + } else + if ((b.block instanceof Cube && block instanceof Staircase)||(b.block instanceof Staircase && block instanceof Cube)) { + Block staircase = (b.block instanceof Staircase)?b:this; + + if (b.equals(staircase)) { + neighbors.DOWN=b.neighbors.UP=false; + } else { + neighbors.DOWN=false; + b.neighbors.UP=true; + } } else { neighbors.DOWN=b.neighbors.UP=block.triangles.get(Texture.BOTTOM).tex.hasTransparency==b.block.triangles.get(Texture.TOP).tex.hasTransparency&&block.triangles.get(Texture.BOTTOM).tex.hasTranslucency==b.block.triangles.get(Texture.TOP).tex.hasTranslucency; } diff --git a/src/sig/BlockDefinition.java b/src/sig/BlockDefinition.java new file mode 100644 index 0000000..95e8247 --- /dev/null +++ b/src/sig/BlockDefinition.java @@ -0,0 +1,10 @@ +package sig; + +public class BlockDefinition { + Class cl; + BlockType type; + BlockDefinition(Class cl,BlockType type) { + this.cl=cl; + this.type=type; + } +} diff --git a/src/sig/Mesh.java b/src/sig/Mesh.java index eec3b6b..637a4d3 100644 --- a/src/sig/Mesh.java +++ b/src/sig/Mesh.java @@ -30,4 +30,8 @@ public class Mesh { public boolean handleCollision(Block b,float x,float y) { return false; } + @Override + public String toString() { + return "Mesh [triangles=" + triangles + ", type=" + type + "]"; + } } diff --git a/src/sig/SigRenderer.java b/src/sig/SigRenderer.java index 748f47b..6959185 100644 --- a/src/sig/SigRenderer.java +++ b/src/sig/SigRenderer.java @@ -7,6 +7,7 @@ import sig.models.Staircase; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelEvent; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; import java.io.File; @@ -26,8 +27,9 @@ import java.awt.Point; import java.awt.Cursor; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; +import java.awt.event.MouseWheelListener; -public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener{ +public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener,MouseWheelListener{ public static boolean WIREFRAME = false; public static boolean PROFILING = false; @@ -100,6 +102,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene public static Cursor invisibleCursor; + public static int selectedMode=0; + void addSpeed(Vector v) { //vCameraSpeed = Vector.add(vCameraSpeed,v); xSpeed+=v.x; @@ -112,12 +116,6 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene Block b2 = blockGrid.get((float)Math.floor(vCamera.x+x-cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z+cameraCollisionPadding)); Block b3 = blockGrid.get((float)Math.floor(vCamera.x+x+cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z-cameraCollisionPadding)); Block b4 = blockGrid.get((float)Math.floor(vCamera.x+x-cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z-cameraCollisionPadding)); - if (b1!=null && !(b1.block instanceof Staircase)|| - b2!=null && !(b2.block instanceof Staircase)|| - b3!=null && !(b3.block instanceof Staircase)|| - b4!=null && !(b4.block instanceof Staircase)) { - return false; - } if (b1!=null) {if (!b1.block.handleCollision(b1,x,z)) {return false;}} if (b2!=null) {if (!b2.block.handleCollision(b2,x,z)) {return false;}} if (b3!=null) {if (!b3.block.handleCollision(b3,x,z)) {return false;}} @@ -298,24 +296,35 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene dirVal=directions[(index+answer.t.b.getFacingDirection().ordinal())%4]; } } + + BlockDefinition bl = null; + switch (selectedMode) { + case 1:{ + bl = new BlockDefinition(Staircase.class,BlockType.PLANKS); + }break; + default:{ + bl = new BlockDefinition(Cube.class,BlockType.DIRT); + }break; + } + switch (dirVal) { case BlockType.FRONT:{ - addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,-1)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); + addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,-1)),bl.cl,bl.type,FacingDirection.SOUTH); }break; case BlockType.BACK:{ - addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,1)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); + addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,1)),bl.cl,bl.type,FacingDirection.SOUTH); }break; case BlockType.LEFT:{ - addBlock(Vector.add(answer.t.b.pos,new Vector(-1,0,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); + addBlock(Vector.add(answer.t.b.pos,new Vector(-1,0,0)),bl.cl,bl.type,FacingDirection.SOUTH); }break; case BlockType.RIGHT:{ - addBlock(Vector.add(answer.t.b.pos,new Vector(1,0,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); + addBlock(Vector.add(answer.t.b.pos,new Vector(1,0,0)),bl.cl,bl.type,FacingDirection.SOUTH); }break; case BlockType.TOP:{ - addBlock(Vector.add(answer.t.b.pos,new Vector(0,1,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); + addBlock(Vector.add(answer.t.b.pos,new Vector(0,1,0)),bl.cl,bl.type,FacingDirection.SOUTH); }break; case BlockType.BOTTOM:{ - addBlock(Vector.add(answer.t.b.pos,new Vector(0,-1,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); + addBlock(Vector.add(answer.t.b.pos,new Vector(0,-1,0)),bl.cl,bl.type,FacingDirection.SOUTH); }break; } } else @@ -418,6 +427,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene f.getContentPane().addMouseListener(this); f.getContentPane().addMouseMotionListener(this); f.addKeyListener(this); + f.getContentPane().addMouseWheelListener(this); f.setSize(SCREEN_WIDTH,SCREEN_HEIGHT); f.add(panel,BorderLayout.CENTER); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -519,10 +529,15 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene @Override public void mouseDragged(MouseEvent e) { + controlCamera(e); } @Override public void mouseMoved(MouseEvent e) { + controlCamera(e); + } + + private void controlCamera(MouseEvent e) { Point middle = new Point((int)(panel.getLocationOnScreen().x+panel.getWidth()/2), (int)(panel.getLocationOnScreen().y+panel.getHeight()/2)); //System.out.println((middle.x-e.getXOnScreen())+","+(middle.y-e.getYOnScreen())); int diffX=Math.max(-100,Math.min(100,e.getXOnScreen()-middle.x)); @@ -623,4 +638,10 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene }break; } } + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + selectedMode+=Math.signum(e.getWheelRotation()); + System.out.println("Mode "+selectedMode+"."); + } } diff --git a/src/sig/models/Staircase.java b/src/sig/models/Staircase.java index ac7abbc..ea34618 100644 --- a/src/sig/models/Staircase.java +++ b/src/sig/models/Staircase.java @@ -84,23 +84,22 @@ public class Staircase extends Mesh{ Block b3 = SigRenderer.blockGrid.get((float)Math.floor(x+SigRenderer.cameraCollisionPadding)+"_"+(float)Math.floor(y+yy)+"_"+(float)Math.floor(z-SigRenderer.cameraCollisionPadding)); Block b4 = SigRenderer.blockGrid.get((float)Math.floor(x-SigRenderer.cameraCollisionPadding)+"_"+(float)Math.floor(y+yy)+"_"+(float)Math.floor(z-SigRenderer.cameraCollisionPadding)); //System.out.println(b1+","+b2+","+b3+","+b4); - if ((b1!=null&&!b1.block.equals(staircaseCheck))|| - (b2!=null&&!b2.block.equals(staircaseCheck))|| - (b3!=null&&!b3.block.equals(staircaseCheck))|| - (b4!=null&&!b4.block.equals(staircaseCheck))) { + if ((b1!=null)|| + (b2!=null)|| + (b3!=null)|| + (b4!=null)) { return false; } } return true; } public boolean handleCollision(Block b,float x,float z) { - if (SigRenderer.currentStaircase!=null&& - SigRenderer.currentStaircase==this) { - float diffX=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.x+x-b.pos.x)); - float diffZ=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.z+z-b.pos.z)); + if (SigRenderer.currentStaircase!=null&&b.pos.y==SigRenderer.currentStaircase.triangles.get(0).b.pos.y) { + float diffX=Math.min(1,Math.max(0f,SigRenderer.vCamera.x-b.pos.x)); + float diffZ=Math.min(1,Math.max(0f,SigRenderer.vCamera.z-b.pos.z)); switch (b.getFacingDirection()) { case EAST: { - if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffX+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { + if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffX+x+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { SigRenderer.vCamera.y=b.pos.y+diffX+0.3f; } else { SigRenderer.fallSpd=0; @@ -108,7 +107,7 @@ public class Staircase extends Mesh{ } }break; case NORTH: { - if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffZ)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { + if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffZ+z)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { SigRenderer.vCamera.y=b.pos.y+(1-diffZ)+0.3f; } else { SigRenderer.fallSpd=0; @@ -116,7 +115,7 @@ public class Staircase extends Mesh{ } }break; case SOUTH: { - if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffZ+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { + if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffZ+z+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { SigRenderer.vCamera.y=b.pos.y+diffZ+0.3f; } else { SigRenderer.fallSpd=0; @@ -124,7 +123,7 @@ public class Staircase extends Mesh{ } }break; case WEST: { - if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffX)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { + if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffX+x)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { SigRenderer.vCamera.y=b.pos.y+(1-diffX)+0.3f; } else { SigRenderer.fallSpd=0; @@ -135,41 +134,39 @@ public class Staircase extends Mesh{ SigRenderer.fallSpd=0; return true; } - if (this!=SigRenderer.currentStaircase && SigRenderer.vCamera.y>=b.pos.y) { + if (SigRenderer.vCamera.y>=b.pos.y&&SigRenderer.vCamera.y=b.pos.y+3f) { valid=true; } else { - float diffX=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.x+x-b.pos.x)); - float diffZ=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.z+z-b.pos.z)); switch (b.getFacingDirection()) { case EAST: { - if (SigRenderer.vCamera.xb.pos.z+0.5f&&checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffZ)+0.3f, SigRenderer.vCamera.z+z,this)) { + if (SigRenderer.vCamera.z>b.pos.z+0.5f) { valid=true; } }break; case SOUTH: { - if (SigRenderer.vCamera.zb.pos.x+0.5f&&checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffX)+0.3f, SigRenderer.vCamera.z+z,this)) { + if (SigRenderer.vCamera.x>b.pos.x+0.5f) { valid=true; } }break; } - } - if (valid) { - SigRenderer.currentStaircase=this; - return true; + if (valid) { + SigRenderer.currentStaircase=this; + return true; + } } } return false; } -} +} \ No newline at end of file