add collectibles to map

add highlighting when hovering collectibles
viewport-stuff
Matthias 2 years ago
parent 796f2bead0
commit 9e6bca542a

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.1" orientation="orthogonal" renderorder="right-down" width="30" height="30" tilewidth="64" tileheight="64" infinite="0" nextlayerid="11" nextobjectid="7">
<map version="1.9" tiledversion="1.9.1" orientation="orthogonal" renderorder="right-down" width="30" height="30" tilewidth="64" tileheight="64" infinite="0" nextlayerid="12" nextobjectid="9">
<properties>
<property name="id" type="int" value="2"/>
</properties>
@ -254,6 +254,19 @@
</properties>
</object>
</objectgroup>
<objectgroup id="11" name="Collectibles">
<object id="7" x="576" y="1408" width="64" height="64">
<properties>
<property name="item" value="compass"/>
</properties>
</object>
<object id="8" x="640" y="1344" width="64" height="64">
<properties>
<property name="item" value="drill"/>
</properties>
<ellipse/>
</object>
</objectgroup>
<objectgroup id="8" name="Spawnpoints">
<object id="1" x="1057" y="1434">
<properties>

@ -4,16 +4,19 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input.Keys
import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
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.math.MathUtils
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3
import com.badlogic.gdx.utils.Json
import com.last.commit.config.GameConfig
import com.last.commit.map.Interactable
import com.last.commit.map.TimeMap
import com.last.commit.stages.InventoryStage
import kotlin.math.floor
@ -30,6 +33,9 @@ class FirstScreen : Screen, InputProcessor {
lateinit var map: TimeMap // = TimeMap("tiled/base.tmx")
val playerTexture = Texture("sprites/characters.png")
val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43))
var shapeRenderer = ShapeRenderer()
val highlightColor = Color(0f, 0f, 1f, 0.5f)
lateinit var inventoryStage: InventoryStage
@ -45,6 +51,7 @@ class FirstScreen : Screen, InputProcessor {
player.addItemToInventory("drill")
inventoryStage = InventoryStage(player.inventory)
shapeRenderer.setAutoShapeType(true);
Gdx.input.setInputProcessor(this)
}
@ -68,6 +75,10 @@ class FirstScreen : Screen, InputProcessor {
val mousePosition: Vector2 = getMousePosition()
player.lookAt(mousePosition)
var interactables = map.getInteractablesAt(
player.getAbsoluteDirection()
)
batch.projectionMatrix = camera.combined
batch.begin()
@ -75,9 +86,31 @@ class FirstScreen : Screen, InputProcessor {
this.player.render(batch)
batch.end()
//TODO: auslagern in sperate Methode
renderInteractables(interactables)
inventoryStage.draw()
}
fun renderInteractables(interactables: List<Interactable>) {
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
shapeRenderer.setProjectionMatrix(this.camera.combined);
shapeRenderer.setColor(highlightColor);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
for (interactable in interactables) {
shapeRenderer.rect(
interactable.getCollider().x,
interactable.getCollider().y,
interactable.getCollider().width,
interactable.getCollider().height
)
}
shapeRenderer.end()
Gdx.gl.glDisable(GL20.GL_BLEND);
}
private fun getMousePosition(): Vector2 {
val unprojectedMousePosition = camera.unproject(Vector3(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(), 0f))
return Vector2(unprojectedMousePosition.x, unprojectedMousePosition.y)
@ -183,6 +216,7 @@ class FirstScreen : Screen, InputProcessor {
override fun dispose() {
// Destroy screen's assets here.
batch.dispose()
shapeRenderer.dispose()
}
override fun keyDown(keycode: Int): Boolean {

@ -89,7 +89,7 @@ class Player(private val textureRegion: TextureRegion) : Collidable {
}
/**
* Returns the direction of the player in World positon
* Returns the direction of the player in World positon considerung the interactionRange
*/
fun getAbsoluteDirection(): Vector2 {
return position.cpy().add(getRelativeDirection())

@ -0,0 +1,22 @@
package com.last.commit.map
import com.badlogic.gdx.math.Rectangle
class Collectible(name: String, x: Float, y: Float, width: Float, height: Float) : Interactable {
val name: String
private val collider: Rectangle
init {
this.name = name
this.collider = Rectangle(x, y, width, height)
}
override fun interact() {
}
override fun getCollider(): Rectangle {
return this.collider
}
}

@ -0,0 +1,9 @@
package com.last.commit.map
import com.badlogic.gdx.math.Rectangle
interface Interactable {
fun getCollider(): Rectangle
fun interact()
}

@ -19,8 +19,9 @@ import com.last.commit.Wall
class TimeMap(fileName: String) {
private val CELL_SIZE = 64
private val walls: Array<Wall> = Array()
val mapLoader:TmxMapLoader = TmxMapLoader()
private val walls = Array<Wall>()
private val collectibles = Array<Collectible>()
val mapLoader: TmxMapLoader = TmxMapLoader()
var mapRenderer: OrthogonalTiledMapRenderer
var map: TiledMap
var gridWidth = 0
@ -38,6 +39,7 @@ class TimeMap(fileName: String) {
mapRenderer = OrthogonalTiledMapRenderer(map)
loadDimensions()
loadWalls()
loadCollectibles()
}
fun teleport(player: Player) {
@ -51,6 +53,7 @@ class TimeMap(fileName: String) {
mapRenderer.map = map
loadDimensions()
loadWalls()
loadCollectibles()
}
} else {
println("Found illegal teleporter. ${teleporter.properties.get("id")}")
@ -113,6 +116,30 @@ class TimeMap(fileName: String) {
}
}
fun loadCollectibles() {
this.collectibles.clear()
val collectiableLayer = map.layers["Collectibles"]
if (collectiableLayer == null) {
println("Could not load collectibles layer. Check map.")
return
}
val collectibleMapObjects = collectiableLayer.objects
for (mapObject in collectibleMapObjects) {
val mapObjectProperties = mapObject.properties
val x = mapObjectProperties.get("x", Float::class.java)
val y = mapObjectProperties.get("y", Float::class.java)
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))
} else {
println("Found non-rectangular map object at ${x}-${y} skipping it")
}
}
println("Loaded ${collectibles.size} collectibles")
}
private fun findDoorByGridPosition(gridX: Int, gridY: Int): Door? {
for (wall in walls) {
if (wall.gridX == gridX && wall.gridY == gridY && wall is Door) {
@ -162,4 +189,14 @@ class TimeMap(fileName: String) {
return false
}
fun getInteractablesAt(absoluteDirection: Vector2): List<Interactable> {
val interactables = ArrayList<Interactable>()
val c = collectibles.filter { it.getCollider().contains(absoluteDirection) }
interactables.addAll(c)
val w = walls.filter { it.getCollider().contains(absoluteDirection) }
// interactables.addAll(w)
return interactables
}
}

@ -1,5 +1,9 @@
package com.last.commit.map
interface Toggleable {
interface Toggleable : Interactable {
fun toggle()
override fun interact() {
toggle()
}
}
Loading…
Cancel
Save