DEADSOFTWARE

One GameProc object for everything
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / objects / Drop.java
1 package ru.deadsoftware.cavecraft.game.objects;
3 import com.badlogic.gdx.math.Intersector;
4 import com.badlogic.gdx.math.Rectangle;
5 import com.badlogic.gdx.math.Vector2;
6 import ru.deadsoftware.cavecraft.game.GameProc;
8 import java.io.Serializable;
10 import static ru.deadsoftware.cavecraft.GameScreen.GP;
12 public class Drop implements Serializable {
14 private int id;
15 public boolean pickedUp = false;
16 public Vector2 move, pos;
18 public Drop(float x, float y, int id) {
19 this.id = id;
20 pos = new Vector2(x, y);
21 move = new Vector2(0, -1);
22 }
24 public int closeToPlayer() {
25 boolean c1 = Intersector.overlaps(new Rectangle(GP.player.pos.x - 16, GP.player.pos.y - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), getRect());
26 boolean c2 = Intersector.overlaps(new Rectangle((GP.player.pos.x + GP.world.getWidthPx()) - 16, GP.player.pos.y - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), getRect());
27 boolean c3 = Intersector.overlaps(new Rectangle((GP.player.pos.x - GP.world.getWidthPx()) - 16, GP.player.pos.y - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), getRect());
28 if (c1) return 1;
29 if (c2) return 2;
30 if (c3) return 3;
31 return 0;
32 }
34 public void moveToPlayer() {
35 int ctp = closeToPlayer();
36 if (ctp > 0) {
37 float px = GP.player.pos.x;
38 float py = GP.player.pos.y;
39 switch (ctp) {
40 case 2:
41 px += GP.world.getWidthPx();
42 break;
43 case 3:
44 px -= GP.world.getWidthPx();
45 break;
46 }
47 float dx = 0, dy = 0;
48 if (px + GP.player.getWidth() < pos.x + 4) dx = -.5f;
49 else if (px > pos.x + 4) dx = .5f;
50 if (py + GP.player.getHeight() < pos.y + 4) dy = -.5f;
51 else if (py > pos.y + 4) dy = .5f;
52 move.add(dx, dy);
53 if (move.x > 2) move.x = 1;
54 if (move.x < -2) move.x = -1;
55 if (move.y > 2) move.y = 1;
56 if (move.y < -2) move.y = -1;
57 }
58 }
60 public void pickUpDrop(Player pl) {
61 for (int i = 0; i < pl.inv.length; i++) {
62 if (pl.inv[i] == 0 || pl.inv[i] == id) {
63 pl.inv[i] = id;
64 pickedUp = true;
65 break;
66 }
67 }
68 }
70 public int getId() {
71 return id;
72 }
74 public Rectangle getRect() {
75 return new Rectangle(pos.x, pos.y, 8, 8);
76 }
78 }