Skip to content

Commit

Permalink
Add sign interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
silas-hw committed Aug 12, 2024
1 parent 49bd811 commit 034af11
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 20 deletions.
19 changes: 19 additions & 0 deletions src/assets/scngame.tiled-project
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@
"wangset",
"project"
]
},
{
"color": "#ffa0a0a4",
"drawFill": true,
"id": 10,
"members": [
{
"name": "DialogID",
"type": "string",
"value": "unset"
}
],
"name": "Sign",
"type": "class",
"useAs": [
"object",
"tile",
"project"
]
}
]
}
2 changes: 1 addition & 1 deletion src/assets/scngame.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"tilesets/testset1.tsx"
],
"project": "scngame.tiled-project",
"property.type": "BitMask",
"property.type": "string",
"recentFiles": [
"tilesets/testset1.tsx",
"tilemaps/testmap1.tmx"
Expand Down
44 changes: 41 additions & 3 deletions src/assets/tilemaps/testmap1.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="8" tileheight="8" infinite="0" nextlayerid="7" nextobjectid="315">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="8" tileheight="8" infinite="0" nextlayerid="8" nextobjectid="323">
<tileset firstgid="1" source="../tilesets/testset1.tsx"/>
<tileset firstgid="101" source="../tilesets/trees.tsx"/>
<layer id="1" name="Tile Layer 1" width="30" height="20">
Expand All @@ -24,6 +24,30 @@
1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,
3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
<objectgroup id="6" name="Object Layer 1">
Expand All @@ -47,8 +71,8 @@
<properties>
<property name="CollisionLayer" type="class" propertytype="BitMask">
<properties>
<property name="0" type="bool" value="false"/>
<property name="1" type="bool" value="true"/>
<property name="0" type="bool" value="true"/>
<property name="1" type="bool" value="false"/>
<property name="2" type="bool" value="false"/>
<property name="3" type="bool" value="false"/>
<property name="4" type="bool" value="false"/>
Expand All @@ -61,5 +85,19 @@
<property name="damage" type="float" value="1"/>
</properties>
</object>
<object id="319" type="Sign" x="152" y="16" width="16" height="16">
<properties>
<property name="DialogID" value="test_dialog_1"/>
</properties>
</object>
<object id="322" type="Wall" x="152" y="16" width="16" height="16">
<properties>
<property name="CollisionLayer" type="class" propertytype="BitMask">
<properties>
<property name="0" type="bool" value="true"/>
</properties>
</property>
</properties>
</object>
</objectgroup>
</map>
Binary file modified src/assets/tilesets/testset1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/assets/tilesets/testset1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
<tile id="40" probability="0.1"/>
<tile id="41" probability="0.1"/>
<tile id="42" probability="0.1"/>
<tile id="60">
<objectgroup draworder="index" id="3">
<object id="2" type="Wall" x="0" y="0" width="8" height="8">
<properties>
<property name="CollisionLayer" type="class" propertytype="BitMask">
<properties>
<property name="0" type="bool" value="true"/>
<property name="1" type="bool" value="true"/>
</properties>
</property>
</properties>
</object>
</objectgroup>
</tile>
<wangsets>
<wangset name="path1" type="corner" tile="-1">
<wangcolor name="dirt" color="#ff0000" tile="-1" probability="1"/>
Expand Down
8 changes: 8 additions & 0 deletions src/core/src/com/mygdx/scngame/entity/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class Player extends Entity {

public Texture texture;

public final int WIDTH = 16;
public final int HEIGHT = 16;

public float speed = 100;

// from center of body, so account for half of width/height
public final float interactDistance = 14f;

public Player(AssetManager assets) {
texture = assets.get("sprites/test.png", Texture.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.mygdx.scngame.entity.player.states;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.dongbat.jbump.*;
import com.mygdx.scngame.SCNGame;
import com.mygdx.scngame.entity.EntityState;
import com.mygdx.scngame.entity.player.Player;
import com.mygdx.scngame.event.GlobalEventBus;
import com.mygdx.scngame.physics.Box;
import com.mygdx.scngame.physics.InteractBox;
import com.mygdx.scngame.settings.Controls;

import java.util.ArrayList;

public class PlayerMoveState implements EntityState<Player> {

private float invisTimer = 0f;
Expand All @@ -21,25 +27,36 @@ public class PlayerMoveState implements EntityState<Player> {
protected Player container;
protected World<Box> world;

private enum Direction {
UP, DOWN, LEFT, RIGHT
}

private Direction facing = Direction.LEFT;


@Override
public EntityState<? super Player> update(float delta) {
int dx = 0;
int dy = 0;

if(container.context.isKeyPressed(Controls.UP.getKeycode())) {
dy++;
facing = Direction.UP;
}

if(container.context.isKeyPressed(Controls.DOWN.getKeycode())) {
dy--;
facing = Direction.DOWN;
}

if(container.context.isKeyPressed(Controls.LEFT.getKeycode())) {
dx--;
facing = Direction.LEFT;
}

if(container.context.isKeyPressed(Controls.RIGHT.getKeycode())) {
dx++;
facing = Direction.RIGHT;
}

container.direction.set(dx, dy);
Expand All @@ -49,18 +66,51 @@ public EntityState<? super Player> update(float delta) {
return new PlayerDashState();
}

if(container.context.isKeyJustPressed(Input.Keys.E)) {
GlobalEventBus.getInstance().startDialog("test_dialog_1");
if(container.context.isKeyJustPressed(Controls.INTERACT.getKeycode())) {
// query world for interact boxes
float y = container.position.y + container.HEIGHT/2;
float x = container.position.x + container.WIDTH/2;
float dirX = x;
float dirY = y;

switch(facing) {
case UP:
dirY += container.interactDistance;
break;

case DOWN:
dirY += -container.interactDistance;
break;

case LEFT:
dirX += -container.interactDistance;
break;

case RIGHT:
dirX += container.interactDistance;
break;

}
ArrayList<Item> arr = new ArrayList<>();

// ray cast from center of body
world.querySegment(x, y, dirX, dirY, InteractBox.INTERACT_FILTER, arr);

if(arr.size() > 0) {
InteractBox interactBox = (InteractBox) arr.get(0).userData;
interactBox.interact();
}
}

container.position.mulAdd(container.direction, 500f*delta);
container.position.mulAdd(container.direction, container.speed*delta);

return null;
}

@Override
public EntityState<? super Player> draw(SpriteBatch batch, ShapeRenderer shape, float alpha) {
batch.draw(container.texture, container.position.x, container.position.y);

return null;
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/src/com/mygdx/scngame/physics/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ private static class globalFilter implements CollisionFilter {

@Override
public Response filter(Item item, Item other) {
if(item == null) return null;
if(other == null) return Response.cross;

// if not a Box, escape
if(!(item.userData instanceof Box) || !(other.userData instanceof Box)) {
return null;
Expand Down
18 changes: 18 additions & 0 deletions src/core/src/com/mygdx/scngame/physics/InteractBox.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package com.mygdx.scngame.physics;

import com.dongbat.jbump.CollisionFilter;
import com.dongbat.jbump.Item;
import com.dongbat.jbump.Response;

public abstract class InteractBox extends Box {
public boolean solid = false;
public abstract void interact();

public static final CollisionFilter INTERACT_FILTER = new globalFilter();

private static class globalFilter implements CollisionFilter {

@Override
public Response filter(Item item, Item other) {
if(item.userData instanceof InteractBox) {
return Response.cross;
}

return null;
}
}
}
29 changes: 16 additions & 13 deletions src/core/src/com/mygdx/scngame/screens/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.mygdx.scngame.physics.Box;
import com.mygdx.scngame.physics.DamageBox;
import com.mygdx.scngame.physics.HitBox;
import com.mygdx.scngame.physics.InteractBox;
import com.mygdx.scngame.scene.Scene;
import com.mygdx.scngame.screens.data.ScreenData;

Expand Down Expand Up @@ -111,17 +112,6 @@ public void show() {
else parseObjectLayer(layer);
}

Box wall = new Box();
wall.solid = true;
wall.setLayer(0, true);

world.add(new Item<>(wall), 150, 150, 150, 150);

DamageBox damage = new DamageBox(5f, DamageBox.DamageType.DEFAULT);
damage.setLayer(1, true);

world.add(new Item<>(damage), -150, -150, 150, 150);

scene.addEntity(player);

scene.setWorld(world);
Expand All @@ -143,7 +133,9 @@ private void parseTileLayer(TiledMapTileLayer layer) {
for(int y = 0 ; y<layer.getHeight(); y++) {

// the x and y of objects within a tile layer or held by a tile are relative to that tile
parseTile(layer.getCell(x, y).getTile(), x * width, y * height);
// also, if a tile is empty then the cell is null!
TiledMapTileLayer.Cell cell = layer.getCell(x, y);
if(cell != null) parseTile(cell.getTile(), x * width, y * height);
}
}
}
Expand Down Expand Up @@ -203,6 +195,17 @@ private void parseMapObject(MapObject obj, float offsetX, float offsetY) {
setBoxCollisionLayers(collisionLayer, newDamage);

world.add(new Item<>(newDamage), x + offsetX, y + offsetY, width, height);
} else if(type.equals("Sign")) {
String dialogID = properties.get("DialogID", String.class);

InteractBox box = new InteractBox() {
@Override
public void interact() {
GlobalEventBus.getInstance().startDialog(dialogID);
}
};

world.add(new Item<>(box), x + offsetX, y + offsetY, width, height);
} else if(type.equals("PathNode")) {
pathNodes.put(obj);
} else if(obj instanceof TiledMapTileMapObject && type.equals("")) {
Expand Down Expand Up @@ -265,7 +268,7 @@ public void render(float delta) {
for(Item item : world.getItems()) {
Rect rec = world.getRect(item);

if(item.userData instanceof Entity) {
if(item.userData instanceof InteractBox) {
shape.setColor(255/256f, 140/256f, 0, 0.4f);
} else if(item.userData instanceof HitBox) {
shape.setColor(36/256f, 122/256f, 227/256f, 0.4f);
Expand Down

0 comments on commit 034af11

Please sign in to comment.