Commits

Chuck Adams  committed 7106bac

fleshed out fov, still buggy

  • Participants
  • Parent commits 8eb77aa

Comments (0)

Files changed (5)

File src/main/java/net/fishbulb/jcod/demo/Demo.java

 import com.badlogic.gdx.graphics.Pixmap;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.scenes.scene2d.Stage;
-import com.badlogic.gdx.scenes.scene2d.actions.Actions;
 import com.google.common.collect.Iterators;
 import net.fishbulb.jcod.Console;
 import net.fishbulb.jcod.display.ImageTileset;
     DemoApplet active;
 
     List<Class<? extends DemoApplet>> allApplets = new ArrayList<Class<? extends DemoApplet>>() {{
+        add(Fov.class);
         add(Colors.class);
         add(Offscreen.class);
         add(Lines.class);
         float ypos = (Gdx.app.getGraphics().getHeight() - (display.getPrefHeight() + APPLET_Y + TILE_HEIGHT)) / 2;
 
         // drop down from top
-        display.addAction(Actions.moveTo(xpos, Gdx.app.getGraphics().getHeight() + root.getHeight()));
-        display.addAction(Actions.moveTo(xpos, ypos, 1));
+//        display.addAction(Actions.moveTo(xpos, Gdx.app.getGraphics().getHeight() + root.getHeight()));
+//        display.addAction(Actions.moveTo(xpos, ypos, 1));
     }
 
 

File src/main/java/net/fishbulb/jcod/demo/Fov.java

 
 import com.badlogic.gdx.graphics.Color;
 import net.fishbulb.jcod.Console;
+import net.fishbulb.jcod.fov.Basic;
+import net.fishbulb.jcod.fov.FovAlgorithm;
+import net.fishbulb.jcod.fov.FovMap;
+import net.fishbulb.jcod.util.BlendMode;
+import net.fishbulb.jcod.util.CharCodes;
 
 public class Fov extends DemoApplet {
     String[] map = new String[]{
     boolean recompute = true;
     boolean lightWalls = true;
 
-    Color darkWall = new Color(0,0, 0.35f,1f);
-    Color lightWall = new Color(0.5f, 0.4f, 0.2f,1f);
-    Color darkGround = new Color(0.2f, 0.2f, 0.6f,1);
-    Color lightGround = new Color(0.8f, 0.7f, 0.2f,1f);
+    Color darkWall = new Color(0, 0, 0.35f, 1f);
+    Color lightWall = new Color(0.5f, 0.4f, 0.2f, 1f);
+    Color darkGround = new Color(0.2f, 0.2f, 0.6f, 1);
+    Color lightGround = new Color(0.8f, 0.7f, 0.2f, 1f);
 
     int px = 20;
     int py = 10;
+
+    FovMap light;
+    FovAlgorithm algo;
+
     private long lastUpdate;
 
     public Fov(Console parent) {
         super(parent);
+        light = new FovMap(width, height);
+        algo = new Basic(light);
+        initFov();
+    }
+
+    private void initFov() {
+        for (int y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                light.setTransparent(x, y, map[y].charAt(x) != '#');
+            }
+        }
     }
 
     @Override public void update() {
         if ((now - lastUpdate) < updateMillis) return;
         lastUpdate = now;
 
+        console.clear();
+        console.setDefaultForeground(Color.BLACK);
+        console.putChar(px, py, '@', BlendMode.None);
+        // algo.computeFov(px, py, 10, false);
+        // algo.castRay(px,py,0,0,10,true);
+        light.setFov(px,py,true);
 
-
+        for (int y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                char c = map[y].charAt(x);
+                boolean lit = light.isFov(x, y);
+                switch (c) {
+                    case '=':
+                        console.putChar(x, y, CharCodes.OEM.DHLINE, BlendMode.None);
+                        console.setCharBackground(x, y, lit ? lightGround : darkGround);
+                        break;
+                    case '#':
+                        console.setCharBackground(x, y, lit ? lightWall : darkWall);
+                        break;
+                    case ' ':
+                        console.setCharBackground(x, y, lit ? lightGround : darkGround);
+                        break;
+                }
+            }
+        }
     }
 }

File src/main/java/net/fishbulb/jcod/fov/Basic.java

 package net.fishbulb.jcod.fov;
 
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+
 public class Basic extends FovAlgorithm {
 
     public Basic(FovMap map) {
         super(map);
     }
 
+    @Override
+    public void computeFov(int x, int y, int maxRadius, boolean walls) {
+        int xo,yo;
+        int xmin=0;
+        int ymin=0;
+        int xmax=map.getWidth();
+        int ymax=map.getHeight();
+        int r2=maxRadius*maxRadius;
+        if ( maxRadius > 0 ) {
+            xmin=max(0, x- maxRadius);
+            ymin=max(0, y - maxRadius);
+            xmax=min(map.getWidth(), x+ maxRadius + 1);
+            ymax=min(map.getHeight(), y + maxRadius + 1);
+        }
+        map.clearFov();
+        
+        xo=xmin; yo=ymin;
+        while ( xo < xmax ) {
+            castRay(x,y,xo++,yo,r2,walls);
+        }
+        xo=xmax-1;yo=ymin+1;
+        while ( yo < ymax ) {
+            castRay(x,y,xo,yo++,r2,walls);
+        }
+        xo=xmax-2;yo=ymax-1;
+        while ( xo >= 0 ) {
+            castRay(x,y,xo--,yo,r2,walls);
+        }
+        xo=xmin;yo=ymax-2;
+        while ( yo > 0 ) {
+            castRay(x,y,xo,yo--,r2,walls);
+        }
+//        if ( walls ) {
+//		/* post-processing artefact fix */
+//            TCOD_map_postproc(m,xmin,ymin,x,y,-1,-1);
+//            TCOD_map_postproc(m,x,ymin,xmax-1,y,1,-1);
+//            TCOD_map_postproc(m,xmin,y,x,ymax-1,-1,1);
+//            TCOD_map_postproc(m,x,y,xmax-1,ymax-1,1,1);
+//        }
+    }
+
 
 }

File src/main/java/net/fishbulb/jcod/fov/FovAlgorithm.java

 public abstract class FovAlgorithm {
 
     @Getter @Setter
-    private FovMap map;
+    protected FovMap map;
 
     protected FovAlgorithm(FovMap map) {
         this.map = map;
         new RayCast(x0, x0, x1, y1, r, w).cast();
     }
 
-
+    public abstract void computeFov(int x, int y, int maxRadius, boolean walls);
 
 }

File src/main/java/net/fishbulb/jcod/fov/FovMap.java

         }
     }
 
+    public void clearFov() {
+        for (Cell c: cells) c.fov = false;
+    }
+
     public boolean contains(int x, int y) {
         return (x >= 0 && y >= 0 && x < width && y < height);
     }