Browse Source

snuvi script commands to set ambient lighting and lights, lighting tests, cleanup

Kajetan Johannes Hammerle 5 years ago
parent
commit
f1993ee4b9

+ 12 - 7
levels/00-Tech_Demo.snuvi

@@ -4,11 +4,10 @@ event.load("tile_hit");
 event.load("auto_tile_interact");
 //event.load("entity_spawn");
 
-sgoto(200, @test);
+sgoto(0, @loop);
 
 @main
 wait();
-
 if(event == "level_reset")
 {
     onLevelReset();
@@ -29,18 +28,24 @@ elseif(event == "entity_spawn")
 {
     print(entity.getType(entity));
 }
-
 goto(@main);
 
 
-@test
-//entity.remove($platform);
-//level.finish();
+@loop
+
+light.setPositionSmooth(0, entity.getX(entity.getHero()) + 16, entity.getY(entity.getHero()) + 32);
+
+sgoto(0, @loop);
 goto(@main);
 
 
 function onLevelReset()
 {
+    light.setAmbient(0.0, 0.0, 0.0);
+    light.setColor(0, 1.2, 0.9, 0);
+    light.setPosition(0, entity.getX(entity.getHero()), entity.getY(entity.getHero()));
+    light.setStrength(0, 0.005);
+    
     hero = entity.getHero();
     x = tile.toTileCoord(entity.getX(hero));
     y = tile.toTileCoord(entity.getY(hero));
@@ -112,7 +117,7 @@ function onAutoInteract(x, y, ent)
     elseif(x == 8 && y == 10)
     {
         //entity.setMotionY(ent, tile.scale(-80));
-        entity.addHealth(entity.getHero(), -1);
+        //entity.addHealth(entity.getHero(), -1);
     }
 }
 

BIN
lib/SnuviEngine.jar


+ 27 - 1
src/me/hammerle/supersnuvi/Game.java

@@ -128,8 +128,8 @@ public class Game extends Engine
             SoundUtils.stopSound(SoundUtils.Sound.MENU_MUSIC);
             
             snuviScheduler.setActiveLevel(currentLevel);
-            snuviScheduler.tick(currentLevel);
             currentLevel.tick();
+            snuviScheduler.tick(currentLevel);
             
             // doing that here to prevent concurent modification
             if(currentLevel.isFinished())
@@ -837,5 +837,31 @@ public class Game extends Engine
             ((PlatformController) ((Entity) in[0].get(sc)).getController()).clearMoveData();
             return Void.TYPE;
         });
+        
+        snuviParser.registerFunction("light.setambient", (sc, in) -> 
+        {
+            currentLevel.setAmbientLight(in[0].getFloat(sc), in[1].getFloat(sc), in[2].getFloat(sc));
+            return Void.TYPE;
+        });
+        snuviParser.registerFunction("light.setcolor", (sc, in) -> 
+        {
+            currentLevel.getLight(in[0].getInt(sc)).setColor(in[1].getFloat(sc), in[2].getFloat(sc), in[3].getFloat(sc));
+            return Void.TYPE;
+        });
+        snuviParser.registerFunction("light.setposition", (sc, in) -> 
+        {
+            currentLevel.getLight(in[0].getInt(sc)).setPosition(in[1].getFloat(sc), in[2].getFloat(sc));
+            return Void.TYPE;
+        });
+        snuviParser.registerFunction("light.setpositionsmooth", (sc, in) -> 
+        {
+            currentLevel.getLight(in[0].getInt(sc)).setPositionSmooth(in[1].getFloat(sc), in[2].getFloat(sc));
+            return Void.TYPE;
+        });
+        snuviParser.registerFunction("light.setstrength", (sc, in) -> 
+        {
+            currentLevel.getLight(in[0].getInt(sc)).setStrength(in[1].getFloat(sc));
+            return Void.TYPE;
+        });
     }
 }

+ 1 - 0
src/me/hammerle/supersnuvi/gamelogic/FileLevel.java

@@ -172,6 +172,7 @@ public final class FileLevel extends Level
         removeEntities();
         spawnEntity(h);
         messages.clear();
+        setAmbientLight(1.0f, 1.0f, 1.0f);
         
         getTileUpdater().updateAll();
         

+ 62 - 10
src/me/hammerle/supersnuvi/gamelogic/Level.java

@@ -9,6 +9,7 @@ import me.hammerle.snuviscript.code.Script;
 import me.hammerle.supersnuvi.Game;
 import me.hammerle.supersnuvi.entity.Entity;
 import me.hammerle.supersnuvi.entity.EntityBuilder;
+import me.hammerle.supersnuvi.rendering.Light;
 import me.hammerle.supersnuvi.rendering.TileUpdater;
 import me.hammerle.supersnuvi.tiles.Location;
 import me.hammerle.supersnuvi.tiles.Tile;
@@ -17,12 +18,34 @@ import me.hammerle.supersnuvi.util.Utils;
 
 public abstract class Level
 {
+    // level data
+    private final TileUpdater tileUpdater = new TileUpdater();
+    // entity related
+    private final HashMap<Integer, Entity> entities = new HashMap<>();
+    private final LinkedList<Entity> spawnQueue = new LinkedList<>();
+    private Entity hero = null;
+    private int entityCounter = 0;
+    // controlling
+    private boolean shouldReset = false;
+    private boolean done = false;
+    // lighting
+    private float red = 1.0f;
+    private float green = 1.0f;
+    private float blue = 1.0f;
+    private Light[] lights = new Light[32];
+    
+    public Level()
+    {
+        for(int i = 0; i < lights.length; i++)
+        {
+            lights[i] = new Light();
+        }
+    }
+    
     // -------------------------------------------------------------------------
     // level data
     // -------------------------------------------------------------------------
     
-    private final TileUpdater tileUpdater = new TileUpdater();
-    
     public abstract LevelData getData();
     
     public String getName()
@@ -76,11 +99,6 @@ public abstract class Level
     // entity related
     // -------------------------------------------------------------------------
     
-    private final HashMap<Integer, Entity> entities = new HashMap<>();
-    private final LinkedList<Entity> spawnQueue = new LinkedList<>();
-    private Entity hero = null;
-    private int entityCounter = 0;
-    
     public final Entity getHero()
     {
         return hero;
@@ -146,9 +164,6 @@ public abstract class Level
     // controlling
     // -------------------------------------------------------------------------
     
-    private boolean shouldReset = false;
-    private boolean done = false;
-    
     public abstract void tick();
     
     public final void finishLevel()
@@ -174,6 +189,12 @@ public abstract class Level
     
     public final void reset()
     {
+        // reset lights
+        for(Light l : lights)
+        {
+            l.reset();
+        }
+        
         onReset();
         shouldReset = false;
         done = false;
@@ -293,4 +314,35 @@ public abstract class Level
     {
         callEvent(name, null, null);
     }
+    
+    // -------------------------------------------------------------------------
+    // lighting
+    // -------------------------------------------------------------------------
+    
+    public final void setAmbientLight(float r, float g, float b)
+    {
+        red = r;
+        green = g;
+        blue = b;
+    }
+    
+    public final float getAmbientRed()
+    {
+        return red;
+    }
+    
+    public final float getAmbientGreen()
+    {
+        return green;
+    }
+    
+    public final float getAmbientBlue()
+    {
+        return blue;
+    }
+    
+    public Light getLight(int index)
+    {
+        return lights[index];
+    }
 }

+ 21 - 0
src/me/hammerle/supersnuvi/rendering/LevelRenderer.java

@@ -37,9 +37,30 @@ public class LevelRenderer implements IRenderer<Level>
         {
             updateMeshes(level);
             lastRenderedLevel = level;
+            
+            for(int i = 0; i < 32; i++)
+            {
+                level.getLight(i).markDirty();
+            }
         }
 
+        Shader.setAmbientLight(level.getAmbientRed(), level.getAmbientGreen(), level.getAmbientBlue());
+        Shader.setLightEnabled(true);
+        for(int i = 0; i < 1; i++)
+        {
+            Light l = level.getLight(i);
+            if(l.isDirty())
+            {
+                sh.setLightColor(i, l.getRed(), l.getGreen(), l.getBlue());
+                sh.setLightStrength(i, l.getStrength());
+                l.clearDirtyFlag();
+            }
+            sh.setLightLocation(i, 
+                    Utils.interpolate(l.getLastX(), l.getX(), lag) + cameraX, 
+                    Utils.interpolate(l.getLastY(), l.getY(), lag) + cameraY);
+        }
         renderTilesAndEntities(sh, lag, level);
+        Shader.setLightEnabled(false);
         renderOverlay(sh, level);
     }
 

+ 109 - 0
src/me/hammerle/supersnuvi/rendering/Light.java

@@ -0,0 +1,109 @@
+package me.hammerle.supersnuvi.rendering;
+
+public class Light
+{
+    private float red = 0.0f;
+    private float green = 0.0f;
+    private float blue = 0.0f;
+    private float lastX = 0.0f;
+    private float lastY = 0.0f;
+    private float x = 0.0f;
+    private float y = 0.0f;
+    private float strength = 0.0f;
+    private boolean dirty = false;
+    
+    public void setColor(float r, float g, float b)
+    {
+        markDirty();
+        red = r;
+        green = g;
+        blue = b;
+    }
+    
+    public void setPosition(float x, float y)
+    {
+        markDirty();
+        this.x = x;
+        this.y = y;
+        lastX = x;
+        lastY = y;
+    }
+    
+    public void setPositionSmooth(float x, float y)
+    {
+        markDirty();
+        lastX = this.x;
+        lastY = this.y;
+        this.x = x;
+        this.y = y;
+    }
+    
+    public void setStrength(float f)
+    {
+        markDirty();
+        strength = f;
+    }
+
+    public float getRed()
+    {
+        return red;
+    }
+    
+    public float getGreen()
+    {
+        return green;
+    }
+    
+    public float getBlue()
+    {
+        return blue;
+    }
+    
+    public float getX()
+    {
+        return x;
+    }
+    
+    public float getY()
+    {
+        return y;
+    }
+    
+    public float getLastX()
+    {
+        return lastX;
+    }
+    
+    public float getLastY()
+    {
+        return lastY;
+    }
+    
+    public float getStrength()
+    {
+        return strength;
+    }
+    
+    public boolean isDirty()
+    {
+        return dirty;
+    }
+    
+    public void markDirty()
+    {
+        dirty = true;
+    }
+    
+    public void clearDirtyFlag()
+    {
+        dirty = false;
+    }
+    
+    public void reset()
+    {
+        markDirty();
+        setColor(0.0f, 0.0f, 0.0f);
+        setPosition(0.0f, 0.0f);
+        setStrength(0.0f);
+    }
+}

+ 2 - 2
src/me/hammerle/supersnuvi/snuviscript/SnuviScheduler.java

@@ -48,14 +48,14 @@ public class SnuviScheduler implements ISnuviScheduler
     @Override
     public int scheduleTask(Runnable r, long delay)
     { 
-        queue.add(new Task(r, Math.max(delay, 1), currentLevel));
+        queue.add(new Task(r, Math.max(delay, 2), currentLevel));
         return id++;
     }
     
     @Override
     public int scheduleTask(Runnable r)
     { 
-        return scheduleTask(r, 1);
+        return scheduleTask(r, 2);
     }
     
     public void tick(Level current)

+ 0 - 17
src/me/hammerle/supersnuvi/util/Utils.java

@@ -4,28 +4,11 @@ import me.hammerle.supersnuvi.tiles.Tile;
 
 public class Utils 
 {
-    public static float round(float d)
-    {
-        return Math.round(d * 10000) / 10000f;
-    }
-    
     public static float interpolate(float from, float to, float factor)
     {
         return from + (to - from) * factor;
     }
     
-    public static float interpolateY(float x1, float y1, float x2, float y2, float x3)
-    {
-        float k = (y2 - y1) / (x2 - x1);
-        return k * x3 + y1 - k * x1;
-    }
-    
-    public static float interpolateX(float x1, float y1, float x2, float y2, float y3)
-    {
-        float k = (x2 - x1) / (y2 - y1);
-        return (y3 - y1) * k + x1;
-    }
-    
     public static int toBlock(float c)
     {
         return (int) (c / Tile.SIZE);