Created
January 3, 2022 16:38
-
-
Save IvanShafran/86dc2ecfba6fa6577a7621c8eca7ed8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Move the cake off the screen right away | |
private fun initializeCake() { | |
cakeSize = height / 8 | |
moveCakeToStartPoint() | |
} | |
private fun moveCakeToStartPoint() { | |
// Choose a random position | |
cakeRect.left = width + width * Random.nextFloat() | |
cakeRect.right = cakeRect.left + cakeSize | |
// Choose a random line | |
val isTopLine = Random.nextBoolean() | |
cakeRect.top = getObjectYTopForLine(cakeSize, isTopLine).toFloat() | |
cakeRect.bottom = cakeRect.top + cakeSize | |
} | |
// Mpve the cake by time | |
private fun moveCake() { | |
val currentTime = System.currentTimeMillis() | |
val deltaTime = currentTime - previousTimestamp | |
val deltaX = cakeSpeed * width * deltaTime | |
cakeRect.left -= deltaX | |
cakeRect.right = cakeRect.left + cakeSize | |
previousTimestamp = currentTime | |
} | |
// If the cake and the player intersect each other then count a point | |
private fun checkPlayerCaughtCake() { | |
if (RectF.intersects(playerRect, cakeRect)) { | |
score += if (flags.isLeftEyeOpen && flags.isRightEyeOpen) 1 else 2 | |
moveCakeToStartPoint() | |
} | |
} | |
// If the player misses the cake then move the cake off the screen again | |
private fun checkCakeIsOutOfScreenStart() { | |
if (cakeRect.right < 0) { | |
moveCakeToStartPoint() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment