Commit 57091560 authored by Daniel Drießen's avatar Daniel Drießen

Game now properly stops after endGame()

Bug fixed that allows to go back to the EndGameDialog
parent 6bc187a1
package io.ender.pathchaser
import android.app.Activity
import android.app.ActivityManager
import android.app.Dialog
import android.os.Bundle
import android.view.View
......@@ -32,21 +34,25 @@ class EndGameDialog private constructor(a: GameActivity, private val score: Int)
btnRestart.setOnClickListener {
writeHighscore()
ownerActivity?.finish()
context.startActivity<GameActivity> {}
}
btnHighscores.setOnClickListener {
writeHighscore()
ownerActivity?.finish()
context.startActivity<Highscores> { }
}
btnMainMenu.setOnClickListener {
writeHighscore()
ownerActivity?.finish()
context.startActivity<MainMenu> { }
}
setOnDismissListener {
writeHighscore()
ownerActivity?.finish()
context.startActivity<MainMenu> { }
}
}
......@@ -69,6 +75,7 @@ class EndGameDialog private constructor(a: GameActivity, private val score: Int)
layout.width = WindowManager.LayoutParams.MATCH_PARENT
layout.height = WindowManager.LayoutParams.WRAP_CONTENT
diag.show()
diag.ownerActivity = a
diag.window!!.attributes = layout
}
}
......
......@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.*
import android.os.SystemClock.sleep
import android.util.DisplayMetrics
import android.view.MotionEvent
import android.view.SurfaceHolder
......@@ -14,7 +15,7 @@ import io.ender.pathchaser.util.intersects
import java.io.Serializable
@SuppressLint("ViewConstructor")
class Game(private val gameActivity: GameActivity, context: Context, val options: GameOptions) : SurfaceView(context),
class Game(private val gameActivity: GameActivity, val options: GameOptions) : SurfaceView(gameActivity),
SurfaceHolder.Callback, Serializable {
@Transient
lateinit var gameThread: GameThread
......@@ -24,7 +25,7 @@ class Game(private val gameActivity: GameActivity, context: Context, val options
private lateinit var pathManager: PathManager
private lateinit var player: Player
var size = PointF().apply {
DisplayMetrics().also { (context as Activity).windowManager.defaultDisplay.getMetrics(it) }
DisplayMetrics().also { gameActivity.windowManager.defaultDisplay.getMetrics(it) }
.let { set(it.widthPixels.toFloat(), it.heightPixels.toFloat()) }
}
var speed: Float = options.difficulty.initialSpeed
......@@ -147,6 +148,7 @@ class Game(private val gameActivity: GameActivity, context: Context, val options
fun gameEnd() {
gameover = true
sleep(200)
gameActivity.runOnUiThread {
EndGameDialog.show(gameActivity, highscore)
}
......
......@@ -13,7 +13,7 @@ class GameActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
game = Game(this,
game = Game(
this,
Game.GameOptions(Preferences.difficulty)
)
......@@ -34,7 +34,7 @@ class GameActivity : AppCompatActivity() {
game.gameThread = GameThread(game.holder, game)
}
} else {
game = Game(this,
game = Game(
this,
Game.GameOptions(Preferences.difficulty)
)
......
......@@ -15,7 +15,7 @@ class GameThread(private val surfaceholder: SurfaceHolder, private val game: Gam
override fun run() {
start = System.currentTimeMillis().toDouble()
while (running)
while (running && !game.gameover)
computeFrame()
}
......
......@@ -6,6 +6,7 @@ import io.ender.pathchaser.paths.PathManager
import io.ender.pathchaser.util.Constants
import io.ender.pathchaser.util.contains
import io.ender.pathchaser.util.intersects
import kotlin.concurrent.thread
class Player(pm: PathManager) : Sprite(pm) {
var lives = pm.game.options.difficulty.lives
......@@ -17,7 +18,7 @@ class Player(pm: PathManager) : Sprite(pm) {
field = value
} else {
field = 0
pm.game.gameEnd()
thread { pm.game.gameEnd() }
}
}
var invincible = false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment