DEADSOFTWARE

Update version script
[cavedroid.git] / core / src / main / kotlin / ru / deadsoftware / cavedroid / game / mobs / Pig.kt
1 package ru.deadsoftware.cavedroid.game.mobs
3 import com.badlogic.gdx.graphics.g2d.SpriteBatch
4 import com.badlogic.gdx.math.Vector2
5 import ru.deadsoftware.cavedroid.game.GameItemsHolder
6 import ru.deadsoftware.cavedroid.game.model.dto.SaveDataDto
7 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
8 import ru.deadsoftware.cavedroid.misc.utils.drawSprite
9 import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getBackgroundLeg
10 import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getBody
11 import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getForegroundLeg
12 import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getLeftLegRelativeX
13 import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getLegsRelativeY
14 import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getRightLegRelativeX
16 class Pig(x: Float, y: Float) : PeacefulMob(x, y, WIDTH, HEIGHT, randomDir(), MAX_HEALTH) {
18 override fun getSpeed(): Float {
19 return SPEED
20 }
22 override fun changeDir() {
23 switchDir()
24 velocity = Vector2(direction.basis * speed, 0f)
25 }
27 override fun jump() {
28 velocity.y = JUMP_VELOCITY
29 }
31 override fun damage(damage: Int) {
32 super.damage(damage)
34 if (damage > 0) {
35 if (canJump()) {
36 jump()
37 }
38 }
39 }
41 override fun getDrop(gameItemsHolder: GameItemsHolder): List<InventoryItem> {
42 return listOf(gameItemsHolder.getItem("porkchop_raw").toInventoryItem())
43 }
45 override fun draw(spriteBatch: SpriteBatch, x: Float, y: Float, delta: Float) {
46 updateAnimation(delta)
48 val leftLegX = x + getLeftLegRelativeX(direction)
49 val rightLegX = x + getRightLegRelativeX(direction)
50 val legY = y + getLegsRelativeY()
52 spriteBatch.drawSprite(getBackgroundLeg(), leftLegX, legY, -anim, tint = tintColor)
53 spriteBatch.drawSprite(getBackgroundLeg(), rightLegX, legY, -anim, tint = tintColor)
54 spriteBatch.drawSprite(getBody(direction), x, y, tint = tintColor)
55 spriteBatch.drawSprite(getForegroundLeg(), leftLegX, legY, anim, tint = tintColor)
56 spriteBatch.drawSprite(getForegroundLeg(), rightLegX, legY, anim, tint = tintColor)
57 }
59 override fun getSaveData(): SaveDataDto.PigSaveData {
60 return SaveDataDto.PigSaveData(
61 version = SAVE_DATA_VERSION,
62 x = x,
63 y = y,
64 width = width,
65 height = height,
66 velocityX = velocity.x,
67 velocityY = velocity.y,
68 type = mType,
69 animDelta = mAnimDelta,
70 anim = mAnim,
71 direction = mDirection,
72 dead = mDead,
73 canJump = mCanJump,
74 flyMode = mFlyMode,
75 maxHealth = mMaxHealth,
76 health = mHealth
77 )
78 }
80 companion object {
81 private const val SAVE_DATA_VERSION = 1
83 private const val WIDTH = 25f
84 private const val HEIGHT = 18f
85 private const val SPEED = 48f
86 private const val JUMP_VELOCITY = -133.332f
87 private const val MAX_HEALTH = 10
89 fun fromSaveData(saveData: SaveDataDto.PigSaveData): Pig {
90 saveData.verifyVersion(SAVE_DATA_VERSION)
92 return Pig(saveData.x, saveData.y).apply {
93 velocity.x = saveData.velocityX
94 velocity.y = saveData.velocityY
95 mAnimDelta = saveData.animDelta
96 mAnim = saveData.anim
97 mDirection = saveData.direction
98 mDead = saveData.dead
99 mCanJump = saveData.canJump
100 mFlyMode = saveData.flyMode
101 mMaxHealth = saveData.maxHealth
102 mHealth = saveData.health