From d23f0bdab03f4ef33fed2c5603b086a2ddba3cb7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 19 Nov 2022 13:23:15 +0100 Subject: [PATCH] Move grid and pixel position state to separate class --- .../kotlin/com/last/commit/map/Collectible.kt | 13 ++++++++---- .../kotlin/com/last/commit/map/Position.kt | 21 +++++++++++++++++++ .../kotlin/com/last/commit/map/TimeMap.kt | 12 ++++++----- 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 core/src/main/kotlin/com/last/commit/map/Position.kt diff --git a/core/src/main/kotlin/com/last/commit/map/Collectible.kt b/core/src/main/kotlin/com/last/commit/map/Collectible.kt index 633c627..7da3d86 100644 --- a/core/src/main/kotlin/com/last/commit/map/Collectible.kt +++ b/core/src/main/kotlin/com/last/commit/map/Collectible.kt @@ -1,16 +1,21 @@ package com.last.commit.map import com.badlogic.gdx.math.Rectangle +import Position -class Collectible(name: String, x: Float, y: Float, val gridX: Int, val gridY: Int, width: Float, height: Float) : - Interactable { +class Collectible( + name: String, + val pos: Position, + width: Float, + height: Float +) : Interactable { val name: String private val collider: Rectangle init { this.name = name - this.collider = Rectangle(x, y, width, height) + this.collider = Rectangle(pos.x, pos.y, width, height) } override fun interact(otherCollider: Rectangle) { @@ -20,4 +25,4 @@ class Collectible(name: String, x: Float, y: Float, val gridX: Int, val gridY: I override fun getCollider(): Rectangle { return this.collider } -} \ No newline at end of file +} diff --git a/core/src/main/kotlin/com/last/commit/map/Position.kt b/core/src/main/kotlin/com/last/commit/map/Position.kt new file mode 100644 index 0000000..7dd5014 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/map/Position.kt @@ -0,0 +1,21 @@ +import com.badlogic.gdx.math.Vector2 + +public data class Position( + public val pos: Vector2, + public val gridPos: Vector2, +) { + + public val x: Float + get() = pos.x + + public val y: Float + get() = pos.y + + public val gridX: Int + get() = gridPos.x.toInt() + + public val gridY: Int + get() = gridPos.y.toInt() + + constructor(x: Float, y: Float, gridX: Int, gridY: Int): this(Vector2(x, y), Vector2(gridX.toFloat(), gridY.toFloat())) +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/map/TimeMap.kt b/core/src/main/kotlin/com/last/commit/map/TimeMap.kt index fae83c6..4383123 100644 --- a/core/src/main/kotlin/com/last/commit/map/TimeMap.kt +++ b/core/src/main/kotlin/com/last/commit/map/TimeMap.kt @@ -14,6 +14,7 @@ import com.badlogic.gdx.utils.Array import com.last.commit.Collidable import com.last.commit.Player import com.last.commit.Wall +import Position class TimeMap(fileName: String) { @@ -124,6 +125,7 @@ class TimeMap(fileName: String) { return } val collectibleMapObjects = collectiableLayer.objects + for (mapObject in collectibleMapObjects) { val mapObjectProperties = mapObject.properties val x = mapObjectProperties.get("x", Float::class.java) @@ -132,9 +134,10 @@ class TimeMap(fileName: String) { val gridY = Math.round(y / mapTileHeight) val width = mapObjectProperties.get("width", Float::class.java) val height = mapObjectProperties.get("height", Float::class.java) + if (mapObject is RectangleMapObject) { val itemName = mapObjectProperties.get("item", String::class.java) - this.collectibles.add(Collectible(itemName, x, y, gridX, gridY, width, height)) + this.collectibles.add(Collectible(itemName, Position(x, y, gridX, gridY), width, height)) } else { println("Found non-rectangular map object at ${x}-${y} skipping it") } @@ -149,7 +152,7 @@ class TimeMap(fileName: String) { } } for (collectible in collectibles) { - if (collectible.gridX == gridX && collectible.gridY == gridY) { + if (collectible.pos.gridX == gridX && collectible.pos.gridY == gridY) { return collectible } } @@ -162,11 +165,10 @@ class TimeMap(fileName: String) { println("Interacting with element at $gridX:$gridY") //if no door is found return - val door: Interactable = this.findInteractableAtPosition(gridX, gridY) ?: return + val interactable: Interactable = this.findInteractableAtPosition(gridX, gridY) ?: return //else continue - door.interact(blockingCollider) - + interactable.interact(blockingCollider) }