From: fred-boy Date: Sun, 9 Sep 2018 11:10:31 +0000 (+0700) Subject: Improve autojump and AI X-Git-Tag: alpha0.4~95 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=d5e611f9fb8deb93a9403b41f17dc000b9fa63ce;p=cavedroid.git Improve autojump and AI --- diff --git a/core/src/ru/deadsoftware/cavecraft/GameScreen.java b/core/src/ru/deadsoftware/cavecraft/GameScreen.java index d359fe9..dc7bb60 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameScreen.java +++ b/core/src/ru/deadsoftware/cavecraft/GameScreen.java @@ -11,6 +11,7 @@ import ru.deadsoftware.cavecraft.misc.*; public class GameScreen implements Screen { public static int FPS; + public static boolean SHOW_DEBUG = false; private GameProc gameProc; private Renderer renderer; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java index 9aa90f2..e44e005 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Input; import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavecraft.CaveGame; +import ru.deadsoftware.cavecraft.GameScreen; import ru.deadsoftware.cavecraft.game.mobs.Pig; import ru.deadsoftware.cavecraft.misc.AppState; import ru.deadsoftware.cavecraft.misc.Assets; @@ -86,6 +87,10 @@ public class GameInput { case Input.Keys.ESCAPE: case Input.Keys.BACK: CaveGame.STATE = AppState.GOTO_MENU; break; + + case Input.Keys.F1: + GameScreen.SHOW_DEBUG = !GameScreen.SHOW_DEBUG; + break; } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 34c4df1..c335908 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -26,9 +26,11 @@ public class GamePhysics { switch (dir) { case 0: bl = gameProc.world.getForeMap((int)((rect.x-8)/16),(int)((rect.y+rect.height-8)/16)); + if (checkColl(new Rectangle(rect.x-16, rect.y-18, rect.width, rect.height))) bl=0; break; case 1: bl = gameProc.world.getForeMap((int)((rect.x+rect.width+8)/16),(int)((rect.y+rect.height-8)/16)); + if (checkColl(new Rectangle(rect.x+16, rect.y-18, rect.width, rect.height))) bl=0; break; default: bl=0; @@ -121,6 +123,7 @@ public class GamePhysics { 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.canJump) mob.changeDir(); } } if (mob.position.x+mob.width/2<0) mob.position.x+=gameProc.world.getWidth()*16; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 38680f9..b528972 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.CaveGame; +import ru.deadsoftware.cavecraft.GameScreen; import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.objects.Player; import ru.deadsoftware.cavecraft.misc.Assets; @@ -171,7 +172,7 @@ public class GameRenderer extends Renderer { private void drawGamePlay() { drawWorldBackground(); - Mob.animateMobs(); + //Mob.animateMobs(); for (Mob mob : gameProc.mobs) drawMob(mob); drawPlayer(gameProc.player); drawWorldForeground(); @@ -195,6 +196,13 @@ public class GameRenderer extends Renderer { if (CaveGame.TOUCH) drawTouchGui(); + if (GameScreen.SHOW_DEBUG) { + drawString("FPS: "+GameScreen.FPS,0, 0); + drawString("X: "+(int)(gameProc.player.position.x/16),0, 10); + drawString("Y: "+(int)(gameProc.player.position.y/16),0, 20); + drawString("Mobs: "+gameProc.mobs.size(), 0, 30); + } + spriteBatch.end(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java index 7b773d0..0b0f9b3 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Mob.java @@ -9,24 +9,15 @@ import java.io.Serializable; public abstract class Mob implements Serializable{ - public static int ANIM_SPEED = 6; - public static int ANIMATION = 0; + public int ANIM_SPEED = 6; public Vector2 position; public Vector2 moveX, moveY; - public int width, height, dir; + public int width, height, dir, animation; public boolean canJump; public boolean agressive; - public static void animateMobs() { - Assets.pigSprite[0][1].setRotation(ANIMATION); - Assets.pigSprite[1][1].setRotation(-ANIMATION); - ANIMATION+=ANIM_SPEED; - if (ANIMATION>=60 || ANIMATION<=-60) { - ANIM_SPEED = -ANIM_SPEED; - } - } - public abstract void ai(); + public abstract void changeDir(); public abstract void draw(SpriteBatch spriteBatch, float x, float y); public abstract Rectangle getRect(); diff --git a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java index aaa3019..10c4242 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavecraft/game/mobs/Pig.java @@ -11,24 +11,38 @@ import ru.deadsoftware.cavecraft.game.GameProc; public class Pig extends Mob{ public Pig(int x, int y) { + dir = MathUtils.random(1); position = new Vector2(x, y); - moveX = new Vector2(0, 0); + moveX = new Vector2(-1+dir*2, 0); moveY = new Vector2(0, 0); width = 25; height = 18; - dir = 0; canJump = false; agressive = false; } @Override - public void ai() { - if (MathUtils.randomBoolean(.0025f)) dir=-dir+1; + public void changeDir() { + dir=-dir+1; moveX.set(-1+2*dir,0); + if (MathUtils.randomBoolean(.0025f)) { + moveX.set(0, 0); + } + } + + @Override + public void ai() { + if (MathUtils.randomBoolean(.0025f)) changeDir(); + if (moveX.x != 0f) animation+=ANIM_SPEED; else animation=0; + if (animation>=60 || animation<=-60) { + ANIM_SPEED = -ANIM_SPEED; + } } @Override public void draw(SpriteBatch spriteBatch, float x, float y) { + Assets.pigSprite[0][1].setRotation(animation); + Assets.pigSprite[1][1].setRotation(-animation); //back legs Assets.pigSprite[1][1].setPosition(x-4+(9-dir*9),y+6); Assets.pigSprite[1][1].draw(spriteBatch);