Add playable example.

add wall collision
add interactable doors
viewport-stuff
Matthias 2 years ago
parent 673bc06cc1
commit 911b636530

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<TextureAtlas imagePath="sprites.png">
<!--
Created with ShoeBox
http://renderhjs.net/shoebox/
-->
<SubTexture name="hitman1_gun.png" x="210" y="440" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="hitman1_hold.png" x="332" y="308" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="hitman1_machine.png" x="160" y="440" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="hitman1_reload.png" x="213" y="220" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="hitman1_silencer.png" x="55" y="440" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="hitman1_stand.png" x="337" y="0" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="hitman2_gun.png" x="111" y="88" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="hitman2_hold.png" x="332" y="352" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="hitman2_machine.png" x="110" y="440" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="hitman2_reload.png" x="255" y="308" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="hitman2_silencer.png" x="0" y="396" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="hitman2_stand.png" x="368" y="352" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="manBlue_gun.png" x="111" y="0" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manBlue_hold.png" x="332" y="176" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="manBlue_machine.png" x="111" y="44" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manBlue_reload.png" x="253" y="220" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="manBlue_silencer.png" x="55" y="396" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="manBlue_stand.png" x="369" y="264" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="manBrown_gun.png" x="111" y="132" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manBrown_hold.png" x="336" y="88" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="manBrown_machine.png" x="161" y="132" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manBrown_reload.png" x="260" y="440" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="manBrown_silencer.png" x="55" y="352" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="manBrown_stand.png" x="368" y="132" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="manOld_gun.png" x="162" y="352" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manOld_hold.png" x="333" y="396" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="manOld_machine.png" x="161" y="0" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manOld_reload.png" x="255" y="264" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="manOld_silencer.png" x="0" y="308" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="manOld_stand.png" x="368" y="308" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="manRed_gun.png" x="161" y="88" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manRed_hold.png" x="336" y="44" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="manRed_machine.png" x="163" y="176" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="manRed_reload.png" x="261" y="0" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="manRed_silencer.png" x="0" y="264" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="manRed_stand.png" x="369" y="396" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="robot1_gun.png" x="211" y="44" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="robot1_hold.png" x="332" y="220" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="robot1_machine.png" x="163" y="220" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="robot1_reload.png" x="255" y="352" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="robot1_silencer.png" x="0" y="440" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="robot1_stand.png" x="368" y="176" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="robot2_gun.png" x="162" y="264" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="robot2_hold.png" x="333" y="264" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="robot2_machine.png" x="161" y="44" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="robot2_reload.png" x="253" y="176" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="robot2_silencer.png" x="0" y="352" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="robot2_stand.png" x="337" y="440" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="soldier1_gun.png" x="110" y="220" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="soldier1_hold.png" x="261" y="44" width="38" height="43" frameX="-0" frameY="-0" frameWidth="38" frameHeight="43"/>
<SubTexture name="soldier1_machine.png" x="58" y="132" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="soldier1_reload.png" x="212" y="264" width="42" height="43" frameX="-0" frameY="-0" frameWidth="42" frameHeight="43"/>
<SubTexture name="soldier1_silencer.png" x="0" y="88" width="57" height="43" frameX="-0" frameY="-0" frameWidth="57" frameHeight="43"/>
<SubTexture name="soldier1_stand.png" x="300" y="440" width="36" height="43" frameX="-0" frameY="-0" frameWidth="36" frameHeight="43"/>
<SubTexture name="soldier2_gun.png" x="58" y="0" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="soldier2_hold.png" x="293" y="176" width="38" height="43" frameX="-0" frameY="-0" frameWidth="38" frameHeight="43"/>
<SubTexture name="soldier2_machine.png" x="57" y="220" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="soldier2_reload.png" x="211" y="132" width="42" height="43" frameX="-0" frameY="-0" frameWidth="42" frameHeight="43"/>
<SubTexture name="soldier2_silencer.png" x="0" y="132" width="57" height="43" frameX="-0" frameY="-0" frameWidth="57" frameHeight="43"/>
<SubTexture name="soldier2_stand.png" x="296" y="396" width="36" height="43" frameX="-0" frameY="-0" frameWidth="36" frameHeight="43"/>
<SubTexture name="survivor1_gun.png" x="110" y="352" width="51" height="43" frameX="-0" frameY="-0" frameWidth="51" frameHeight="43"/>
<SubTexture name="survivor1_hold.png" x="295" y="264" width="37" height="43" frameX="-0" frameY="-0" frameWidth="37" frameHeight="43"/>
<SubTexture name="survivor1_machine.png" x="110" y="396" width="51" height="43" frameX="-0" frameY="-0" frameWidth="51" frameHeight="43"/>
<SubTexture name="survivor1_reload.png" x="254" y="396" width="41" height="43" frameX="-0" frameY="-0" frameWidth="41" frameHeight="43"/>
<SubTexture name="survivor1_silencer.png" x="0" y="220" width="56" height="43" frameX="-0" frameY="-0" frameWidth="56" frameHeight="43"/>
<SubTexture name="survivor1_stand.png" x="301" y="0" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="survivor2_gun.png" x="110" y="308" width="51" height="43" frameX="-0" frameY="-0" frameWidth="51" frameHeight="43"/>
<SubTexture name="survivor2_hold.png" x="294" y="132" width="37" height="43" frameX="-0" frameY="-0" frameWidth="37" frameHeight="43"/>
<SubTexture name="survivor2_machine.png" x="110" y="264" width="51" height="43" frameX="-0" frameY="-0" frameWidth="51" frameHeight="43"/>
<SubTexture name="survivor2_reload.png" x="212" y="396" width="41" height="43" frameX="-0" frameY="-0" frameWidth="41" frameHeight="43"/>
<SubTexture name="survivor2_silencer.png" x="0" y="176" width="56" height="43" frameX="-0" frameY="-0" frameWidth="56" frameHeight="43"/>
<SubTexture name="survivor2_stand.png" x="300" y="44" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="weapon_gun.png" x="26" y="495" width="19" height="10" frameX="-0" frameY="-0" frameWidth="19" frameHeight="10"/>
<SubTexture name="weapon_machine.png" x="0" y="484" width="33" height="10" frameX="-0" frameY="-0" frameWidth="33" frameHeight="10"/>
<SubTexture name="weapon_silencer.png" x="0" y="495" width="25" height="10" frameX="-0" frameY="-0" frameWidth="25" frameHeight="10"/>
<SubTexture name="womanGreen_gun.png" x="110" y="176" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="womanGreen_hold.png" x="261" y="88" width="38" height="43" frameX="-0" frameY="-0" frameWidth="38" frameHeight="43"/>
<SubTexture name="womanGreen_machine.png" x="58" y="88" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="womanGreen_reload.png" x="212" y="352" width="42" height="43" frameX="-0" frameY="-0" frameWidth="42" frameHeight="43"/>
<SubTexture name="womanGreen_silencer.png" x="0" y="44" width="57" height="43" frameX="-0" frameY="-0" frameWidth="57" frameHeight="43"/>
<SubTexture name="womanGreen_stand.png" x="295" y="308" width="36" height="43" frameX="-0" frameY="-0" frameWidth="36" frameHeight="43"/>
<SubTexture name="womanOld_gun.png" x="58" y="44" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="womanOld_hold.png" x="293" y="220" width="38" height="43" frameX="-0" frameY="-0" frameWidth="38" frameHeight="43"/>
<SubTexture name="womanOld_machine.png" x="57" y="176" width="52" height="43" frameX="-0" frameY="-0" frameWidth="52" frameHeight="43"/>
<SubTexture name="womanOld_reload.png" x="212" y="308" width="42" height="43" frameX="-0" frameY="-0" frameWidth="42" frameHeight="43"/>
<SubTexture name="womanOld_silencer.png" x="0" y="0" width="57" height="43" frameX="-0" frameY="-0" frameWidth="57" frameHeight="43"/>
<SubTexture name="womanOld_stand.png" x="295" y="352" width="36" height="43" frameX="-0" frameY="-0" frameWidth="36" frameHeight="43"/>
<SubTexture name="zoimbie1_gun.png" x="211" y="88" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="zoimbie1_hold.png" x="300" y="88" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="zoimbie1_machine.png" x="211" y="0" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="zoimbie1_reload.png" x="254" y="132" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="zoimbie1_silencer.png" x="55" y="264" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="zoimbie1_stand.png" x="368" y="220" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
<SubTexture name="zombie2_gun.png" x="162" y="396" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="zombie2_hold.png" x="332" y="132" width="35" height="43" frameX="-0" frameY="-0" frameWidth="35" frameHeight="43"/>
<SubTexture name="zombie2_machine.png" x="162" y="308" width="49" height="43" frameX="-0" frameY="-0" frameWidth="49" frameHeight="43"/>
<SubTexture name="zombie2_reload.png" x="213" y="176" width="39" height="43" frameX="-0" frameY="-0" frameWidth="39" frameHeight="43"/>
<SubTexture name="zombie2_silencer.png" x="55" y="308" width="54" height="43" frameX="-0" frameY="-0" frameWidth="54" frameHeight="43"/>
<SubTexture name="zombie2_stand.png" x="371" y="0" width="33" height="43" frameX="-0" frameY="-0" frameWidth="33" frameHeight="43"/>
</TextureAtlas>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.9" tiledversion="1.9.1" name="demo-tileset" tilewidth="16" tileheight="16" spacing="1" tilecount="1036" columns="37">
<image source="../../../libgdx/assets/kenney/roguelike-modern-city-pack/Spritesheet/roguelikeCity_magenta.png" width="628" height="475"/>
</tileset>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.9" tiledversion="1.9.1" name="roguelike" tilewidth="16" tileheight="16" spacing="1" tilecount="1767" columns="57">
<image source="D:/kenney/Kenney Game Assets/Art/Roguelike pack (1700 assets)/Spritesheet/roguelikeSheet_transparent.png" width="968" height="526"/>
</tileset>

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 KiB

File diff suppressed because it is too large Load Diff

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

@ -1,22 +1,145 @@
package com.last.commit
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.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.math.MathUtils
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3
import com.last.commit.map.TimeMap
import kotlin.math.floor
/** First screen of the application. Displayed after the application is created. */
class FirstScreen : Screen {
val state = ColorState();
class FirstScreen : Screen, InputProcessor {
private var delta = 0f
private var isColliding = false
val state = ColorState()
val batch = SpriteBatch()
val camera = OrthographicCamera(800f, 600f)
val map:TimeMap = TimeMap("tiled/base.tmx")
val playerTexture = Texture("sprites/characters.png")
val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43))
override fun show() {
// Prepare your screen here.
this.spawnPlayer()
this.updateCamera()
Gdx.input.setInputProcessor(this)
}
override fun render(delta: Float) {
state.step((delta * 1000).toLong())
this.delta = delta
// state.step((delta * 1000).toLong())
// Draw your screen here. "delta" is the time since last render in seconds.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f)
handleInput()
handleMapBorderCollision()
val mousePosition: Vector2 = getMousePosition()
player.lookAt(mousePosition)
batch.projectionMatrix = camera.combined
batch.begin()
this.map.render(batch, camera, delta)
this.player.render(batch)
batch.end()
}
private fun getMousePosition(): Vector2 {
val unprojectedMousePosition = camera.unproject(Vector3(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(), 0f))
return Vector2(unprojectedMousePosition.x, unprojectedMousePosition.y)
}
private fun handleMapBorderCollision() {
val mapWidth: Int = map.width
val mapHeight: Int = map.height
val playerSize: Float = player.getSize()
val playerX: Float = MathUtils.clamp(this.player.getX(), 0f, mapWidth - playerSize)
val playerY: Float = MathUtils.clamp(this.player.getY(), 0f, mapHeight - playerSize)
this.player.setPosition(playerX, playerY)
}
private fun handleInput() {
val horizontalMovement = Vector2()
if (Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)) {
horizontalMovement.sub(Vector2.X)
}
if (Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT)) {
horizontalMovement.add(Vector2.X)
}
this.player.move(horizontalMovement, delta)
checkCollision()
if (this.isColliding) {
horizontalMovement.rotateDeg(180f)
this.player.move(horizontalMovement, delta)
}
val verticalMovement = Vector2()
if (Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP)) {
verticalMovement.add(Vector2.Y)
}
if (Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN)) {
verticalMovement.sub(Vector2.Y)
}
this.player.move(verticalMovement, delta)
checkCollision()
if (this.isColliding) {
verticalMovement.rotateDeg(180f)
this.player.move(verticalMovement, delta)
}
val hasMoved = !horizontalMovement.isZero || !verticalMovement.isZero
if (hasMoved) {
updateCamera()
}
}
private fun spawnPlayer() {
val playerSpawn: Vector2 = map.getPlayerSpawn()
this.player.position = playerSpawn
}
private fun checkCollision() {
this.isColliding = map.isCollidingWithWall(player)
}
fun updateCamera() {
val cX: Float
val cY: Float
val halfScreenWidth = camera.viewportWidth / 2
val halfScreenHeight = camera.viewportHeight / 2
val playerXPosition: Float = this.player.getX()
val playerYPosition: Float = this.player.getY()
val mapWidth: Int = map.width
val mapHeight: Int = map.height
cX = if (playerXPosition < halfScreenWidth) {
halfScreenWidth
} else if (playerXPosition > mapWidth - halfScreenWidth) {
mapWidth - halfScreenWidth
} else {
playerXPosition
}
cY = if (playerYPosition < halfScreenHeight) {
halfScreenHeight
} else if (playerYPosition > mapHeight - halfScreenHeight) {
mapHeight - halfScreenHeight
} else {
playerYPosition
}
camera.position[cX, cY] = 0f
camera.update()
}
override fun resize(width: Int, height: Int) {
@ -37,5 +160,72 @@ class FirstScreen : Screen {
override fun dispose() {
// Destroy screen's assets here.
batch.dispose()
}
override fun keyDown(keycode: Int): Boolean {
// TODO: Auto-generated method stub
return false
}
override fun keyUp(keycode: Int): Boolean {
if (keycode == Keys.ESCAPE) {
Gdx.app.exit()
}
return false
}
override fun keyTyped(character: Char): Boolean {
if (character == 'e') {
openDoor()
}
// TODO Auto-generated method stub
return false
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
val mouseCoordinates: Vector2 = toWorldCoordinates(screenX.toFloat(), screenY.toFloat())
println("Mouse World coordinates is ${mouseCoordinates.x}:${mouseCoordinates.y}")
val playerDirection: Vector2 = player.getAbsoluteDirection()
println("Player interactor is ${playerDirection.x}:${playerDirection.y}")
map.toggleDoorAt(playerDirection.x, playerDirection.y, player.getCollider())
// TODO Auto-generated method stub
return false
}
fun openDoor() {
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())
}
fun toWorldCoordinates(x: Float, y: Float): Vector2 {
val mouseInWorldPosition = camera.unproject(Vector3(x, y, 0f))
return Vector2(
floor(mouseInWorldPosition.x.toDouble() / this.map.getTileWidth()).toFloat(),
floor(mouseInWorldPosition.y.toDouble() / this.map.getTileHeight()).toFloat()
)
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
//TODO: ("Not yet implemented")
return false
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
//TODO: ("Not yet implemented")
return false
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
//TODO: "Not yet implemented"
return false
}
override fun scrolled(amountX: Float, amountY: Float): Boolean {
//TODO: Not yet implemented
return false
}
}

@ -0,0 +1,116 @@
package com.last.commit
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2
class Player(private val textureRegion: TextureRegion) : Collidable {
private var collider: Rectangle = Rectangle(0f, 0f, 0f, 0f)
var position: Vector2 = Vector2.Zero
private var direction = Vector2.Zero
private val movementSpeed = 200f
private val interactionRange = 60f
init {
val size = Math.max(textureRegion.regionWidth, textureRegion.regionHeight).toFloat()
collider = Rectangle(0f, 0f, size, size)
position = Vector2()
}
fun getX(): Float {
return position.x
}
fun getY(): Float {
return position.y
}
fun move(v: Vector2, delta: Float) {
updatePosition(v, delta)
updateCollider()
}
private fun updatePosition(v: Vector2, delta: Float) {
position.add(v.setLength(movementSpeed * delta))
}
private fun updateCollider() {
this.collider.x = position.x
this.collider.y = position.y
}
override fun getCollider(): Rectangle {
return collider
}
fun setPosition(x: Float, y: Float) {
position[x] = y
}
fun getWidth(): Float {
return collider.getWidth()
}
fun getHeight(): Float {
return collider.getHeight()
}
fun getSize(): Float {
return Math.max(getWidth(), getHeight())
}
fun lookAt(position: Vector2) {
calculateRotationTo(position)
}
private fun calculateRotationTo(position: Vector2) {
val playerPosition = this.position.cpy()
playerPosition.add(collider.width / 2f, collider.height / 2f)
direction = position.cpy()
direction.sub(playerPosition)
direction.setLength(interactionRange)
}
/**
* Returns the direction relative to the players center.
*/
fun getRelativeDirection(): Vector2 {
return direction.cpy().add(getHalfPlayerWidth(), getHalfPlayerHeight())
}
/**
* Returns the direction of the player in World positon
*/
fun getAbsoluteDirection(): Vector2 {
return position.cpy().add(getRelativeDirection())
}
private fun getHalfPlayerWidth(): Float {
return getWidth() / 2
}
private fun getHalfPlayerHeight(): Float {
return getHeight() / 2
}
fun getRotation(): Float {
return direction.angleDeg()
}
fun render(batch: SpriteBatch) {
val halfPlayerWidth: Float = getHalfPlayerWidth() // TODO maybe use collider
// dimensions
val halfPlayerHeight: Float = getHalfPlayerHeight() // TODO maybe use collider
// dimensions
batch.draw(
textureRegion, getX(), getY(),
halfPlayerWidth, halfPlayerHeight, getWidth(),
getHeight(), 1f, 1f,
getRotation()
)
}
}

@ -0,0 +1,34 @@
package com.last.commit
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell
import com.badlogic.gdx.math.Rectangle
open class Wall(var gridX: Int, var gridY: Int, wallCollider: Rectangle, cell: Cell) : Collidable {
var wallCollider: Rectangle
var cell: Cell
init {
this.wallCollider = wallCollider
this.cell = cell
}
override fun getCollider(): Rectangle {
return wallCollider
}
fun collidesWidth(collidable: Collidable): Boolean {
return if (isCollidable) {
wallCollider.overlaps(collidable.getCollider())
} else false
}
open val isCollidable: Boolean
get() = true
override fun toString(): String {
return java.lang.String.format(
"Wall: %f:%f - %f:%f", wallCollider.x, wallCollider.y, wallCollider.width,
wallCollider.height
)
}
}

@ -0,0 +1,39 @@
package com.last.commit.map
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell
import com.badlogic.gdx.math.Rectangle
import com.last.commit.Wall
class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) :
Wall(gridX, gridY, wallCollider, cell), Toggleable {
override fun toggle() {
println("Toggling door$this")
isOpen = !isOpen
}
var isOpen: Boolean
get() = !isClosed
set(isOpen) {
val currentRotation: Int = cell.getRotation()
if (isOpen) {
cell.setRotation(currentRotation + 1)
} else {
cell.setRotation(currentRotation - 1)
}
}
val isClosed: Boolean
get() {
val currentRotation: Int = cell.getRotation()
return currentRotation == 0
}
override val isCollidable: Boolean
get() = !isOpen
override fun toString(): String {
return String.format(
"Door: %f:%f - %f:%f (isOpen: %b)", wallCollider.x, wallCollider.y, wallCollider.width,
wallCollider.height, isOpen
)
}
}

@ -0,0 +1,144 @@
package com.last.commit.map
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer
import com.badlogic.gdx.maps.tiled.TmxMapLoader
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.utils.Array
import com.last.commit.Collidable
import com.last.commit.Wall
class TimeMap(fileName: String) {
private val CELL_SIZE = 64
private val walls: Array<Wall> = Array()
val mapLoader:TmxMapLoader = TmxMapLoader()
var mapRenderer: OrthogonalTiledMapRenderer
val map: TiledMap
var gridWidth = 0
var gridHeight = 0
var width = 0
var height = 0
var mapTileWidth = 0
var mapTileHeight = 0
var halfMapTileWidth = 0f
var halfMapTileHeight = 0f
init {
map = mapLoader.load(fileName)
mapRenderer = OrthogonalTiledMapRenderer(map)
loadDimensions()
loadWalls()
}
fun getPlayerSpawn(): Vector2 {
val mapLayers = map.layers
val spawnPoints = mapLayers["Spawnpoints"].objects
for (spawnPoint in spawnPoints) {
val spawnPointProperties = spawnPoint.properties
if (spawnPointProperties != null && spawnPointProperties.containsKey("playerSpawn")) {
val isPlayerSpawn = spawnPointProperties.get("playerSpawn", Boolean::class.java)
if (java.lang.Boolean.TRUE == isPlayerSpawn) {
val x = spawnPointProperties.get("x", Float::class.java)
val y = spawnPointProperties.get("y", Float::class.java)
return Vector2(x, y)
}
}
}
Gdx.app.debug("SurvislandMap", "No spawn point found in map. Return map origin.")
return Vector2.Zero
}
private fun loadDimensions() {
val prop = map.properties
this.gridWidth = prop.get("width", Int::class.java)
this.gridHeight = prop.get("height", Int::class.java)
this.width = gridWidth * CELL_SIZE
this.height = gridHeight * CELL_SIZE
this.mapTileWidth = map.properties.get("tilewidth", Int::class.java)
this.mapTileHeight = map.properties.get("tileheight", Int::class.java)
this.halfMapTileWidth = mapTileWidth / 2f
this.halfMapTileHeight = mapTileHeight / 2f
}
private fun loadWalls() {
val wallsLayer = map.layers["Walls"] as TiledMapTileLayer
for (column in 0 until wallsLayer.width) {
for (row in 0 until wallsLayer.height) {
val cell: TiledMapTileLayer.Cell? = wallsLayer.getCell(column, row)
if (cell != null) {
val isDoor: Boolean = cell.getTile().getProperties().get("isDoor", false, Boolean::class.java)
val wallCollider = Rectangle(
column.toFloat() * wallsLayer.tileWidth,
row.toFloat() * wallsLayer.tileHeight, wallsLayer.tileWidth.toFloat(),
wallsLayer.tileHeight.toFloat()
)
val wall: Wall
if (java.lang.Boolean.TRUE == isDoor) {
wall = Door(column, row, wallCollider, cell)
} else {
wall = Wall(column, row, wallCollider, cell)
}
walls.add(wall)
}
}
}
}
private fun findDoorByGridPosition(gridX: Int, gridY: Int): Door? {
for (wall in walls) {
if (wall.gridX == gridX && wall.gridY == gridY && wall is Door) {
return wall
}
}
return null
}
fun toggleDoorAt(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}")
}
fun getTileWidth(): Float {
return mapTileWidth.toFloat()
}
fun getTileHeight(): Float {
return mapTileHeight.toFloat()
}
fun render(batch: SpriteBatch, camera: OrthographicCamera, delta: Float) {
mapRenderer.setView(camera)
mapRenderer.render()
}
fun isCollidingWithWall(collidable: Collidable): Boolean {
for (wall in walls) {
if (wall.collidesWidth(collidable)) {
return true
}
}
return false
}
}

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