From a7ae39b1ed142e467b11689cd47010e5b9f90ceb Mon Sep 17 00:00:00 2001 From: fred-boy Date: Thu, 12 Apr 2018 22:57:28 +0700 Subject: [PATCH] Better spawn point pick --- .../cavecraft/game/GameInputHandler.java | 1 + .../cavecraft/game/GamePhysics.java | 4 +++- .../deadsoftware/cavecraft/game/GameProc.java | 2 +- .../cavecraft/game/GameWorld.java | 21 +++++++++++++++++++ .../cavecraft/game/objects/Player.java | 4 ++-- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index 102a0f7..0b714dd 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -2,6 +2,7 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Input; import com.badlogic.gdx.utils.TimeUtils; +import ru.deadsoftware.cavecraft.GameScreen; public class GameInputHandler { diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 8b79fe0..e461a93 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -91,7 +91,9 @@ public class GamePhysics { while (checkColl(pl.getRect())) pl.position.x += d; } } - + if (pl.position.y > gameProc.world.getHeight()*16) { + pl.position = gameProc.world.getSpawnPoint().cpy(); + } /*if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) { pl.moveY.add(0, -8); pl.canJump = false; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index b955d23..0e3993d 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -31,7 +31,7 @@ public class GameProc { world = new GameWorld(4096,256); renderer = new GameRenderer(this); physics = new GamePhysics(this); - player = new Player(world.getWidth()/2*16+4, 0); + player = new Player(world.getSpawnPoint()); mobs = new Array(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index c32bbd9..97d05cb 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.Gdx; +import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.Items; public class GameWorld { @@ -71,4 +72,24 @@ public class GameWorld { setBackMap(x,y,value); } + public Vector2 getSpawnPoint() { + float x=0, y=0; + boolean found = false; + x = getWidth()/2; + while (!found) { + for (int i = 0; i < getHeight(); i++) { + if (getForeMap((int)x, i)>0 && + Items.BLOCKS.getValueAt(getForeMap((int)x, i)).collision) { + y = i-3; + found = true; + break; + } + } + if (!found) x--; + } + x = x*16 + 4; + y *= 16; + return new Vector2(x,y); + } + } diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java index bcebde6..92c8171 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java @@ -15,8 +15,8 @@ public class Player { public int[] inventory; public boolean flyMode = false; - public Player(int x, int y) { - position = new Vector2(x, y); + public Player(Vector2 spawnPoint) { + position = spawnPoint.cpy(); moveX = new Vector2(0, 0); moveY = new Vector2(0, 0); width = 4; -- 2.29.2