X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fobjects%2FDrop.java;h=995c79e9bc88a544804c740f8ac5582e9ed68610;hb=2aa65a4cdb47df8014f28342d460fc6639bed885;hp=ed8d4ac08259515d887af14499296a9921b28a99;hpb=0a855ca3c1d0c84de41a928cc99fd8544a933015;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java index ed8d4ac..995c79e 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java @@ -1,77 +1,127 @@ package ru.deadsoftware.cavedroid.game.objects; import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavedroid.game.world.GameWorld; +import ru.deadsoftware.cavedroid.game.mobs.Player; import java.io.Serializable; -import static ru.deadsoftware.cavedroid.GameScreen.GP; +public class Drop extends Rectangle implements Serializable { -public class Drop implements Serializable { + private final int id; + private final Vector2 velocity; + private boolean pickedUp = false; - private int id; - public boolean pickedUp = false; - public Vector2 move, pos; - - public Drop(float x, float y, int id) { + Drop(float x, float y, int id) { + super(x, y, 8, 8); this.id = id; - pos = new Vector2(x, y); - move = new Vector2(0, -1); + this.velocity = new Vector2(0, -1); + } + + public Vector2 getVelocity() { + return velocity; } - public int closeToPlayer() { - 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()); - 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()); - 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()); - if (c1) return 1; - if (c2) return 2; - if (c3) return 3; + public int closeToPlayer(GameWorld gameWorld, Player player) { + boolean[] c = new boolean[3]; + + c[0] = Intersector.overlaps(new Rectangle(player.getX() - 16, + player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this); + c[1] = Intersector.overlaps(new Rectangle((player.getX() + gameWorld.getWidthPx()) - 16, + player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this); + c[2] = Intersector.overlaps(new Rectangle((player.getX() - gameWorld.getWidthPx()) - 16, + player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this); + + for (int i = 0; i < 3; i++) { + if (c[i]) { + return i + 1; + } + } + return 0; } - public void moveToPlayer() { - int ctp = closeToPlayer(); + public void moveToPlayer(GameWorld gameWorld, Player player, int ctp) { if (ctp > 0) { - float px = GP.player.pos.x; - float py = GP.player.pos.y; + float px = player.getX(); + float py = player.getY(); + switch (ctp) { case 2: - px += GP.world.getWidthPx(); + px += gameWorld.getWidthPx(); break; case 3: - px -= GP.world.getWidthPx(); + px -= gameWorld.getWidthPx(); break; } + float dx = 0, dy = 0; - if (px + GP.player.getWidth() < pos.x + 4) dx = -.5f; - else if (px > pos.x + 4) dx = .5f; - if (py + GP.player.getHeight() < pos.y + 4) dy = -.5f; - else if (py > pos.y + 4) dy = .5f; - move.add(dx, dy); - if (move.x > 2) move.x = 1; - if (move.x < -2) move.x = -1; - if (move.y > 2) move.y = 1; - if (move.y < -2) move.y = -1; + + if (px + player.getWidth() < x + 4) { + dx = -.5f; + } else if (px > x + 4) { + dx = .5f; + } + + if (py + player.getHeight() < y + 4) { + dy = -.5f; + } else if (py > y + 4) { + dy = .5f; + } + + velocity.add(dx, dy); + + if (velocity.x > 2) { + velocity.x = 1; + } else if (velocity.x < -2) { + velocity.x = -1; + } + + if (velocity.y > 2) { + velocity.y = 1; + } else if (velocity.y < -2) { + velocity.y = -1; + } } } public void pickUpDrop(Player pl) { - for (int i = 0; i < pl.inv.length; i++) { - if (pl.inv[i] == 0 || pl.inv[i] == id) { - pl.inv[i] = id; + for (int i = 0; i < pl.inventory.length; i++) { + if (pl.inventory[i] == 0 || pl.inventory[i] == id) { + pl.inventory[i] = id; pickedUp = true; break; } } } + private void checkWorldBounds() { +// if (x + 8 > world.getWidthPx()) { +// x -= world.getWidthPx(); +// } else if (x < 0) { +// x += world.getWidthPx(); +// } + } + + public void move(float delta) { + x += velocity.x * delta; + y += velocity.y * delta; + checkWorldBounds(); + y = MathUtils.round(y); + } + public int getId() { return id; } - public Rectangle getRect() { - return new Rectangle(pos.x, pos.y, 8, 8); + public boolean isPickedUp() { + return pickedUp; } + public void setPickedUp(boolean pickedUp) { + this.pickedUp = pickedUp; + } }