DEADSOFTWARE

Remove seed from default world config
[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, float delta) {
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 = -300f;
65 } else if (px > x + 4) {
66 dx = 300f;
67 }
69 if (py + player.getHeight() < y + 4) {
70 dy = -300f;
71 } else if (py > y + 4) {
72 dy = .300f;
73 }
75 velocity.add(dx * delta, dy * delta);
77 if (velocity.x > 300f) {
78 velocity.x = 300f;
79 } else if (velocity.x < -300f) {
80 velocity.x = -300f;
81 }
83 if (velocity.y > 300f) {
84 velocity.y = 300f;
85 } else if (velocity.y < -300f) {
86 velocity.y = -300f;
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;