Class LevelEntity

java.lang.Object
com.codename1.gaming.platformer.LevelEntity
Direct Known Subclasses:
BlockEntity, Checkpoint, Pickup, Projectile, Spring, Switch

public abstract class LevelEntity extends Object

Something in a level that is not an actor: a coin, a spring, a door, a projectile, a crumbling block.

An entity is a rectangle with a getType() name that the view maps to art. Each tick the world calls step, and calls onTouch for every living actor that overlaps it. An entity that must block movement (a door, a gate, a crumbling block) writes Tile#SOLID cells into the grid rather than colliding itself, so the controller stays one fast tile scan.

Subclasses must be deterministic, like abilities, and fold any state that changes play into hashState.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    Height, fixed point.
    int
    Width, fixed point.
    int
    Left edge, fixed point.
    int
    Top edge, fixed point.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    LevelEntity(String id, String type, int x, int y, int w, int h)
    Creates an entity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes the entity at the end of the tick.
    The entity's id.
    int
    A small number the view reads to pick a frame: open or closed, lit or not, how cracked.
    int
    How far outside its rectangle the entity still counts as touched.
    The entity's kind.
    long
    hashState(long h)
    Mixes the entity's play-relevant state into a hash.
    boolean
    Whether the entity still takes part.
    void
    Called once when the entity is added to a world, before the first step: the place to write solid cells.
    void
    Called each tick a living actor overlaps the entity.
    void
    Advances one tick.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • x

      public int x
      Left edge, fixed point.
    • y

      public int y
      Top edge, fixed point.
    • w

      public int w
      Width, fixed point.
    • h

      public int h
      Height, fixed point.
  • Constructor Details

    • LevelEntity

      protected LevelEntity(String id, String type, int x, int y, int w, int h)

      Creates an entity.

      Parameters
      • id: a name, unique in its world
      • type: the kind, which the view maps to art ("coin", "spring")
      • x: left, fixed point
      • y: top, fixed point
      • w: width, fixed point
      • h: height, fixed point
  • Method Details

    • getId

      public String getId()

      The entity's id.

      Returns

      the id

    • getType

      public String getType()

      The entity's kind.

      Returns

      the type name

    • isActive

      public boolean isActive()

      Whether the entity still takes part. Inactive entities are skipped and not drawn; the world drops them at the end of the tick.

      Returns

      true while active

    • deactivate

      public void deactivate()
      Removes the entity at the end of the tick.
    • getPhase

      public int getPhase()

      A small number the view reads to pick a frame: open or closed, lit or not, how cracked. The default is 0.

      Returns

      the visual phase

    • getTouchMargin

      public int getTouchMargin()

      How far outside its rectangle the entity still counts as touched. An entity that writes solid cells (a door) needs a margin, since an actor can never overlap a solid cell. The default is 0.

      Returns

      the margin, fixed point

    • onAdded

      public void onAdded(PlatformWorld world)

      Called once when the entity is added to a world, before the first step: the place to write solid cells. The default does nothing.

      Parameters
      • world: the world
    • step

      public void step(PlatformWorld world)

      Advances one tick. The default does nothing.

      Parameters
      • world: the world
    • onTouch

      public void onTouch(PlatformWorld world, Actor c)

      Called each tick a living actor overlaps the entity.

      Parameters
      • world: the world
      • c: the actor
    • hashState

      public long hashState(long h)

      Mixes the entity's play-relevant state into a hash. The default mixes the position and whether it is active.

      Parameters
      • h: the running hash
      Returns

      the updated hash