DEADSOFTWARE

Code improvements
[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 public class Drop implements Serializable {
12 private int id;
13 public boolean pickedUp = false;
14 public Vector2 move, pos;
16 public Drop(float x, float y, int id) {
17 this.id = id;
18 pos = new Vector2(x, y);
19 move = new Vector2(0, -1);
20 }
22 public int closeToPlayer(GameProc gp) {
23 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());
24 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());
25 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());
26 if (c1) return 1;
27 if (c2) return 2;
28 if (c3) return 3;
29 return 0;
30 }
32 public void moveToPlayer(GameProc gp) {
33 int ctp = closeToPlayer(gp);
34 if (ctp > 0) {
35 float px = gp.player.pos.x;
36 float py = gp.player.pos.y;
37 switch (ctp) {
38 case 2:
39 px += gp.world.getWidthPx();
40 break;
41 case 3:
42 px -= gp.world.getWidthPx();
43 break;
44 }
45 float dx = 0, dy = 0;
46 if (px + gp.player.getWidth() < pos.x + 4) dx = -.5f;
47 else if (px > pos.x + 4) dx = .5f;
48 if (py + gp.player.getHeight() < pos.y + 4) dy = -.5f;
49 else if (py > pos.y + 4) dy = .5f;
50 move.add(dx, dy);
51 if (move.x > 2) move.x = 1;
52 if (move.x < -2) move.x = -1;
53 if (move.y > 2) move.y = 1;
54 if (move.y < -2) move.y = -1;
55 }
56 }
58 public void pickUpDrop(Player pl) {
59 for (int i = 0; i < pl.inv.length; i++) {
60 if (pl.inv[i] == 0 || pl.inv[i] == id) {
61 pl.inv[i] = id;
62 pickedUp = true;
63 break;
64 }
65 }
66 }
68 public int getId() {
69 return id;
70 }
72 public Rectangle getRect() {
73 return new Rectangle(pos.x, pos.y, 8, 8);
74 }
76 }