From: fredboy Date: Tue, 16 Apr 2024 14:05:33 +0000 (+0700) Subject: Fix mob physics X-Git-Tag: alpha0.4.2~7 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=59ff9326fad76d34cbadd3fa3e4b4984892238a4;p=cavedroid.git Fix mob physics --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 3825394..6b69faa 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -264,7 +264,7 @@ public class GamePhysics { mobXColl(mob); if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) { - mob.getVelocity().y += PL_JUMP_VELOCITY; + mob.getVelocity().y = PL_JUMP_VELOCITY; mob.setCanJump(false); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 4ad04e8..2b58f7c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -56,6 +56,30 @@ public abstract class Mob extends Rectangle implements Serializable { return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT; } + private boolean isAnimationIncreasing() { + return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0; + } + + protected final void updateAnimation(float delta) { + if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) { + mAnim += mAnimDelta * delta; + } else { + mAnim = 0; + } + + if (mAnim > 60f) { + mAnim = 60f; + mAnimDelta = -ANIMATION_SPEED; + } else if (mAnim < -60f) { + mAnim = -60f; + mAnimDelta = ANIMATION_SPEED; + } + + if (mVelocity.x == 0f && isAnimationIncreasing()) { + mAnimDelta = -mAnimDelta; + } + } + /** * @return The X coordinate of a mob in blocks */ diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java index 15185ef..376c68b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java @@ -1,10 +1,13 @@ package ru.deadsoftware.cavedroid.game.mobs; +import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavedroid.game.GamePhysics; import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt; import static ru.deadsoftware.cavedroid.misc.Assets.pigSprite; @@ -12,18 +15,18 @@ public class Pig extends Mob { public Pig(float x, float y) { super(x, y, 25, 18, randomDir(), Type.MOB); - mVelocity = new Vector2(looksLeft() ? -1 : 1, 0); + mVelocity = new Vector2((looksLeft() ? -1 : 1) * GamePhysics.PL_SPEED, 0); } @Override public void changeDir() { switchDir(); - mVelocity.x = -1 + 2 * dirMultiplier(); + mVelocity.x = (-1 + 2 * dirMultiplier()) * GamePhysics.PL_SPEED; } @Override public void ai(GameWorld gameWorld, float delta) { - if (MathUtils.randomBoolean(.0025f)) { + if (MathUtils.randomBoolean(delta)) { if (mVelocity.x != 0f) { mVelocity.x = 0; } else { @@ -44,19 +47,16 @@ public class Pig extends Mob { @Override public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { - pigSprite[0][1].setRotation(getAnim()); - pigSprite[1][1].setRotation(-getAnim()); - //back legs - pigSprite[1][1].setPosition(x + (9 - dirMultiplier() * 9), y + 12); - pigSprite[1][1].draw(spriteBatch); - pigSprite[1][1].setPosition(x + 21 - (9 * dirMultiplier()), y + 12); - pigSprite[1][1].draw(spriteBatch); - //head & body - spriteBatch.draw(Assets.pigSprite[dirMultiplier()][0], x, y); - //front legs - pigSprite[0][1].setPosition(x + (9 - dirMultiplier() * 9), y + 12); - pigSprite[0][1].draw(spriteBatch); - pigSprite[0][1].setPosition(x + 21 - (9 * dirMultiplier()), y + 12); - pigSprite[0][1].draw(spriteBatch); + updateAnimation(delta); + + final Sprite frontLeg = pigSprite[0][1]; + final Sprite backLeg = pigSprite[1][1]; + final Sprite body = pigSprite[dirMultiplier()][0]; + + SpriteUtilsKt.draw(spriteBatch, backLeg, x + (9 - dirMultiplier() * 9), y + 12, -mAnim); + SpriteUtilsKt.draw(spriteBatch, backLeg, x + 21 - (9 * dirMultiplier()), y + 12, -mAnim); + SpriteUtilsKt.draw(spriteBatch, body, x, y); + SpriteUtilsKt.draw(spriteBatch, frontLeg, x + (9 - dirMultiplier() * 9), y + 12, mAnim); + SpriteUtilsKt.draw(spriteBatch, frontLeg, x + 21 - (9 * dirMultiplier()), y + 12, mAnim); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 580665e..10a1885 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -45,30 +45,6 @@ public class Player extends Mob { return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight()); } - private boolean isAnimationIncreasing() { - return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0; - } - - private void updateAnimation(float delta) { - if (mVelocity.x != 0f || Math.abs(mAnim) > 5f) { - mAnim += mAnimDelta * delta; - } else { - mAnim = 0; - } - - if (mAnim > 60f) { - mAnim = 60f; - mAnimDelta = -ANIMATION_SPEED; - } else if (mAnim < -60f) { - mAnim = -60f; - mAnimDelta = ANIMATION_SPEED; - } - - if (mVelocity.x == 0f && isAnimationIncreasing()) { - mAnimDelta = -mAnimDelta; - } - } - public void setDir(Direction dir) { if (dir != getDirection()) { switchDir();