summaryrefslogtreecommitdiff
path: root/src/main/kotlin/de/romjaki
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/de/romjaki')
-rw-r--r--src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt4
-rw-r--r--src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt12
-rw-r--r--src/main/kotlin/de/romjaki/pluggabledino/main.kt7
-rw-r--r--src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt10
-rw-r--r--src/main/kotlin/de/romjaki/pluggabledino/states/LostState.kt39
5 files changed, 63 insertions, 9 deletions
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt
index 9b39e4b..b21761c 100644
--- a/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt
+++ b/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt
@@ -7,11 +7,11 @@ import de.romjaki.pluggabledino.font
import org.newdawn.slick.Graphics
import org.newdawn.slick.Input
import java.awt.Rectangle
-import kotlin.math.min
+import kotlin.math.max
class Button(private val text: String, val x: Float, val y: Float) {
- val width = min(buttonImage.width, font.getWidth(text) + 10)
+ val width = max(buttonImage.width, font.getWidth(text) + 10)
val image = buttonImage.getScaledCopy(width, buttonImage.height)
private var lastClicked = false
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt b/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt
index cc940cf..2495b9c 100644
--- a/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt
+++ b/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt
@@ -78,15 +78,22 @@ class GameWorld : ContactListener {
//#endregion
cactiBodyDef = BodyDef()
+ cactiBodyDef.type = BodyType.KINEMATIC
createCactus()
}
fun createCactus() {
val body = world.createBody(cactiBodyDef)
+ body.position.set(10f, 39f)
val shape = PolygonShape()
shape.setAsBox(1f, 1f)
- body.createFixture(shape, 0f)
+ val cactiFixtureDef = FixtureDef()
+ cactiFixtureDef.shape = dinoBox
+ cactiFixtureDef.isSensor = true
+ cactiFixtureDef.density = 0.1f
+ cactiFixtureDef.friction = 0f
+ body.createFixture(cactiFixtureDef)
cacti.add(body)
}
@@ -98,6 +105,9 @@ class GameWorld : ContactListener {
dino.applyForceToCenter(Vec2(0f, -2000f))
}
}
+ cacti.forEach {
+ it.linearVelocity.set(-delta * 1000, 0f)
+ }
world.step(delta, 4, 3)
world.setContactListener(this)
}
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/main.kt b/src/main/kotlin/de/romjaki/pluggabledino/main.kt
index a829089..1e86140 100644
--- a/src/main/kotlin/de/romjaki/pluggabledino/main.kt
+++ b/src/main/kotlin/de/romjaki/pluggabledino/main.kt
@@ -5,10 +5,7 @@ import de.romjaki.pluggabledino.api.PluginLoader
import de.romjaki.pluggabledino.events.InitEvent
import de.romjaki.pluggabledino.events.PostInitEvent
import de.romjaki.pluggabledino.events.PreInitEvent
-import de.romjaki.pluggabledino.states.GameState
-import de.romjaki.pluggabledino.states.MainMenu
-import de.romjaki.pluggabledino.states.SettingsState
-import de.romjaki.pluggabledino.states.SplashScreen
+import de.romjaki.pluggabledino.states.*
import org.newdawn.slick.AppGameContainer
import org.newdawn.slick.GameContainer
import org.newdawn.slick.Input
@@ -24,6 +21,7 @@ const val SPLASHSCREEN = 0
const val MAINMENU = 1
const val SETTINGS = 3
const val GAME = 2
+const val LOST = 4
fun main(args: Array<String>) {
PluginLoader.loadPlugins()
@@ -45,5 +43,6 @@ class Application : StateBasedGame("Dino Game v$VERSION") {
addState(MainMenu())
addState(SettingsState())
addState(GameState())
+ addState(LostState())
}
}
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt b/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt
index 479f7f7..0d4fff0 100644
--- a/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt
+++ b/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt
@@ -21,6 +21,9 @@ class GameState : BasicGameState() {
if (container!!.input.isKeyDown(Input.KEY_R)) {
world = GameWorld()
}
+ if (world.hurt) {
+ game!!.enterState(LOST)
+ }
world.update(delta / 1000f, container.input)
dinoAnimated.update(delta.toLong())
}
@@ -34,11 +37,14 @@ class GameState : BasicGameState() {
override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) {
g!!
g.background = Color.lightGray
- g.color = Color.red
+ if (world.hurt) {
+ g.background = Color.red
+ }
+ g.color = Color.green
g.drawImage(dinoAnimated.currentFrame, world.dino.position.x * WIDTH / 50, world.dino.position.y * HEIGHT / 50 - dinoAnimated.height)
g.drawImage(groundline, 0f, HEIGHT * 39 / 50f)
for (cactus in world.cacti) {
- g.drawImage(cactusImg, cactus.position.x, cactus.position.y)
+ g.drawImage(cactusImg, cactus.position.x * WIDTH / 50, cactus.position.y * HEIGHT / 50 - cactusImg.height)
}
}
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/states/LostState.kt b/src/main/kotlin/de/romjaki/pluggabledino/states/LostState.kt
new file mode 100644
index 0000000..ce245b6
--- /dev/null
+++ b/src/main/kotlin/de/romjaki/pluggabledino/states/LostState.kt
@@ -0,0 +1,39 @@
+package de.romjaki.pluggabledino.states
+
+import de.romjaki.pluggabledino.*
+import de.romjaki.pluggabledino.api.Button
+import org.newdawn.slick.Color
+import org.newdawn.slick.GameContainer
+import org.newdawn.slick.Graphics
+import org.newdawn.slick.state.BasicGameState
+import org.newdawn.slick.state.StateBasedGame
+
+class LostState : BasicGameState() {
+ override fun init(container: GameContainer?, game: StateBasedGame?) {
+ game!!
+ playAgain.addClickHandler {
+ game.enterState(GAME)
+ }
+ back.addClickHandler {
+ game.enterState(MAINMENU)
+ }
+ }
+
+ override fun update(container: GameContainer?, game: StateBasedGame?, delta: Int) {
+ playAgain.update(container!!.input)
+ back.update(container.input)
+ }
+
+ override fun getID(): Int =
+ LOST
+
+ val back = Button("Back to Main Menu", WIDTH / 2f, HEIGHT / 2f + 50)
+ val playAgain = Button("PLAY AGAIN", WIDTH / 2f, HEIGHT / 2f + 100)
+
+ override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) {
+ g!!
+ g.background = Color.red
+ back.draw(g)
+ playAgain.draw(g)
+ }
+} \ No newline at end of file