summaryrefslogtreecommitdiff
path: root/src/main/kotlin/de/romjaki/pluggabledino/states/GameState.kt
blob: db173f2fc5a7784c26e8dacd1034e2f8e99295c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
import org.newdawn.slick.Graphics
import org.newdawn.slick.Input
import org.newdawn.slick.state.BasicGameState
import org.newdawn.slick.state.StateBasedGame

class GameState : BasicGameState() {
    override fun init(container: GameContainer?, game: StateBasedGame?) {
    }

    override fun enter(container: GameContainer?, game: StateBasedGame?) {
        world = GameWorld()
        anthemSound.loop()
        count = 0
    }

    override fun leave(container: GameContainer?, game: StateBasedGame?) {
        anthemSound.pause()
    }

    override fun update(container: GameContainer?, game: StateBasedGame?, delta: Int) {
        game!!
        count += delta

        if (container!!.input.isKeyDown(Input.KEY_R)) {
            world = GameWorld()
        }
        if (world.hurt) {
            lastscore = count / 100
            Events.broadcastEvent(GameLostEvent(lastscore))
            return game.enterState(LOST)
        }
        println(delta)
        world.update(delta / 1000f, container.input.isKeyDown(Input.KEY_UP))
        dinoAnimated.update(delta.toLong())
        Events.broadcastEvent(GameUpdateEvent(game, delta, container, world))
    }

    lateinit var world: GameWorld

    var count: Int = 0

    override fun getID(): Int =
            GAME


    override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) {
        g!!
        container!!
        game!!

        g.scale(WIDTH_RATIO, HEIGHT_RATIO)
        g.background = Color.white
        if (world.hurt) {
            g.background = Color.red
        }
        g.color = Color.green
        g.drawImage(background, 0f, 0f)
        g.drawStringCentered((count / 100).toString(), WIDTH / 2f, HEIGHT / 2f)
        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 * WIDTH / 50, cactus.position.y * HEIGHT / 50 - cactusImg.height)
        }
        for (bird in world.birdd) {
            g.drawImage(BirdImg, bird.position.x * WIDTH / 50, bird.position.y * HEIGHT / 50 - BirdImg.height)
        }
        Events.broadcastEvent(GameRenderEvent(g, game, container))

    }

}