diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java
index b7a975caf3b4217032573afdfc200760515f78f3..fc9d61bbe778e37f1c1186fa1387f0be207fb8d6 100644 (file)
package ru.deadsoftware.cavedroid.game.mobs;
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 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;
import ru.deadsoftware.cavedroid.game.world.GameWorld;
import java.io.Serializable;
}
public enum Direction {
}
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;
}
protected Vector2 mVelocity;
private boolean mCanJump;
private boolean mFlyMode;
private boolean mCanJump;
private boolean mFlyMode;
+ private final int mMaxHealth;
+ private int mHealth;
+
/**
* @param x in pixels
* @param y in pixels
/**
* @param x in pixels
* @param y in pixels
* @param height in pixels
* @param mDirection Direction in which mob is looking
*/
* @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;
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() {
}
protected static Direction randomDir() {
return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0;
}
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) {
protected final void updateAnimation(float delta) {
- if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) {
- mAnim += mAnimDelta * 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;
}
} else {
mAnim = 0;
}
- if (mAnim > 60f) {
- mAnim = 60f;
+ if (mAnim > maxAnim) {
+ mAnim = maxAnim;
mAnimDelta = -ANIMATION_SPEED;
mAnimDelta = -ANIMATION_SPEED;
- } else if (mAnim < -60f) {
- mAnim = -60f;
+ } else if (mAnim < -maxAnim) {
+ mAnim = -maxAnim;
mAnimDelta = ANIMATION_SPEED;
}
mAnimDelta = ANIMATION_SPEED;
}
mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
}
mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
}
- protected final int dirMultiplier() {
- return looksLeft() ? 0 : 1;
- }
-
public final boolean isDead() {
return mDead;
}
public final boolean isDead() {
return mDead;
}
return mVelocity;
}
return mVelocity;
}
+ protected final void setVelocity(Vector2 velocity) {
+ mVelocity = velocity;
+ }
+
public final boolean canJump() {
return mCanJump;
}
public final boolean canJump() {
return mCanJump;
}
return mType;
}
return mType;
}
- public void checkWorldBounds(GameWorld gameWorld) {
+ public final void checkWorldBounds(GameWorld gameWorld) {
if (x + width / 2 < 0) {
x += gameWorld.getWidthPx();
}
if (x + width / 2 < 0) {
x += gameWorld.getWidthPx();
}
}
}
}
}
+ public final int getHealth() {
+ return mHealth;
+ }
+
+ 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();
+ }
+
+ 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 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 void changeDir();
+
+ public abstract float getSpeed();
+
+ public abstract void jump();
}
}