DEADSOFTWARE

b6f21bb1d8e6c4509c43d82955a73768a1bb57d6
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / objects / Drop.kt
1 package ru.deadsoftware.cavedroid.game.objects
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 val amount: Int = 1,
14 ) : Rectangle(x, y, DROP_SIZE, DROP_SIZE) {
16 val itemKey = _item.params.key
17 val velocity = getInitialVelocity()
18 var pickedUp = false
20 @Transient
21 lateinit var item: Item
22 private set
24 init {
25 item = _item
26 }
28 fun initItem(gameItemsHolder: GameItemsHolder) {
29 if (this::item.isInitialized) {
30 return
31 }
33 item = gameItemsHolder.getItem(itemKey)
34 }
36 fun canMagnetTo(rectangle: Rectangle): Boolean {
37 val magnetArea = getMagnetArea()
38 return Intersector.overlaps(magnetArea, rectangle)
39 }
41 private fun getMagnetArea(): Rectangle {
42 return Rectangle(
43 /* x = */ x - MAGNET_DISTANCE,
44 /* y = */ y - MAGNET_DISTANCE,
45 /* width = */ width + MAGNET_DISTANCE * 2,
46 /* height = */ height + MAGNET_DISTANCE * 2,
47 )
48 }
50 companion object {
51 private const val MAGNET_DISTANCE = 8f
53 const val MAGNET_VELOCITY = 256f
54 const val DROP_SIZE = 8f
56 private fun getInitialVelocity(): Vector2 = Vector2(0f, -1f)
57 }
58 }