DEADSOFTWARE

Support reading item ids from json
[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 */
36 data class Block(
37 val id: Int,
38 val key: String,
39 val left: Int,
40 val top: Int,
41 val right: Int,
42 val bottom: Int,
43 val hp: Int,
44 val drop: String,
45 val collision: Boolean,
46 val background: Boolean,
47 val transparent: Boolean,
48 val requiresBlock: Boolean,
49 val fluid: Boolean,
50 val meta: String,
51 private val texture: Texture?,
52 val animated: Boolean,
53 val frames: Int,
54 private val spriteLeft: Int,
55 private val spriteTop: Int,
56 private val spriteRight: Int,
57 private val spriteBottom: Int
58 ) {
60 val width = 16 - right - left
61 val height = 16 - top - bottom
63 private val spriteWidth = 16 - spriteLeft - spriteRight
64 private val spriteHeight = 16 - spriteTop - spriteBottom
66 private val sprite: Sprite?
67 get() {
68 return if (animated) {
69 animation[currentFrame()]
70 } else {
71 field
72 }
73 }
76 private val animation: Array<Sprite>
78 init {
79 if (frames !in 0..Int.MAX_VALUE) {
80 throw IllegalArgumentException("Animation frames must be in range [0, ${Int.MAX_VALUE}]")
81 }
83 animation = if (animated) {
84 if (texture == null) {
85 throw IllegalArgumentException("Cannot derive animation frames from null sprite")
86 }
87 Array(frames) { y ->
88 Sprite(texture, spriteLeft, 16 * y + spriteTop, spriteWidth, spriteHeight).apply {
89 flip(false, true)
90 }
91 }
92 } else {
93 emptyArray()
94 }
96 sprite = if (animated) { animation[0] } else {
97 if (texture != null) {
98 Sprite(texture, spriteLeft, spriteTop, spriteWidth, spriteHeight).apply {
99 flip(false, true)
101 } else {
102 null
107 private fun currentFrame() = if (animated) {
108 ((System.currentTimeMillis() / ANIMATION_FRAME_DURATION) % frames).toInt()
109 } else {
113 fun requireSprite() = sprite ?: throw IllegalStateException("Sprite is null")
115 fun draw(spriter: SpriteBatch, x: Float, y: Float) {
116 requireSprite().apply {
117 setBounds(x + spriteLeft, y + spriteTop, spriteWidth.toFloat(), spriteHeight.toFloat())
118 draw(spriter)
122 fun getRectangle(x: Int, y: Int) =
123 Rectangle(x * 16f + left, y * 16f + this.top, width.toFloat(), height.toFloat())
125 fun hasDrop() = drop != "none"
127 fun toJump() = top < 8 && collision
129 fun getItem() = GameItems.getItem(GameItems.getBlockKey(id))
131 @Deprecated(DEPRECATION_MESSAGE)
132 fun hasCollision() = collision
134 @Deprecated(DEPRECATION_MESSAGE)
135 fun isBackground() = background
137 @Deprecated(DEPRECATION_MESSAGE)
138 fun isTransparent() = transparent
140 @Deprecated(DEPRECATION_MESSAGE)
141 fun isFluid() = fluid
143 @Deprecated(DEPRECATION_MESSAGE)
144 fun requiresBlock() = requiresBlock
146 @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
147 fun getTexture() = sprite