X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;ds=sidebyside;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmobs%2FMob.java;h=394346f2b9252f31d4982d7b2c4771d9d42bcf1c;hb=1c004c0ce7e183e773b5b486295c25e39732e899;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..394346f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -1,9 +1,11 @@ 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; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavedroid.game.GameItemsHolder; import ru.deadsoftware.cavedroid.game.world.GameWorld; import java.io.Serializable; @@ -22,8 +24,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 +57,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 +71,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 +90,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 +176,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 +200,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 +224,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 +233,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 ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, float delta); public abstract void changeDir(); + + public abstract float getSpeed(); + + public abstract void jump(); }