DEADSOFTWARE

Support different block texture sizes and animation
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / objects / Block.kt
1 @file:Suppress("DeprecatedCallableAddReplaceWith")
3 package ru.deadsoftware.cavedroid.game.objects
5 import com.badlogic.gdx.graphics.Texture
6 import com.badlogic.gdx.graphics.g2d.Sprite
7 import com.badlogic.gdx.graphics.g2d.SpriteBatch
8 import com.badlogic.gdx.math.Rectangle
10 private const val ANIMATION_FRAME_DURATION = 100L
11 private const val DEPRECATION_MESSAGE =
12 "Deprecated since moved to Kotlin. Use generated getter or kotlin property access."
14 /**
15 * @param left margin from left edge
16 * @param top margin from top edge
17 * @param right margin from right edge
18 * @param bottom margin from bottom edge
19 * @param hp hit points
20 * @param drop id of an item the block will drop when destroyed
21 * @param collision true if block has collision
22 * @param background true if block should be drawn behind player
23 * @param transparent true if block is transparent and renderer should draw a block behind it
24 * @param requiresBlock true if block should break when there is no block with collision under it
25 * @param fluid true if fluid
26 * @param meta extra info for storing
27 * @param texture block's texture
28 * @param animated indicates if block has animation
29 * @param frames number of animation frames. ignored if animated is false
30 * @param spriteLeft block's sprite x on texture
31 * @param spriteTop block's sprite y on texture
32 * @param spriteRight block's sprite right edge on texture
33 * @param spriteBottom block's sprite bottom on texture
34 */
35 data class Block(
36 val left: Int,
37 val top: Int,
38 val right: Int,
39 val bottom: Int,
40 val hp: Int,
41 val drop: String,
42 val collision: Boolean,
43 val background: Boolean,
44 val transparent: Boolean,
45 val requiresBlock: Boolean,
46 val fluid: Boolean,
47 val meta: String,
48 private val texture: Texture?,
49 val animated: Boolean,
50 val frames: Int,
51 private val spriteLeft: Int,
52 private val spriteTop: Int,
53 private val spriteRight: Int,
54 private val spriteBottom: Int
55 ) {
57 val width = 16 - right - left
58 val height = 16 - top - bottom
60 private val spriteWidth = 16 - spriteLeft - spriteRight
61 private val spriteHeight = 16 - spriteTop - spriteBottom
63 private val sprite: Sprite?
64 get() {
65 return if (animated) {
66 animation[currentFrame()]
67 } else {
68 field
69 }
70 }
73 private val animation: Array<Sprite>
75 init {
76 if (frames !in 0..Int.MAX_VALUE) {
77 throw IllegalArgumentException("Animation frames must be in range [0, ${Int.MAX_VALUE}]")
78 }
80 animation = if (animated) {
81 if (texture == null) {
82 throw IllegalArgumentException("Cannot derive animation frames from null sprite")
83 }
84 Array(frames) { y ->
85 Sprite(texture, spriteLeft, 16 * y + spriteTop, spriteWidth, spriteHeight).apply {
86 flip(false, true)
87 }
88 }
89 } else {
90 emptyArray()
91 }
93 sprite = if (animated) { animation[0] } else {
94 if (texture != null) {
95 Sprite(texture, spriteLeft, spriteTop, spriteWidth, spriteHeight).apply {
96 flip(false, true)
97 }
98 } else {
99 null
104 private fun currentFrame() = if (animated) {
105 ((System.currentTimeMillis() / ANIMATION_FRAME_DURATION) % frames).toInt()
106 } else {
110 fun requireSprite() = sprite ?: throw IllegalStateException("Sprite is null")
112 fun draw(spriter: SpriteBatch, x: Float, y: Float) {
113 requireSprite().apply {
114 setBounds(x + spriteLeft, y + spriteTop, spriteWidth.toFloat(), spriteHeight.toFloat())
115 draw(spriter)
119 fun getRectangle(x: Int, y: Int) =
120 Rectangle(x * 16f + left, y * 16f + this.top, width.toFloat(), height.toFloat())
122 fun hasDrop() = drop != "none"
124 fun toJump() = top < 8 && collision
126 @Deprecated(DEPRECATION_MESSAGE)
127 fun hasCollision() = collision
129 @Deprecated(DEPRECATION_MESSAGE)
130 fun isBackground() = background
132 @Deprecated(DEPRECATION_MESSAGE)
133 fun isTransparent() = transparent
135 @Deprecated(DEPRECATION_MESSAGE)
136 fun isFluid() = fluid
138 @Deprecated(DEPRECATION_MESSAGE)
139 fun requiresBlock() = requiresBlock
141 @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
142 fun getTexture() = sprite