update interaction with doors/items

viewport-stuff
Matthias 2 years ago
parent edfeb7b645
commit d5842be016

@ -252,7 +252,7 @@ class FirstScreen : Screen, InputProcessor {
val playerDirection: Vector2 = player.getAbsoluteDirection()
println("Player interactor is ${playerDirection.x}:${playerDirection.y}")
map.toggleDoorAt(playerDirection.x, playerDirection.y, player.getCollider())
map.interactWith(playerDirection.x, playerDirection.y, player.getCollider())
// TODO Auto-generated method stub
return false
}
@ -261,7 +261,7 @@ class FirstScreen : Screen, InputProcessor {
println("Attempt to toggle door")
val playerDirection: Vector2 = player.getAbsoluteDirection()
println("Player interactor is ${playerDirection.x}:${playerDirection.y}")
map.toggleDoorAt(playerDirection.x, playerDirection.y, player.getCollider())
map.interactWith(playerDirection.x, playerDirection.y, player.getCollider())
}
fun toWorldCoordinates(x: Float, y: Float): Vector2 {

@ -2,7 +2,8 @@ package com.last.commit.map
import com.badlogic.gdx.math.Rectangle
class Collectible(name: String, x: Float, y: Float, width: Float, height: Float) : Interactable {
class Collectible(name: String, x: Float, y: Float, val gridX: Int, val gridY: Int, width: Float, height: Float) :
Interactable {
val name: String
private val collider: Rectangle
@ -12,8 +13,8 @@ class Collectible(name: String, x: Float, y: Float, width: Float, height: Float)
this.collider = Rectangle(x, y, width, height)
}
override fun interact() {
override fun interact(otherCollider: Rectangle) {
println("Interacting with item $name")
}
override fun getCollider(): Rectangle {

@ -7,9 +7,19 @@ import com.last.commit.Wall
class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) :
Wall(gridX, gridY, wallCollider, cell), Interactable {
override fun interact() {
println("Toggling door $this")
isOpen = !isOpen
override fun interact(otherCollider: Rectangle) {
println("interacting with door $this")
if (isClosed) {
isOpen = true
} else if (isOpen) {
if (getCollider().overlaps(otherCollider)) {
// can't close the door cause it is colliding with given collider
} else {
isOpen = false
}
}
println("Door is now open = $isOpen")
}
var isOpen: Boolean

@ -5,5 +5,5 @@ import com.badlogic.gdx.math.Rectangle
interface Interactable {
fun getCollider(): Rectangle
fun interact()
fun interact(otherCollider: Rectangle)
}

@ -127,12 +127,14 @@ class TimeMap(fileName: String) {
for (mapObject in collectibleMapObjects) {
val mapObjectProperties = mapObject.properties
val x = mapObjectProperties.get("x", Float::class.java)
val gridX = Math.round(x / mapTileWidth)
val y = mapObjectProperties.get("y", Float::class.java)
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, width, height))
this.collectibles.add(Collectible(itemName, x, y, gridX, gridY, width, height))
} else {
println("Found non-rectangular map object at ${x}-${y} skipping it")
}
@ -140,30 +142,31 @@ class TimeMap(fileName: String) {
println("Loaded ${collectibles.size} collectibles")
}
private fun findDoorByGridPosition(gridX: Int, gridY: Int): Door? {
private fun findInteractableAtPosition(gridX: Int, gridY: Int): Interactable? {
for (door in doors) {
if (door.gridX == gridX && door.gridY == gridY && door is Door) {
return door
}
}
for (collectible in collectibles) {
if (collectible.gridX == gridX && collectible.gridY == gridY) {
return collectible
}
}
return null
}
fun toggleDoorAt(x: Float, y: Float, blockingCollider: Rectangle?) {
fun interactWith(x: Float, y: Float, blockingCollider: Rectangle) {
val gridX = x.toInt() / CELL_SIZE
val gridY = y.toInt() / CELL_SIZE
println("Toggling door at $gridX:$gridY")
val door: Door = this.findDoorByGridPosition(gridX, gridY) ?: return
if (door.isClosed) {
door.isOpen = true
} else if (door.isOpen) {
if (door.getCollider().overlaps(blockingCollider)) {
// can't close the door cause it is colliding with given collider
} else {
door.isOpen = false
}
}
println("Door is now open = ${door.isOpen}")
println("Interacting with element at $gridX:$gridY")
//if no door is found return
val door: Interactable = this.findInteractableAtPosition(gridX, gridY) ?: return
//else continue
door.interact(blockingCollider)
}

Loading…
Cancel
Save