DEADSOFTWARE

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