Move grid and pixel position state to separate class

viewport-stuff
trivernis 1 year ago
parent 5245aaea96
commit d23f0bdab0
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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
}
}
}

@ -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()))
}

@ -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)
}

Loading…
Cancel
Save