diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + 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 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt new file mode 100644 index 0000000..3fc614f --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -0,0 +1,55 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:34 + */ + +class Sound(val name:String, val url: String, val volume: Double = 0.75) { + var audio: HTMLAudioElement + + init { + println("CREATING: $name") + audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } + + fun play() { + println("PLAYING: $name") + audio.currentTime = 0.0 + audio.play() + } + + fun pause() { + audio.pause() + } +} + +object Sounds { + val sounds: MutableMap = HashMap() + + fun load(name: String, url: String, volume: Double = 0.75 ) { + sounds.put(name, Sound(name, url, volume)) + } + + fun play(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.play() + } + + fun pause(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.pause() + } +} diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt new file mode 100644 index 0000000..3fc614f --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -0,0 +1,55 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:34 + */ + +class Sound(val name:String, val url: String, val volume: Double = 0.75) { + var audio: HTMLAudioElement + + init { + println("CREATING: $name") + audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } + + fun play() { + println("PLAYING: $name") + audio.currentTime = 0.0 + audio.play() + } + + fun pause() { + audio.pause() + } +} + +object Sounds { + val sounds: MutableMap = HashMap() + + fun load(name: String, url: String, volume: Double = 0.75 ) { + sounds.put(name, Sound(name, url, volume)) + } + + fun play(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.play() + } + + fun pause(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.pause() + } +} diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index e78798a..12042ca 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -2,47 +2,87 @@ import com.persesgames.game.Game import com.persesgames.game.Screen +import com.persesgames.input.InputProcessor +import com.persesgames.input.KeyCode +import com.persesgames.input.Keys +import com.persesgames.sound.Music +import com.persesgames.sound.Sounds import com.persesgames.sprite.Sprite import com.persesgames.sprite.SpriteBatch import com.persesgames.text.Texts import com.persesgames.texture.Textures -import org.khronos.webgl.WebGLRenderingContext /** * Created by rnentjes on 19-4-16. */ +class GameInputProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + println("charCode: $charCode") + if (charCode == 32) { + Music.play("sounds/Explosion7.ogg", 0.5) + } + } + + override fun keyUp(keyCode: Int) { + } + +} + class WelcomeScreen: Screen() { var sprites = SpriteBatch() - var x = 1f - var y = 1f + var x = 0f + var y = 0f var sprite = Sprite("SHIP") override fun loadResources() { Textures.load("SHIP", "images/ship2.png") + Sounds.load("EXPLOSION", "sounds/Explosion7.ogg") + + Music.play("music/DST-TechnoBasic.ogg", 1.0, looping = true) + + Keys.setInputProcessor(GameInputProcessor()) } override fun update(time: Float, delta: Float) { - x = Math.sin(time.toDouble()).toFloat() * 150f - y = Math.cos(time.toDouble()).toFloat() * 150f + val speed = 500f // pixels per second + if (Keys.isDown(KeyCode.LEFT)) { + x -= delta * speed; + } + + if (Keys.isDown(KeyCode.RIGHT)) { + x += delta * speed; + } + + if (Keys.isDown(KeyCode.UP)) { + y += delta * speed; + } + + if (Keys.isDown(KeyCode.DOWN)) { + y -= delta * speed; + } + + if (Keys.isDown(KeyCode.SPACE)) { + //Music.play("sounds/Explosion7.ogg", 0.5) + } } override fun render() { - for (index in 0..2500) { - val x = Math.random() * 1000f - 500f - val y = Math.random() * 1000f - 500f + for (index in 0..100) { + val x = Math.random() * 2000f - 1000f + val y = Math.random() * 2000f - 1000f sprites.draw(sprite, x.toFloat(), y.toFloat()); } sprites.draw(sprite, x, y); - sprites.draw(sprite, -x, y); - sprites.draw(sprite, x, -y); - sprites.draw(sprite, -x, -y); sprites.render() - Texts.drawText(10f, 40f, "Hello! FPS ${Game.fps}", font = "bold 36pt Arial") + Texts.drawText(20f, 80f, "Hello! FPS ${Game.fps}", font = "bold 72pt Arial") } } @@ -57,7 +97,7 @@ } fun main(args: Array) { - Game.view.setToWidth(1000f); + Game.view.setToWidth(4000f); Game.start(WelcomeScreen()) } diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt new file mode 100644 index 0000000..3fc614f --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -0,0 +1,55 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:34 + */ + +class Sound(val name:String, val url: String, val volume: Double = 0.75) { + var audio: HTMLAudioElement + + init { + println("CREATING: $name") + audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } + + fun play() { + println("PLAYING: $name") + audio.currentTime = 0.0 + audio.play() + } + + fun pause() { + audio.pause() + } +} + +object Sounds { + val sounds: MutableMap = HashMap() + + fun load(name: String, url: String, volume: Double = 0.75 ) { + sounds.put(name, Sound(name, url, volume)) + } + + fun play(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.play() + } + + fun pause(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.pause() + } +} diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index e78798a..12042ca 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -2,47 +2,87 @@ import com.persesgames.game.Game import com.persesgames.game.Screen +import com.persesgames.input.InputProcessor +import com.persesgames.input.KeyCode +import com.persesgames.input.Keys +import com.persesgames.sound.Music +import com.persesgames.sound.Sounds import com.persesgames.sprite.Sprite import com.persesgames.sprite.SpriteBatch import com.persesgames.text.Texts import com.persesgames.texture.Textures -import org.khronos.webgl.WebGLRenderingContext /** * Created by rnentjes on 19-4-16. */ +class GameInputProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + println("charCode: $charCode") + if (charCode == 32) { + Music.play("sounds/Explosion7.ogg", 0.5) + } + } + + override fun keyUp(keyCode: Int) { + } + +} + class WelcomeScreen: Screen() { var sprites = SpriteBatch() - var x = 1f - var y = 1f + var x = 0f + var y = 0f var sprite = Sprite("SHIP") override fun loadResources() { Textures.load("SHIP", "images/ship2.png") + Sounds.load("EXPLOSION", "sounds/Explosion7.ogg") + + Music.play("music/DST-TechnoBasic.ogg", 1.0, looping = true) + + Keys.setInputProcessor(GameInputProcessor()) } override fun update(time: Float, delta: Float) { - x = Math.sin(time.toDouble()).toFloat() * 150f - y = Math.cos(time.toDouble()).toFloat() * 150f + val speed = 500f // pixels per second + if (Keys.isDown(KeyCode.LEFT)) { + x -= delta * speed; + } + + if (Keys.isDown(KeyCode.RIGHT)) { + x += delta * speed; + } + + if (Keys.isDown(KeyCode.UP)) { + y += delta * speed; + } + + if (Keys.isDown(KeyCode.DOWN)) { + y -= delta * speed; + } + + if (Keys.isDown(KeyCode.SPACE)) { + //Music.play("sounds/Explosion7.ogg", 0.5) + } } override fun render() { - for (index in 0..2500) { - val x = Math.random() * 1000f - 500f - val y = Math.random() * 1000f - 500f + for (index in 0..100) { + val x = Math.random() * 2000f - 1000f + val y = Math.random() * 2000f - 1000f sprites.draw(sprite, x.toFloat(), y.toFloat()); } sprites.draw(sprite, x, y); - sprites.draw(sprite, -x, y); - sprites.draw(sprite, x, -y); - sprites.draw(sprite, -x, -y); sprites.render() - Texts.drawText(10f, 40f, "Hello! FPS ${Game.fps}", font = "bold 36pt Arial") + Texts.drawText(20f, 80f, "Hello! FPS ${Game.fps}", font = "bold 72pt Arial") } } @@ -57,7 +97,7 @@ } fun main(args: Array) { - Game.view.setToWidth(1000f); + Game.view.setToWidth(4000f); Game.start(WelcomeScreen()) } diff --git a/web/index.html b/web/index.html index 39ffcc1..906bce0 100644 --- a/web/index.html +++ b/web/index.html @@ -17,7 +17,6 @@ - diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt new file mode 100644 index 0000000..3fc614f --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -0,0 +1,55 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:34 + */ + +class Sound(val name:String, val url: String, val volume: Double = 0.75) { + var audio: HTMLAudioElement + + init { + println("CREATING: $name") + audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } + + fun play() { + println("PLAYING: $name") + audio.currentTime = 0.0 + audio.play() + } + + fun pause() { + audio.pause() + } +} + +object Sounds { + val sounds: MutableMap = HashMap() + + fun load(name: String, url: String, volume: Double = 0.75 ) { + sounds.put(name, Sound(name, url, volume)) + } + + fun play(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.play() + } + + fun pause(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.pause() + } +} diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index e78798a..12042ca 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -2,47 +2,87 @@ import com.persesgames.game.Game import com.persesgames.game.Screen +import com.persesgames.input.InputProcessor +import com.persesgames.input.KeyCode +import com.persesgames.input.Keys +import com.persesgames.sound.Music +import com.persesgames.sound.Sounds import com.persesgames.sprite.Sprite import com.persesgames.sprite.SpriteBatch import com.persesgames.text.Texts import com.persesgames.texture.Textures -import org.khronos.webgl.WebGLRenderingContext /** * Created by rnentjes on 19-4-16. */ +class GameInputProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + println("charCode: $charCode") + if (charCode == 32) { + Music.play("sounds/Explosion7.ogg", 0.5) + } + } + + override fun keyUp(keyCode: Int) { + } + +} + class WelcomeScreen: Screen() { var sprites = SpriteBatch() - var x = 1f - var y = 1f + var x = 0f + var y = 0f var sprite = Sprite("SHIP") override fun loadResources() { Textures.load("SHIP", "images/ship2.png") + Sounds.load("EXPLOSION", "sounds/Explosion7.ogg") + + Music.play("music/DST-TechnoBasic.ogg", 1.0, looping = true) + + Keys.setInputProcessor(GameInputProcessor()) } override fun update(time: Float, delta: Float) { - x = Math.sin(time.toDouble()).toFloat() * 150f - y = Math.cos(time.toDouble()).toFloat() * 150f + val speed = 500f // pixels per second + if (Keys.isDown(KeyCode.LEFT)) { + x -= delta * speed; + } + + if (Keys.isDown(KeyCode.RIGHT)) { + x += delta * speed; + } + + if (Keys.isDown(KeyCode.UP)) { + y += delta * speed; + } + + if (Keys.isDown(KeyCode.DOWN)) { + y -= delta * speed; + } + + if (Keys.isDown(KeyCode.SPACE)) { + //Music.play("sounds/Explosion7.ogg", 0.5) + } } override fun render() { - for (index in 0..2500) { - val x = Math.random() * 1000f - 500f - val y = Math.random() * 1000f - 500f + for (index in 0..100) { + val x = Math.random() * 2000f - 1000f + val y = Math.random() * 2000f - 1000f sprites.draw(sprite, x.toFloat(), y.toFloat()); } sprites.draw(sprite, x, y); - sprites.draw(sprite, -x, y); - sprites.draw(sprite, x, -y); - sprites.draw(sprite, -x, -y); sprites.render() - Texts.drawText(10f, 40f, "Hello! FPS ${Game.fps}", font = "bold 36pt Arial") + Texts.drawText(20f, 80f, "Hello! FPS ${Game.fps}", font = "bold 72pt Arial") } } @@ -57,7 +97,7 @@ } fun main(args: Array) { - Game.view.setToWidth(1000f); + Game.view.setToWidth(4000f); Game.start(WelcomeScreen()) } diff --git a/web/index.html b/web/index.html index 39ffcc1..906bce0 100644 --- a/web/index.html +++ b/web/index.html @@ -17,7 +17,6 @@ - diff --git a/web/js/test.js b/web/js/test.js deleted file mode 100644 index 6a0dafa..0000000 --- a/web/js/test.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Created by rnentjes on 11-5-16. - */ - -var bla = new Float32Array(1); - -bla[0]=1; diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt new file mode 100644 index 0000000..3fc614f --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -0,0 +1,55 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:34 + */ + +class Sound(val name:String, val url: String, val volume: Double = 0.75) { + var audio: HTMLAudioElement + + init { + println("CREATING: $name") + audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } + + fun play() { + println("PLAYING: $name") + audio.currentTime = 0.0 + audio.play() + } + + fun pause() { + audio.pause() + } +} + +object Sounds { + val sounds: MutableMap = HashMap() + + fun load(name: String, url: String, volume: Double = 0.75 ) { + sounds.put(name, Sound(name, url, volume)) + } + + fun play(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.play() + } + + fun pause(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.pause() + } +} diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index e78798a..12042ca 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -2,47 +2,87 @@ import com.persesgames.game.Game import com.persesgames.game.Screen +import com.persesgames.input.InputProcessor +import com.persesgames.input.KeyCode +import com.persesgames.input.Keys +import com.persesgames.sound.Music +import com.persesgames.sound.Sounds import com.persesgames.sprite.Sprite import com.persesgames.sprite.SpriteBatch import com.persesgames.text.Texts import com.persesgames.texture.Textures -import org.khronos.webgl.WebGLRenderingContext /** * Created by rnentjes on 19-4-16. */ +class GameInputProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + println("charCode: $charCode") + if (charCode == 32) { + Music.play("sounds/Explosion7.ogg", 0.5) + } + } + + override fun keyUp(keyCode: Int) { + } + +} + class WelcomeScreen: Screen() { var sprites = SpriteBatch() - var x = 1f - var y = 1f + var x = 0f + var y = 0f var sprite = Sprite("SHIP") override fun loadResources() { Textures.load("SHIP", "images/ship2.png") + Sounds.load("EXPLOSION", "sounds/Explosion7.ogg") + + Music.play("music/DST-TechnoBasic.ogg", 1.0, looping = true) + + Keys.setInputProcessor(GameInputProcessor()) } override fun update(time: Float, delta: Float) { - x = Math.sin(time.toDouble()).toFloat() * 150f - y = Math.cos(time.toDouble()).toFloat() * 150f + val speed = 500f // pixels per second + if (Keys.isDown(KeyCode.LEFT)) { + x -= delta * speed; + } + + if (Keys.isDown(KeyCode.RIGHT)) { + x += delta * speed; + } + + if (Keys.isDown(KeyCode.UP)) { + y += delta * speed; + } + + if (Keys.isDown(KeyCode.DOWN)) { + y -= delta * speed; + } + + if (Keys.isDown(KeyCode.SPACE)) { + //Music.play("sounds/Explosion7.ogg", 0.5) + } } override fun render() { - for (index in 0..2500) { - val x = Math.random() * 1000f - 500f - val y = Math.random() * 1000f - 500f + for (index in 0..100) { + val x = Math.random() * 2000f - 1000f + val y = Math.random() * 2000f - 1000f sprites.draw(sprite, x.toFloat(), y.toFloat()); } sprites.draw(sprite, x, y); - sprites.draw(sprite, -x, y); - sprites.draw(sprite, x, -y); - sprites.draw(sprite, -x, -y); sprites.render() - Texts.drawText(10f, 40f, "Hello! FPS ${Game.fps}", font = "bold 36pt Arial") + Texts.drawText(20f, 80f, "Hello! FPS ${Game.fps}", font = "bold 72pt Arial") } } @@ -57,7 +97,7 @@ } fun main(args: Array) { - Game.view.setToWidth(1000f); + Game.view.setToWidth(4000f); Game.start(WelcomeScreen()) } diff --git a/web/index.html b/web/index.html index 39ffcc1..906bce0 100644 --- a/web/index.html +++ b/web/index.html @@ -17,7 +17,6 @@ - diff --git a/web/js/test.js b/web/js/test.js deleted file mode 100644 index 6a0dafa..0000000 --- a/web/js/test.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Created by rnentjes on 11-5-16. - */ - -var bla = new Float32Array(1); - -bla[0]=1; diff --git a/web/music/DST-TacticalSpace.ogg b/web/music/DST-TacticalSpace.ogg new file mode 100644 index 0000000..5e5b834 --- /dev/null +++ b/web/music/DST-TacticalSpace.ogg Binary files differ diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt new file mode 100644 index 0000000..3fc614f --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -0,0 +1,55 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:34 + */ + +class Sound(val name:String, val url: String, val volume: Double = 0.75) { + var audio: HTMLAudioElement + + init { + println("CREATING: $name") + audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } + + fun play() { + println("PLAYING: $name") + audio.currentTime = 0.0 + audio.play() + } + + fun pause() { + audio.pause() + } +} + +object Sounds { + val sounds: MutableMap = HashMap() + + fun load(name: String, url: String, volume: Double = 0.75 ) { + sounds.put(name, Sound(name, url, volume)) + } + + fun play(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.play() + } + + fun pause(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.pause() + } +} diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index e78798a..12042ca 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -2,47 +2,87 @@ import com.persesgames.game.Game import com.persesgames.game.Screen +import com.persesgames.input.InputProcessor +import com.persesgames.input.KeyCode +import com.persesgames.input.Keys +import com.persesgames.sound.Music +import com.persesgames.sound.Sounds import com.persesgames.sprite.Sprite import com.persesgames.sprite.SpriteBatch import com.persesgames.text.Texts import com.persesgames.texture.Textures -import org.khronos.webgl.WebGLRenderingContext /** * Created by rnentjes on 19-4-16. */ +class GameInputProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + println("charCode: $charCode") + if (charCode == 32) { + Music.play("sounds/Explosion7.ogg", 0.5) + } + } + + override fun keyUp(keyCode: Int) { + } + +} + class WelcomeScreen: Screen() { var sprites = SpriteBatch() - var x = 1f - var y = 1f + var x = 0f + var y = 0f var sprite = Sprite("SHIP") override fun loadResources() { Textures.load("SHIP", "images/ship2.png") + Sounds.load("EXPLOSION", "sounds/Explosion7.ogg") + + Music.play("music/DST-TechnoBasic.ogg", 1.0, looping = true) + + Keys.setInputProcessor(GameInputProcessor()) } override fun update(time: Float, delta: Float) { - x = Math.sin(time.toDouble()).toFloat() * 150f - y = Math.cos(time.toDouble()).toFloat() * 150f + val speed = 500f // pixels per second + if (Keys.isDown(KeyCode.LEFT)) { + x -= delta * speed; + } + + if (Keys.isDown(KeyCode.RIGHT)) { + x += delta * speed; + } + + if (Keys.isDown(KeyCode.UP)) { + y += delta * speed; + } + + if (Keys.isDown(KeyCode.DOWN)) { + y -= delta * speed; + } + + if (Keys.isDown(KeyCode.SPACE)) { + //Music.play("sounds/Explosion7.ogg", 0.5) + } } override fun render() { - for (index in 0..2500) { - val x = Math.random() * 1000f - 500f - val y = Math.random() * 1000f - 500f + for (index in 0..100) { + val x = Math.random() * 2000f - 1000f + val y = Math.random() * 2000f - 1000f sprites.draw(sprite, x.toFloat(), y.toFloat()); } sprites.draw(sprite, x, y); - sprites.draw(sprite, -x, y); - sprites.draw(sprite, x, -y); - sprites.draw(sprite, -x, -y); sprites.render() - Texts.drawText(10f, 40f, "Hello! FPS ${Game.fps}", font = "bold 36pt Arial") + Texts.drawText(20f, 80f, "Hello! FPS ${Game.fps}", font = "bold 72pt Arial") } } @@ -57,7 +97,7 @@ } fun main(args: Array) { - Game.view.setToWidth(1000f); + Game.view.setToWidth(4000f); Game.start(WelcomeScreen()) } diff --git a/web/index.html b/web/index.html index 39ffcc1..906bce0 100644 --- a/web/index.html +++ b/web/index.html @@ -17,7 +17,6 @@ - diff --git a/web/js/test.js b/web/js/test.js deleted file mode 100644 index 6a0dafa..0000000 --- a/web/js/test.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Created by rnentjes on 11-5-16. - */ - -var bla = new Float32Array(1); - -bla[0]=1; diff --git a/web/music/DST-TacticalSpace.ogg b/web/music/DST-TacticalSpace.ogg new file mode 100644 index 0000000..5e5b834 --- /dev/null +++ b/web/music/DST-TacticalSpace.ogg Binary files differ diff --git a/web/music/DST-TechnoBasic.ogg b/web/music/DST-TechnoBasic.ogg new file mode 100644 index 0000000..5d62f45 --- /dev/null +++ b/web/music/DST-TechnoBasic.ogg Binary files differ diff --git a/lib/kotludens/com/persesgames/game/Game.kt b/lib/kotludens/com/persesgames/game/Game.kt index 29053ed..28d042f 100644 --- a/lib/kotludens/com/persesgames/game/Game.kt +++ b/lib/kotludens/com/persesgames/game/Game.kt @@ -213,7 +213,7 @@ fun gameLoop() { if (!Textures.ready()) { - Game.gl().clearColor(1f, 0f, 0f, 1f) + Game.gl().clearColor(1f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) } else { resize(); diff --git a/lib/kotludens/com/persesgames/input/Keys.kt b/lib/kotludens/com/persesgames/input/Keys.kt new file mode 100644 index 0000000..a30b761 --- /dev/null +++ b/lib/kotludens/com/persesgames/input/Keys.kt @@ -0,0 +1,96 @@ +package com.persesgames.input + +import org.w3c.dom.events.Event +import org.w3c.dom.events.KeyboardEvent +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:18 + */ + +enum class KeyCode(val keyCode: Int) { + LEFT(37), + UP(38), + DOWN(40), + RIGHT(39), + SPACE(32), +} + +interface InputProcessor { + + fun keyPressed(charCode: Int) + + fun keyDown(keyCode: Int) + + fun keyUp(keyCode: Int) + +} + +class DefaultProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + } + + override fun keyUp(keyCode: Int) { + } +} + +object Keys { + + private val keys: MutableMap = HashMap(); + private var inputProcesser: InputProcessor = DefaultProcessor() + + init { + val body = document.body + if (body != null) { + body.on("keydown", true) { + Keys.keyDown(it) + } + + body.on("keyup", true) { + Keys.keyUp(it) + } + + body.on("keypress", true) { + Keys.keyPress(it) + } + } + } + + fun setInputProcessor(processor: InputProcessor) { + this.inputProcesser = processor + } + + private fun keyDown(key: Event) { + if (key is KeyboardEvent) { + keys.put(key.keyCode, Date().getTime()) + + inputProcesser.keyDown(key.keyCode) + } + } + + private fun keyUp(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyUp(key.keyCode) + + keys.remove(key.keyCode) + } + } + + private fun keyPress(key: Event) { + if (key is KeyboardEvent) { + inputProcesser.keyPressed(key.charCode) + } + } + + fun isDown(keyCode: Int) = keys.containsKey(keyCode) + + fun isDown(keyCode: KeyCode) = keys.containsKey(keyCode.keyCode) + +} diff --git a/lib/kotludens/com/persesgames/sound/Music.kt b/lib/kotludens/com/persesgames/sound/Music.kt new file mode 100644 index 0000000..7e1879b --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Music.kt @@ -0,0 +1,47 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document +import kotlin.dom.on + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 13:02 + */ + +object Music { + val playing: MutableSet = HashSet() + + fun load(url: String): HTMLAudioElement { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + + return audio; + } + + fun play(url: String, volume: Double = 0.75, looping: Boolean = false) { + val audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.volume = volume + audio.play() + + audio.on("ended", true, { + if (looping) { + audio.currentTime = 0.0 + audio.play() + } else { + println("REMOVING: $audio") + audio.remove() + playing.remove(audio) + } + }) + } + + fun stopAll() { + + } +} diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt new file mode 100644 index 0000000..3fc614f --- /dev/null +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -0,0 +1,55 @@ +package com.persesgames.sound + +import org.w3c.dom.HTMLAudioElement +import java.util.* +import kotlin.browser.document + +/** + * User: rnentjes + * Date: 18-5-16 + * Time: 12:34 + */ + +class Sound(val name:String, val url: String, val volume: Double = 0.75) { + var audio: HTMLAudioElement + + init { + println("CREATING: $name") + audio = document.createElement("audio") as HTMLAudioElement + + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } + + fun play() { + println("PLAYING: $name") + audio.currentTime = 0.0 + audio.play() + } + + fun pause() { + audio.pause() + } +} + +object Sounds { + val sounds: MutableMap = HashMap() + + fun load(name: String, url: String, volume: Double = 0.75 ) { + sounds.put(name, Sound(name, url, volume)) + } + + fun play(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.play() + } + + fun pause(name: String) { + val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") + + sound.pause() + } +} diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index e78798a..12042ca 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -2,47 +2,87 @@ import com.persesgames.game.Game import com.persesgames.game.Screen +import com.persesgames.input.InputProcessor +import com.persesgames.input.KeyCode +import com.persesgames.input.Keys +import com.persesgames.sound.Music +import com.persesgames.sound.Sounds import com.persesgames.sprite.Sprite import com.persesgames.sprite.SpriteBatch import com.persesgames.text.Texts import com.persesgames.texture.Textures -import org.khronos.webgl.WebGLRenderingContext /** * Created by rnentjes on 19-4-16. */ +class GameInputProcessor: InputProcessor { + override fun keyDown(keyCode: Int) { + } + + override fun keyPressed(charCode: Int) { + println("charCode: $charCode") + if (charCode == 32) { + Music.play("sounds/Explosion7.ogg", 0.5) + } + } + + override fun keyUp(keyCode: Int) { + } + +} + class WelcomeScreen: Screen() { var sprites = SpriteBatch() - var x = 1f - var y = 1f + var x = 0f + var y = 0f var sprite = Sprite("SHIP") override fun loadResources() { Textures.load("SHIP", "images/ship2.png") + Sounds.load("EXPLOSION", "sounds/Explosion7.ogg") + + Music.play("music/DST-TechnoBasic.ogg", 1.0, looping = true) + + Keys.setInputProcessor(GameInputProcessor()) } override fun update(time: Float, delta: Float) { - x = Math.sin(time.toDouble()).toFloat() * 150f - y = Math.cos(time.toDouble()).toFloat() * 150f + val speed = 500f // pixels per second + if (Keys.isDown(KeyCode.LEFT)) { + x -= delta * speed; + } + + if (Keys.isDown(KeyCode.RIGHT)) { + x += delta * speed; + } + + if (Keys.isDown(KeyCode.UP)) { + y += delta * speed; + } + + if (Keys.isDown(KeyCode.DOWN)) { + y -= delta * speed; + } + + if (Keys.isDown(KeyCode.SPACE)) { + //Music.play("sounds/Explosion7.ogg", 0.5) + } } override fun render() { - for (index in 0..2500) { - val x = Math.random() * 1000f - 500f - val y = Math.random() * 1000f - 500f + for (index in 0..100) { + val x = Math.random() * 2000f - 1000f + val y = Math.random() * 2000f - 1000f sprites.draw(sprite, x.toFloat(), y.toFloat()); } sprites.draw(sprite, x, y); - sprites.draw(sprite, -x, y); - sprites.draw(sprite, x, -y); - sprites.draw(sprite, -x, -y); sprites.render() - Texts.drawText(10f, 40f, "Hello! FPS ${Game.fps}", font = "bold 36pt Arial") + Texts.drawText(20f, 80f, "Hello! FPS ${Game.fps}", font = "bold 72pt Arial") } } @@ -57,7 +97,7 @@ } fun main(args: Array) { - Game.view.setToWidth(1000f); + Game.view.setToWidth(4000f); Game.start(WelcomeScreen()) } diff --git a/web/index.html b/web/index.html index 39ffcc1..906bce0 100644 --- a/web/index.html +++ b/web/index.html @@ -17,7 +17,6 @@ - diff --git a/web/js/test.js b/web/js/test.js deleted file mode 100644 index 6a0dafa..0000000 --- a/web/js/test.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Created by rnentjes on 11-5-16. - */ - -var bla = new Float32Array(1); - -bla[0]=1; diff --git a/web/music/DST-TacticalSpace.ogg b/web/music/DST-TacticalSpace.ogg new file mode 100644 index 0000000..5e5b834 --- /dev/null +++ b/web/music/DST-TacticalSpace.ogg Binary files differ diff --git a/web/music/DST-TechnoBasic.ogg b/web/music/DST-TechnoBasic.ogg new file mode 100644 index 0000000..5d62f45 --- /dev/null +++ b/web/music/DST-TechnoBasic.ogg Binary files differ diff --git a/web/sounds/Explosion7.ogg b/web/sounds/Explosion7.ogg new file mode 100644 index 0000000..bf97093 --- /dev/null +++ b/web/sounds/Explosion7.ogg Binary files differ