|
@@ -1,8 +1,13 @@
|
|
|
package me.hammerle.supersnuvi.entity;
|
|
|
|
|
|
-import me.hammerle.supersnuvi.entity.animator.EntityAnimator;
|
|
|
-import me.hammerle.supersnuvi.entity.ai.EntityController;
|
|
|
+import me.hammerle.supersnuvi.entity.components.animator.EntityAnimator;
|
|
|
+import me.hammerle.supersnuvi.entity.components.ai.EntityController;
|
|
|
import java.util.LinkedList;
|
|
|
+import me.hammerle.supersnuvi.entity.components.Energy;
|
|
|
+import me.hammerle.supersnuvi.entity.components.Health;
|
|
|
+import me.hammerle.supersnuvi.entity.components.ItemCollector;
|
|
|
+import me.hammerle.supersnuvi.entity.components.Movement;
|
|
|
+import me.hammerle.supersnuvi.entity.components.MovementPenalty;
|
|
|
import me.hammerle.supersnuvi.gamelogic.Level;
|
|
|
import me.hammerle.supersnuvi.rendering.Game;
|
|
|
import me.hammerle.supersnuvi.tiles.Location;
|
|
@@ -10,7 +15,7 @@ import me.hammerle.supersnuvi.util.CollisionBox;
|
|
|
import me.hammerle.supersnuvi.util.Face;
|
|
|
import me.hammerle.supersnuvi.util.Utils;
|
|
|
|
|
|
-public class Entity
|
|
|
+public final class Entity
|
|
|
{
|
|
|
// 1 Tile = 1 m
|
|
|
// Pixel m s Pixel
|
|
@@ -19,34 +24,34 @@ public class Entity
|
|
|
// added correction factor, real life gravity seems to strong
|
|
|
public static double GRAVITY = Game.TILE_SIZE * 9.81 / 60 * 0.1;
|
|
|
|
|
|
- private double prevPosX;
|
|
|
- private double prevPrevY;
|
|
|
private double posX;
|
|
|
private double posY;
|
|
|
+ private final double width;
|
|
|
+ private final double height;
|
|
|
+
|
|
|
+ private double prevPosX;
|
|
|
+ private double prevPrevY;
|
|
|
|
|
|
private double prevMotionX;
|
|
|
private double prevMotionY;
|
|
|
private double motionX;
|
|
|
private double motionY;
|
|
|
private boolean onGround;
|
|
|
- private boolean inWater;
|
|
|
|
|
|
private final CollisionBox collisionBox;
|
|
|
- private final double width;
|
|
|
- private final double height;
|
|
|
-
|
|
|
- private double friction;
|
|
|
-
|
|
|
- private final double maxHealth;
|
|
|
- private double health;
|
|
|
-
|
|
|
- private EntityAnimator animator;
|
|
|
-
|
|
|
- private EntityController controller;
|
|
|
|
|
|
private final Level level;
|
|
|
|
|
|
- public Entity(Level level, double x, double y, double width, double height, double health)
|
|
|
+ // components
|
|
|
+ protected EntityAnimator animator;
|
|
|
+ protected EntityController controller;
|
|
|
+ protected Health health;
|
|
|
+ protected Energy energy;
|
|
|
+ protected Movement move;
|
|
|
+ protected ItemCollector itemCollector;
|
|
|
+ protected MovementPenalty movePenalty;
|
|
|
+
|
|
|
+ protected Entity(Level level, double x, double y, double width, double height)
|
|
|
{
|
|
|
this.level = level;
|
|
|
this.posX = x;
|
|
@@ -56,14 +61,18 @@ public class Entity
|
|
|
this.motionX = 0;
|
|
|
this.motionY = 0;
|
|
|
this.onGround = false;
|
|
|
- this.inWater = false;
|
|
|
this.collisionBox = new CollisionBox(0, 0, width, height);
|
|
|
this.width = width;
|
|
|
this.height = height;
|
|
|
- this.maxHealth = health;
|
|
|
- this.health = health;
|
|
|
- this.animator = new EntityAnimator(this);
|
|
|
- this.controller = new EntityController(this);
|
|
|
+
|
|
|
+ // components
|
|
|
+ this.animator = EntityAnimator.NULL;
|
|
|
+ this.controller = EntityController.NULL;
|
|
|
+ this.health = Health.NULL;
|
|
|
+ this.energy = Energy.NULL;
|
|
|
+ this.move = Movement.NULL;
|
|
|
+ this.itemCollector = ItemCollector.NULL;
|
|
|
+ this.movePenalty = MovementPenalty.NULL;
|
|
|
}
|
|
|
|
|
|
public Level getLevel()
|
|
@@ -72,15 +81,49 @@ public class Entity
|
|
|
}
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
- // utility
|
|
|
+ // components
|
|
|
+ //--------------------------------------------------------------------------
|
|
|
+
|
|
|
+ public EntityAnimator getAnimator()
|
|
|
+ {
|
|
|
+ return animator;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Health getHealth()
|
|
|
+ {
|
|
|
+ return health;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Energy getEnergy()
|
|
|
+ {
|
|
|
+ return energy;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Movement getMovement()
|
|
|
+ {
|
|
|
+ return move;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ItemCollector getItemCollector()
|
|
|
+ {
|
|
|
+ return itemCollector;
|
|
|
+ }
|
|
|
+
|
|
|
+ public MovementPenalty getMovementPenalty()
|
|
|
+ {
|
|
|
+ return movePenalty;
|
|
|
+ }
|
|
|
+
|
|
|
+ //--------------------------------------------------------------------------
|
|
|
+ // basic stuff
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
- public final double squaredDistance(Entity other)
|
|
|
+ public double squaredDistance(Entity other)
|
|
|
{
|
|
|
return (posX - other.posX) * (posX - other.posX) + (posY - other.posY) * (posY - other.posY);
|
|
|
}
|
|
|
|
|
|
- public final double signedDistanceX(Entity other)
|
|
|
+ public double signedDistanceX(Entity other)
|
|
|
{
|
|
|
if(posX < other.posX)
|
|
|
{
|
|
@@ -89,24 +132,34 @@ public class Entity
|
|
|
return Math.max(0.0, posX - (other.posX + other.width));
|
|
|
}
|
|
|
|
|
|
- //--------------------------------------------------------------------------
|
|
|
- // controller - either ai or player
|
|
|
- //--------------------------------------------------------------------------
|
|
|
+ public double getWidth()
|
|
|
+ {
|
|
|
+ return width;
|
|
|
+ }
|
|
|
|
|
|
- public final void setController(EntityController controller)
|
|
|
+ public double getHeight()
|
|
|
{
|
|
|
- this.controller = controller;
|
|
|
+ return height;
|
|
|
+ }
|
|
|
+
|
|
|
+ public double getX()
|
|
|
+ {
|
|
|
+ return posX;
|
|
|
+ }
|
|
|
+
|
|
|
+ public double getY()
|
|
|
+ {
|
|
|
+ return posY;
|
|
|
}
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
// ticking
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
- public final void tick()
|
|
|
+ public void tick()
|
|
|
{
|
|
|
controller.tick();
|
|
|
-
|
|
|
- inWater = false;
|
|
|
+ movePenalty.reset();
|
|
|
|
|
|
if(isAffectedByGravity())
|
|
|
{
|
|
@@ -143,89 +196,32 @@ public class Entity
|
|
|
posY = prevPrevY;
|
|
|
}
|
|
|
|
|
|
- friction = 0.7f;
|
|
|
+ movePenalty.setFriction(0.7);
|
|
|
doCollision();
|
|
|
-
|
|
|
- if(motionX < 0.0)
|
|
|
- {
|
|
|
- motionX += friction;
|
|
|
- if(motionX > 0.0)
|
|
|
- {
|
|
|
- motionX = 0.0;
|
|
|
- }
|
|
|
- }
|
|
|
- else if(motionX > 0.0)
|
|
|
- {
|
|
|
- motionX -= friction;
|
|
|
- if(motionX < 0.0)
|
|
|
- {
|
|
|
- motionX = 0.0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ movePenalty.applyFriction();
|
|
|
+ energy.tick();
|
|
|
animator.tick();
|
|
|
}
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
// gravity, friction
|
|
|
//--------------------------------------------------------------------------
|
|
|
-
|
|
|
- public final boolean jump()
|
|
|
- {
|
|
|
- if(onGround)
|
|
|
- {
|
|
|
- onGround = false;
|
|
|
- motionY += getJumpPower();
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
public boolean isAffectedByGravity()
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public double getJumpPower()
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
public boolean isOnGround()
|
|
|
{
|
|
|
return onGround;
|
|
|
- }
|
|
|
-
|
|
|
- public final double getMaxSpeedModifier()
|
|
|
- {
|
|
|
- double max = 1;
|
|
|
- if(inWater)
|
|
|
- {
|
|
|
- max *= 0.65;
|
|
|
- }
|
|
|
- return max;
|
|
|
- }
|
|
|
-
|
|
|
- public final void setInWater(boolean b)
|
|
|
- {
|
|
|
- inWater = b;
|
|
|
- }
|
|
|
-
|
|
|
- public final boolean isInWater()
|
|
|
- {
|
|
|
- return inWater;
|
|
|
- }
|
|
|
-
|
|
|
- public void setFriction(double friction)
|
|
|
- {
|
|
|
- this.friction = friction;
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
// collision stuff
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
- public final CollisionBox getBox()
|
|
|
+ public CollisionBox getBox()
|
|
|
{
|
|
|
return collisionBox.reset().offset(posX, posY);
|
|
|
}
|
|
@@ -546,72 +542,26 @@ public class Entity
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public final void onCollideWithTile(Location loc, Face face)
|
|
|
+ public void onCollideWithTile(Location loc, Face face)
|
|
|
{
|
|
|
controller.onCollideWithTile(loc, face);
|
|
|
}
|
|
|
|
|
|
- public final void onCollideWithEntity(Entity ent, Face face)
|
|
|
+ public void onCollideWithEntity(Entity ent, Face face)
|
|
|
{
|
|
|
controller.onCollideWithEntity(ent, face);
|
|
|
}
|
|
|
|
|
|
- //--------------------------------------------------------------------------
|
|
|
- // health
|
|
|
- //--------------------------------------------------------------------------
|
|
|
-
|
|
|
- public double getHealth()
|
|
|
- {
|
|
|
- return health;
|
|
|
- }
|
|
|
-
|
|
|
- public double getMaxHealth()
|
|
|
- {
|
|
|
- return maxHealth;
|
|
|
- }
|
|
|
-
|
|
|
- public void addHealth(double h)
|
|
|
- {
|
|
|
- health += h;
|
|
|
- if(health > maxHealth)
|
|
|
- {
|
|
|
- health = maxHealth;
|
|
|
- }
|
|
|
- else if(health < 0.0)
|
|
|
- {
|
|
|
- onDeath();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void addHealthPercent(double h)
|
|
|
- {
|
|
|
- addHealth(h * maxHealth);
|
|
|
- }
|
|
|
-
|
|
|
- public void onDeath()
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
//--------------------------------------------------------------------------
|
|
|
// basic coord and motion stuff
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
- public final double getX()
|
|
|
- {
|
|
|
- return posX;
|
|
|
- }
|
|
|
-
|
|
|
- public final double getY()
|
|
|
- {
|
|
|
- return posY;
|
|
|
- }
|
|
|
-
|
|
|
- public final double getPreviousX()
|
|
|
+ public double getPreviousX()
|
|
|
{
|
|
|
return prevPosX;
|
|
|
}
|
|
|
|
|
|
- public final double getPreviousY()
|
|
|
+ public double getPreviousY()
|
|
|
{
|
|
|
return prevPrevY;
|
|
|
}
|
|
@@ -650,48 +600,4 @@ public class Entity
|
|
|
{
|
|
|
return prevMotionY;
|
|
|
}
|
|
|
-
|
|
|
- //--------------------------------------------------------------------------
|
|
|
- // rendering stuff
|
|
|
- //--------------------------------------------------------------------------
|
|
|
-
|
|
|
- public final double getWidth()
|
|
|
- {
|
|
|
- return width;
|
|
|
- }
|
|
|
-
|
|
|
- public final double getHeight()
|
|
|
- {
|
|
|
- return height;
|
|
|
- }
|
|
|
-
|
|
|
- public final double getRenderX()
|
|
|
- {
|
|
|
- return posX;
|
|
|
- }
|
|
|
-
|
|
|
- public final double getRenderY()
|
|
|
- {
|
|
|
- return posY + height;
|
|
|
- }
|
|
|
-
|
|
|
- public final void setAnimator(EntityAnimator animator)
|
|
|
- {
|
|
|
- this.animator = animator;
|
|
|
- }
|
|
|
-
|
|
|
- public EntityAnimator getAnimator()
|
|
|
- {
|
|
|
- return animator;
|
|
|
- }
|
|
|
-
|
|
|
- public double getRenderOffsetX()
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- public double getRenderOffsetY()
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
}
|