Block removal implemented.

origin
Joshua Sigona 3 years ago
parent 4d3d72eba2
commit 91d94cc249
  1. 12
      src/sig/MouseHandler.java
  2. 37
      src/sig/SigRenderer.java
  3. 5
      src/sig/utils/DrawUtils.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;
}
}

@ -12,9 +12,7 @@ import java.io.IOException;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.awt.Toolkit; import java.awt.Toolkit;
@ -61,8 +59,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
aHeld=false,sHeld=false,dHeld=false,wHeld=false; aHeld=false,sHeld=false,dHeld=false,wHeld=false;
public static MouseEvent request; public static MouseEvent request;
public static Block answer; public static MouseHandler answer;
public static Block tempAnswer = null; public static MouseHandler tempAnswer = null;
public void runGameLoop() { public void runGameLoop() {
if (upHeld) { if (upHeld) {
@ -93,7 +91,12 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
yaw+=TURNSPEED; yaw+=TURNSPEED;
} }
if (answer!=null) { 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; answer=null;
} }
} }
@ -104,12 +107,34 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
b.updateFaces(); 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) { SigRenderer(JFrame f) {
//cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false)); //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false));
Random r = new Random(438107); Random r = new Random(438107);
for (int x=0;x<64;x++) { for (int x=0;x<64;x++) {
for (int z=0;z<64;z++) { 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) { if (Math.random()<=0.5) {
addBlock(new Vector(x,y,z),BlockType.GLASS); addBlock(new Vector(x,y,z),BlockType.GLASS);

@ -4,6 +4,7 @@ import java.awt.Color;
import java.util.Arrays; import java.util.Arrays;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import sig.MouseHandler;
import sig.SigRenderer; import sig.SigRenderer;
import sig.Texture; import sig.Texture;
import sig.Triangle; import sig.Triangle;
@ -93,7 +94,7 @@ public class DrawUtils {
tex_w=(1.0f-t)*tex_sw+t*tex_ew; tex_w=(1.0f-t)*tex_sw+t*tex_ew;
if (tex_w>SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j]) { if (tex_w>SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j]) {
if (SigRenderer.request!=null&&j==SigRenderer.request.getX()&&i==SigRenderer.request.getY()) { 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); int col = texture.getColor(tex_u/tex_w,tex_v/tex_w,colorMult/255f);
if (((col&0xFF000000)>>>24)!=0) { if (((col&0xFF000000)>>>24)!=0) {
@ -164,7 +165,7 @@ public class DrawUtils {
tex_w=(1.0f-t)*tex_sw+t*tex_ew; tex_w=(1.0f-t)*tex_sw+t*tex_ew;
if (tex_w>SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j]) { if (tex_w>SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j]) {
if (SigRenderer.request!=null&&j==SigRenderer.request.getX()&&i==SigRenderer.request.getY()) { 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); int col = texture.getColor(tex_u/tex_w,tex_v/tex_w,colorMult/255f);
if (((col&0xFF000000)>>>24)!=0) { if (((col&0xFF000000)>>>24)!=0) {

Loading…
Cancel
Save