Console::EntityStore

console 0.2.0 · app/console/entity.rb · 235 lines · view source

Also defined here: Console::EntityDraw

Console::Entity -- a flat, fast entity store.

Entities are plain Hashes with x, y, w, h, kind and a unique id. Keeping them Hash-shaped matters: DragonRuby renders Hash primitives faster than class instances, and a hash entity can be passed straight to args.outputs.sprites << when it has a path.

spawn Bullet, kind: :bullet, x: 100, y: 100,
                  sprite: 'star', vx: 4, vy: 0
each_entity :bullet do |b|
  move b, vx: b.vx, vy: b.vy
  despawn b if off_screen?(b)
end

kind is the tag used for querying, animation lookup (sprites/<kind>/<state>) and collision groups.

def initialize
line 22
def size
line 28
def spawn(klass = nil, attributes = {})

Create an entity. klass may be a Class (which must respond to defaults or take a Hash) or any Symbol used purely as documentation.

Returns the entity Hash.

def despawn(entity)
line 64
def despawn_if(&block)
line 72
def [](id)
line 78
def clear(kind = nil)
line 82
def count(kind = nil)
line 91
def any?(kind)
line 96
def all(kind = nil)
line 100
def each(kind = nil, &block)
line 105
def each_with_kind(kind, &block)
line 114

queries

def colliding(rect, kind = nil)

All entities of kind intersecting rect.

def colliding_one(rect, kind = nil)

The first entity of kind intersecting rect.

def nearest(rect, kind = nil)
line 142

motion

def move(entity, vx: 0, vy: 0)
line 159
def move_to(entity, x, y)
line 165
def toward(entity, target, speed)
line 171
def center(rect)

Distance from a point to the centre of rect.

def sprite_path(console, entity)

Resolve an entity's sprite. Prefers an active animation, then entity[:sprite], then entity[:path], then the generated placeholder.

def draw_entity(console, entity, spec = {})

Draw an entity, honouring an active animation, tint, alpha, flips and rotation. Routes through Draw so the camera transform applies.

draw_entity entity
draw_entity entity, angle: spin