DEADSOFTWARE

Code improvements
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / objects / Player.java
index 74f7249c751030d1778e03b55b2c11acec8d1337..5672807cc1205182190ff939d84913adc81f8c55 100644 (file)
@@ -2,6 +2,8 @@ package ru.deadsoftware.cavecraft.game.objects;
 
 import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavecraft.game.GameItems;
+import ru.deadsoftware.cavecraft.game.GameWorld;
 
 import java.io.Serializable;
 
@@ -10,23 +12,76 @@ public class Player implements Serializable {
     public static int ANIM_SPEED = 6;
 
     public Vector2 pos;
-    public Vector2 move;
-    public int width, height, dir, texWidth;
+    public Vector2 mov;
+    private int width, height, dir, hp;
     public boolean canJump;
     public int[] inv;
     public boolean flyMode = false;
+    public int gameMode;
 
-    public Player(Vector2 spawnPoint) {
-        pos = spawnPoint.cpy();
-        move = new Vector2(0, 0);
+    public Player(GameWorld world, int gameMode) {
+        this.gameMode = gameMode;
+        mov = new Vector2(0, 0);
         width = 4;
         height = 30;
-        texWidth = 8;
         inv = new int[9];
+        hp = 20;
+        pos = getSpawnPoint(world).cpy();
+    }
+
+    public void respawn(GameWorld world) {
+        pos.set(getSpawnPoint(world));
+        mov.setZero();
+        hp = 20;
+    }
+
+    private Vector2 getSpawnPoint(GameWorld world) {
+        int x = 0, y;
+        for (y = 0; y < world.getHeight(); y++) {
+            if (y == world.getHeight() - 1) {
+                y = 60;
+                world.setForeMap(x, y, 1);
+                break;
+            }
+            if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).hasCollision()) break;
+        }
+        return new Vector2(x * 16 + 8 - (float) getWidth() / 2, (float) y * 16 - getHeight());
+    }
+
+    public int getMapX() {
+        return (int) (pos.x + (getWidth() / 2)) / 16;
+    }
+
+    public int getMapY() {
+        return (int) (pos.y + (getHeight() / 2)) / 16;
+    }
+
+    public int getWidth() {
+        return width;
+    }
+
+    public int getHeight() {
+        return height;
+    }
+
+    public int getHp() {
+        return hp;
+    }
+
+    public void setHp(int hp) {
+        this.hp = hp;
+    }
+
+    public int getDir() {
+        return dir;
+    }
+
+    public void setDir(int dir) {
+        this.dir = dir;
     }
 
     public Rectangle getRect() {
-        return new Rectangle(pos.x + 2, pos.y, width, height);
+        return new Rectangle(pos.x, pos.y, getWidth(), getHeight());
     }
 
 }