From 91d94cc249b27dc330711842e99e5ebeb4089d72 Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Mon, 8 Nov 2021 02:26:56 +0900 Subject: [PATCH] Block removal implemented. --- src/sig/MouseHandler.java | 12 ++++++++++++ src/sig/SigRenderer.java | 37 ++++++++++++++++++++++++++++++------ src/sig/utils/DrawUtils.java | 5 +++-- 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/sig/MouseHandler.java diff --git a/src/sig/MouseHandler.java b/src/sig/MouseHandler.java new file mode 100644 index 0000000..634c584 --- /dev/null +++ b/src/sig/MouseHandler.java @@ -0,0 +1,12 @@ +package sig; + +import java.awt.event.MouseEvent; + +public class MouseHandler { + public MouseEvent e; + public Block b; + public MouseHandler(MouseEvent e,Block b) { + this.e=e; + this.b=b; + } +} diff --git a/src/sig/SigRenderer.java b/src/sig/SigRenderer.java index f8116d4..e4f4364 100644 --- a/src/sig/SigRenderer.java +++ b/src/sig/SigRenderer.java @@ -12,9 +12,7 @@ import java.io.IOException; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; -import java.util.Queue; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.awt.Toolkit; @@ -61,8 +59,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene aHeld=false,sHeld=false,dHeld=false,wHeld=false; public static MouseEvent request; - public static Block answer; - public static Block tempAnswer = null; + public static MouseHandler answer; + public static MouseHandler tempAnswer = null; public void runGameLoop() { if (upHeld) { @@ -93,7 +91,12 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene yaw+=TURNSPEED; } if (answer!=null) { - addBlock(Vector.add(answer.pos,new Vector(0,1,0)),BlockType.PLANKS); + if (answer.e.getButton()==MouseEvent.BUTTON1) { + addBlock(Vector.add(answer.b.pos,new Vector(0,1,0)),BlockType.PLANKS); + } else + if (answer.e.getButton()==MouseEvent.BUTTON3) { + removeBlock(answer.b.pos); + } answer=null; } } @@ -104,12 +107,34 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene b.updateFaces(); } + public static void removeBlock(Vector pos) { + if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y+1)+"_"+pos.z)) { + SigRenderer.blockGrid.get(pos.x+"_"+(pos.y+1)+"_"+pos.z).neighbors.DOWN=false; + } + if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y-1)+"_"+pos.z)) { + SigRenderer.blockGrid.get(pos.x+"_"+(pos.y-1)+"_"+pos.z).neighbors.UP=false; + } + if (SigRenderer.blockGrid.containsKey((pos.x-1)+"_"+(pos.y)+"_"+pos.z)) { + SigRenderer.blockGrid.get((pos.x-1)+"_"+(pos.y)+"_"+pos.z).neighbors.RIGHT=false; + } + if (SigRenderer.blockGrid.containsKey((pos.x+1)+"_"+(pos.y)+"_"+pos.z)) { + SigRenderer.blockGrid.get((pos.x+1)+"_"+(pos.y)+"_"+pos.z).neighbors.LEFT=false; + } + if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y)+"_"+(pos.z+1))) { + SigRenderer.blockGrid.get(pos.x+"_"+(pos.y)+"_"+(pos.z+1)).neighbors.BACKWARD=false; + } + if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y)+"_"+(pos.z-1))) { + SigRenderer.blockGrid.get(pos.x+"_"+(pos.y)+"_"+(pos.z-1)).neighbors.FORWARD=false; + } + blockGrid.remove(pos.x+"_"+pos.y+"_"+pos.z); + } + SigRenderer(JFrame f) { //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false)); Random r = new Random(438107); for (int x=0;x<64;x++) { for (int z=0;z<64;z++) { - addBlock(new Vector(x,0,z),BlockType.DIRT); + addBlock(new Vector(x,0,z),BlockType.GRASS); /* if (Math.random()<=0.5) { addBlock(new Vector(x,y,z),BlockType.GLASS); diff --git a/src/sig/utils/DrawUtils.java b/src/sig/utils/DrawUtils.java index 03ce004..3bcfde6 100644 --- a/src/sig/utils/DrawUtils.java +++ b/src/sig/utils/DrawUtils.java @@ -4,6 +4,7 @@ import java.awt.Color; import java.util.Arrays; import java.awt.image.BufferedImage; +import sig.MouseHandler; import sig.SigRenderer; import sig.Texture; import sig.Triangle; @@ -93,7 +94,7 @@ public class DrawUtils { tex_w=(1.0f-t)*tex_sw+t*tex_ew; if (tex_w>SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j]) { if (SigRenderer.request!=null&&j==SigRenderer.request.getX()&&i==SigRenderer.request.getY()) { - SigRenderer.tempAnswer=ref.b; + SigRenderer.tempAnswer=new MouseHandler(SigRenderer.request,ref.b); } int col = texture.getColor(tex_u/tex_w,tex_v/tex_w,colorMult/255f); if (((col&0xFF000000)>>>24)!=0) { @@ -164,7 +165,7 @@ public class DrawUtils { tex_w=(1.0f-t)*tex_sw+t*tex_ew; if (tex_w>SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j]) { if (SigRenderer.request!=null&&j==SigRenderer.request.getX()&&i==SigRenderer.request.getY()) { - SigRenderer.tempAnswer=ref.b; + SigRenderer.tempAnswer=new MouseHandler(SigRenderer.request,ref.b); } int col = texture.getColor(tex_u/tex_w,tex_v/tex_w,colorMult/255f); if (((col&0xFF000000)>>>24)!=0) {