DEADSOFTWARE

995c79e9bc88a544804c740f8ac5582e9ed68610
[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.MathUtils;
5 import com.badlogic.gdx.math.Rectangle;
6 import com.badlogic.gdx.math.Vector2;
7 import ru.deadsoftware.cavedroid.game.world.GameWorld;
8 import ru.deadsoftware.cavedroid.game.mobs.Player;
10 import java.io.Serializable;
12 public class Drop extends Rectangle implements Serializable {
14 private final int id;
15 private final Vector2 velocity;
16 private boolean pickedUp = false;
18 Drop(float x, float y, int id) {
19 super(x, y, 8, 8);
20 this.id = id;
21 this.velocity = new Vector2(0, -1);
22 }
24 public Vector2 getVelocity() {
25 return velocity;
26 }
28 public int closeToPlayer(GameWorld gameWorld, Player player) {
29 boolean[] c = new boolean[3];
31 c[0] = Intersector.overlaps(new Rectangle(player.getX() - 16,
32 player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this);
33 c[1] = Intersector.overlaps(new Rectangle((player.getX() + gameWorld.getWidthPx()) - 16,
34 player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this);
35 c[2] = Intersector.overlaps(new Rectangle((player.getX() - gameWorld.getWidthPx()) - 16,
36 player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this);
38 for (int i = 0; i < 3; i++) {
39 if (c[i]) {
40 return i + 1;
41 }
42 }
44 return 0;
45 }
47 public void moveToPlayer(GameWorld gameWorld, Player player, int ctp) {
48 if (ctp > 0) {
49 float px = player.getX();
50 float py = player.getY();
52 switch (ctp) {
53 case 2:
54 px += gameWorld.getWidthPx();
55 break;
56 case 3:
57 px -= gameWorld.getWidthPx();
58 break;
59 }
61 float dx = 0, dy = 0;
63 if (px + player.getWidth() < x + 4) {
64 dx = -.5f;
65 } else if (px > x + 4) {
66 dx = .5f;
67 }
69 if (py + player.getHeight() < y + 4) {
70 dy = -.5f;
71 } else if (py > y + 4) {
72 dy = .5f;
73 }
75 velocity.add(dx, dy);
77 if (velocity.x > 2) {
78 velocity.x = 1;
79 } else if (velocity.x < -2) {
80 velocity.x = -1;
81 }
83 if (velocity.y > 2) {
84 velocity.y = 1;
85 } else if (velocity.y < -2) {
86 velocity.y = -1;
87 }
88 }
89 }
91 public void pickUpDrop(Player pl) {
92 for (int i = 0; i < pl.inventory.length; i++) {
93 if (pl.inventory[i] == 0 || pl.inventory[i] == id) {
94 pl.inventory[i] = id;
95 pickedUp = true;
96 break;
97 }
98 }
99 }
101 private void checkWorldBounds() {
102 // if (x + 8 > world.getWidthPx()) {
103 // x -= world.getWidthPx();
104 // } else if (x < 0) {
105 // x += world.getWidthPx();
106 // }
109 public void move(float delta) {
110 x += velocity.x * delta;
111 y += velocity.y * delta;
112 checkWorldBounds();
113 y = MathUtils.round(y);
116 public int getId() {
117 return id;
120 public boolean isPickedUp() {
121 return pickedUp;
124 public void setPickedUp(boolean pickedUp) {
125 this.pickedUp = pickedUp;