summaryrefslogtreecommitdiff
path: root/src/main/kotlin/de/romjaki/pluggabledino/game/GameWorld.kt
blob: 87b7a2c76a0511509d9e9d38a6714d4b5cab27fe (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package de.romjaki.pluggabledino.game



import org.jbox2d.callbacks.ContactImpulse
import org.jbox2d.callbacks.ContactListener
import org.jbox2d.collision.Manifold
import org.jbox2d.collision.shapes.PolygonShape
import org.jbox2d.common.Vec2
import org.jbox2d.dynamics.*
import org.jbox2d.dynamics.contacts.Contact
import org.newdawn.slick.Input
import java.util.*

class GameWorld : ContactListener {
    override fun endContact(contact: Contact?) {

    }

    override fun beginContact(contact: Contact?) {
        contact!!
        val bodies = listOf(contact.fixtureA.body, contact.fixtureB.body)
        if (bodies.contains(dino) && cacti.any { bodies.contains(it) }) {
            hurt = true

        }
        else if (bodies.contains(dino) && birdd.any { bodies.contains(it) }) {
            hurt = true

        }
    }

    override fun preSolve(contact: Contact?, oldManifold: Manifold?) {
    }

    override fun postSolve(contact: Contact?, impulse: ContactImpulse?) {
    }

    var hurt = false

    val world: World

    val cacti = mutableListOf<Body>()

    val cactiBodyDef: BodyDef

    val birdd = mutableListOf<Body>()

    val birddBodyDef: BodyDef

    var speed = 1000

    val groundBody: Body

    val random = Random()

    var delay = 0f

    var bg = 0f

    val dino: Body

    val groundBodyDef: BodyDef

    val groundBox: PolygonShape

    val dinoDef: BodyDef

    val dinoBox: PolygonShape

    val dinoFixtureDef: FixtureDef

    init {
        val gravity = Vec2(0f, 40f)
        world = World(gravity)

        //#region GROUND
        groundBodyDef = BodyDef()
        groundBodyDef.position.set(0f, 50f)
        groundBody = world.createBody(groundBodyDef)
        groundBox = PolygonShape()
        groundBox.setAsBox(50f, 10f)
        groundBody.createFixture(groundBox, 0f)
        //#endregion

        //#region DINO
        dinoDef = BodyDef()
        dinoDef.type = BodyType.DYNAMIC
        dinoDef.position.set(4f, 39f)
        dino = world.createBody(dinoDef)
        dinoBox = PolygonShape()
        dinoBox.setAsBox(1f, 1f)
        dinoFixtureDef = FixtureDef()
        dinoFixtureDef.shape = dinoBox
        dinoFixtureDef.density = 0.9f
        dinoFixtureDef.friction = 0.3f
        dino.createFixture(dinoFixtureDef)
        //#endregion

        cactiBodyDef = BodyDef()
        cactiBodyDef.type = BodyType.KINEMATIC

        birddBodyDef = BodyDef()
        birddBodyDef.type = BodyType.KINEMATIC

        createCactus1()
        createBird1()
    }






    fun rand(from: Int, to: Int): Int {
        return random.nextInt(to - from) + from
    }

    fun createBird1(){



        val body = world.createBody(birddBodyDef)
        body.position.set(50f, 33f)
        val shape = PolygonShape()
        shape.setAsBox(1f, 1f)
        val birddFixtureDef = FixtureDef()
        birddFixtureDef.shape = dinoBox
        birddFixtureDef.isSensor = true
        birddFixtureDef.density = 0.1f
        birddFixtureDef.friction = 0f
        body.createFixture(birddFixtureDef)
        birdd.add(body)
        speed += 0

    }




    fun createCactus1() {

        val body = world.createBody(cactiBodyDef)
        body.position.set(100f, 39f)
        val shape = PolygonShape()
        shape.setAsBox(1f, 1f)
        val cactiFixtureDef = FixtureDef()
        cactiFixtureDef.shape = dinoBox
        cactiFixtureDef.isSensor = true
        cactiFixtureDef.density = 0.1f
        cactiFixtureDef.friction = 0f
        body.createFixture(cactiFixtureDef)
        cacti.add(body)
        speed += 20


    }


    fun update(delta: Float, input: Input) {
        if (input.isKeyDown(Input.KEY_UP)) {
            tryJump()
        }
        delay -= delta
        bg -= delta

        if (delay < 0) {

            createCactus1()

            delay = random.nextFloat() + rand(2, 3)
        }

        if (bg < -5) {


            createBird1()
            bg =  random.nextFloat() + rand(1, 2)
        }
        cacti.forEach {
            it.linearVelocity.set(-delta * speed, 0f)
        }
        birdd.forEach {
            it.linearVelocity.set(-delta * speed, 0f)
        }

        world.step(delta, 4, 3)
        world.setContactListener(this)
    }

    fun tryJump() {
        if (isDinoOnGround()) {
            dino.applyForceToCenter(Vec2(0f, -4250f))
        }
    }

    private fun isDinoOnGround(): Boolean =
            dino.linearVelocity.y == 0.0f

}