DEADSOFTWARE

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