From 01f293d956e7ad17163c67b8f2a2ebfd8f64ac6d Mon Sep 17 00:00:00 2001 From: fred-boy Date: Wed, 25 Apr 2018 16:07:49 +0700 Subject: [PATCH] Better pig AI --- .../cavecraft/game/GameInput.java | 5 ++ .../cavecraft/game/GamePhysics.java | 46 +++++++++++-------- .../deadsoftware/cavecraft/game/GameProc.java | 2 +- .../deadsoftware/cavecraft/game/mobs/Mob.java | 1 + .../deadsoftware/cavecraft/game/mobs/Pig.java | 16 ++----- 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java index bded581..c0fc292 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInput.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.game.mobs.Pig; import ru.deadsoftware.cavecraft.misc.AppState; import ru.deadsoftware.cavecraft.misc.Assets; import ru.deadsoftware.cavecraft.CaveGame; @@ -78,6 +79,10 @@ public class GameInput { else CaveGame.STATE = AppState.GAME_PLAY; break; + case Input.Keys.G: + gameProc.mobs.add(new Pig(gameProc.cursorX*16, gameProc.cursorY*16)); + break; + case Input.Keys.ESCAPE: case Input.Keys.BACK: CaveGame.STATE = AppState.GOTO_MENU; break; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 5677c24..34c4df1 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -22,21 +22,13 @@ public class GamePhysics { } private boolean checkJump(Rectangle rect, int dir) { - int bl = 0; + int bl; switch (dir) { case 0: - bl = gameProc.world.getForeMap( - (int)((rect.x+(rect.width/2))/16) - 1, - (int)(rect.y/16)+1); - if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16))>0) bl=0; - if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16)-1)>0) bl=0; + bl = gameProc.world.getForeMap((int)((rect.x-8)/16),(int)((rect.y+rect.height-8)/16)); break; case 1: - bl = gameProc.world.getForeMap( - (int)((rect.x+(rect.width/2))/16) + 1, - (int)(rect.y/16)+1); - if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16))>0) bl=0; - if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16)-1)>0) bl=0; + bl = gameProc.world.getForeMap((int)((rect.x+rect.width+8)/16),(int)((rect.y+rect.height-8)/16)); break; default: bl=0; @@ -69,7 +61,7 @@ public class GamePhysics { pl.position.add(pl.moveY); if (checkColl(pl.getRect())) { int d = -1; - if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1; + if (pl.moveY.y<0) d=1; if (d==-1) { pl.flyMode = false; pl.canJump = true; @@ -107,24 +99,38 @@ public class GamePhysics { 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; + if (mob.moveY.y<0) d=1; + if (d==-1) mob.canJump = true; 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); + if (mob.moveY.y<18) mob.moveY.add(gravity); mob.position.add(mob.moveX); + if (checkColl(mob.getRect())) { + if (mob.canJump) { + mob.position.y-=8; + } + if (checkColl(mob.getRect())) { + if (mob.canJump) mob.position.y+=8; + 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; + } + } if (mob.position.x+mob.width/2<0) mob.position.x+=gameProc.world.getWidth()*16; if (mob.position.x+mob.width/2>gameProc.world.getWidth()*16) mob.position.x-=gameProc.world.getWidth()*16; - 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; + if (mob.position.y > gameProc.world.getHeight()*16) { + mob.position.y = 0; + } + if (checkJump(mob.getRect(), mob.dir) && mob.canJump && !mob.moveX.equals(Vector2.Zero)) { + mob.moveY.add(0, -8); + mob.canJump = false; } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index ccf5e2b..ce88856 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -40,7 +40,7 @@ public class GameProc implements Serializable{ player = new Player(world.getSpawnPoint()); mobs = new ArrayList(); for (int i=0; i<16; i++) { - mobs.add(new Pig(i*256, 128&16, this)); + mobs.add(new Pig(i*256, 196*16)); } physics = new GamePhysics(this); if (!CaveGame.TOUCH) ctrlMode = 1; diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java index cfa932a..7b773d0 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java @@ -15,6 +15,7 @@ public abstract class Mob implements Serializable{ public Vector2 moveX, moveY; public int width, height, dir; public boolean canJump; + public boolean agressive; public static void animateMobs() { Assets.pigSprite[0][1].setRotation(ANIMATION); diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java index 879b667..aaa3019 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java @@ -4,15 +4,13 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavecraft.game.GameWorld; import ru.deadsoftware.cavecraft.misc.Assets; import ru.deadsoftware.cavecraft.game.GameProc; public class Pig extends Mob{ - private GameProc gameProc; - - public Pig(int x, int y, GameProc gameProc) { - this.gameProc = gameProc; + public Pig(int x, int y) { position = new Vector2(x, y); moveX = new Vector2(0, 0); moveY = new Vector2(0, 0); @@ -20,17 +18,13 @@ public class Pig extends Mob{ height = 18; dir = 0; canJump = false; + agressive = false; } @Override public void ai() { - if (canJump && position.x>16 && position.x<(gameProc.world.getWidth()-1)*16 && - gameProc.world.getForeMap((int)(position.x/16)+(dir*2-1), (int)((position.y+height)/16))>0 && - gameProc.world.getForeMap((int)(position.x/16)+(dir*2-1), (int)((position.y)/16))==0) - moveY.add(0, -8); - if (MathUtils.randomBoolean(.0001f)) dir++; - if (dir>1) dir = 0; - moveX.set(-1.5f+3*dir,0); + if (MathUtils.randomBoolean(.0025f)) dir=-dir+1; + moveX.set(-1+2*dir,0); } @Override -- 2.29.2