DEADSOFTWARE

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