X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmobs%2FMob.java;h=3ee37d37e626914cef7b41e55c151c42ec743c3b;hb=0d6ab84418440a30f2de555cb30ce21af8dac12e;hp=0f765925bc1679bed83830f29011a5f0bd88def8;hpb=97fa700baa58d95bd4b655366a35bcdac4bcaac9;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 0f76592..3ee37d3 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -1,38 +1,92 @@ package ru.deadsoftware.cavedroid.game.mobs; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Color; 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.cavedroid.game.GameWorld; +import com.badlogic.gdx.utils.Timer; +import org.jetbrains.annotations.Nullable; +import ru.deadsoftware.cavedroid.game.GameItemsHolder; +import ru.deadsoftware.cavedroid.game.model.dto.SaveDataDto; +import ru.deadsoftware.cavedroid.game.model.item.InventoryItem; +import ru.deadsoftware.cavedroid.game.world.GameWorld; +import ru.deadsoftware.cavedroid.misc.Saveable; -import java.io.Serializable; +import java.util.Collections; +import java.util.List; /** * Mob class. */ -public abstract class Mob extends Rectangle implements Serializable { +public abstract class Mob extends Rectangle implements Saveable { + + private static final float DAMAGE_TINT_TIMEOUT_S = 0.5f; + private static final Color DAMAGE_TINT_COLOR = new Color(0xff8080ff); + + private static final float HIT_RANGE = 8f; + + protected static int ANIMATION_SPEED = 360; public enum Type { MOB, - SAND, - GRAVEL + FALLING_BLOCK } public enum Direction { - LEFT, - RIGHT + + LEFT(0, -1), + RIGHT(1, 1); + + private final int index; + private final int basis; + + /** + * Index for this direction (left = 0, right = 1) + */ + public final int getIndex() { + return index; + } + + /** + * Basis for this direction (left = -1, right = 1) + */ + public final int getBasis() { + return basis; + } + + Direction(int index, int basis) { + this.index = index; + this.basis = basis; + } + } + + private class ResetTakeDamageTask extends Timer.Task { + + @Override + public void run() { + mTakingDamage = false; + } } - protected Vector2 mMove; + protected Vector2 mVelocity; protected Type mType; - protected int mAnimDelta = 6; - protected int mAnim; + protected int mAnimDelta = ANIMATION_SPEED; + protected float mAnim; - private Direction mDirection; - private boolean mDead; - private boolean mCanJump; - private boolean mFlyMode; + protected Direction mDirection; + protected boolean mDead; + protected boolean mCanJump; + protected boolean mFlyMode; + + protected int mMaxHealth; + protected int mHealth; + + private boolean mTakingDamage = false; + + @Nullable + private ResetTakeDamageTask mResetTakeDamageTask = null; /** * @param x in pixels @@ -41,19 +95,57 @@ public abstract class Mob extends Rectangle implements Serializable { * @param height in pixels * @param mDirection Direction in which mob is looking */ - protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) { + protected Mob(float x, float y, float width, float height, Direction mDirection, Type type, int maxHealth) { super(x, y, width, height); - mMove = new Vector2(0, 0); + mVelocity = new Vector2(0, 0); mCanJump = false; mDead = false; this.mDirection = mDirection; this.mType = type; + this.mMaxHealth = maxHealth; + this.mHealth = mMaxHealth; } protected static Direction randomDir() { return MathUtils.randomBoolean(.5f) ? Direction.LEFT : Direction.RIGHT; } + private boolean isAnimationIncreasing() { + return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0; + } + + private void checkHealth() { + mHealth = MathUtils.clamp(mHealth, 0, mMaxHealth); + + if (mHealth <= 0) { + kill(); + } + } + + protected final void updateAnimation(float delta) { + final float velocityMultiplier = (Math.abs(getVelocity().x) / getSpeed()); + final float animMultiplier = (velocityMultiplier == 0f ? 1f : velocityMultiplier) * delta; + final float maxAnim = 60f * (velocityMultiplier == 0f ? 1f : velocityMultiplier); + + if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * animMultiplier) { + mAnim += mAnimDelta * animMultiplier; + } else { + mAnim = 0; + } + + if (mAnim > maxAnim) { + mAnim = maxAnim; + mAnimDelta = -ANIMATION_SPEED; + } else if (mAnim < -maxAnim) { + mAnim = -maxAnim; + mAnimDelta = ANIMATION_SPEED; + } + + if (mVelocity.x == 0f && isAnimationIncreasing()) { + mAnimDelta = -mAnimDelta; + } + } + /** * @return The X coordinate of a mob in blocks */ @@ -112,32 +204,32 @@ public abstract class Mob extends Rectangle implements Serializable { mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT; } - protected final int dirMultiplier() { - return looksLeft() ? 0 : 1; - } - public final boolean isDead() { return mDead; } - public final int getAnim() { + public final float getAnim() { return mAnim; } /** * Set's mob's dead variable to true and nothing else. It doesn't delete the */ - public final void kill() { + public void kill() { mDead = true; } - public final void move() { - x += mMove.x; - y += mMove.y; + public final void move(float delta) { + x += mVelocity.x * delta; + y += mVelocity.y * delta; } - public final Vector2 getMove() { - return mMove; + public final Vector2 getVelocity() { + return mVelocity; + } + + protected final void setVelocity(Vector2 velocity) { + mVelocity = velocity; } public final boolean canJump() { @@ -160,7 +252,7 @@ public abstract class Mob extends Rectangle implements Serializable { return mType; } - public void checkWorldBounds(GameWorld gameWorld) { + public final void checkWorldBounds(GameWorld gameWorld) { if (x + width / 2 < 0) { x += gameWorld.getWidthPx(); } @@ -169,9 +261,96 @@ public abstract class Mob extends Rectangle implements Serializable { } } - public abstract void draw(SpriteBatch spriteBatch, float x, float y); + public final int getHealth() { + return mHealth; + } - public abstract void ai(GameWorld gameWorld); + public final int getMaxHealth() { + return mMaxHealth; + } + + public final void attachToController(MobsController controller) { + controller.addMob(this); + } + + public void damage(int damage) { + if (damage == 0) { + return; + } + + if (damage < 0) { + Gdx.app.error(this.getClass().getSimpleName(), "Damage cant be negative!"); + return; + } + + if (mHealth <= Integer.MIN_VALUE + damage) { + mHealth = Integer.MIN_VALUE + damage; + } + + mHealth -= damage; + checkHealth(); + + setTakingDamage(true); + } + + public void heal(int heal) { + if (heal < 0) { + Gdx.app.error(this.getClass().getSimpleName(), "Heal cant be negative!"); + return; + } + + if (mHealth >= Integer.MAX_VALUE - heal) { + mHealth = Integer.MAX_VALUE - heal; + } + + mHealth += heal; + checkHealth(); + } + + public Rectangle getHitBox() { + return new Rectangle(x - HIT_RANGE, y - HIT_RANGE, width + HIT_RANGE, height + HIT_RANGE); + } + + public boolean isTakingDamage() { + return mTakingDamage; + } + + public void setTakingDamage(boolean takingDamage) { + mTakingDamage = takingDamage; + + if (takingDamage) { + if (mResetTakeDamageTask != null && mResetTakeDamageTask.isScheduled()) { + mResetTakeDamageTask.cancel(); + } else if (mResetTakeDamageTask == null) { + mResetTakeDamageTask = new ResetTakeDamageTask(); + } + + Timer.schedule(mResetTakeDamageTask, DAMAGE_TINT_TIMEOUT_S); + } + } + + protected Color getTintColor() { + return isTakingDamage() ? DAMAGE_TINT_COLOR : Color.WHITE; + } + + public List getDrop(GameItemsHolder gameItemsHolder) { + return Collections.emptyList(); + } + + public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta); + + public abstract void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, MobsController mobsController, float delta); public abstract void changeDir(); + + public abstract float getSpeed(); + + public abstract void jump(); + + @Override + public abstract SaveDataDto.MobSaveDataDto getSaveData(); + + public static Mob fromSaveData(SaveDataDto.MobSaveDataDto saveData) { + return MobSaveDataMapperKt.fromSaveData(saveData); + } }