Skip to content

Commit

Permalink
Add BitMask map object property class and load them in appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
silas-hw committed Aug 12, 2024
1 parent 234ebbd commit 49bd811
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 31 deletions.
76 changes: 62 additions & 14 deletions src/assets/scngame.tiled-project
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,58 @@
"properties": [
],
"propertyTypes": [
{
"color": "#ffa0a0a4",
"drawFill": true,
"id": 9,
"members": [
{
"name": "0",
"type": "bool",
"value": false
},
{
"name": "1",
"type": "bool",
"value": false
},
{
"name": "2",
"type": "bool",
"value": false
},
{
"name": "3",
"type": "bool",
"value": false
},
{
"name": "4",
"type": "bool",
"value": false
},
{
"name": "5",
"type": "bool",
"value": false
},
{
"name": "6",
"type": "bool",
"value": false
},
{
"name": "7",
"type": "bool",
"value": false
}
],
"name": "BitMask",
"type": "class",
"useAs": [
"property"
]
},
{
"id": 5,
"name": "DamageType",
Expand All @@ -28,8 +80,12 @@
"members": [
{
"name": "CollisionLayer",
"type": "string",
"value": ""
"propertyType": "BitMask",
"type": "class",
"value": {
"0": false,
"1": false
}
},
{
"name": "DamageType",
Expand Down Expand Up @@ -87,18 +143,10 @@
"members": [
{
"name": "CollisionLayer",
"type": "string",
"value": ""
},
{
"name": "CollisionMask",
"type": "string",
"value": ""
},
{
"name": "solid",
"type": "bool",
"value": false
"propertyType": "BitMask",
"type": "class",
"value": {
}
}
],
"name": "Wall",
Expand Down
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": "object",
"property.type": "BitMask",
"recentFiles": [
"tilesets/testset1.tsx",
"tilemaps/testmap1.tmx"
Expand Down
39 changes: 37 additions & 2 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="312">
<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">
<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 @@ -26,5 +26,40 @@
3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
</data>
</layer>
<objectgroup id="6" name="Object Layer 1"/>
<objectgroup id="6" name="Object Layer 1">
<object id="312" type="Wall" x="32" y="8" width="32" height="16">
<properties>
<property name="CollisionLayer" type="class" propertytype="BitMask">
<properties>
<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"/>
<property name="5" type="bool" value="false"/>
<property name="6" type="bool" value="false"/>
<property name="7" type="bool" value="false"/>
</properties>
</property>
</properties>
</object>
<object id="314" type="DamageWall" x="152" y="128" width="32" height="32">
<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="2" type="bool" value="false"/>
<property name="3" type="bool" value="false"/>
<property name="4" type="bool" value="false"/>
<property name="5" type="bool" value="false"/>
<property name="6" type="bool" value="false"/>
<property name="7" type="bool" value="false"/>
</properties>
</property>
<property name="DamageType" propertytype="DamageType" value="DEFAULT"/>
<property name="damage" type="float" value="1"/>
</properties>
</object>
</objectgroup>
</map>
4 changes: 1 addition & 3 deletions src/core/src/com/mygdx/scngame/entity/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Player(AssetManager assets) {

health = new HealthComponent(500f);
hurtbox = new HurtBox(health, 16, 32, 5f);
hurtbox.setCollisionMask(1, true);
hurtbox.setCollisionMask(0, true);

this.state = new PlayerMoveState();
this.state.setContainer(this);
Expand Down Expand Up @@ -73,8 +73,6 @@ public void update(float delta) throws IllegalStateException {
}

hurtbox.update(delta, this.position);

System.out.println(health.getHealth());
}

@Override
Expand Down
64 changes: 64 additions & 0 deletions src/core/src/com/mygdx/scngame/map/MyTmxMapLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.mygdx.scngame.map;

import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.XmlReader;

public class MyTmxMapLoader extends TmxMapLoader {

public MyTmxMapLoader(InternalFileHandleResolver internalFileHandleResolver) {
super(internalFileHandleResolver);
}

@Override
protected void loadProperties (final MapProperties properties, XmlReader.Element element) {
if (element == null) return;
if (element.getName().equals("properties")) {
for (XmlReader.Element property : element.getChildrenByName("property")) {
final String name = property.getAttribute("name", null);
String value = property.getAttribute("value", null);
String type = property.getAttribute("type", null);
if (value == null) {
value = property.getText();
}
if (type != null && type.equals("object")) {
// Wait until the end of [loadTiledMap] to fetch the object
try {
// Value should be the id of the object being pointed to
final int id = Integer.parseInt(value);
// Create [Runnable] to fetch object and add it to props
Runnable fetch = new Runnable() {
@Override
public void run () {
MapObject object = idToObject.get(id);
properties.put(name, object);
}
};
// [Runnable] should not run until the end of [loadTiledMap]
runOnEndOfLoadTiled.add(fetch);
} catch (Exception exception) {
throw new GdxRuntimeException(
"Error parsing property [\" + name + \"] of type \"object\" with value: [" + value + "]", exception);
}
} else if (type != null && type.equals("class")) {
// A 'class' property is a property which is itself a set of properties
MapProperties classProperty = new MapProperties();
String className = property.getAttribute("propertytype");

classProperty.put("type", className);

// the actual properties of a 'class' property are stored as a new properties tag
loadProperties(classProperty, property.getChildByName("properties"));
properties.put(name, classProperty);

} else {
Object castValue = castProperty(name, value, type);
properties.put(name, castValue);
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/core/src/com/mygdx/scngame/physics/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
public class Box {

/** A binary value representing what layers of other boxes that this box will collide with */
public int mask = 0b00000000;
private int mask = 0b00000000;

/** a binary value representing the layers on which the box will be collided with by other boxes */
public int layer = 0b00000000;
private int layer = 0b00000000;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Array;
import com.mygdx.scngame.SCNGame;
import com.mygdx.scngame.map.MyTmxMapLoader;
import com.mygdx.scngame.screens.data.ScreenData;
import com.mygdx.scngame.settings.Settings;

Expand Down Expand Up @@ -79,7 +80,7 @@ public void show() {
loadAssets(assetManager, "skin/", skinExtensions, Skin.class);
loadAssets(assetManager, "music/", audioExtensions, Music.class);

assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
assetManager.setLoader(TiledMap.class, new MyTmxMapLoader(new InternalFileHandleResolver()));
loadAssets(assetManager, "tilemaps/", tilemapExtensions, TiledMap.class);

Gdx.app.log(logTag, "All assets set to load!");
Expand Down
35 changes: 27 additions & 8 deletions src/core/src/com/mygdx/scngame/screens/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.MapLayer;
Expand All @@ -22,7 +21,6 @@
import com.dongbat.jbump.Item;
import com.dongbat.jbump.Rect;
import com.dongbat.jbump.World;
import com.mygdx.scngame.SCNGame;
import com.mygdx.scngame.dialog.Dialog;
import com.mygdx.scngame.entity.Entity;
import com.mygdx.scngame.entity.player.Player;
Expand All @@ -35,8 +33,8 @@
import com.mygdx.scngame.physics.HitBox;
import com.mygdx.scngame.scene.Scene;
import com.mygdx.scngame.screens.data.ScreenData;
import com.mygdx.scngame.settings.PrefSettings;
import com.mygdx.scngame.settings.Settings;

import java.util.Iterator;

public class GameScreen implements Screen {

Expand Down Expand Up @@ -122,8 +120,6 @@ public void show() {
DamageBox damage = new DamageBox(5f, DamageBox.DamageType.DEFAULT);
damage.setLayer(1, true);

System.out.println(wall.layer);

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

scene.addEntity(player);
Expand Down Expand Up @@ -190,17 +186,22 @@ private void parseMapObject(MapObject obj, float offsetX, float offsetY) {
if(type.equals("Wall")) {
Box newWall = new Box();
newWall.solid = true;
newWall.layer = Integer.parseInt(properties.get("CollisionLayer", String.class), 2);

MapProperties collisionLayer = obj.getProperties().get("CollisionLayer", MapProperties.class);
setBoxCollisionLayers(collisionLayer, newWall);


world.add(new Item<>(newWall), x + offsetX, y + offsetY, width, height);
} else if(type.equals("DamageWall")) {
String dtype = properties.get("DamageType", String.class);
float damage = properties.get("damage", float.class);
DamageBox newDamage = new DamageBox(damage, DamageBox.DamageType.valueOf(dtype));

newDamage.layer = Integer.parseInt(properties.get("CollisionLayer", String.class), 2);
newDamage.solid = false;

MapProperties collisionLayer = obj.getProperties().get("CollisionLayer", MapProperties.class);
setBoxCollisionLayers(collisionLayer, newDamage);

world.add(new Item<>(newDamage), x + offsetX, y + offsetY, width, height);
} else if(type.equals("PathNode")) {
pathNodes.put(obj);
Expand All @@ -222,6 +223,20 @@ private void parseMapObject(MapObject obj, float offsetX, float offsetY) {
}
}

/**
* Sets the collision layer of a box based on a CollisionLayer map property
*/
private void setBoxCollisionLayers(MapProperties collisionLayer, Box box) {
for (Iterator<String> it = collisionLayer.getKeys(); it.hasNext(); ) {
String key = it.next();

if("type".equals(key)) continue;

boolean set = collisionLayer.get(key, Boolean.class);
box.setLayer(Integer.parseInt(key, 10), set);
}
}

@Override
public void render(float delta) {
Gdx.graphics.setTitle("" + Gdx.graphics.getFramesPerSecond());
Expand Down Expand Up @@ -278,6 +293,10 @@ public void render(float delta) {
if(Gdx.input.isKeyJustPressed(Input.Keys.K)) {
screenData.settings().setDialogScale(screenData.settings().getDialogScale() - 0.1f);
}

if(Gdx.input.isKeyJustPressed(Input.Keys.L)) {
screenData.settings().setMusicVolume(0f);
}
}

@Override
Expand Down

0 comments on commit 49bd811

Please sign in to comment.