DEADSOFTWARE

Code improvements
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / objects / Player.java
1 package ru.deadsoftware.cavecraft.game.objects;
3 import com.badlogic.gdx.math.Rectangle;
4 import com.badlogic.gdx.math.Vector2;
5 import ru.deadsoftware.cavecraft.game.GameItems;
6 import ru.deadsoftware.cavecraft.game.GameWorld;
8 import java.io.Serializable;
10 public class Player implements Serializable {
12 public static int ANIM_SPEED = 6;
14 public Vector2 pos;
15 public Vector2 mov;
16 private int width, height, dir, hp;
17 public boolean canJump;
18 public int[] inv;
19 public boolean flyMode = false;
20 public int gameMode;
22 public Player(GameWorld world, int gameMode) {
23 this.gameMode = gameMode;
24 mov = new Vector2(0, 0);
25 width = 4;
26 height = 30;
27 inv = new int[9];
28 hp = 20;
29 pos = getSpawnPoint(world).cpy();
30 }
32 public void respawn(GameWorld world) {
33 pos.set(getSpawnPoint(world));
34 mov.setZero();
35 hp = 20;
36 }
38 private Vector2 getSpawnPoint(GameWorld world) {
39 int x = 0, y;
40 for (y = 0; y < world.getHeight(); y++) {
41 if (y == world.getHeight() - 1) {
42 y = 60;
43 world.setForeMap(x, y, 1);
44 break;
45 }
46 if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).hasCollision()) break;
47 }
48 return new Vector2(x * 16 + 8 - (float) getWidth() / 2, (float) y * 16 - getHeight());
49 }
51 public int getMapX() {
52 return (int) (pos.x + (getWidth() / 2)) / 16;
53 }
55 public int getMapY() {
56 return (int) (pos.y + (getHeight() / 2)) / 16;
57 }
59 public int getWidth() {
60 return width;
61 }
63 public int getHeight() {
64 return height;
65 }
67 public int getHp() {
68 return hp;
69 }
71 public void setHp(int hp) {
72 this.hp = hp;
73 }
75 public int getDir() {
76 return dir;
77 }
79 public void setDir(int dir) {
80 this.dir = dir;
81 }
83 public Rectangle getRect() {
84 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());
85 }
87 }