DEADSOFTWARE

0b93809af232d0dce08cdf90fad13cd7cf23f090
[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 id: Int,
37 val left: Int,
38 val top: Int,
39 val right: Int,
40 val bottom: Int,
41 val hp: Int,
42 val drop: String,
43 val collision: Boolean,
44 val background: Boolean,
45 val transparent: Boolean,
46 val requiresBlock: Boolean,
47 val fluid: Boolean,
48 val meta: String,
49 private val texture: Texture?,
50 val animated: Boolean,
51 val frames: Int,
52 private val spriteLeft: Int,
53 private val spriteTop: Int,
54 private val spriteRight: Int,
55 private val spriteBottom: Int
56 ) {
58 val width = 16 - right - left
59 val height = 16 - top - bottom
61 private val spriteWidth = 16 - spriteLeft - spriteRight
62 private val spriteHeight = 16 - spriteTop - spriteBottom
64 private val sprite: Sprite?
65 get() {
66 return if (animated) {
67 animation[currentFrame()]
68 } else {
69 field
70 }
71 }
74 private val animation: Array<Sprite>
76 init {
77 if (frames !in 0..Int.MAX_VALUE) {
78 throw IllegalArgumentException("Animation frames must be in range [0, ${Int.MAX_VALUE}]")
79 }
81 animation = if (animated) {
82 if (texture == null) {
83 throw IllegalArgumentException("Cannot derive animation frames from null sprite")
84 }
85 Array(frames) { y ->
86 Sprite(texture, spriteLeft, 16 * y + spriteTop, spriteWidth, spriteHeight).apply {
87 flip(false, true)
88 }
89 }
90 } else {
91 emptyArray()
92 }
94 sprite = if (animated) { animation[0] } else {
95 if (texture != null) {
96 Sprite(texture, spriteLeft, spriteTop, spriteWidth, spriteHeight).apply {
97 flip(false, true)
98 }
99 } else {
100 null
105 private fun currentFrame() = if (animated) {
106 ((System.currentTimeMillis() / ANIMATION_FRAME_DURATION) % frames).toInt()
107 } else {
111 fun requireSprite() = sprite ?: throw IllegalStateException("Sprite is null")
113 fun draw(spriter: SpriteBatch, x: Float, y: Float) {
114 requireSprite().apply {
115 setBounds(x + spriteLeft, y + spriteTop, spriteWidth.toFloat(), spriteHeight.toFloat())
116 draw(spriter)
120 fun getRectangle(x: Int, y: Int) =
121 Rectangle(x * 16f + left, y * 16f + this.top, width.toFloat(), height.toFloat())
123 fun hasDrop() = drop != "none"
125 fun toJump() = top < 8 && collision
127 @Deprecated(DEPRECATION_MESSAGE)
128 fun hasCollision() = collision
130 @Deprecated(DEPRECATION_MESSAGE)
131 fun isBackground() = background
133 @Deprecated(DEPRECATION_MESSAGE)
134 fun isTransparent() = transparent
136 @Deprecated(DEPRECATION_MESSAGE)
137 fun isFluid() = fluid
139 @Deprecated(DEPRECATION_MESSAGE)
140 fun requiresBlock() = requiresBlock
142 @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
143 fun getTexture() = sprite