diff --git a/KotlinTest.ipr b/KotlinTest.ipr index a15861c..8e60e82 100644 --- a/KotlinTest.ipr +++ b/KotlinTest.ipr @@ -93,9 +93,6 @@ - - - + + + diff --git a/KotlinTest.ipr b/KotlinTest.ipr index a15861c..8e60e82 100644 --- a/KotlinTest.ipr +++ b/KotlinTest.ipr @@ -93,9 +93,6 @@ - - - + + + diff --git a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt index 55bfa02..cfe6a92 100644 --- a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt +++ b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt @@ -10,10 +10,6 @@ * Created by rnentjes on 22-7-16. */ -class MapRenderer(data: MapData) { - -} - class MapData { var version: Int = 1 var properties: MutableMap = HashMap() @@ -127,10 +123,13 @@ val tw = 1f / tilesHor.toFloat() val th = 1f / tilesVer.toFloat() - tcLeft = xi * tw - tcTop = yi * th - tcRight = tcLeft + tw - tcBottom = tcTop - th + val pixelW = 0.1f / tileset.tilewidth + val pixelH = 0.1f / tileset.tileheight + + tcLeft = xi * tw + pixelW + tcTop = yi * th + pixelH + tcRight = tcLeft + tw - pixelW + tcBottom = tcTop - th - pixelH } } } diff --git a/KotlinTest.ipr b/KotlinTest.ipr index a15861c..8e60e82 100644 --- a/KotlinTest.ipr +++ b/KotlinTest.ipr @@ -93,9 +93,6 @@ - - - + + + diff --git a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt index 55bfa02..cfe6a92 100644 --- a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt +++ b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt @@ -10,10 +10,6 @@ * Created by rnentjes on 22-7-16. */ -class MapRenderer(data: MapData) { - -} - class MapData { var version: Int = 1 var properties: MutableMap = HashMap() @@ -127,10 +123,13 @@ val tw = 1f / tilesHor.toFloat() val th = 1f / tilesVer.toFloat() - tcLeft = xi * tw - tcTop = yi * th - tcRight = tcLeft + tw - tcBottom = tcTop - th + val pixelW = 0.1f / tileset.tilewidth + val pixelH = 0.1f / tileset.tileheight + + tcLeft = xi * tw + pixelW + tcTop = yi * th + pixelH + tcRight = tcLeft + tw - pixelW + tcBottom = tcTop - th - pixelH } } } diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt index 08a147e..8d8c549 100644 --- a/lib/kotludens/com/persesgames/sound/Sounds.kt +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -10,36 +10,42 @@ * Time: 12:34 */ -class Sound(val name:String, val url: String, val volume: Double = 0.75) { - var audio: HTMLAudioElement +class Sound(val name:String, val url: String, val volume: Double = 0.75, val numberOfChannels: Int) { + var channels: Array + var nextChannel: Int = 0 init { println("CREATING: $name") - audio = document.createElement("audio") as HTMLAudioElement + channels = Array(numberOfChannels, { document.createElement("audio") as HTMLAudioElement }) - - audio.src = url - audio.pause() - audio.load() - audio.volume = volume + for (audio in channels) { + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } } fun play() { - println("PLAYING: $name") - audio.currentTime = 0.0 - audio.play() + println("PLAYING: $name - $nextChannel") + channels[nextChannel].currentTime = 0.0 + channels[nextChannel].play() + + nextChannel = (nextChannel + 1) % channels.size } fun pause() { - audio.pause() + for (audio in channels) { + 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 load(name: String, url: String, volume: Double = 0.75, channels: Int = 1 ) { + sounds.put(name, Sound(name, url, volume, channels)) } fun play(name: String, volume: Float = 0.75f) { diff --git a/KotlinTest.ipr b/KotlinTest.ipr index a15861c..8e60e82 100644 --- a/KotlinTest.ipr +++ b/KotlinTest.ipr @@ -93,9 +93,6 @@ - - - + + + diff --git a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt index 55bfa02..cfe6a92 100644 --- a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt +++ b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt @@ -10,10 +10,6 @@ * Created by rnentjes on 22-7-16. */ -class MapRenderer(data: MapData) { - -} - class MapData { var version: Int = 1 var properties: MutableMap = HashMap() @@ -127,10 +123,13 @@ val tw = 1f / tilesHor.toFloat() val th = 1f / tilesVer.toFloat() - tcLeft = xi * tw - tcTop = yi * th - tcRight = tcLeft + tw - tcBottom = tcTop - th + val pixelW = 0.1f / tileset.tilewidth + val pixelH = 0.1f / tileset.tileheight + + tcLeft = xi * tw + pixelW + tcTop = yi * th + pixelH + tcRight = tcLeft + tw - pixelW + tcBottom = tcTop - th - pixelH } } } diff --git a/lib/kotludens/com/persesgames/sound/Sounds.kt b/lib/kotludens/com/persesgames/sound/Sounds.kt index 08a147e..8d8c549 100644 --- a/lib/kotludens/com/persesgames/sound/Sounds.kt +++ b/lib/kotludens/com/persesgames/sound/Sounds.kt @@ -10,36 +10,42 @@ * Time: 12:34 */ -class Sound(val name:String, val url: String, val volume: Double = 0.75) { - var audio: HTMLAudioElement +class Sound(val name:String, val url: String, val volume: Double = 0.75, val numberOfChannels: Int) { + var channels: Array + var nextChannel: Int = 0 init { println("CREATING: $name") - audio = document.createElement("audio") as HTMLAudioElement + channels = Array(numberOfChannels, { document.createElement("audio") as HTMLAudioElement }) - - audio.src = url - audio.pause() - audio.load() - audio.volume = volume + for (audio in channels) { + audio.src = url + audio.pause() + audio.load() + audio.volume = volume + } } fun play() { - println("PLAYING: $name") - audio.currentTime = 0.0 - audio.play() + println("PLAYING: $name - $nextChannel") + channels[nextChannel].currentTime = 0.0 + channels[nextChannel].play() + + nextChannel = (nextChannel + 1) % channels.size } fun pause() { - audio.pause() + for (audio in channels) { + 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 load(name: String, url: String, volume: Double = 0.75, channels: Int = 1 ) { + sounds.put(name, Sound(name, url, volume, channels)) } fun play(name: String, volume: Float = 0.75f) { diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index 77b30c0..799fa85 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -5,10 +5,8 @@ import com.persesgames.input.EmptyInputProcessor import com.persesgames.input.KeyCode import com.persesgames.input.Keys -import com.persesgames.map.tiled.MapRenderer import com.persesgames.map.tiled.TiledMap import com.persesgames.sound.Music -import com.persesgames.sound.Sound import com.persesgames.sound.Sounds import com.persesgames.sprite.Sprite import com.persesgames.sprite.SpriteBatch @@ -22,7 +20,7 @@ * Created by rnentjes on 19-4-16. */ -class GameInputProcessor: EmptyInputProcessor() { +class GameInputProcessor : EmptyInputProcessor() { override fun keyPressed(charCode: Int) { println("charCode: $charCode") @@ -41,7 +39,7 @@ var music: HTMLAudioElement? = null var showFPS: Boolean = true -class WelcomeScreen: Screen() { +class WelcomeScreen : Screen() { override fun loadResources() { println("loading resource!") @@ -53,8 +51,7 @@ Keys.setInputProcessor(GameInputProcessor()) } - override fun update(time: Float, delta: Float) { - } + override fun update(time: Float, delta: Float) { } override fun render() { @@ -64,7 +61,7 @@ } } -class GameScreen: Screen() { +class GameScreen : Screen() { val map = TiledMap("maps", "level_1_01.json") var sprites = SpriteBatch() @@ -77,8 +74,8 @@ override fun loadResources() { Textures.load("SHIP", "images/ship2.png") - Sounds.load("EXPLOSION", "sounds/Explosion7.ogg") - Sounds.load("DROP", "sounds/Bomb_Drop.ogg") + Sounds.load("EXPLOSION", "sounds/Explosion7.ogg", channels = 2) + Sounds.load("DROP", "sounds/Bomb_Drop.ogg", channels = 4) music = Music.play("music/DST-TechnoBasic.mp3", 0.5, looping = true) @@ -87,6 +84,7 @@ println("width: ${map.data.width}") println("height: ${map.data.height}") println("layers: ${map.data.layers?.size}") + val layers = map.data.layers if (layers != null) { println("layer0: ${layers[0].name}") @@ -152,8 +150,8 @@ Texts.drawText(20f, 150f, "Drawing $numberOfSprites sprites per frame.") if (showFPS) { - Texts.drawText(20f, 100f, "FPS ${Game.fps}", font = "bold 72pt Arial", fillStyle="red") - Texts.drawText(15f, -20f, "Music by DST", font = "bold 28pt Arial", fillStyle="green") + Texts.drawText(20f, 100f, "FPS ${Game.fps}", font = "bold 72pt Arial", fillStyle = "red") + Texts.drawText(15f, -20f, "Music by DST", font = "bold 28pt Arial", fillStyle = "green") } }