DEADSOFTWARE

Update README
[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(
10 x: Float,
11 y: Float,
12 _item: Item,
13 ) : Rectangle(x, y, DROP_SIZE, DROP_SIZE) {
15 val itemKey = _item.params.key
16 val velocity = getInitialVelocity()
17 var pickedUp = false
19 @Transient
20 lateinit var item: Item
21 private set
23 init {
24 item = _item
25 }
27 fun initItem(gameItemsHolder: GameItemsHolder) {
28 if (this::item.isInitialized) {
29 return
30 }
32 item = gameItemsHolder.getItem(itemKey)
33 }
35 fun canMagnetTo(rectangle: Rectangle): Boolean {
36 val magnetArea = getMagnetArea()
37 return Intersector.overlaps(magnetArea, rectangle)
38 }
40 private fun getMagnetArea(): Rectangle {
41 return Rectangle(
42 /* x = */ x - MAGNET_DISTANCE,
43 /* y = */ y - MAGNET_DISTANCE,
44 /* width = */ width + MAGNET_DISTANCE * 2,
45 /* height = */ height + MAGNET_DISTANCE * 2,
46 )
47 }
49 companion object {
50 private const val MAGNET_DISTANCE = 16f
52 const val MAGNET_VELOCITY = 128f
53 const val DROP_SIZE = 8f
55 private fun getInitialVelocity(): Vector2 = Vector2(0f, -1f)
56 }
57 }