DEADSOFTWARE

CaveGame in kotlin
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / objects / drop / Drop.kt
1 package ru.deadsoftware.cavedroid.game.objects.drop
3 import com.badlogic.gdx.math.Intersector
4 import com.badlogic.gdx.math.Rectangle
5 import com.badlogic.gdx.math.Vector2
6 import ru.deadsoftware.cavedroid.game.GameItemsHolder
7 import ru.deadsoftware.cavedroid.game.model.item.Item
9 class Drop @JvmOverloads constructor(
10 x: Float,
11 y: Float,
12 _item: Item,
13 _amount: Int = 1,
14 ) : Rectangle(x, y, DROP_SIZE, DROP_SIZE) {
16 var amount: Int = _amount
17 private set
19 val itemKey = _item.params.key
20 val velocity = getInitialVelocity()
21 var pickedUp = false
23 @Suppress("UNNECESSARY_LATEINIT")
24 @Transient
25 lateinit var item: Item
26 private set
28 init {
29 item = _item
30 }
32 fun initItem(gameItemsHolder: GameItemsHolder) {
33 if (this::item.isInitialized) {
34 return
35 }
37 item = gameItemsHolder.getItem(itemKey)
38 }
40 fun canMagnetTo(rectangle: Rectangle): Boolean {
41 val magnetArea = getMagnetArea()
42 return Intersector.overlaps(magnetArea, rectangle)
43 }
45 @JvmOverloads
46 fun subtract(count: Int = 1) {
47 if (count < 0) {
48 throw IllegalArgumentException("Can't subtract negative amount")
49 }
51 amount -= count
52 }
54 private fun getMagnetArea(): Rectangle {
55 return Rectangle(
56 /* x = */ x - MAGNET_DISTANCE,
57 /* y = */ y - MAGNET_DISTANCE,
58 /* width = */ width + MAGNET_DISTANCE * 2,
59 /* height = */ height + MAGNET_DISTANCE * 2,
60 )
61 }
63 companion object {
64 private const val MAGNET_DISTANCE = 8f
66 const val MAGNET_VELOCITY = 256f
67 const val DROP_SIZE = 8f
69 private fun getInitialVelocity(): Vector2 = Vector2(0f, -1f)
70 }
71 }