X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavecraft%2Fgame%2Fobjects%2FDrop.java;h=271be8c88389cc863eb8c897fc9c2696cc97660b;hb=d7f5950fc751cec8fa64005dd1886cac4081ee99;hp=e967f880314480a1fd9b9d73c77aedea4729622f;hpb=3fb849898964a43a51cc6483ac72f5e3abbd01f0;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java index e967f88..271be8c 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java @@ -1,25 +1,63 @@ package ru.deadsoftware.cavecraft.game.objects; +import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavecraft.game.GameProc; import java.io.Serializable; public class Drop implements Serializable { private int id; public boolean pickedUp = false; - public Vector2 move, position; + public Vector2 move, pos; public Drop(float x, float y, int id) { this.id = id; - position = new Vector2(x, y); + pos = new Vector2(x, y); move = new Vector2(0, -1); } + public int closeToPlayer(GameProc gp) { + 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()); + 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()); + 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()); + if (c1) return 1; + if (c2) return 2; + if (c3) return 3; + return 0; + } + + public void moveToPlayer(GameProc gp) { + int ctp = closeToPlayer(gp); + if (ctp > 0) { + float px = gp.player.pos.x; + float py = gp.player.pos.y; + switch (ctp) { + case 2: + px += gp.world.getWidth() * 16; + break; + case 3: + px -= gp.world.getWidth() * 16; + break; + } + float dx = 0, dy = 0; + if (px + gp.player.texWidth < pos.x + 4) dx = -.5f; + else if (px > pos.x + 4) dx = .5f; + if (py + gp.player.height < 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; + } + } + public void pickUpDrop(Player pl) { - for (int i = 0; i < pl.inventory.length; i++) { - if (pl.inventory[i] == 0 || pl.inventory[i] == id) { - pl.inventory[i] = id; + for (int i = 0; i < pl.inv.length; i++) { + if (pl.inv[i] == 0 || pl.inv[i] == id) { + pl.inv[i] = id; pickedUp = true; break; } @@ -31,7 +69,7 @@ public class Drop implements Serializable { } public Rectangle getRect() { - return new Rectangle(position.x, position.y, 8, 8); + return new Rectangle(pos.x, pos.y, 8, 8); } }