diff options
Diffstat (limited to 'src/main/kotlin/de/romjaki/pluggabledino')
8 files changed, 82 insertions, 21 deletions
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt index 7ce8649..8c436c3 100644 --- a/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt @@ -12,6 +12,8 @@ object PluginLoader { private val klaxon = Klaxon() private const val pluginFolder = "plugins" + private var devPlugin: String? = null + val plugins get() = _plugins.toMap() @@ -22,15 +24,20 @@ object PluginLoader { Scanner( cl.getResourceAsStream("plugin.json")) .useDelimiter("\\A") - .next())!! - val pluginClass = cl.loadClass(pluginInfo.clazz) + .next()) ?: throw PluginMetaMissingException(url.toString()) + return loadPlugin(pluginInfo, cl.loadClass(pluginInfo.clazz)) + } + + private fun loadPlugin(pluginInfo: PluginMeta, pluginClass: Class<*>): IPlugin? { if (!IPlugin::class.java.isAssignableFrom(pluginClass)) { return null } val plugin = pluginClass.newInstance() as IPlugin plugin.meta = pluginInfo _plugins[pluginInfo.name] = plugin + return plugin + } private fun checkDependencies() { @@ -56,10 +63,26 @@ object PluginLoader { .map { it.toURI().toURL() }.forEach { loadPlugin(it) } + installDevPlugin() checkDependencies() injectDependencies() } + private fun installDevPlugin() { + if (devPlugin == null) { + return + } + val clazz = Class.forName(devPlugin) + val meta = klaxon.parse<PluginMeta>( + Scanner( + clazz + .classLoader + .getResourceAsStream("plugin.json")) + .useDelimiter("\\A") + .next()) ?: throw PluginMetaMissingException("<dev>") + loadPlugin(meta, clazz) + } + private fun injectDependencies() { for ((name, instance) in _plugins) { @@ -73,4 +96,8 @@ object PluginLoader { } } } + + fun loadDevPlugin(className: String) { + devPlugin = className + } }
\ No newline at end of file diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/PluginMetaMissingException.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/PluginMetaMissingException.kt new file mode 100644 index 0000000..1c1335b --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/PluginMetaMissingException.kt @@ -0,0 +1,3 @@ +package de.romjaki.pluggabledino.api + +class PluginMetaMissingException(s: String) : Throwable("$s plugin is missing a plugin.json") diff --git a/src/main/kotlin/de/romjaki/pluggabledino/events/GameLostEvent.kt b/src/main/kotlin/de/romjaki/pluggabledino/events/GameLostEvent.kt new file mode 100644 index 0000000..cb0ddb9 --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/events/GameLostEvent.kt @@ -0,0 +1,5 @@ +package de.romjaki.pluggabledino.events + +import de.romjaki.pluggabledino.api.Event + +data class GameLostEvent(val score: Int) : Event
\ No newline at end of file diff --git a/src/main/kotlin/de/romjaki/pluggabledino/events/GameRenderEvent.kt b/src/main/kotlin/de/romjaki/pluggabledino/events/GameRenderEvent.kt new file mode 100644 index 0000000..b2925e7 --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/events/GameRenderEvent.kt @@ -0,0 +1,9 @@ +package de.romjaki.pluggabledino.events + +import de.romjaki.pluggabledino.api.Event +import org.newdawn.slick.GameContainer +import org.newdawn.slick.Graphics +import org.newdawn.slick.state.StateBasedGame + + +data class GameRenderEvent(val graphics: Graphics, val game: StateBasedGame, val container: GameContainer) : Event
\ No newline at end of file diff --git a/src/main/kotlin/de/romjaki/pluggabledino/events/GameUpdateEvent.kt b/src/main/kotlin/de/romjaki/pluggabledino/events/GameUpdateEvent.kt new file mode 100644 index 0000000..550c853 --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/events/GameUpdateEvent.kt @@ -0,0 +1,8 @@ +package de.romjaki.pluggabledino.events + +import de.romjaki.pluggabledino.api.Event +import de.romjaki.pluggabledino.game.GameWorld +import org.newdawn.slick.Game +import org.newdawn.slick.GameContainer + +data class GameUpdateEvent(val game: Game, val delta: Int, val container: GameContainer, val world: GameWorld) : Event
\ No newline at end of file diff --git a/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt b/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt index edafa69..b881a4a 100644 --- a/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt +++ b/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt @@ -1,7 +1,5 @@ package de.romjaki.pluggabledino.game -import de.romjaki.pluggabledino.highscore -import de.romjaki.pluggabledino.score import org.jbox2d.callbacks.ContactImpulse import org.jbox2d.callbacks.ContactListener import org.jbox2d.collision.Manifold @@ -10,7 +8,7 @@ import org.jbox2d.common.Vec2 import org.jbox2d.dynamics.* import org.jbox2d.dynamics.contacts.Contact import org.newdawn.slick.Input -import java.util.Random +import java.util.* class GameWorld : ContactListener { override fun endContact(contact: Contact?) { @@ -118,22 +116,16 @@ class GameWorld : ContactListener { fun update(delta: Float, input: Input) { if (input.isKeyDown(Input.KEY_UP)) { - if (isDinoOnGround()) { - print("Jump") - dino.applyForceToCenter(Vec2(0f, -4250f)) - } + tryJump(); } delay -= delta - if (delay < 0) { + if (delay < 0) { createCactus1() - delay = random.nextFloat() + rand(2, 3) + delay = random.nextFloat() + rand(2, 3) } - - - cacti.forEach { it.linearVelocity.set(-delta * speed, 0f) } @@ -142,6 +134,12 @@ class GameWorld : ContactListener { world.setContactListener(this) } + fun tryJump() { + if (isDinoOnGround()) { + dino.applyForceToCenter(Vec2(0f, -4250f)) + } + } + private fun isDinoOnGround(): Boolean = dino.linearVelocity.y == 0.0f diff --git a/src/main/kotlin/de/romjaki/pluggabledino/main.kt b/src/main/kotlin/de/romjaki/pluggabledino/main.kt index 07ca687..970f38e 100644 --- a/src/main/kotlin/de/romjaki/pluggabledino/main.kt +++ b/src/main/kotlin/de/romjaki/pluggabledino/main.kt @@ -26,11 +26,14 @@ const val MAINMENU = 1 const val SETTINGS = 3 const val GAME = 2 const val LOST = 4 -var lastscore = 0 +var lastscore = 0 var highscore = 0 var score = 0 fun main(args: Array<String>) { + if (args.size > 1 && args[0] == "dev") { + PluginLoader.loadDevPlugin(args[1]) + } PluginLoader.loadPlugins() Input.disableControllers() val app = AppGameContainer(Application()) diff --git a/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt b/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt index e579933..5786837 100644 --- a/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt +++ b/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt @@ -1,6 +1,10 @@ package de.romjaki.pluggabledino.states import de.romjaki.pluggabledino.* +import de.romjaki.pluggabledino.api.Events +import de.romjaki.pluggabledino.events.GameLostEvent +import de.romjaki.pluggabledino.events.GameRenderEvent +import de.romjaki.pluggabledino.events.GameUpdateEvent import de.romjaki.pluggabledino.game.GameWorld import org.newdawn.slick.Color import org.newdawn.slick.GameContainer @@ -16,10 +20,10 @@ class GameState : BasicGameState() { override fun enter(container: GameContainer?, game: StateBasedGame?) { world = GameWorld() count = 0 - -} + } override fun update(container: GameContainer?, game: StateBasedGame?, delta: Int) { + game!! count += delta if (container!!.input.isKeyDown(Input.KEY_R)) { @@ -27,10 +31,12 @@ class GameState : BasicGameState() { } if (world.hurt) { lastscore = count / 100 - game!!.enterState(LOST) + Events.broadcastEvent(GameLostEvent(lastscore)) + return game.enterState(LOST) } world.update(delta / 1000f, container.input) dinoAnimated.update(delta.toLong()) + Events.broadcastEvent(GameUpdateEvent(game, delta, container, world)) } lateinit var world: GameWorld @@ -42,9 +48,9 @@ class GameState : BasicGameState() { override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) { - - g!! + container!! + game!! g.scale(WIDTH_RATIO, HEIGHT_RATIO) g.drawStringCentered((count / 100).toString(), WIDTH / 2f, HEIGHT / 2f) @@ -59,6 +65,8 @@ class GameState : BasicGameState() { g.drawImage(cactusImg, cactus.position.x * WIDTH / 50, cactus.position.y * HEIGHT / 50 - cactusImg.height) } - } + Events.broadcastEvent(GameRenderEvent(g, game, container)) + + } } |