Skip to content

Commit

Permalink
Added support for box2d lights
Browse files Browse the repository at this point in the history
  • Loading branch information
collinsmith committed May 5, 2019
1 parent 4fc44bb commit 1c2a299
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -84,6 +84,7 @@ project(":tools") {
dependencies {
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.box2dlights:box2dlights:1.4"
}
}

Expand Down
48 changes: 44 additions & 4 deletions tools/src/com/riiablo/map/WallAggregatorTool.java
Expand Up @@ -20,8 +20,11 @@
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Filter;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.riiablo.COFs;
Expand Down Expand Up @@ -49,6 +52,9 @@
import com.riiablo.loader.TXTLoader;
import com.riiablo.mpq.MPQFileHandleResolver;

import box2dLight.PointLight;
import box2dLight.RayHandler;

public class WallAggregatorTool extends ApplicationAdapter {
private static final String TAG = "WallAggregatorTool";

Expand All @@ -70,6 +76,8 @@ public static void main(String[] args) {
Box2DDebugRenderer box2dDebug;
Map map;
Body playerBody;
RayHandler rayHandler;
PointLight light;

WallAggregatorTool(String home) {
this.home = new FileHandle(home);
Expand Down Expand Up @@ -121,6 +129,7 @@ public void create() {

GridPoint2 origin = map.find(Map.ID.TOWN_ENTRY_1);
camera.translate(origin.x / 2, -origin.y);
camera.zoom = 0.05f;

BodyDef playerDef = new BodyDef();
playerDef.type = BodyDef.BodyType.DynamicBody;
Expand All @@ -135,20 +144,47 @@ public void create() {

playerShape.dispose();

rayHandler = new RayHandler(world);
rayHandler.setAmbientLight(0.5f);
rayHandler.setShadows(true);

light = new PointLight(rayHandler, 256);
light.attachToBody(playerBody);
light.setDistance(32);
light.setSoft(false);
light.setSoftnessLength(0);
light.setColor(1, 1, 1, 0.5f);
light.setContactFilter(
(short) DT1.Tile.FLAG_BLOCK_LIGHT_LOS,
(short) 0,
(short) (DT1.Tile.FLAG_BLOCK_LIGHT_LOS));

IntMap<Filter> filters = new IntMap<>();

int tx = 70;
int ty = 0;
for (int y = 0; y < 280; y++) {
for (int x = 0; x < 200; x++) {
if (map.flags(tx + x, ty + y) != 0) {
int flags = map.flags(tx + x, ty + y);
if (flags != 0) {
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.StaticBody;
def.position.set(x, -y);

PolygonShape shape = new PolygonShape();
shape.setAsBox(0.5f, 0.5f);

Filter filter = filters.get(flags);
if (filter == null) {
filters.put(flags, filter = new Filter());
filter.categoryBits = 0xFF;
filter.maskBits = (short) flags;
filter.groupIndex = 0;
}

Body body = world.createBody(def);
body.createFixture(shape, 0);
Fixture f = body.createFixture(shape, 0);
f.setFilterData(filter);

shape.dispose();
}
Expand All @@ -161,11 +197,11 @@ public boolean scrolled(int amount) {
final float AMOUNT = 0.05f;
switch (amount) {
case -1:
camera.zoom = MathUtils.clamp(camera.zoom - AMOUNT, 0.01f, 2);
camera.zoom = MathUtils.clamp(camera.zoom - AMOUNT, 0.05f, 2);
camera.update();
break;
case 1:
camera.zoom = MathUtils.clamp(camera.zoom + AMOUNT, 0.01f, 2);
camera.zoom = MathUtils.clamp(camera.zoom + AMOUNT, 0.05f, 2);
camera.update();
break;
}
Expand Down Expand Up @@ -215,12 +251,16 @@ public void render() {
camera.position.set(playerBody.getPosition(), 0);
camera.update();
box2dDebug.render(world, camera.combined);

rayHandler.setCombinedMatrix(camera);
rayHandler.updateAndRender();
}

@Override
public void dispose() {
map.dispose();
world.dispose();
rayHandler.dispose();
Riiablo.assets.dispose();
Riiablo.shapes.dispose();
Riiablo.textures.dispose();
Expand Down

0 comments on commit 1c2a299

Please sign in to comment.