From: fredboy Date: Tue, 16 Apr 2024 10:25:22 +0000 (+0700) Subject: Add player head rotation X-Git-Tag: alpha0.4.2~10 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=e7a1e15a93abaafa8e2e0435336483a198cc697c;p=cavedroid.git Add player head rotation --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 7221d16..29c8188 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.TimeUtils; import com.google.common.collect.Range; import ru.deadsoftware.cavedroid.MainConfig; @@ -169,11 +170,18 @@ public class GameInput { Gdx.graphics.getWidth()) + gameRenderer.getCamX()); mCurX = tmpX / 16; - mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / - Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16; + final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / + Gdx.graphics.getHeight()) + gameRenderer.getCamY()); + mCurY = tmpY / 16; + if (tmpX < 0) { mCurX--; } + + final double a = tmpX - mPlayer.x; + final double b = tmpY - mPlayer.y; + + mPlayer.headRotation = (float) Math.atan(b / a) * MathUtils.radDeg; } if (pastX != mCurX || pastY != mCurY) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 3335b7f..580665e 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -1,9 +1,12 @@ 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.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt; public class Player extends Mob { @@ -11,6 +14,7 @@ public class Player extends Mob { public int slot; public final int gameMode; public boolean swim; + public float headRotation = 0f; public Player() { super(0, 0, 4, 30, randomDir(), Type.MOB); @@ -45,33 +49,13 @@ public class Player extends Mob { return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0; } - public void setDir(Direction dir) { - if (dir != getDirection()) { - switchDir(); - } - } - - @Override - public void ai(GameWorld gameWorld, float delta) { - } - - @Override - public void changeDir() { - } - - @Override - public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { + private void updateAnimation(float delta) { if (mVelocity.x != 0f || Math.abs(mAnim) > 5f) { mAnim += mAnimDelta * delta; } else { mAnim = 0; } - Assets.playerSprite[0][2].setRotation(mAnim); - Assets.playerSprite[1][2].setRotation(-mAnim); - Assets.playerSprite[0][3].setRotation(-mAnim); - Assets.playerSprite[1][3].setRotation(mAnim); - if (mAnim > 60f) { mAnim = 60f; mAnimDelta = -ANIMATION_SPEED; @@ -83,23 +67,39 @@ public class Player extends Mob { if (mVelocity.x == 0f && isAnimationIncreasing()) { mAnimDelta = -mAnimDelta; } + } - //back hand - Assets.playerSprite[1][2].setPosition(x + 2, y + 8); - Assets.playerSprite[1][2].draw(spriteBatch); - //back leg - Assets.playerSprite[1][3].setPosition(x + 2, y + 20); - Assets.playerSprite[1][3].draw(spriteBatch); - //front leg - Assets.playerSprite[0][3].setPosition(x + 2, y + 20); - Assets.playerSprite[0][3].draw(spriteBatch); - //head - spriteBatch.draw(Assets.playerSprite[dirMultiplier()][0], x, y); - //body - spriteBatch.draw(Assets.playerSprite[dirMultiplier()][1], x + 2, y + 8); - //front hand - Assets.playerSprite[0][2].setPosition(x + 2, y + 8); - Assets.playerSprite[0][2].draw(spriteBatch); + public void setDir(Direction dir) { + if (dir != getDirection()) { + switchDir(); + } + } + + @Override + public void ai(GameWorld gameWorld, float delta) { + } + + @Override + public void changeDir() { + } + + @Override + public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { + updateAnimation(delta); + + final Sprite backHand = Assets.playerSprite[1][2]; + final Sprite backLeg = Assets.playerSprite[1][3]; + final Sprite frontLeg = Assets.playerSprite[0][3]; + final Sprite head = Assets.playerSprite[dirMultiplier()][0]; + final Sprite body = Assets.playerSprite[dirMultiplier()][1]; + final Sprite frontHand = Assets.playerSprite[0][2]; + + SpriteUtilsKt.draw(spriteBatch, backHand, x + 2, y + 8, -mAnim); + SpriteUtilsKt.draw(spriteBatch, backLeg, x + 2, y + 20, mAnim); + SpriteUtilsKt.draw(spriteBatch, frontLeg, x + 2, y + 20, -mAnim); + SpriteUtilsKt.draw(spriteBatch, head, x, y, headRotation); + SpriteUtilsKt.draw(spriteBatch, body, x + 2, y + 8); + SpriteUtilsKt.draw(spriteBatch, frontHand, x + 2, y + 8, mAnim); } } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index 335e3c0..90a7ec1 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -49,6 +49,12 @@ public class Assets { } } + private static void setPlayerHeadOrigin() { + for (Sprite[] sprites : playerSprite) { + sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight()); + } + } + /** * Loads texture names and sizes from json/texture_regions.json, cuts them to TextureRegions * and puts to {@link #textureRegions} HashMap @@ -76,6 +82,7 @@ public class Assets { loadMob(playerSprite, "char"); loadMob(pigSprite, "pig"); loadJSON(); + setPlayerHeadOrigin(); minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true); minecraftFont.getData().setScale(.375f); } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt new file mode 100644 index 0000000..d9f52d5 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt @@ -0,0 +1,14 @@ +package ru.deadsoftware.cavedroid.misc.utils + +import com.badlogic.gdx.graphics.g2d.Sprite +import com.badlogic.gdx.graphics.g2d.SpriteBatch + +/** + * Draw sprite at given position rotated by [rotation] degrees + */ +@JvmOverloads +fun SpriteBatch.draw(sprite: Sprite, x: Float, y: Float, rotation: Float = 0f) { + sprite.rotation = rotation + sprite.setPosition(x, y) + sprite.draw(this) +}