DEADSOFTWARE

020caf5356c7b4d7dd142afbc7c045f3787e53e2
[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
9 import ru.deadsoftware.cavedroid.game.GameItems
11 private const val ANIMATION_FRAME_DURATION = 100L
12 private const val DEPRECATION_MESSAGE =
13 "Deprecated since moved to Kotlin. Use generated getter or kotlin property access."
15 /**
16 * @param left margin from left edge
17 * @param top margin from top edge
18 * @param right margin from right edge
19 * @param bottom margin from bottom edge
20 * @param hp hit points
21 * @param drop id of an item the block will drop when destroyed
22 * @param collision true if block has collision
23 * @param background true if block should be drawn behind player
24 * @param transparent true if block is transparent and renderer should draw a block behind it
25 * @param requiresBlock true if block should break when there is no block with collision under it
26 * @param fluid true if fluid
27 * @param meta extra info for storing
28 * @param texture block's texture
29 * @param animated indicates if block has animation
30 * @param frames number of animation frames. ignored if animated is false
31 * @param spriteLeft block's sprite x on texture
32 * @param spriteTop block's sprite y on texture
33 * @param spriteRight block's sprite right edge on texture
34 * @param spriteBottom block's sprite bottom on texture
35 * @param fullBlockKey for slabs. block for two slabs of one kind
36 */
37 data class Block(
38 val id: Int,
39 val key: String,
40 val left: Int,
41 val top: Int,
42 val right: Int,
43 val bottom: Int,
44 val hp: Int,
45 val drop: String,
46 val collision: Boolean,
47 val background: Boolean,
48 val transparent: Boolean,
49 val requiresBlock: Boolean,
50 val fluid: Boolean,
51 val meta: String,
52 private val texture: Texture?,
53 val animated: Boolean,
54 val frames: Int,
55 private val spriteLeft: Int,
56 private val spriteTop: Int,
57 private val spriteRight: Int,
58 private val spriteBottom: Int,
59 val fullBlockKey: String?,
60 ) {
62 val width = 16 - right - left
63 val height = 16 - top - bottom
65 private val spriteWidth = 16 - spriteLeft - spriteRight
66 private val spriteHeight = 16 - spriteTop - spriteBottom
68 private val sprite: Sprite?
69 get() {
70 return if (animated) {
71 animation[currentFrame()]
72 } else {
73 field
74 }
75 }
78 private val animation: Array<Sprite>
80 init {
81 if (frames !in 0..Int.MAX_VALUE) {
82 throw IllegalArgumentException("Animation frames must be in range [0, ${Int.MAX_VALUE}]")
83 }
85 animation = if (animated) {
86 if (texture == null) {
87 throw IllegalArgumentException("Cannot derive animation frames from null sprite")
88 }
89 Array(frames) { y ->
90 Sprite(texture, spriteLeft, 16 * y + spriteTop, spriteWidth, spriteHeight).apply {
91 flip(false, true)
92 }
93 }
94 } else {
95 emptyArray()
96 }
98 sprite = if (animated) { animation[0] } else {
99 if (texture != null) {
100 Sprite(texture, spriteLeft, spriteTop, spriteWidth, spriteHeight).apply {
101 flip(false, true)
103 } else {
104 null
109 private fun currentFrame() = if (animated) {
110 ((System.currentTimeMillis() / ANIMATION_FRAME_DURATION) % frames).toInt()
111 } else {
115 fun requireSprite() = sprite ?: throw IllegalStateException("Sprite is null")
117 fun draw(spriter: SpriteBatch, x: Float, y: Float) {
118 requireSprite().apply {
119 setBounds(x + spriteLeft, y + spriteTop, spriteWidth.toFloat(), spriteHeight.toFloat())
120 draw(spriter)
124 fun getRectangle(x: Int, y: Int) =
125 Rectangle(x * 16f + left, y * 16f + this.top, width.toFloat(), height.toFloat())
127 fun hasDrop() = drop != "none"
129 fun toJump() = top < 8 && collision
131 fun getItem() = GameItems.getItem(GameItems.getBlockKey(id))
133 @Deprecated(DEPRECATION_MESSAGE)
134 fun hasCollision() = collision
136 @Deprecated(DEPRECATION_MESSAGE)
137 fun isBackground() = background
139 @Deprecated(DEPRECATION_MESSAGE)
140 fun isTransparent() = transparent
142 @Deprecated(DEPRECATION_MESSAGE)
143 fun isFluid() = fluid
145 @Deprecated(DEPRECATION_MESSAGE)
146 fun requiresBlock() = requiresBlock
148 @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
149 fun getTexture() = sprite