DEADSOFTWARE

Add furnace, more craft and items
[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 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 @Suppress("UNNECESSARY_LATEINIT")
21 @Transient
22 lateinit var item: Item
23 private set
25 init {
26 item = _item
27 }
29 fun initItem(gameItemsHolder: GameItemsHolder) {
30 if (this::item.isInitialized) {
31 return
32 }
34 item = gameItemsHolder.getItem(itemKey)
35 }
37 fun canMagnetTo(rectangle: Rectangle): Boolean {
38 val magnetArea = getMagnetArea()
39 return Intersector.overlaps(magnetArea, rectangle)
40 }
42 private fun getMagnetArea(): Rectangle {
43 return Rectangle(
44 /* x = */ x - MAGNET_DISTANCE,
45 /* y = */ y - MAGNET_DISTANCE,
46 /* width = */ width + MAGNET_DISTANCE * 2,
47 /* height = */ height + MAGNET_DISTANCE * 2,
48 )
49 }
51 companion object {
52 private const val MAGNET_DISTANCE = 8f
54 const val MAGNET_VELOCITY = 256f
55 const val DROP_SIZE = 8f
57 private fun getInitialVelocity(): Vector2 = Vector2(0f, -1f)
58 }
59 }