Skip to content

Class: Entity

Michael Ebens edited this page Jun 22, 2018 · 3 revisions

Entities are the core building blocks of your game. Every separate object within your game's world that needs to update and/or draw itself, will (or, should) be an entity. So in a simple shooter for example, the player, each enemy, and probably every bullet would be an entity.

If you haven't read the documentation for World already, then you probably should. Nevertheless, here's the basic relationship between entities and worlds. Worlds are containers which hold and process the group of entities which have been added to them. Entities can only be assigned to one world at a time, and this world will call all their callback functions, such as update and draw, at the appropriate time. For example, the entire chain for the update callback would be: love.run -> love.update -> ammo.update -> World:update -> Entity:update.

Usually, you'll want to create a subclass for each distinct type of entity you'll need, overriding and adding functionality as needed. For example, you could have a Player class, an Enemy class (which may have subclasses for each enemy type), and a Bullet class.

Properties

active
A boolean dictating whether or not this entity should have its update callback called by the world.

layer
The current layer on which this entity is being drawn. If the layer you set this to doesn't already exist in the world, it will be created with a default scale of 1. See World for more information.

pos
A Vector for the position of this entity. Note that this is local to the current parent (if any).

visible
A boolean dictating whether or no this entity should have its draw callback called by the world.

world
The current world to which this entity is attached. If you set this, the entity will be removed from the current world and added to the new one. Setting this to nil is shortcut for removing the entity from the current world.

x
A shortcut to Entity.pos.x.

y
A shortcut to Entity.pos.y.

Methods

initialize(x, y)
Initialises the entity. When overriding this in your subclasses, you must call this using Entity.initialize(self).

x: Sets the X position. Defaults to 0.
y: Sets the Y position. Defaults to 0.

added()
Called by the world right after this entity has been added to its world. This does nothing by default.

draw()
Called by the world at the time this entity should draw itself. This does nothing by default. An example would be:

function Player:draw()
  love.graphics.draw(self.image, self.x, self.y)
  love.graphics.draw(self.particles, self.x, self.y)
end

removed()
Called right before this entity is removed from its world. This does nothing by default.

update(dt)
Called by the world at this time this entity should update itself. This does nothing by default. An example would be:

function Player:update(dt)
  if input.down("left") then self.x = self.x - self.speed * dt end
  if input.down("right") then self.x = self.x + self.speed * dt end
end

dt: The delta time.