b45b942697356a3b23450a355e8b5b5178dc9b09
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;
22 public final int[] inventory
;
24 public final int gameMode
;
26 public float headRotation
= 0f;
29 super(0, 0, 4, 30, randomDir(), Type
.MOB
);
31 inventory
= new int[9];
35 public void respawn(GameWorld gameWorld
) {
36 Vector2 pos
= getSpawnPoint(gameWorld
);
43 public void pickUpDrop(Drop drop
) {
44 for (int i
= 0; i
< inventory
.length
; i
++) {
45 if (inventory
[i
] == 0 || inventory
[i
] == drop
.getId()) {
46 inventory
[i
] = drop
.getId();
47 drop
.setPickedUp(true);
53 private Vector2
getSpawnPoint(GameWorld gameWorld
) {
55 for (y
= 0; y
< gameWorld
.getHeight(); y
++) {
56 if (y
== gameWorld
.getHeight() - 1) {
58 gameWorld
.setForeMap(0, y
, 1);
61 if (gameWorld
.hasForeAt(0, y
) && gameWorld
.getForeMapBlock(0, y
).hasCollision()) {
65 return new Vector2(8 - getWidth() / 2, (float) y
* 16 - getHeight());
68 public void setDir(Direction dir
) {
69 if (dir
!= getDirection()) {
75 public float getSpeed() {
81 mVelocity
.y
= JUMP_VELOCITY
;
85 public void ai(GameWorld gameWorld
, float delta
) {
89 public void changeDir() {
92 private void drawItem(SpriteBatch spriteBatch
, float x
, float y
) {
93 final int itemId
= inventory
[slot
];
94 final Item item
= GameItems
.getItem(itemId
);
96 @CheckForNull final Sprite sprite
= item
.isBlock()
97 ? item
.toBlock().getTexture()
100 if (sprite
== null) {
104 if (!item
.isTool()) {
105 sprite
.setSize(Drop
.DROP_SIZE
, Drop
.DROP_SIZE
);
108 final float handLength
= Assets
.playerSprite
[0][2].getHeight();
110 final SpriteOrigin spriteOrigin
= item
.getDefaultOrigin();
111 final int handMultiplier
= -getDirection().getBasis();
112 final float xOffset
= (-1 + getDirection().getIndex()) * sprite
.getWidth() + 4 + handMultiplier
* (sprite
.getWidth() * spriteOrigin
.getX());
113 final float yOffset
= item
.isTool() ?
-sprite
.getHeight() / 2 : 0;
115 float rotate
= mAnim
+ 30;
117 final float itemX
= x
+ handLength
* MathUtils
.sin(handMultiplier
* mAnim
* MathUtils
.degRad
) + xOffset
;
118 final float itemY
= y
+ handLength
* MathUtils
.cos(handMultiplier
* mAnim
* MathUtils
.degRad
) + yOffset
;
121 sprite
.setFlip(true, sprite
.isFlipY());
122 SpriteUtilsKt
.applyOrigin(sprite
, spriteOrigin
.getFlipped(true, false));
124 sprite
.setFlip(false, sprite
.isFlipY());
125 SpriteUtilsKt
.applyOrigin(sprite
, spriteOrigin
);
128 SpriteUtilsKt
.drawSprite(spriteBatch
, sprite
, itemX
, itemY
, -handMultiplier
* rotate
);
130 // dont forget to reset
131 sprite
.setFlip(false, sprite
.isFlipY());
132 sprite
.setRotation(0);
133 sprite
.setOriginCenter();
137 public void draw(SpriteBatch spriteBatch
, float x
, float y
, float delta
) {
138 updateAnimation(delta
);
140 final Sprite backHand
= Assets
.playerSprite
[1][2];
141 final Sprite backLeg
= Assets
.playerSprite
[1][3];
142 final Sprite frontLeg
= Assets
.playerSprite
[0][3];
143 final Sprite head
= Assets
.playerSprite
[getDirection().getIndex()][0];
144 final Sprite body
= Assets
.playerSprite
[getDirection().getIndex()][1];
145 final Sprite frontHand
= Assets
.playerSprite
[0][2];
147 SpriteUtilsKt
.drawSprite(spriteBatch
, backHand
, x
+ 2, y
+ 8, -mAnim
);
150 drawItem(spriteBatch
, x
, y
);
153 SpriteUtilsKt
.drawSprite(spriteBatch
, backLeg
, x
+ 2, y
+ 20, mAnim
);
154 SpriteUtilsKt
.drawSprite(spriteBatch
, frontLeg
, x
+ 2, y
+ 20, -mAnim
);
155 SpriteUtilsKt
.drawSprite(spriteBatch
, head
, x
, y
, headRotation
);
156 SpriteUtilsKt
.drawSprite(spriteBatch
, body
, x
+ 2, y
+ 8);
159 drawItem(spriteBatch
, x
, y
);
162 SpriteUtilsKt
.drawSprite(spriteBatch
, frontHand
, x
+ 2, y
+ 8, mAnim
);