From 083e40ac85e4aca2df81d1ca16c0a54561c3bd38 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Wed, 11 Apr 2018 16:26:47 +0700 Subject: [PATCH] Add mobs --- .../cavecraft/game/GamePhysics.java | 37 ++++++++++++++++-- .../deadsoftware/cavecraft/game/GameProc.java | 9 +++++ .../cavecraft/game/GameRenderer.java | 9 +++++ .../cavecraft/game/mobs/Human.java | 38 +++++++++++++++++++ .../deadsoftware/cavecraft/game/mobs/Mob.java | 16 ++++++++ 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java create mode 100644 core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index bfde61c..f49eb17 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -1,9 +1,9 @@ package ru.deadsoftware.cavecraft.game; -import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.objects.Player; public class GamePhysics { @@ -33,7 +33,7 @@ public class GamePhysics { return false; } - private void movePlayer(Player pl) { + private void playerPhy(Player pl) { pl.position.add(pl.moveY); if (checkColl(pl.getRect())) { pl.canJump = true; @@ -70,8 +70,39 @@ public class GamePhysics { gameProc.renderer.camTargetPos.x = gameProc.world.getWidth()*16-gameProc.renderer.camera.viewportWidth; } + private void mobPhy(Mob mob) { + mob.position.add(mob.moveY); + if (checkColl(mob.getRect())) { + mob.canJump = true; + int d = -1; + if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1; + mob.position.y = MathUtils.round(mob.position.y); + while (checkColl(mob.getRect())) mob.position.y+=d; + mob.moveY.setZero(); + } else { + mob.canJump = false; + } + mob.moveY.add(gravity); + mob.position.add(mob.moveX); + if (mob.position.x<32 || + mob.position.x+mob.width>gameProc.world.getWidth()*16-32) + mob.position.sub(mob.moveX); + if (checkColl(mob.getRect())) { + int d = 0; + if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1; + mob.position.x = MathUtils.round(mob.position.x); + while (checkColl(mob.getRect())) mob.position.x+=d; + } +} + public void update(float delta) { - movePlayer(gameProc.player); + + for (Mob mob : gameProc.mobs) { + mob.ai(); + mobPhy(mob); + } + playerPhy(gameProc.player); + if (gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x <= 8 && gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x >= -8) { gameProc.renderer.camera.position.x = gameProc.renderer.camTargetPos.x; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 6fc7dd9..4c63912 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -1,6 +1,9 @@ package ru.deadsoftware.cavecraft.game; +import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.TimeUtils; +import ru.deadsoftware.cavecraft.game.mobs.Human; +import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.objects.Player; public class GameProc { @@ -9,6 +12,8 @@ public class GameProc { public Player player; + public Array mobs; + public GameWorld world; public GameRenderer renderer; public GamePhysics physics; @@ -26,6 +31,10 @@ public class GameProc { renderer = new GameRenderer(this); physics = new GamePhysics(this); player = new Player(); + mobs = new Array(); + for (int i=0; i<6; i++) { + mobs.add(new Human(64*(i+1),0, this)); + } } public void resetRenderer() { diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 4ab0704..6a41ea2 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -11,6 +11,7 @@ import ru.deadsoftware.cavecraft.Assets; import ru.deadsoftware.cavecraft.CaveGame; import ru.deadsoftware.cavecraft.Items; import ru.deadsoftware.cavecraft.GameScreen; +import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.objects.Player; public class GameRenderer { @@ -64,6 +65,11 @@ public class GameRenderer { } } + private void drawMob(Mob mob) { + spriteBatch.draw(Assets.playerSkin[mob.dir], + mob.position.x - camera.position.x, mob.position.y - camera.position.y); + } + private void drawPlayer(Player pl) { spriteBatch.draw(Assets.playerSkin[pl.dir], pl.position.x - camera.position.x, pl.position.y - camera.position.y); @@ -104,6 +110,9 @@ public class GameRenderer { spriteBatch.begin(); drawWorld(); + for (Mob mob : gameProc.mobs) { + drawMob(mob); + } drawPlayer(gameProc.player); drawGUI(); spriteBatch.end(); diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java new file mode 100644 index 0000000..318ea15 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Human.java @@ -0,0 +1,38 @@ +package ru.deadsoftware.cavecraft.game.mobs; + +import com.badlogic.gdx.math.RandomXS128; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavecraft.game.GameProc; + +public class Human extends Mob{ + + private RandomXS128 rand = new RandomXS128(); + private GameProc gameProc; + + public Human(int x, int y, GameProc gameProc) { + this.gameProc = gameProc; + position = new Vector2(x, y); + moveX = new Vector2(0, 0); + moveY = new Vector2(0, 0); + width = 8; + height = 30; + dir = 1; + canJump = false; + } + + @Override + public void ai() { + if (canJump && gameProc.world.getForeMap( + (int)(position.x/16)+(dir*2-1), (int)(position.y/16)+1)>0) + moveY.add(0, -8); + if (rand.nextInt(500)>490) dir++; + if (dir>1) dir = 0; + moveX.setZero(); + moveX.add(-2+4*dir, 0); + } + + public Rectangle getRect() { + return new Rectangle(position.x, position.y, width, height); + } +} diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java new file mode 100644 index 0000000..054f089 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java @@ -0,0 +1,16 @@ +package ru.deadsoftware.cavecraft.game.mobs; + +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; + +public abstract class Mob { + + public Vector2 position; + public Vector2 moveX, moveY; + public int width, height, dir; + public boolean canJump; + + public abstract void ai(); + public abstract Rectangle getRect(); + +} -- 2.29.2