Commit 4a7c464f authored by Nico Eckes's avatar Nico Eckes

Fix lockups, WIP: storing

parent 8de5c691
......@@ -14,11 +14,10 @@ import java.io.Serializable
@SuppressLint("ViewConstructor")
class Game(@Transient private val gameActivity: GameActivity, val options: GameOptions) : SurfaceView(gameActivity),
SurfaceHolder.Callback, Serializable {
@Transient
var gameThread: GameThread? = null
var ticks = 0
private set
var collectedCoins = 0
private var paused = true
private lateinit var pathManager: PathManager
private lateinit var player: Player
......@@ -30,12 +29,11 @@ class Game(@Transient private val gameActivity: GameActivity, val options: GameO
var speed: Float = options.difficulty.initialSpeed
private var background = Color.BLACK
private val highscore
get() = collectedCoins * Constants.pointsCoinSimple + (ticks * 6) / Constants.targetFPS //????
get() = collectedCoins * Constants.pointsCoinSimple + ticks * Constants.pointsPerTick
init {
holder.addCallback(this)
isFocusable = true
}
override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
......@@ -46,20 +44,16 @@ class Game(@Transient private val gameActivity: GameActivity, val options: GameO
player.setPos((width / 2).toFloat(), (height / 2).toFloat())
pathManager.player = player
}
gameThread?.computeFrame()
}
override fun surfaceChanged(surfaceHolder: SurfaceHolder, format: Int, width: Int, height: Int) = Unit
override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
gameThread?.interrupt()
}
override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) = Unit
@SuppressLint("ClickableViewAccessibility" /* performClick would be useless */)
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
if (gameThread?.paused != false) {
if (paused) {
if (player.intersects(Path().apply {
addRect(
event.x,
......@@ -70,29 +64,30 @@ class Game(@Transient private val gameActivity: GameActivity, val options: GameO
)
})) {
player.setTouchPos(event.x, event.y)
gameThread?.paused = false
paused = false
}
} else {
player.setTouchPos(event.x, event.y)
}
}
MotionEvent.ACTION_UP -> {
gameThread?.paused = true
player.setTouchPos(event.x, event.y)
paused = true
}
}
return true
}
fun update() {
player.update()
pathManager.update()
if (ticks % 30 == 0)
speed += options.difficulty.speedPerSecond
if (player.invincible)
blink()
ticks++
if (!paused) {
player.update()
pathManager.update()
if (ticks % 30 == 0)
speed += options.difficulty.speedPerSecond
if (player.invincible)
blink()
ticks++
}
}
override fun draw(canvas: Canvas) {
......@@ -137,7 +132,7 @@ class Game(@Transient private val gameActivity: GameActivity, val options: GameO
}
fun gameEnd() {
gameThread?.interrupt()
gameActivity.gameEnd()
gameActivity.runOnUiThread {
EndGameDialog.show(gameActivity, highscore)
}
......
......@@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity
import io.ender.pathchaser.util.Preferences
class GameActivity : AppCompatActivity() {
private var gameThread: GameThread? = null
lateinit var game: Game
override fun onCreate(savedInstanceState: Bundle?) {
......@@ -16,8 +17,8 @@ class GameActivity : AppCompatActivity() {
override fun onPause() {
super.onPause()
game.gameThread?.interrupt()
game.gameThread = null
gameThread?.interrupt()
gameThread = null
}
override fun onSaveInstanceState(outState: Bundle) {
......@@ -32,7 +33,7 @@ class GameActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
game.gameThread = GameThread(game.holder, game)
gameThread = GameThread(game)
}
override fun onWindowFocusChanged(hasFocus: Boolean) { // fullscreen trick
......@@ -46,4 +47,8 @@ class GameActivity : AppCompatActivity() {
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
}
}
fun gameEnd() {
gameThread?.interrupt()
}
}
package io.ender.pathchaser
import android.view.SurfaceHolder
import android.util.Log
import io.ender.pathchaser.util.Constants
import java.util.*
class GameThread(private val surfaceholder: SurfaceHolder, private val game: Game) : Thread() {
class GameThread(private val game: Game) : Thread() {
companion object {
private const val targetTime = (1000 / Constants.targetFPS)
}
var paused = true
init {
name = "PathChaser Game thread"
computeFrame()
......@@ -18,38 +15,36 @@ class GameThread(private val surfaceholder: SurfaceHolder, private val game: Gam
}
override fun run() {
while (true) computeFrame()
Log.d("GAME", "starting thread")
try {
while (true) computeFrame()
} catch (e: InterruptedException) {
}
}
fun computeFrame() {
private fun computeFrame() {
Log.d("GAME2", "tick")
val start = System.currentTimeMillis()
if (!paused) {
val canvas = surfaceholder.lockCanvas()
if (canvas != null) {
try {
synchronized(canvas) {
game.update()
game.draw(canvas)
}
} catch (e: Exception) {
e.printStackTrace()
}
game.update()
surfaceholder.unlockCanvasAndPost(canvas)
val canvas = game.holder.lockCanvas()
if (canvas != null) {
try {
synchronized(canvas) {
game.draw(canvas)
}
} catch (e: Exception) {
e.printStackTrace()
}
game.holder.unlockCanvasAndPost(canvas)
}
val waitTime = targetTime - (System.currentTimeMillis() - start)
println("${Date()},$waitTime")
if (waitTime > 0) {
try {
Thread.sleep(waitTime)
} catch (e: InterruptedException) {
return
}
Thread.sleep(waitTime)
}
}
}
......@@ -8,7 +8,7 @@ import splitties.resources.appStr
object Constants {
const val playerRadius = 50f
const val startCircleRadius = 200f
const val pointsPerPixel = 0.5
const val pointsPerTick = 1
const val pointsCoinSimple = 100
const val blinkMs = 300L
const val invincibleSec = 4
......
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