diff --git a/build.gradle b/build.gradle index 6f9a26e..f345e30 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'perses.games' -version '1.1.2-SNAPSHOT' +version '1.1.3-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'idea' diff --git a/build.gradle b/build.gradle index 6f9a26e..f345e30 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'perses.games' -version '1.1.2-SNAPSHOT' +version '1.1.3-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'idea' diff --git a/src/main/kotlin/games/perses/game/Game.kt b/src/main/kotlin/games/perses/game/Game.kt index a8d0578..544b6e9 100644 --- a/src/main/kotlin/games/perses/game/Game.kt +++ b/src/main/kotlin/games/perses/game/Game.kt @@ -74,6 +74,7 @@ var currentTime = start var currentDelta = 0f var pause: Boolean = false + var clearScreenEveryFrame = true var clearRed = 0f var clearGreen = 0f @@ -152,38 +153,44 @@ } fun gameLoop() { - if (!Textures.ready()) { - gl().clearColor(1f, 1f, 1f, 1f) - gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - } else { - resize() - - if (!pause) { - html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - - gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + try { + if (!Textures.ready()) { + gl().clearColor(1f, 1f, 1f, 1f) gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } else { + resize() - gl().enable(WebGLRenderingContext.BLEND) - gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); + if (!pause) { + html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - val time = Date().getTime() - currentDelta = ((time - currentTime) / 1000f).toFloat() - currentTime = time + if (clearScreenEveryFrame) { + gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } - val timeInSeconds = (currentTime - start) / 1000f + gl().enable(WebGLRenderingContext.BLEND) + gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); - fpsCountTime += currentDelta - fpsCount++ - while (fpsCountTime > 1f) { - fps = fpsCount - fpsCountTime -= 1f - fpsCount = 0 + val time = Date().getTime() + currentDelta = ((time - currentTime) / 1000f).toFloat() + currentTime = time + + val timeInSeconds = (currentTime - start) / 1000f + + fpsCountTime += currentDelta + fpsCount++ + while (fpsCountTime > 1f) { + fps = fpsCount + fpsCountTime -= 1f + fpsCount = 0 + } + + currentScreen.update(timeInSeconds.toFloat(), currentDelta) + currentScreen.render() } - - currentScreen.update(timeInSeconds.toFloat(), currentDelta) - currentScreen.render() } + } catch(e: Exception) { + console.log(e.message, e) } window.requestAnimationFrame { diff --git a/build.gradle b/build.gradle index 6f9a26e..f345e30 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'perses.games' -version '1.1.2-SNAPSHOT' +version '1.1.3-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'idea' diff --git a/src/main/kotlin/games/perses/game/Game.kt b/src/main/kotlin/games/perses/game/Game.kt index a8d0578..544b6e9 100644 --- a/src/main/kotlin/games/perses/game/Game.kt +++ b/src/main/kotlin/games/perses/game/Game.kt @@ -74,6 +74,7 @@ var currentTime = start var currentDelta = 0f var pause: Boolean = false + var clearScreenEveryFrame = true var clearRed = 0f var clearGreen = 0f @@ -152,38 +153,44 @@ } fun gameLoop() { - if (!Textures.ready()) { - gl().clearColor(1f, 1f, 1f, 1f) - gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - } else { - resize() - - if (!pause) { - html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - - gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + try { + if (!Textures.ready()) { + gl().clearColor(1f, 1f, 1f, 1f) gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } else { + resize() - gl().enable(WebGLRenderingContext.BLEND) - gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); + if (!pause) { + html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - val time = Date().getTime() - currentDelta = ((time - currentTime) / 1000f).toFloat() - currentTime = time + if (clearScreenEveryFrame) { + gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } - val timeInSeconds = (currentTime - start) / 1000f + gl().enable(WebGLRenderingContext.BLEND) + gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); - fpsCountTime += currentDelta - fpsCount++ - while (fpsCountTime > 1f) { - fps = fpsCount - fpsCountTime -= 1f - fpsCount = 0 + val time = Date().getTime() + currentDelta = ((time - currentTime) / 1000f).toFloat() + currentTime = time + + val timeInSeconds = (currentTime - start) / 1000f + + fpsCountTime += currentDelta + fpsCount++ + while (fpsCountTime > 1f) { + fps = fpsCount + fpsCountTime -= 1f + fpsCount = 0 + } + + currentScreen.update(timeInSeconds.toFloat(), currentDelta) + currentScreen.render() } - - currentScreen.update(timeInSeconds.toFloat(), currentDelta) - currentScreen.render() } + } catch(e: Exception) { + console.log(e.message, e) } window.requestAnimationFrame { diff --git a/src/main/kotlin/games/perses/input/Keys.kt b/src/main/kotlin/games/perses/input/Keys.kt index e003e82..1860155 100644 --- a/src/main/kotlin/games/perses/input/Keys.kt +++ b/src/main/kotlin/games/perses/input/Keys.kt @@ -1,6 +1,7 @@ package games.perses.input import games.perses.game.Game +import org.w3c.dom.Element import org.w3c.dom.events.Event import org.w3c.dom.events.KeyboardEvent import org.w3c.dom.events.MouseEvent @@ -118,8 +119,8 @@ 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()) + val vx: Float = Game.view.screenToGameCoordX(event.getX(Game.html.container).toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.getY(Game.html.container).toFloat()) inputProcesser.pointerClick(event.button.toInt(), vx, vy) } @@ -127,8 +128,8 @@ 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()) + val vx: Float = Game.view.screenToGameCoordX(event.getX(Game.html.container).toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.getY(Game.html.container).toFloat()) //println("mouse move ${event.clientX.toFloat()}, ${event.clientY.toFloat()} -> $vx, $vy") } @@ -145,3 +146,7 @@ } } + +fun MouseEvent.getX(element: Element) = this.pageX - element.getBoundingClientRect().left + +fun MouseEvent.getY(element: Element) = this.pageY - element.getBoundingClientRect().top diff --git a/build.gradle b/build.gradle index 6f9a26e..f345e30 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'perses.games' -version '1.1.2-SNAPSHOT' +version '1.1.3-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'idea' diff --git a/src/main/kotlin/games/perses/game/Game.kt b/src/main/kotlin/games/perses/game/Game.kt index a8d0578..544b6e9 100644 --- a/src/main/kotlin/games/perses/game/Game.kt +++ b/src/main/kotlin/games/perses/game/Game.kt @@ -74,6 +74,7 @@ var currentTime = start var currentDelta = 0f var pause: Boolean = false + var clearScreenEveryFrame = true var clearRed = 0f var clearGreen = 0f @@ -152,38 +153,44 @@ } fun gameLoop() { - if (!Textures.ready()) { - gl().clearColor(1f, 1f, 1f, 1f) - gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - } else { - resize() - - if (!pause) { - html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - - gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + try { + if (!Textures.ready()) { + gl().clearColor(1f, 1f, 1f, 1f) gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } else { + resize() - gl().enable(WebGLRenderingContext.BLEND) - gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); + if (!pause) { + html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - val time = Date().getTime() - currentDelta = ((time - currentTime) / 1000f).toFloat() - currentTime = time + if (clearScreenEveryFrame) { + gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } - val timeInSeconds = (currentTime - start) / 1000f + gl().enable(WebGLRenderingContext.BLEND) + gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); - fpsCountTime += currentDelta - fpsCount++ - while (fpsCountTime > 1f) { - fps = fpsCount - fpsCountTime -= 1f - fpsCount = 0 + val time = Date().getTime() + currentDelta = ((time - currentTime) / 1000f).toFloat() + currentTime = time + + val timeInSeconds = (currentTime - start) / 1000f + + fpsCountTime += currentDelta + fpsCount++ + while (fpsCountTime > 1f) { + fps = fpsCount + fpsCountTime -= 1f + fpsCount = 0 + } + + currentScreen.update(timeInSeconds.toFloat(), currentDelta) + currentScreen.render() } - - currentScreen.update(timeInSeconds.toFloat(), currentDelta) - currentScreen.render() } + } catch(e: Exception) { + console.log(e.message, e) } window.requestAnimationFrame { diff --git a/src/main/kotlin/games/perses/input/Keys.kt b/src/main/kotlin/games/perses/input/Keys.kt index e003e82..1860155 100644 --- a/src/main/kotlin/games/perses/input/Keys.kt +++ b/src/main/kotlin/games/perses/input/Keys.kt @@ -1,6 +1,7 @@ package games.perses.input import games.perses.game.Game +import org.w3c.dom.Element import org.w3c.dom.events.Event import org.w3c.dom.events.KeyboardEvent import org.w3c.dom.events.MouseEvent @@ -118,8 +119,8 @@ 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()) + val vx: Float = Game.view.screenToGameCoordX(event.getX(Game.html.container).toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.getY(Game.html.container).toFloat()) inputProcesser.pointerClick(event.button.toInt(), vx, vy) } @@ -127,8 +128,8 @@ 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()) + val vx: Float = Game.view.screenToGameCoordX(event.getX(Game.html.container).toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.getY(Game.html.container).toFloat()) //println("mouse move ${event.clientX.toFloat()}, ${event.clientY.toFloat()} -> $vx, $vy") } @@ -145,3 +146,7 @@ } } + +fun MouseEvent.getX(element: Element) = this.pageX - element.getBoundingClientRect().left + +fun MouseEvent.getY(element: Element) = this.pageY - element.getBoundingClientRect().top diff --git a/src/main/kotlin/games/perses/sound/Sounds.kt b/src/main/kotlin/games/perses/sound/Sounds.kt index 4efa4dd..b2d0c2c 100644 --- a/src/main/kotlin/games/perses/sound/Sounds.kt +++ b/src/main/kotlin/games/perses/sound/Sounds.kt @@ -9,7 +9,7 @@ * Time: 12:34 */ -class Sound(val name:String, val url: String, val volume: Double = 0.75, val numberOfChannels: Int) { +class Sound(val name:String, val url: String, val defaultVolume: Double = 0.75, val numberOfChannels: Int) { var channels: Array var nextChannel: Int = 0 @@ -22,13 +22,14 @@ audio.src = url audio.pause() audio.load() - audio.volume = volume + audio.volume = defaultVolume } } } - fun play() { + fun play(volume: Double = defaultVolume) { //println("PLAYING: $name - $nextChannel") + channels[nextChannel]?.volume = volume channels[nextChannel]?.currentTime = 0.0 channels[nextChannel]?.play() @@ -49,10 +50,14 @@ sounds.put(name, Sound(name, url, volume, channels)) } - fun play(name: String, volume: Float = 0.75f) { + fun play(name: String, volume: Double? = null) { val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") - sound.play() + if (volume != null) { + sound.play(volume) + } else { + sound.play() + } } fun pause(name: String) { diff --git a/build.gradle b/build.gradle index 6f9a26e..f345e30 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'perses.games' -version '1.1.2-SNAPSHOT' +version '1.1.3-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'idea' diff --git a/src/main/kotlin/games/perses/game/Game.kt b/src/main/kotlin/games/perses/game/Game.kt index a8d0578..544b6e9 100644 --- a/src/main/kotlin/games/perses/game/Game.kt +++ b/src/main/kotlin/games/perses/game/Game.kt @@ -74,6 +74,7 @@ var currentTime = start var currentDelta = 0f var pause: Boolean = false + var clearScreenEveryFrame = true var clearRed = 0f var clearGreen = 0f @@ -152,38 +153,44 @@ } fun gameLoop() { - if (!Textures.ready()) { - gl().clearColor(1f, 1f, 1f, 1f) - gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - } else { - resize() - - if (!pause) { - html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - - gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + try { + if (!Textures.ready()) { + gl().clearColor(1f, 1f, 1f, 1f) gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } else { + resize() - gl().enable(WebGLRenderingContext.BLEND) - gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); + if (!pause) { + html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - val time = Date().getTime() - currentDelta = ((time - currentTime) / 1000f).toFloat() - currentTime = time + if (clearScreenEveryFrame) { + gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } - val timeInSeconds = (currentTime - start) / 1000f + gl().enable(WebGLRenderingContext.BLEND) + gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); - fpsCountTime += currentDelta - fpsCount++ - while (fpsCountTime > 1f) { - fps = fpsCount - fpsCountTime -= 1f - fpsCount = 0 + val time = Date().getTime() + currentDelta = ((time - currentTime) / 1000f).toFloat() + currentTime = time + + val timeInSeconds = (currentTime - start) / 1000f + + fpsCountTime += currentDelta + fpsCount++ + while (fpsCountTime > 1f) { + fps = fpsCount + fpsCountTime -= 1f + fpsCount = 0 + } + + currentScreen.update(timeInSeconds.toFloat(), currentDelta) + currentScreen.render() } - - currentScreen.update(timeInSeconds.toFloat(), currentDelta) - currentScreen.render() } + } catch(e: Exception) { + console.log(e.message, e) } window.requestAnimationFrame { diff --git a/src/main/kotlin/games/perses/input/Keys.kt b/src/main/kotlin/games/perses/input/Keys.kt index e003e82..1860155 100644 --- a/src/main/kotlin/games/perses/input/Keys.kt +++ b/src/main/kotlin/games/perses/input/Keys.kt @@ -1,6 +1,7 @@ package games.perses.input import games.perses.game.Game +import org.w3c.dom.Element import org.w3c.dom.events.Event import org.w3c.dom.events.KeyboardEvent import org.w3c.dom.events.MouseEvent @@ -118,8 +119,8 @@ 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()) + val vx: Float = Game.view.screenToGameCoordX(event.getX(Game.html.container).toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.getY(Game.html.container).toFloat()) inputProcesser.pointerClick(event.button.toInt(), vx, vy) } @@ -127,8 +128,8 @@ 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()) + val vx: Float = Game.view.screenToGameCoordX(event.getX(Game.html.container).toFloat()) + val vy: Float = Game.view.screenToGameCoordY(event.getY(Game.html.container).toFloat()) //println("mouse move ${event.clientX.toFloat()}, ${event.clientY.toFloat()} -> $vx, $vy") } @@ -145,3 +146,7 @@ } } + +fun MouseEvent.getX(element: Element) = this.pageX - element.getBoundingClientRect().left + +fun MouseEvent.getY(element: Element) = this.pageY - element.getBoundingClientRect().top diff --git a/src/main/kotlin/games/perses/sound/Sounds.kt b/src/main/kotlin/games/perses/sound/Sounds.kt index 4efa4dd..b2d0c2c 100644 --- a/src/main/kotlin/games/perses/sound/Sounds.kt +++ b/src/main/kotlin/games/perses/sound/Sounds.kt @@ -9,7 +9,7 @@ * Time: 12:34 */ -class Sound(val name:String, val url: String, val volume: Double = 0.75, val numberOfChannels: Int) { +class Sound(val name:String, val url: String, val defaultVolume: Double = 0.75, val numberOfChannels: Int) { var channels: Array var nextChannel: Int = 0 @@ -22,13 +22,14 @@ audio.src = url audio.pause() audio.load() - audio.volume = volume + audio.volume = defaultVolume } } } - fun play() { + fun play(volume: Double = defaultVolume) { //println("PLAYING: $name - $nextChannel") + channels[nextChannel]?.volume = volume channels[nextChannel]?.currentTime = 0.0 channels[nextChannel]?.play() @@ -49,10 +50,14 @@ sounds.put(name, Sound(name, url, volume, channels)) } - fun play(name: String, volume: Float = 0.75f) { + fun play(name: String, volume: Double? = null) { val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") - sound.play() + if (volume != null) { + sound.play(volume) + } else { + sound.play() + } } fun pause(name: String) { diff --git a/src/main/kotlin/games/perses/text/Texts.kt b/src/main/kotlin/games/perses/text/Texts.kt index ae6a87a..56f4834 100644 --- a/src/main/kotlin/games/perses/text/Texts.kt +++ b/src/main/kotlin/games/perses/text/Texts.kt @@ -5,15 +5,20 @@ /** * Created by rnentjes on 16-5-16. */ - object Texts { // TODO: use same coords for webgl and canvas 2d fun drawText(x: Float, y: Float, message: String, font: String = "bold 24pt Arial", fillStyle: String = "white") { var yy = y + var xx = x if (yy < 0) { yy += Game.view.height } + if (xx < 0) { + xx += Game.view.width + } + yy = Game.view.height - yy + Game.html.canvas2d.fillStyle = fillStyle Game.html.canvas2d.font = font Game.html.canvas2d.fillText(message, x.toDouble(), yy.toDouble())