class Scar::Space

Overview

A Space is a container for one independent layer of your application, like UI, Game World, Background, ...

A space contains Entitys and Systems.

Spaces live inside Scenes.

Spaces should not interact with each other.

Each Space has a default camera object, accessible through space#camera. The camera object has the entity id __camera.

Example usage:

# Create a space with the id "ui", a system and two entities
sp = Scar::Space.new("ui", UpdateUISystem, health_bar, exit_button, 1)
scene << sp

Defined in:

scar/space.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(id : String, z : Int32 = 0) #

[View source]
def self.new(id : String, *entities_or_systems : Entity | System, z : Int32 = 0) #

[View source]

Instance Method Detail

def <<(entity : Entity) #

Adds an Entity to the space and returns self


[View source]
def <<(sys : System) #

Adds a System to the space and returns self


[View source]
def <<(*entities_or_systems : Entity | System) #

Adds multiple Entities or Systems to the space and returns self


[View source]
def [](id : String) : Entity #

Returns the Entity with given id


[View source]
def []?(id : String) : Entity | Nil #

Returns the Entity with given id or return nil of no entity is found


[View source]
def camera : Scar::Objects::Camera #

The default Camera object


[View source]
def camera=(camera : Scar::Objects::Camera) #

The default Camera object


[View source]
def each_with(comp_type : T.class, &block : Entity, T -> ) forall T #

For each Entity having the given component in the space, yield the entity and it's component


[View source]
def each_with(comp_type : T.class, *other_comp_types : Component.class, &block : Entity, T -> ) forall T #

For each Entity having the given components in the space, yield the entity and it's comp_type component

Example usage:

# Subtract hp from every enemy entity in the player's range
space.each_with(EnemyComponent, Scar::Components::Sprite) do |ent, comp|
  comp.hp -= 10 if player.in_range?(ent)
end

[View source]
def id : String #

[View source]
def id=(id : String) #

[View source]
def z : Int32 #

The z-coordinate of this space, used to specify the rendering order of multiple spaces


[View source]
def z=(z : Int32) #

The z-coordinate of this space, used to specify the rendering order of multiple spaces


[View source]