Skip to content

Commit

Permalink
add physic class and enumeration to Tiled (including parsing in the g…
Browse files Browse the repository at this point in the history
…ame)
  • Loading branch information
Quillraven committed Mar 22, 2024
1 parent 48a7e5f commit fa9fa67
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 29 deletions.
28 changes: 24 additions & 4 deletions assets/maps/objects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@
<grid orientation="orthogonal" width="1" height="1"/>
<tile id="0">
<properties>
<property name="GameObject" value="FROG"/>
<property name="GameObject" propertytype="GameObject" value="FROG"/>
</properties>
<image width="32" height="32" source="../graphics/object/frog.png"/>
<objectgroup draworder="index" id="2">
<object id="3" x="8" y="10" width="17" height="16"/>
<object id="6" x="8" y="15" width="17" height="17">
<ellipse/>
<object id="3" type="FixtureDef" x="7" y="11" width="18" height="20">
<properties>
<property name="isSensor" type="bool" value="true"/>
<property name="userData" value="hitbox"/>
</properties>
</object>
<object id="7" x="9" y="11">
<polyline points="0,2 0,18"/>
</object>
<object id="8" type="FixtureDef" x="24" y="11">
<polyline points="0,2 0,18"/>
</object>
<object id="13" type="FixtureDef" x="9" y="28" width="15" height="4">
<properties>
<property name="friction" type="float" value="1"/>
<property name="isChain" type="bool" value="true"/>
</properties>
</object>
<object id="14" type="FixtureDef" x="9" y="13">
<properties>
<property name="restitution" type="float" value="0.2"/>
</properties>
<polyline points="0,0 8,-2 15,0"/>
</object>
</objectgroup>
</tile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import com.badlogic.gdx.Input
import com.badlogic.gdx.InputMultiplexer
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.physics.box2d.FixtureDef
import com.badlogic.gdx.physics.box2d.World
import com.badlogic.gdx.utils.ObjectMap
import com.badlogic.gdx.utils.PropertiesUtils
import com.quillraven.github.quillyjumper.audio.AudioService
import com.quillraven.github.quillyjumper.screen.LoadingScreen
import com.quillraven.github.quillyjumper.util.FixtureDefUserData
import ktx.app.KtxGame
import ktx.app.KtxScreen
import ktx.app.clearScreen
Expand Down Expand Up @@ -65,6 +65,6 @@ class Quillyjumper : KtxGame<KtxScreen>() {
private val log = logger<Quillyjumper>()
const val UNIT_SCALE = 1 / 16f // 16 pixels == 1 meter in Box2D
val GRAVITY = vec2(0f, -30f)
val OBJECT_FIXTURES = mutableMapOf<GameObject, List<FixtureDef>>()
val OBJECT_FIXTURES = mutableMapOf<GameObject, List<FixtureDefUserData>>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class SpawnSystem(
fixedRotation = true
}
fixtureDefs.forEach { fixtureDef ->
body.createFixture(fixtureDef)
fixtureDef.shape.dispose()
body.createFixture(fixtureDef.def).userData = fixtureDef.userData
fixtureDef.def.shape.dispose()
}

// spawn entity
Expand Down Expand Up @@ -133,8 +133,8 @@ class SpawnSystem(
fixedRotation = true
}
val fixtureDef = fixtureDefOf(collObj)
body.createFixture(fixtureDef)
fixtureDef.shape.dispose()
body.createFixture(fixtureDef.def).userData = fixtureDef.userData
fixtureDef.def.shape.dispose()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ import com.badlogic.gdx.math.MathUtils.*
import com.badlogic.gdx.physics.box2d.ChainShape
import com.badlogic.gdx.physics.box2d.CircleShape
import com.badlogic.gdx.physics.box2d.FixtureDef
import com.badlogic.gdx.physics.box2d.PolygonShape
import com.quillraven.github.quillyjumper.Quillyjumper.Companion.UNIT_SCALE
import ktx.app.gdxError
import ktx.math.vec2
import ktx.tiled.property
import ktx.tiled.x
import ktx.tiled.y

fun fixtureDefOf(mapObject: MapObject): FixtureDef {
return when (mapObject) {
data class FixtureDefUserData(val def: FixtureDef, val userData: String)

fun fixtureDefOf(mapObject: MapObject): FixtureDefUserData {
val fixtureDef = when (mapObject) {
is RectangleMapObject -> rectangleFixtureDef(mapObject)
is EllipseMapObject -> ellipseFixtureDef(mapObject)
is PolygonMapObject -> polygonFixtureDef(mapObject)
is PolylineMapObject -> polylineFixtureDef(mapObject)
else -> gdxError("Unsupported MapObject $mapObject")
}

fixtureDef.friction = mapObject.property("friction", 0f)
fixtureDef.restitution = mapObject.property("restitution", 0f)
fixtureDef.density = mapObject.property("density", 0f)
fixtureDef.isSensor = mapObject.property("isSensor", false)

return FixtureDefUserData(fixtureDef, mapObject.property("userData", ""))
}

private fun polylineFixtureDef(mapObject: PolylineMapObject): FixtureDef {
Expand Down Expand Up @@ -101,19 +112,32 @@ private fun rectangleFixtureDef(mapObject: RectangleMapObject): FixtureDef {
val (rectX, rectY, rectW, rectH) = mapObject.rectangle
val boxX = rectX * UNIT_SCALE
val boxY = rectY * UNIT_SCALE
val boxW = rectW * UNIT_SCALE
val boxH = rectH * UNIT_SCALE

val vertices = arrayOf(
vec2(boxX, boxY),
vec2(boxX + boxW, boxY),
vec2(boxX + boxW, boxY + boxH),
vec2(boxX, boxY + boxH),
)
if (mapObject.property("isChain", false)) {
val boxW = rectW * UNIT_SCALE
val boxH = rectH * UNIT_SCALE

// create a chain shaped box
val vertices = arrayOf(
vec2(boxX, boxY),
vec2(boxX + boxW, boxY),
vec2(boxX + boxW, boxY + boxH),
vec2(boxX, boxY + boxH),
)

return FixtureDef().apply {
shape = ChainShape().apply {
createLoop(vertices)
}
}
}

// create a box
val boxW = rectW * UNIT_SCALE * 0.5f
val boxH = rectH * UNIT_SCALE * 0.5f
return FixtureDef().apply {
shape = ChainShape().apply {
createLoop(vertices)
shape = PolygonShape().apply {
setAsBox(boxW, boxH, vec2(boxX + boxW, boxY + boxH), 0f)
}
}
}
54 changes: 54 additions & 0 deletions tiled-project.tiled-project
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,59 @@
"properties": [
],
"propertyTypes": [
{
"color": "#ffa0a0a4",
"drawFill": true,
"id": 2,
"members": [
{
"name": "density",
"type": "float",
"value": 0
},
{
"name": "friction",
"type": "float",
"value": 0
},
{
"name": "isChain",
"type": "bool",
"value": false
},
{
"name": "isSensor",
"type": "bool",
"value": false
},
{
"name": "restitution",
"type": "float",
"value": 0
},
{
"name": "userData",
"type": "string",
"value": ""
}
],
"name": "FixtureDef",
"type": "class",
"useAs": [
"property",
"object",
"project"
]
},
{
"id": 1,
"name": "GameObject",
"storageType": "string",
"type": "enum",
"values": [
"FROG"
],
"valuesAsFlags": false
}
]
}
14 changes: 7 additions & 7 deletions tiled-project.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"activeFile": "assets/maps/test.tmx",
"expandedProjectPaths": [
"assets",
"assets/maps",
"lwjgl3/build",
"."
".",
"assets/maps"
],
"fileStates": {
"": {
Expand All @@ -19,8 +19,8 @@
"scale": 3.598125,
"selectedLayer": -1,
"viewCenter": {
"x": 170.64443286433908,
"y": 158.55480284870592
"x": 157.30415146777833,
"y": 158.2768803196109
}
},
"assets/maps/objects.tmx#objects": {
Expand Down Expand Up @@ -49,8 +49,8 @@
"scale": 4,
"selectedLayer": 1,
"viewCenter": {
"x": 146,
"y": 81.875
"x": 161.75,
"y": 67.125
}
}
},
Expand All @@ -68,7 +68,7 @@
"assets/maps/objects.tsx"
],
"project": "tiled-project.tiled-project",
"property.type": "string",
"property.type": "bool",
"recentFiles": [
"assets/maps/objects.tmx",
"assets/maps/objects.tsx",
Expand Down

0 comments on commit fa9fa67

Please sign in to comment.