scar
  • The scar game library
  • Application Structure
    • App
    • Scenes
    • Spaces
    • Entities
    • Components
    • Systems
    • Objects
    • Drawing
    • Events
  • Builtin Features
    • Actions
    • Assets
    • Config
    • Input
    • Logger
    • Music
    • Tweens
    • Util
    • Vectors & Rects
  • Builtin Components
    • Text
    • Sprite
    • Animated Sprite
  • Builtin Systems
    • Animate Sprites
Powered by GitBook
On this page

Was this helpful?

  1. Application Structure

Spaces

PreviousScenesNextEntities

Last updated 5 years ago

Was this helpful?

A space is a logical container for , and . It holds all things for one 'layer' of a . For example, there would be a space for the background, one for the level and one for the UI. Spaces should not interact with other spaces.

, and can be inserted via the << instance method but not removed. To remove entities, one has to call their Entity#suicide method, all dead entities are removed upon the next update. Systems should not be removed.

Spaces have multiple ways to select . The [] and []? methods can be used for direct access via entity id. Then there is each_with. It is used to get all entities in a space which hold the specified . The call signatures are:

each_with(comp_type, &block)
each_with(comp_type, *other_comp_types, &block)

Both methods accept a block and yields the found and the with the type specified in the first parameter.

comp_type and *comp_types are classes (not instances) which inherit from Scar::Component. comp_types are not yielded but filtered for. This allows to only process entities with certain components. That way, empty components can also be used as tags.

Example

...
# in app#init
backgroundEntity = Entity.new(...)
playerEntity = Entity.new(...)

self << Scene.new(
    Space.new("level",
        LevelScrollSystem.new,
        backgroundEntity,
        playerEntity z: -1),
    Space.new("ui",
        FpsCounterSystem.new,
        Entity.new(...)
    )
)

To see the each_with methods in action, please refer to the section.

entities
objects
systems
scene
Systems
entities
objects
entites
components
entity
component
systems
systems