1 package ru
.deadsoftware
.cavedroid
.game
.mobs
;
3 import com
.badlogic
.gdx
.graphics
.g2d
.Sprite
;
4 import com
.badlogic
.gdx
.graphics
.g2d
.SpriteBatch
;
5 import com
.badlogic
.gdx
.math
.MathUtils
;
6 import com
.badlogic
.gdx
.math
.Vector2
;
7 import ru
.deadsoftware
.cavedroid
.game
.GameItems
;
8 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
9 import ru
.deadsoftware
.cavedroid
.game
.objects
.Item
;
10 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
11 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
12 import ru
.deadsoftware
.cavedroid
.misc
.utils
.SpriteOrigin
;
13 import ru
.deadsoftware
.cavedroid
.misc
.utils
.SpriteUtilsKt
;
15 import javax
.annotation
.CheckForNull
;
17 public class Player
extends Mob
{
19 private static final float SPEED
= 69.072f;
20 private static final float JUMP_VELOCITY
= -133.332f;
21 private static final int MAX_HEALTH
= 20;
23 private boolean hitting
= false;
24 private float hitAnim
= 0f;
25 private float hitAnimDelta
= ANIMATION_SPEED
;
27 public final int[] inventory
;
31 public float headRotation
= 0f;
34 super(0, 0, 4, 30, randomDir(), Type
.MOB
, MAX_HEALTH
);
35 inventory
= new int[9];
39 public void respawn(GameWorld gameWorld
) {
40 Vector2 pos
= getSpawnPoint(gameWorld
);
48 public void pickUpDrop(Drop drop
) {
49 for (int i
= 0; i
< inventory
.length
; i
++) {
50 if (inventory
[i
] == 0 || inventory
[i
] == drop
.getId()) {
51 inventory
[i
] = drop
.getId();
52 drop
.setPickedUp(true);
58 private Vector2
getSpawnPoint(GameWorld gameWorld
) {
60 for (y
= 0; y
< gameWorld
.getHeight(); y
++) {
61 if (y
== gameWorld
.getHeight() - 1) {
63 gameWorld
.setForeMap(0, y
, 1);
66 if (gameWorld
.hasForeAt(0, y
) && gameWorld
.getForeMapBlock(0, y
).hasCollision()) {
70 return new Vector2(8 - getWidth() / 2, (float) y
* 16 - getHeight());
73 public void setDir(Direction dir
) {
74 if (dir
!= getDirection()) {
79 public void setCurrentInventorySlotItem(int itemId
) {
80 inventory
[slot
] = itemId
;
84 public float getSpeed() {
90 mVelocity
.y
= JUMP_VELOCITY
;
94 public void ai(GameWorld gameWorld
, float delta
) {
98 public void changeDir() {
102 public void damage(int damage
) {
106 super.damage(damage
);
110 public void heal(int heal
) {
117 private void drawItem(SpriteBatch spriteBatch
, float x
, float y
, float anim
) {
118 final int itemId
= inventory
[slot
];
119 final Item item
= GameItems
.getItem(itemId
);
121 @CheckForNull final Sprite sprite
= item
.isBlock()
122 ? item
.toBlock().getTexture()
125 if (sprite
== null) {
129 if (!item
.isTool()) {
130 sprite
.setSize(Drop
.DROP_SIZE
, Drop
.DROP_SIZE
);
133 final float handLength
= Assets
.playerSprite
[0][2].getHeight();
135 final SpriteOrigin spriteOrigin
= item
.getDefaultOrigin();
136 final int handMultiplier
= -getDirection().getBasis();
137 final float xOffset
= (-1 + getDirection().getIndex()) * sprite
.getWidth() + 4 + handMultiplier
* (sprite
.getWidth() * spriteOrigin
.getX());
138 final float yOffset
= item
.isTool() ?
-sprite
.getHeight() / 2 : 0;
140 float rotate
= anim
+ 30;
142 final float itemX
= x
+ handLength
* MathUtils
.sin(handMultiplier
* anim
* MathUtils
.degRad
) + xOffset
;
143 final float itemY
= y
+ handLength
* MathUtils
.cos(handMultiplier
* anim
* MathUtils
.degRad
) + yOffset
;
146 sprite
.setFlip(true, sprite
.isFlipY());
147 SpriteUtilsKt
.applyOrigin(sprite
, spriteOrigin
.getFlipped(true, false));
149 sprite
.setFlip(false, sprite
.isFlipY());
150 SpriteUtilsKt
.applyOrigin(sprite
, spriteOrigin
);
153 SpriteUtilsKt
.drawSprite(spriteBatch
, sprite
, itemX
, itemY
, -handMultiplier
* rotate
);
155 // dont forget to reset
156 sprite
.setFlip(false, sprite
.isFlipY());
157 sprite
.setRotation(0);
158 sprite
.setOriginCenter();
161 public void startHitting() {
168 hitAnimDelta
= ANIMATION_SPEED
;
171 public void stopHitting() {
175 private float getRightHandAnim(float delta
) {
176 hitAnim
-= hitAnimDelta
* delta
;
178 if (hitAnim
< 30f || hitAnim
> 90f) {
180 hitAnim
= MathUtils
.clamp(hitAnim
, 30f, 90f);
181 hitAnimDelta
= -hitAnimDelta
;
183 hitAnimDelta
= ANIMATION_SPEED
;
188 if (hitAnim
< hitAnimDelta
* delta
) {
199 public void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
) {
200 updateAnimation(delta
);
202 final Sprite backHand
= Assets
.playerSprite
[1][2];
203 final Sprite backLeg
= Assets
.playerSprite
[1][3];
204 final Sprite frontLeg
= Assets
.playerSprite
[0][3];
205 final Sprite head
= Assets
.playerSprite
[getDirection().getIndex()][0];
206 final Sprite body
= Assets
.playerSprite
[getDirection().getIndex()][1];
207 final Sprite frontHand
= Assets
.playerSprite
[0][2];
209 float backHandAnim
, frontHandAnim
;
211 final float rightHandAnim
= getRightHandAnim(delta
);
214 backHandAnim
= rightHandAnim
;
215 frontHandAnim
= mAnim
;
217 backHandAnim
= -mAnim
;
218 frontHandAnim
= -rightHandAnim
;
221 SpriteUtilsKt
.drawSprite(spriteBatch
, backHand
, x
+ 2, y
+ 8, backHandAnim
);
224 drawItem(spriteBatch
, x
, y
, -backHandAnim
);
227 SpriteUtilsKt
.drawSprite(spriteBatch
, backLeg
, x
+ 2, y
+ 20, mAnim
);
228 SpriteUtilsKt
.drawSprite(spriteBatch
, frontLeg
, x
+ 2, y
+ 20, -mAnim
);
229 SpriteUtilsKt
.drawSprite(spriteBatch
, head
, x
, y
, headRotation
);
230 SpriteUtilsKt
.drawSprite(spriteBatch
, body
, x
+ 2, y
+ 8);
233 drawItem(spriteBatch
, x
, y
, frontHandAnim
);
236 SpriteUtilsKt
.drawSprite(spriteBatch
, frontHand
, x
+ 2, y
+ 8, frontHandAnim
);