summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7cb80fd)
raw | patch | inline | side by side (parent: 7cb80fd)
author | fredboy <fredboy@protonmail.com> | |
Tue, 16 Apr 2024 10:25:22 +0000 (17:25 +0700) | ||
committer | fredboy <fredboy@protonmail.com> | |
Tue, 16 Apr 2024 11:00:34 +0000 (18:00 +0700) |
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java
index 7221d1658f61d16f080f6f1f4c130b483e60b75b..29c8188d9d6c44f07946a8f464913a01fab1e0ea 100644 (file)
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;
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 3335b7f5ebc69322478b8e1eeb7cf82f043b2baa..580665ef8950f1daa3d6ab59faf1776412e7d5ab 100644 (file)
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 {
public int slot;
public final int gameMode;
public boolean swim;
+ public float headRotation = 0f;
public Player() {
super(0, 0, 4, 30, randomDir(), Type.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;
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 335e3c0d2239a450908f325c3b94dbaa38982143..90a7ec1bec4c818a12286161603d751152586f7e 100644 (file)
}
}
+ private static void setPlayerHeadOrigin() {
+ for (Sprite[] sprites : playerSprite) {
+ sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
+ }
+ }
+
/**
* Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
* and puts to {@link #textureRegions} HashMap
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
--- /dev/null
@@ -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)
+}