From 8c0966b107ebbf261f8aa9c516fb90b0d9d230e5 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sun, 30 Sep 2018 23:16:38 +0700 Subject: [PATCH] Drop magnets to player --- .../cavecraft/game/GameItems.java | 16 +++---- .../cavecraft/game/GamePhysics.java | 11 +++-- .../deadsoftware/cavecraft/game/GameProc.java | 23 ++++++---- .../cavecraft/game/GameRenderer.java | 46 +++++++++++-------- .../cavecraft/game/GameWorld.java | 13 ++++++ .../cavecraft/game/objects/Block.java | 15 ++++-- .../cavecraft/game/objects/Drop.java | 38 +++++++++++++++ 7 files changed, 121 insertions(+), 41 deletions(-) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameItems.java b/core/src/ru/deadsoftware/cavecraft/game/GameItems.java index 6a39f05..52c2e75 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameItems.java @@ -193,7 +193,7 @@ public class GameItems { //5 blocks.put("planks", new Block(4, 180, 5)); //6 - blocks.put("sapling", new Block(5, 0, 6, false, false, true)); + blocks.put("sapling", new Block(5, 0, 6, false, false, true, true)); //7 blocks.put("bedrock", new Block(6, -1, 7)); //8 @@ -233,19 +233,19 @@ public class GameItems { //25 blocks.put("cobweb", new Block(24, 1200, 0, false, false, true)); //26 - blocks.put("tallgrass", new Block(25, 0, 0, false, false, true)); + blocks.put("tallgrass", new Block(25, 0, 0, false, false, true, true)); //27 - blocks.put("deadbush", new Block(26, 0, 0, false, false, true)); + blocks.put("deadbush", new Block(26, 0, 0, false, false, true, true)); //28 blocks.put("brick_block", new Block(27, 600, 22)); //29 - blocks.put("dandelion", new Block(28, 0, 23, false, false, true)); + blocks.put("dandelion", new Block(28, 0, 23, false, false, true, true)); //30 - blocks.put("rose", new Block(29, 0, 24, false, false, true)); + blocks.put("rose", new Block(29, 0, 24, false, false, true, true)); //31 - blocks.put("brown_mushroom", new Block(30, 0, 25, false, false, true)); + blocks.put("brown_mushroom", new Block(30, 0, 25, false, false, true, true)); //32 - blocks.put("red_mushroom", new Block(31, 0, 26, false, false, true)); + blocks.put("red_mushroom", new Block(31, 0, 26, false, false, true, true)); //33 blocks.put("wool_while", new Block(32, 75, 27, true, false, false)); //34 @@ -299,7 +299,7 @@ public class GameItems { //58 blocks.put("stone_brick_slab", new Block(0, 8, 16, 8, 56, 450, 51, true, false, true)); //59 - blocks.put("cactus", new Block(1, 0, 14, 16, 57, 39, 52, true, false, true)); + blocks.put("cactus", new Block(1, 0, 14, 16, 57, 39, 52, true, false, true, true)); //60 blocks.put("water_16", new Block(7, -1, 0, false, false, true)); //61 diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index d314b82..dc0c93d 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -68,8 +68,14 @@ public class GamePhysics { } private void dropPhy(Drop drop) { - if (drop.move.y < 9) drop.move.y += gravity.y / 4; + if (drop.closeToPlayer(gp) > 0) { + drop.moveToPlayer(gp); + } else { + if (drop.move.y < 9) drop.move.y += gravity.y / 4; + } drop.pos.add(drop.move); + if (drop.pos.x + 8 > gp.world.getWidth() * 16) drop.pos.x -= gp.world.getWidth() * 16; + else if (drop.pos.x < 0) drop.pos.x += gp.world.getWidth() * 16; drop.pos.y = MathUtils.round(drop.pos.y); while (checkColl(drop.getRect())) { drop.pos.y--; @@ -185,8 +191,7 @@ public class GamePhysics { for (Iterator it = gp.drops.iterator(); it.hasNext(); ) { Drop drop = it.next(); dropPhy(drop); - if (Intersector.overlaps(drop.getRect(), gp.player.getRect())) - drop.pickUpDrop(gp.player); + if (Intersector.overlaps(drop.getRect(), gp.player.getRect())) drop.pickUpDrop(gp.player); if (drop.pickedUp) it.remove(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 08f4425..14b7015 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -365,9 +365,9 @@ public class GameProc implements Serializable { } } - if (world.getForeMap(x, y) == 59) { + if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).rb) { if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).coll) { - world.setForeMap(x, y, 0); + world.destroyForeMap(x, y, this); updateBlock(x, y - 1); } } @@ -409,14 +409,19 @@ public class GameProc implements Serializable { checkCursorBounds(); if (isTouchDown && touchDownBtn == Input.Buttons.LEFT) { - if (world.getForeMap(curX, curY) > 0 && - GameItems.getBlock(world.getForeMap(curX, curY)).getHp() >= 0) {// || world.getBackMap(curX, curY) > 0) { + if ((world.getForeMap(curX, curY) > 0 && GameItems.getBlock(world.getForeMap(curX, curY)).getHp() >= 0) || + world.getBackMap(curX, curY) > 0 && GameItems.getBlock(world.getBackMap(curX, curY)).getHp() >= 0) { blockDmg++; - if (blockDmg >= GameItems.getBlock(world.getForeMap(curX, curY)).getHp()) { - if (GameItems.getBlock(world.getForeMap(curX, curY)).getDrop() > 0) - drops.add(new Drop(curX * 16 + 4, curY * 16 + 4, GameItems.getBlock(world.getForeMap(curX, curY)).getDrop())); - world.placeToForeground(curX, curY, 0); - blockDmg = 0; + if (world.getForeMap(curX, curY) > 0) { + if (blockDmg >= GameItems.getBlock(world.getForeMap(curX, curY)).getHp()) { + world.destroyForeMap(curX, curY, this); + blockDmg = 0; + } + } else if (world.getBackMap(curX, curY) > 0) { + if (blockDmg >= GameItems.getBlock(world.getBackMap(curX, curY)).getHp()) { + world.destroyBackMap(curX, curY, this); + blockDmg = 0; + } } } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 96f8c78..162c77b 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -29,11 +29,11 @@ public class GameRenderer extends Renderer { return y * 16 - getCamY(); } - private void drawWreck() { + private void drawWreck(int bl) { if (gp.blockDmg > 0) { spriter.draw(Assets.wreck[ 10 * gp.blockDmg / - GameItems.getBlock(gp.world.getForeMap(gp.curX, gp.curY)).getHp()], + GameItems.getBlock(bl).getHp()], gp.curX * 16 - getCamX(), gp.curY * 16 - getCamY()); } @@ -53,7 +53,8 @@ public class GameRenderer extends Renderer { spriter.draw( Assets.blockTex[GameItems.getBlock(gp.world.getBackMap(x, y)).getTex()], drawX(x), drawY(y)); - if (gp.world.getForeMap(x, y) == 0) drawWreck(); + if (gp.world.getForeMap(x, y) == 0 && x == gp.curX && y == gp.curY) + drawWreck(gp.world.getBackMap(gp.curX, gp.curY)); Assets.shade.setPosition(drawX(x), drawY(y)); Assets.shade.draw(spriter); } @@ -61,7 +62,8 @@ public class GameRenderer extends Renderer { spriter.draw( Assets.blockTex[GameItems.getBlock(gp.world.getForeMap(x, y)).getTex()], drawX(x), drawY(y)); - drawWreck(); + if (x == gp.curX && y == gp.curY) + drawWreck(gp.world.getForeMap(gp.curX, gp.curY)); } } } @@ -80,7 +82,8 @@ public class GameRenderer extends Renderer { spriter.draw( Assets.blockTex[GameItems.getBlock(gp.world.getForeMap(x, y)).getTex()], drawX(x), drawY(y)); - drawWreck(); + if (x == gp.curX && y == gp.curY) + drawWreck(gp.world.getForeMap(gp.curX, gp.curY)); } } } @@ -98,11 +101,17 @@ public class GameRenderer extends Renderer { private void drawDrop(Drop drop) { switch (GameItems.getItem(drop.getId()).getType()) { case 0: - Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition(drop.pos.x - getCamX() - gp.world.getWidth() * 16, drop.pos.y - getCamY()); + Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition( + drop.pos.x - getCamX() - gp.world.getWidth() * 16, + drop.pos.y - getCamY()); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter); - Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition(drop.pos.x - getCamX(), drop.pos.y - getCamY()); + Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition( + drop.pos.x - getCamX(), + drop.pos.y - getCamY()); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter); - Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition(drop.pos.x - getCamX() + gp.world.getWidth() * 16, drop.pos.y - getCamY()); + Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition( + drop.pos.x - getCamX() + gp.world.getWidth() * 16, + drop.pos.y - getCamY()); Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter); } } @@ -145,12 +154,13 @@ public class GameRenderer extends Renderer { spriter.draw(Assets.plSprite[pl.dir][1], pl.pos.x - getCamX() - 2, pl.pos.y - getCamY() + 8); //item in hand - if (pl.inv[gp.slot] > 0) + if (pl.inv[gp.slot] > 0) { + float handRotation = MathUtils.degRad * Assets.plSprite[0][2].getRotation(); switch (GameItems.getItem(pl.inv[gp.slot]).getType()) { case 0: Assets.blockTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].setPosition( - pl.pos.x - getCamX() - 8 * MathUtils.sin(MathUtils.degRad * Assets.plSprite[0][2].getRotation()), - pl.pos.y - getCamY() + 6 + 8 * MathUtils.cos(MathUtils.degRad * Assets.plSprite[0][2].getRotation())); + pl.pos.x - getCamX() - 8 * MathUtils.sin(handRotation), + pl.pos.y - getCamY() + 6 + 8 * MathUtils.cos(handRotation)); Assets.blockTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].draw(spriter); break; default: @@ -158,12 +168,13 @@ public class GameRenderer extends Renderer { Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].setRotation( -45 + pl.dir * 90 + Assets.plSprite[0][2].getRotation()); Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].setPosition( - pl.pos.x - getCamX() - 10 + (12 * pl.dir) - 8 * MathUtils.sin(MathUtils.degRad * Assets.plSprite[0][2].getRotation()), - pl.pos.y - getCamY() + 2 + 8 * MathUtils.cos(MathUtils.degRad * Assets.plSprite[0][2].getRotation())); + pl.pos.x - getCamX() - 10 + (12 * pl.dir) - 8 * MathUtils.sin(handRotation), + pl.pos.y - getCamY() + 2 + 8 * MathUtils.cos(handRotation)); Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].draw(spriter); Assets.itemTex[GameItems.getItem(pl.inv[gp.slot]).getTex()].flip((pl.dir == 0), false); break; } + } //front hand Assets.plSprite[0][2].setPosition( pl.pos.x - getCamX() - 6, @@ -215,25 +226,25 @@ public class GameRenderer extends Renderer { spriter.draw(Assets.guiCur, gp.curX * 16 - getCamX(), gp.curY * 16 - getCamY()); - spriter.draw(Assets.invBar, getWidth() / 2 - Assets.invBar.getRegionWidth() / 2, 0); + spriter.draw(Assets.invBar, getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2, 0); for (int i = 0; i < 9; i++) { if (gp.player.inv[i] > 0) { switch (GameItems.getItem(gp.player.inv[i]).getType()) { case 0: spriter.draw(Assets.blockTex[GameItems.getItem(gp.player.inv[i]).getTex()], - getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, + getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, 3); break; case 1: spriter.draw(Assets.itemTex[GameItems.getItem(gp.player.inv[i]).getTex()], - getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, + getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, 3); break; } } } spriter.draw(Assets.invBarCur, - getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 - 1 + 20 * gp.slot, + getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 - 1 + 20 * gp.slot, -1); } @@ -285,7 +296,6 @@ public class GameRenderer extends Renderer { drawString("Drops: " + gp.drops.size(), 0, 40); drawString("Block: " + GameItems.getBlockKey(gp.world.getForeMap(gp.curX, gp.curY)), 0, 50); } - spriter.end(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index 073c3d5..d84faa3 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java @@ -1,6 +1,7 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavecraft.game.objects.Drop; public class GameWorld { @@ -111,6 +112,18 @@ public class GameWorld { } } + public void destroyForeMap(int x, int y, GameProc gp) { + if (GameItems.getBlock(getForeMap(x, y)).getDrop() > 0) + gp.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getBlock(getForeMap(x, y)).getDrop())); + placeToForeground(x, y, 0); + } + + public void destroyBackMap(int x, int y, GameProc gp) { + if (GameItems.getBlock(getBackMap(x, y)).getDrop() > 0) + gp.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getBlock(getBackMap(x, y)).getDrop())); + placeToBackground(x, y, 0); + } + public Vector2 getSpawnPoint() { int x = 0, y = 0; while (true) { diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java index 839e383..8d27235 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Block.java @@ -8,17 +8,25 @@ public class Block { private int tex; private int hp, drop; - public boolean coll, bg, tp; + public boolean coll, bg, tp, rb; public Block(int tex, int hp, int drop) { - this(0, 0, 16, 16, tex, hp, drop, true, false, false); + this(0, 0, 16, 16, tex, hp, drop, true, false, false, false); } public Block(int tex, int hp, int drop, boolean coll, boolean bg, boolean tp) { - this(0, 0, 16, 16, tex, hp, drop, coll, bg, tp); + this(0, 0, 16, 16, tex, hp, drop, coll, bg, tp, false); + } + + public Block(int tex, int hp, int drop, boolean coll, boolean bg, boolean tp, boolean rb) { + this(0, 0, 16, 16, tex, hp, drop, coll, bg, tp, rb); } public Block(int x, int y, int w, int h, int tex, int hp, int drop, boolean coll, boolean bg, boolean tp) { + this(x, y, w, h, tex, hp, drop, coll, bg, tp, false); + } + + public Block(int x, int y, int w, int h, int tex, int hp, int drop, boolean coll, boolean bg, boolean tp, boolean rb) { this.x = x; this.y = y; this.w = w; @@ -29,6 +37,7 @@ public class Block { this.coll = coll; this.bg = bg; this.tp = tp; + this.rb = rb; } public int getTex() { diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java index fa60f6b..478a05b 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java @@ -1,7 +1,9 @@ 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; @@ -16,6 +18,42 @@ public class Drop implements Serializable { 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 + (gp.player.texWidth / 2); + float py = gp.player.pos.y + (gp.player.height / 2); + 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 < pos.x + 4) dx = -.5f; + else if (px > pos.x + 4) dx = .5f; + if (py < pos.y + 4) dy = -.5f; + else if (py > pos.y + 4) dy = .5f; + move.add(dx, dy); +// if (move.x > 2) move.x = 2; +// if (move.x < -2) move.x = -2; +// if (move.y > 2) move.y = 2; +// if (move.y < -2) move.y = -2; + } + } + public void pickUpDrop(Player pl) { for (int i = 0; i < pl.inv.length; i++) { if (pl.inv[i] == 0 || pl.inv[i] == id) { -- 2.29.2