diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 28d042f..c65d346 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -85,6 +85,54 @@ println("width: $width, height: $height") } + fun screenToGameCoordX(screenX: Float): Float { + var result = screenX + + when(viewType) { + ViewType.ABSOLUTE -> { + // nop + } + ViewType.WIDTH -> { + result = (screenX / windowWidth * width) - width / 2 + } + ViewType.HEIGHT -> { + result = (screenX / windowWidth * width) - width / 2 + } + ViewType.PROJECTION -> { + // uhm + } + else -> { + throw IllegalStateException("ViewType $viewType not implemented!") + } + } + + return result + } + + fun screenToGameCoordY(screenY: Float): Float { + var result = screenY + + when(viewType) { + ViewType.ABSOLUTE -> { + // nop + } + ViewType.WIDTH -> { + result = (screenY / windowHeight * height) - height / 2 + } + ViewType.HEIGHT -> { + result = (screenY / windowHeight * height) - height / 2 + } + ViewType.PROJECTION -> { + // uhm + } + else -> { + throw IllegalStateException("ViewType $viewType not implemented!") + } + } + + return result + } + fun setToWidth(width: Float) { this.width = width this.viewType = ViewType.WIDTH diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 28d042f..c65d346 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -85,6 +85,54 @@ println("width: $width, height: $height") } + fun screenToGameCoordX(screenX: Float): Float { + var result = screenX + + when(viewType) { + ViewType.ABSOLUTE -> { + // nop + } + ViewType.WIDTH -> { + result = (screenX / windowWidth * width) - width / 2 + } + ViewType.HEIGHT -> { + result = (screenX / windowWidth * width) - width / 2 + } + ViewType.PROJECTION -> { + // uhm + } + else -> { + throw IllegalStateException("ViewType $viewType not implemented!") + } + } + + return result + } + + fun screenToGameCoordY(screenY: Float): Float { + var result = screenY + + when(viewType) { + ViewType.ABSOLUTE -> { + // nop + } + ViewType.WIDTH -> { + result = (screenY / windowHeight * height) - height / 2 + } + ViewType.HEIGHT -> { + result = (screenY / windowHeight * height) - height / 2 + } + ViewType.PROJECTION -> { + // uhm + } + else -> { + throw IllegalStateException("ViewType $viewType not implemented!") + } + } + + return result + } + fun setToWidth(width: Float) { this.width = width this.viewType = ViewType.WIDTH diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt index a30b761..d9b491d 100644 --- a/lib/kotludens/com/persesgames/input/Keys.kt +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -1,7 +1,9 @@ package com.persesgames.input +import com.persesgames.game.Game import org.w3c.dom.events.Event import org.w3c.dom.events.KeyboardEvent +import org.w3c.dom.events.MouseEvent import java.util.* import kotlin.browser.document import kotlin.dom.on @@ -28,9 +30,14 @@ fun keyUp(keyCode: Int) + fun pointerClick(pointer: Int, x: Float, y: Float) + } -class DefaultProcessor: InputProcessor { +open class EmptyInputProcessor : InputProcessor { + override fun pointerClick(pointer: Int, x: Float, y: Float) { + } + override fun keyDown(keyCode: Int) { } @@ -39,12 +46,14 @@ override fun keyUp(keyCode: Int) { } + + } object Keys { private val keys: MutableMap = HashMap(); - private var inputProcesser: InputProcessor = DefaultProcessor() + private var inputProcesser: InputProcessor = EmptyInputProcessor() init { val body = document.body @@ -60,6 +69,22 @@ body.on("keypress", true) { Keys.keyPress(it) } + + body.on("click", true) { + Keys.mouseClick(it) + } + + body.on("mousedown", true) { + Keys.mouseMove(it) + } + + body.on("mouseup", true) { + Keys.mouseMove(it) + } + + body.on("mousemove", true) { + Keys.mouseMove(it) + } } } @@ -89,6 +114,24 @@ } } + private fun mouseClick(event: Event) { + if (event is MouseEvent) { + val vx: Float = Game.view.screenToGameCoordX(event.clientX.toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.clientY.toFloat()) + + inputProcesser.pointerClick(event.button.toInt(), vx, vy) + } + } + + private fun mouseMove(event: Event) { + if (event is MouseEvent) { + val vx: Float = Game.view.screenToGameCoordX(event.clientX.toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.clientY.toFloat()) + + + } + } + fun isDown(keyCode: Int) = keys.containsKey(keyCode) fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 28d042f..c65d346 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -85,6 +85,54 @@ println("width: $width, height: $height") } + fun screenToGameCoordX(screenX: Float): Float { + var result = screenX + + when(viewType) { + ViewType.ABSOLUTE -> { + // nop + } + ViewType.WIDTH -> { + result = (screenX / windowWidth * width) - width / 2 + } + ViewType.HEIGHT -> { + result = (screenX / windowWidth * width) - width / 2 + } + ViewType.PROJECTION -> { + // uhm + } + else -> { + throw IllegalStateException("ViewType $viewType not implemented!") + } + } + + return result + } + + fun screenToGameCoordY(screenY: Float): Float { + var result = screenY + + when(viewType) { + ViewType.ABSOLUTE -> { + // nop + } + ViewType.WIDTH -> { + result = (screenY / windowHeight * height) - height / 2 + } + ViewType.HEIGHT -> { + result = (screenY / windowHeight * height) - height / 2 + } + ViewType.PROJECTION -> { + // uhm + } + else -> { + throw IllegalStateException("ViewType $viewType not implemented!") + } + } + + return result + } + fun setToWidth(width: Float) { this.width = width this.viewType = ViewType.WIDTH diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt index a30b761..d9b491d 100644 --- a/lib/kotludens/com/persesgames/input/Keys.kt +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -1,7 +1,9 @@ package com.persesgames.input +import com.persesgames.game.Game import org.w3c.dom.events.Event import org.w3c.dom.events.KeyboardEvent +import org.w3c.dom.events.MouseEvent import java.util.* import kotlin.browser.document import kotlin.dom.on @@ -28,9 +30,14 @@ fun keyUp(keyCode: Int) + fun pointerClick(pointer: Int, x: Float, y: Float) + } -class DefaultProcessor: InputProcessor { +open class EmptyInputProcessor : InputProcessor { + override fun pointerClick(pointer: Int, x: Float, y: Float) { + } + override fun keyDown(keyCode: Int) { } @@ -39,12 +46,14 @@ override fun keyUp(keyCode: Int) { } + + } object Keys { private val keys: MutableMap = HashMap(); - private var inputProcesser: InputProcessor = DefaultProcessor() + private var inputProcesser: InputProcessor = EmptyInputProcessor() init { val body = document.body @@ -60,6 +69,22 @@ body.on("keypress", true) { Keys.keyPress(it) } + + body.on("click", true) { + Keys.mouseClick(it) + } + + body.on("mousedown", true) { + Keys.mouseMove(it) + } + + body.on("mouseup", true) { + Keys.mouseMove(it) + } + + body.on("mousemove", true) { + Keys.mouseMove(it) + } } } @@ -89,6 +114,24 @@ } } + private fun mouseClick(event: Event) { + if (event is MouseEvent) { + val vx: Float = Game.view.screenToGameCoordX(event.clientX.toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.clientY.toFloat()) + + inputProcesser.pointerClick(event.button.toInt(), vx, vy) + } + } + + private fun mouseMove(event: Event) { + if (event is MouseEvent) { + val vx: Float = Game.view.screenToGameCoordX(event.clientX.toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.clientY.toFloat()) + + + } + } + fun isDown(keyCode: Int) = keys.containsKey(keyCode) fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index 12042ca..7b10d7a 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -2,6 +2,7 @@ import com.persesgames.game.Game import com.persesgames.game.Screen +import com.persesgames.input.EmptyInputProcessor import com.persesgames.input.InputProcessor import com.persesgames.input.KeyCode import com.persesgames.input.Keys @@ -16,9 +17,7 @@ * Created by rnentjes on 19-4-16. */ -class GameInputProcessor: InputProcessor { - override fun keyDown(keyCode: Int) { - } +class GameInputProcessor: EmptyInputProcessor() { override fun keyPressed(charCode: Int) { println("charCode: $charCode") @@ -27,9 +26,9 @@ } } - override fun keyUp(keyCode: Int) { + override fun pointerClick(pointer: Int, x: Float, y: Float) { + println("POINTER $pointer -> ($x, $y)") } - } class WelcomeScreen: Screen() { @@ -97,7 +96,7 @@ } fun main(args: Array) { - Game.view.setToWidth(4000f); + Game.view.setToWidth(2000f); Game.start(WelcomeScreen()) }