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 public final int[] inventory
;
21 public final int gameMode
;
23 public float headRotation
= 0f;
26 super(0, 0, 4, 30, randomDir(), Type
.MOB
);
28 inventory
= new int[9];
32 public void respawn(GameWorld gameWorld
) {
33 Vector2 pos
= getSpawnPoint(gameWorld
);
40 public void pickUpDrop(Drop drop
) {
41 for (int i
= 0; i
< inventory
.length
; i
++) {
42 if (inventory
[i
] == 0 || inventory
[i
] == drop
.getId()) {
43 inventory
[i
] = drop
.getId();
44 drop
.setPickedUp(true);
50 private Vector2
getSpawnPoint(GameWorld gameWorld
) {
52 for (y
= 0; y
< gameWorld
.getHeight(); y
++) {
53 if (y
== gameWorld
.getHeight() - 1) {
55 gameWorld
.setForeMap(0, y
, 1);
58 if (gameWorld
.hasForeAt(0, y
) && gameWorld
.getForeMapBlock(0, y
).hasCollision()) {
62 return new Vector2(8 - getWidth() / 2, (float) y
* 16 - getHeight());
65 public void setDir(Direction dir
) {
66 if (dir
!= getDirection()) {
72 public void ai(GameWorld gameWorld
, float delta
) {
76 public void changeDir() {
79 private void drawItem(SpriteBatch spriteBatch
, float x
, float y
) {
80 final int itemId
= inventory
[slot
];
81 final Item item
= GameItems
.getItem(itemId
);
83 @CheckForNull final Sprite sprite
= item
.isBlock()
84 ? item
.toBlock().getTexture()
92 sprite
.setSize(Drop
.DROP_SIZE
, Drop
.DROP_SIZE
);
95 final float handLength
= Assets
.playerSprite
[0][2].getHeight();
97 final SpriteOrigin spriteOrigin
= item
.getDefaultOrigin();
98 final int handMultiplier
= 1 + -2 * dirMultiplier();
99 final float xOffset
= (-1 + dirMultiplier()) * sprite
.getWidth() + 4 + handMultiplier
* (sprite
.getWidth() * spriteOrigin
.getX());
100 final float yOffset
= item
.isTool() ?
-sprite
.getHeight() / 2 : 0;
102 float rotate
= mAnim
+ 30;
104 final float itemX
= x
+ handLength
* MathUtils
.sin(handMultiplier
* mAnim
* MathUtils
.degRad
) + xOffset
;
105 final float itemY
= y
+ handLength
* MathUtils
.cos(handMultiplier
* mAnim
* MathUtils
.degRad
) + yOffset
;
108 sprite
.setFlip(true, sprite
.isFlipY());
109 SpriteUtilsKt
.applyOrigin(sprite
, spriteOrigin
.getFlipped(true, false));
111 sprite
.setFlip(false, sprite
.isFlipY());
112 SpriteUtilsKt
.applyOrigin(sprite
, spriteOrigin
);
115 SpriteUtilsKt
.draw(spriteBatch
, sprite
, itemX
, itemY
, -handMultiplier
* rotate
);
117 // dont forget to reset
118 sprite
.setFlip(false, sprite
.isFlipY());
119 sprite
.setRotation(0);
120 sprite
.setOriginCenter();
124 public void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
) {
125 updateAnimation(delta
);
127 final Sprite backHand
= Assets
.playerSprite
[1][2];
128 final Sprite backLeg
= Assets
.playerSprite
[1][3];
129 final Sprite frontLeg
= Assets
.playerSprite
[0][3];
130 final Sprite head
= Assets
.playerSprite
[dirMultiplier()][0];
131 final Sprite body
= Assets
.playerSprite
[dirMultiplier()][1];
132 final Sprite frontHand
= Assets
.playerSprite
[0][2];
134 SpriteUtilsKt
.draw(spriteBatch
, backHand
, x
+ 2, y
+ 8, -mAnim
);
137 drawItem(spriteBatch
, x
, y
);
140 SpriteUtilsKt
.draw(spriteBatch
, backLeg
, x
+ 2, y
+ 20, mAnim
);
141 SpriteUtilsKt
.draw(spriteBatch
, frontLeg
, x
+ 2, y
+ 20, -mAnim
);
142 SpriteUtilsKt
.draw(spriteBatch
, head
, x
, y
, headRotation
);
143 SpriteUtilsKt
.draw(spriteBatch
, body
, x
+ 2, y
+ 8);
146 drawItem(spriteBatch
, x
, y
);
149 SpriteUtilsKt
.draw(spriteBatch
, frontHand
, x
+ 2, y
+ 8, mAnim
);