diff --git a/core/assets/Fragment.frag b/core/assets/Fragment.frag index 6158404..8aaf627 100644 --- a/core/assets/Fragment.frag +++ b/core/assets/Fragment.frag @@ -5,6 +5,7 @@ uniform vec2 u_screenResolution; uniform vec2 u_lightCoord[50]; uniform float u_lightIntensity[50]; uniform int u_actualLights; +uniform float u_ambientLight; //"in" varyings from our vertex shader varying vec4 vColor; @@ -31,7 +32,7 @@ void main() { //Distance is measured in pixels - float brightest = 0; + float brightest = u_ambientLight; for(int i = 0;i < u_actualLights;i++){ float dis2Light = getDistance(gl_FragCoord.x, gl_FragCoord.y, u_lightCoord[i].x, u_lightCoord[i].y); float dis2Light2 = (1-(min(dis2Light/(disFull * u_lightIntensity[i]), 1))); diff --git a/core/assets/env.png b/core/assets/env.png index f606856..4dcaac0 100644 Binary files a/core/assets/env.png and b/core/assets/env.png differ diff --git a/core/assets/testmap.tmx b/core/assets/testmap.tmx index 2568d93..cb92c05 100644 --- a/core/assets/testmap.tmx +++ b/core/assets/testmap.tmx @@ -1,5 +1,5 @@ - + @@ -31,7 +31,7 @@ 0,20,20,20,20,20,20,20,1,1,20,20,20,20,20,20,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,20,20,20,20,20,1,1,20,20,20,20,20,20,20,20,10,11,20,20, 0,4,4,4,4,16,4,5,1,1,3,4,16,4,4,4,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,4,4,16,4,5,1,1,3,4,16,4,4,4,4,4,18,19,4,4, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,20,20,20,20,1,1,20,20,20,20,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, -0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,20,20,4,5,1,1,3,4,20,20,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,4,4,4,5,1,1,3,4,4,4,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, @@ -112,18 +112,172 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/src/com/toasted/chuck/ChuckGame.java b/core/src/com/toasted/chuck/ChuckGame.java index 60324e0..c92483f 100644 --- a/core/src/com/toasted/chuck/ChuckGame.java +++ b/core/src/com/toasted/chuck/ChuckGame.java @@ -7,6 +7,7 @@ import java.util.Comparator; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputProcessor; +import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.maps.MapLayer; import com.badlogic.gdx.maps.MapObject; @@ -26,7 +27,9 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{ OrthogonalTiledMapRenderer tmRenderer; ArrayList collisions = new ArrayList(); ArrayList lights = new ArrayList(); - Light playerLight; + + boolean testLightFade = false; + @Override public void create () { Gdx.input.setInputProcessor(this); @@ -45,9 +48,7 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{ }; tiledMap = new TmxMapLoader().load("testmap.tmx"); tmRenderer = new OrthogonalTiledMapRenderer(tiledMap, graphics.getBatch()); - playerLight = new Light(8, 8, 1); - lights.add(playerLight); - lights.add(new Light( 100, 100, .3f)); + lights.add(player.getLight()); MapLayer ml = tiledMap.getLayers().get("collisions"); for(MapObject o: ml.getObjects()){ @@ -56,15 +57,26 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{ collisions.add(r); } - MapLayer lightLayer = tiledMap.getLayers().get("lights"); + MapLayer lightLayer = tiledMap.getLayers().get("staticLights"); Vector2 t = new Vector2(); for(MapObject o: lightLayer.getObjects()){ RectangleMapObject rmo = (RectangleMapObject) o; t = new Rectangle(rmo.getRectangle()).getCenter(t); - lights.add(new Light(t.x, t.y, .3f)); + lights.add(new Light(t.x, t.y, Light.VAL_TORCH)); } - + MapLayer objectLayer = tiledMap.getLayers().get("objectSpawn"); + for(MapObject o: objectLayer.getObjects()){ + RectangleMapObject rmo = (RectangleMapObject) o; + Rectangle r = new Rectangle(rmo.getRectangle()); + t = r.getCenter(t); + +// System.out.println(o.getProperties().get("type")); + if(o.getProperties().get("type").equals("torch")){ + EntityTorch torch = new EntityTorch(t.x - r.getWidth() / 2, t.y - r.getHeight() / 2); + spawnEntity(torch); + } + } } @@ -75,14 +87,17 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{ e.update(Gdx.graphics.getDeltaTime(), entities, collisions); } Collections.sort(entities, zSorter); - playerLight.position.x = player.position.x + 8; - playerLight.position.y = player.position.y + 8; graphics.cam.position.x = player.position.x + 8; graphics.cam.position.y = player.position.y + 8; graphics.cam.update(); + + if(testLightFade){ + graphics.lightValueMultiplier -= Gdx.graphics.getDeltaTime(); + graphics.lightValueMultiplier = Math.max(0, graphics.lightValueMultiplier); + } graphics.prepare(); graphics.passLightsToShader(lights); @@ -98,7 +113,13 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{ e.draw(graphics); } graphics.endSprite(); - System.out.println("FPS:" + Gdx.graphics.getFramesPerSecond()); +// System.out.println("FPS:" + Gdx.graphics.getFramesPerSecond()); + } + public void spawnEntity(Entity e){ + if(e instanceof LightEmitter){ + lights.add(((LightEmitter) e).getLight()); + } + entities.add(e); } @Override @@ -114,6 +135,11 @@ public class ChuckGame extends ApplicationAdapter implements InputProcessor{ @Override public boolean keyUp(int keycode) { player.controller.acceptEvent(keycode, false, player, entities, collisions); + switch(keycode){ + case Keys.F: + testLightFade = true; + break; + } return false; } diff --git a/core/src/com/toasted/chuck/EntityPlayer.java b/core/src/com/toasted/chuck/EntityPlayer.java index fa1b950..475bd95 100644 --- a/core/src/com/toasted/chuck/EntityPlayer.java +++ b/core/src/com/toasted/chuck/EntityPlayer.java @@ -9,7 +9,8 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; -public class EntityPlayer extends Entity{ +public class EntityPlayer extends Entity implements LightEmitter{ + Light illuminator; Texture playerArt = new Texture("player.png"); TextureRegion[] sprites = new TextureRegion[8]; float moveSpeed = 100; @@ -21,6 +22,7 @@ public class EntityPlayer extends Entity{ private ArrayList directionals = new ArrayList();//Don't think I'll use this private boolean[] arrowKeys = new boolean[4]; //UDLR public EntityPlayer(){ + isChuckable = false; for(int i = 0;i < 4;i++){ sprites[i] = new TextureRegion(playerArt, i * 16, 0, 16, 16); @@ -28,6 +30,7 @@ public class EntityPlayer extends Entity{ } lastVelocity = new Vector2(); collision = new Rectangle(0, 0, 16, 16); + illuminator = new Light(position.x + collision.width / 2, position.y + collision.height / 2, Light.VAL_PLAYER); controller = new EntityController(){ public void acceptEvent(int keycode, boolean newState, Entity owner, ArrayList entities, ArrayList collisions) { @@ -171,6 +174,11 @@ public class EntityPlayer extends Entity{ holding.position.x = position.x; holding.position.y = position.y; } + + illuminator.position.x = position.x + collision.width / 2; + illuminator.position.y = position.y + collision.height / 2; + + } @Override @@ -207,4 +215,7 @@ public class EntityPlayer extends Entity{ } return new Vector2(0, -1); } + public Light getLight() { + return illuminator; + } } diff --git a/core/src/com/toasted/chuck/EntityTorch.java b/core/src/com/toasted/chuck/EntityTorch.java new file mode 100644 index 0000000..b67d9bb --- /dev/null +++ b/core/src/com/toasted/chuck/EntityTorch.java @@ -0,0 +1,39 @@ +package com.toasted.chuck; + +import java.util.ArrayList; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.math.Rectangle; + +public class EntityTorch extends EntityBox implements LightEmitter{ + private Light emitter; + private TextureRegion torch = new TextureRegion(new Texture("env.png"),0, 23, 16, 16); + public EntityTorch(float x, float y) { + super(x, y); + emitter = new Light(x + collision.width / 2, y + collision.height / 2, .3f); + } + + @Override + public Light getLight() { + return emitter; + } + + @Override + public void update(float delta, ArrayList entities, ArrayList collisions) { + super.update(delta, entities, collisions); + emitter.position.set(position).add(collision.width / 2, 3 * collision.height / 4); + } + + + public void draw(Graphics g, float dx, float dy){ + if(flyLength >= 0){ + g.getBatch().draw(shadow, position.x, position.y-1); + } + float flyPerc = (Math.max(flyLength, 0) / .5f); + if(flyPerc > 0) + g.getBatch().draw(torch, position.x +dx, position.y + (float)Math.abs(Math.cos((1 - flyPerc) * (Math.PI * 3f / 4f) - (Math.PI / 4f))) * 15 + dy); + else g.getBatch().draw(torch, position.x + dx, position.y + dy); + } + +} diff --git a/core/src/com/toasted/chuck/Graphics.java b/core/src/com/toasted/chuck/Graphics.java index 855d7a8..de20803 100644 --- a/core/src/com/toasted/chuck/Graphics.java +++ b/core/src/com/toasted/chuck/Graphics.java @@ -22,7 +22,7 @@ public class Graphics { final int orthoX = 16 * 28; final int orthoY = 9 * 28; final float orthoScale = orthoX / (float)getWidth(); - + public float lightValueMultiplier = 1f; // for testing fade out ShaderProgram shader = new ShaderProgram(VERTEX, FRAGMENT); public Graphics(){ @@ -77,6 +77,7 @@ public class Graphics { } else { shader.setUniformi("u_actualLights", lights.size()); + shader.setUniformf("u_ambientLight", 0f); int loc = shader.getUniformLocation("u_lightCoord[" + 0 + "]"); int locIn = shader.getUniformLocation("u_lightIntensity[0]"); for(int i = 0;i < lights.size();i++){ @@ -84,7 +85,7 @@ public class Graphics { Vector3 v3 = cam.project(new Vector3(lights.get(i).position.x, lights.get(i).position.y, 0)); Vector2 v = new Vector2(v3.x, v3.y); shader.setUniformf(loc + i, v); - shader.setUniformf(locIn + i, lights.get(i).intensity); + shader.setUniformf(locIn + i, lights.get(i).intensity * lightValueMultiplier); } } } diff --git a/core/src/com/toasted/chuck/Light.java b/core/src/com/toasted/chuck/Light.java index 492d101..7d86609 100644 --- a/core/src/com/toasted/chuck/Light.java +++ b/core/src/com/toasted/chuck/Light.java @@ -3,6 +3,8 @@ package com.toasted.chuck; import com.badlogic.gdx.math.Vector2; public class Light { + public final static float VAL_TORCH = .4f; + public final static float VAL_PLAYER = .9f; private static Vector2 tmp = new Vector2(); Vector2 position; float intensity = 1; diff --git a/core/src/com/toasted/chuck/LightEmitter.java b/core/src/com/toasted/chuck/LightEmitter.java new file mode 100644 index 0000000..2846e37 --- /dev/null +++ b/core/src/com/toasted/chuck/LightEmitter.java @@ -0,0 +1,5 @@ +package com.toasted.chuck; + +public interface LightEmitter { + public Light getLight(); +}