Created
January 3, 2022 16:32
-
-
Save IvanShafran/e8ed33b5b41050f4ee3f35ae817fa1b5 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
private var playerSize = 0 | |
private var playerRect = RectF() | |
// Initialize size depending on screen size | |
private fun initializePlayer() { | |
playerSize = height / 4 | |
playerRect.left = playerSize / 2f | |
playerRect.right = playerRect.left + playerSize | |
} | |
// Saving emotion flags | |
private var flags: EmotionFlags | |
// Set position depending on smile flag | |
private fun movePlayer() { | |
playerRect.top = getObjectYTopForLine(playerSize, isTopLine = flags.isSmile).toFloat() | |
playerRect.bottom = playerRect.top + playerSize | |
} | |
// We get the top object position | |
// to draw it in the center of the first or the second line | |
private fun getObjectYTopForLine(size: Int, isTopLine: Boolean): Int { | |
return if (isTopLine) { | |
width / 2 - width / 4 - size / 2 | |
} else { | |
width / 2 + width / 4 - size / 2 | |
} | |
} | |
// Saving paint to reuse it in onDraw | |
private val playerPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { | |
style = Paint.Style.FILL | |
color = Color.BLUE | |
} | |
// Draw a square in onDraw | |
private fun drawPlayer(canvas: Canvas) { | |
canvas.drawRect(playerRect, playerPaint) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment