X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmobs%2FMob.java;h=feb5ceb08a0165f58c00538678dbcc982e4a6812;hb=63ffd8af5e9788f36fc75b6d5c29ae525eb74692;hp=2b58f7c626817f0fd9789f453b751d7463aa8b59;hpb=59ff9326fad76d34cbadd3fa3e4b4984892238a4;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 2b58f7c..feb5ceb 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -1,5 +1,6 @@ package ru.deadsoftware.cavedroid.game.mobs; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; @@ -22,8 +23,31 @@ public abstract class Mob extends Rectangle implements Serializable { } 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; + } } protected Vector2 mVelocity; @@ -32,10 +56,13 @@ public abstract class Mob extends Rectangle implements Serializable { protected float mAnim; private Direction mDirection; - private boolean mDead; + protected boolean mDead; private boolean mCanJump; private boolean mFlyMode; + private final int mMaxHealth; + private int mHealth; + /** * @param x in pixels * @param y in pixels @@ -43,13 +70,15 @@ 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); 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() { @@ -60,6 +89,14 @@ public abstract class Mob extends Rectangle implements Serializable { 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) { if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) { mAnim += mAnimDelta * delta; @@ -138,10 +175,6 @@ 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; } @@ -166,6 +199,10 @@ public abstract class Mob extends Rectangle implements Serializable { return mVelocity; } + protected final void setVelocity(Vector2 velocity) { + mVelocity = velocity; + } + public final boolean canJump() { return mCanJump; } @@ -186,7 +223,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(); } @@ -195,9 +232,49 @@ public abstract class Mob extends Rectangle implements Serializable { } } + public final int getHealth() { + return mHealth; + } + + public final void attachToController(MobsController controller) { + controller.addMob(this); + } + + public void damage(int damage) { + 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(); + } + + 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 abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta); public abstract void ai(GameWorld gameWorld, float delta); public abstract void changeDir(); + + public abstract float getSpeed(); + + public abstract void jump(); }