Throwable torches

master
Edward Peterson 9 years ago
parent 6b98c2f0e3
commit 24e619452d

@ -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)));

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-up" width="50" height="50" tilewidth="16" tileheight="16" nextobjectid="78">
<map version="1.0" orientation="orthogonal" renderorder="right-up" width="50" height="50" tilewidth="16" tileheight="16" nextobjectid="149">
<tileset firstgid="1" name="env" tilewidth="16" tileheight="16" tilecount="64" columns="8">
<image source="env.png" width="128" height="128"/>
</tileset>
@ -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 @@
<object id="63" x="608" y="352" width="192" height="40"/>
<object id="65" x="608" y="240" width="48" height="112"/>
</objectgroup>
<objectgroup name="lights">
<object id="66" x="384" y="624"/>
<object id="67" x="576" y="624"/>
<object id="68" x="576" y="752"/>
<object id="69" x="384" y="752"/>
<object id="70" x="512" y="544"/>
<object id="71" x="512" y="416"/>
<object id="72" x="784" y="416"/>
<object id="73" x="784" y="768"/>
<object id="74" x="640" y="768"/>
<object id="75" x="384" y="336"/>
<object id="76" x="352" y="304"/>
<object id="77" x="416" y="304"/>
<objectgroup name="staticLights"/>
<objectgroup name="objectSpawn">
<object id="112" x="272" y="745" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="117" x="16" y="745" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="118" x="32" y="569" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="119" x="112" y="569" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="120" x="176" y="569" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="121" x="96" y="377" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="122" x="256" y="569" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="123" x="176" y="377" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="124" x="128" y="297" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="125" x="96" y="169" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="126" x="80" y="9" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="127" x="208" y="9" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="128" x="144" y="9" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="129" x="320" y="9" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="130" x="384" y="9" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="131" x="400" y="137" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="132" x="320" y="137" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="133" x="528" y="57" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="134" x="448" y="57" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="135" x="176" y="169" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="136" x="352" y="281" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="137" x="400" y="281" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="138" x="448" y="297" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="139" x="528" y="297" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="140" x="336" y="409" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="141" x="416" y="409" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="142" x="544" y="377" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="143" x="624" y="377" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="144" x="720" y="377" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="145" x="768" y="377" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="146" x="560" y="585" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="147" x="480" y="585" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
<object id="148" x="400" y="585" width="16" height="16">
<properties>
<property name="type" value="torch"/>
</properties>
</object>
</objectgroup>
</map>

@ -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<Rectangle> collisions = new ArrayList<Rectangle>();
ArrayList<Light> lights = new ArrayList<Light>();
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;
}

@ -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<Integer> directionals = new ArrayList<Integer>();//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<Entity> entities, ArrayList<Rectangle> 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;
}
}

@ -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<Entity> entities, ArrayList<Rectangle> 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);
}
}

@ -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);
}
}
}

@ -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;

@ -0,0 +1,5 @@
package com.toasted.chuck;
public interface LightEmitter {
public Light getLight();
}
Loading…
Cancel
Save