Class: Draco::World

Inherits:
Object
  • Object
show all
Defined in:
lib/draco.rb

Overview

Public: The container for current Entities and Systems.

Defined Under Namespace

Classes: EntityStore

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities: [], systems: []) ⇒ World

Public: Initializes a World.

entities - The Array of Entities for the World (default: []). systems - The Array of System Classes for the World (default: []).



585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/draco.rb', line 585

def initialize(entities: [], systems: [])
  default_entities = self.class.default_entities.map do |default|
    klass, attributes = default
    name = attributes[:as]
    entity = klass.new(attributes)
    instance_variable_set("@#{name}", entity) if name

    entity
  end

  @entities = EntityStore.new(self, default_entities + entities)
  @systems = self.class.default_systems + systems
  after_initialize
end

Class Attribute Details

.default_entitiesObject (readonly)

Internal: Returns the default Entities for the class.



569
570
571
# File 'lib/draco.rb', line 569

def default_entities
  @default_entities
end

.default_systemsObject (readonly)

Internal: Returns the default Systems for the class.



572
573
574
# File 'lib/draco.rb', line 572

def default_systems
  @default_systems
end

Instance Attribute Details

#entitiesObject (readonly)

Public: Returns the Array of Entities.



579
580
581
# File 'lib/draco.rb', line 579

def entities
  @entities
end

#systemsObject (readonly)

Public: Returns the Array of Systems.



576
577
578
# File 'lib/draco.rb', line 576

def systems
  @systems
end

Class Method Details

.entity(entity, defaults = {}) ⇒ Object

Public: Adds a default Entity to the World.

entity - The class of the Entity to add by default. defaults - The Hash of default values for the Entity. (default: {})

Examples

entity(Player)

entity(Player, position: { x: 0, y: 0 })

Returns nothing.



545
546
547
548
549
550
# File 'lib/draco.rb', line 545

def self.entity(entity, defaults = {})
  name = defaults[:as]
  @default_entities.push([entity, defaults])

  attr_reader(name.to_sym) if name
end

.inherited(sub) ⇒ Object

Internal: Resets the default components for each class that inherites Entity.

sub - The class that is inheriting Entity.

Returns nothing.



527
528
529
530
531
# File 'lib/draco.rb', line 527

def self.inherited(sub)
  super
  sub.instance_variable_set(:@default_entities, [])
  sub.instance_variable_set(:@default_systems, [])
end

.systems(*systems) ⇒ Object

Public: Adds default Systems to the World.

systems - The System or Array list of System classes to add to the World.

Examples

systems(RenderSprites)

systems(RenderSprites, RenderLabels)

Returns nothing.



563
564
565
# File 'lib/draco.rb', line 563

def self.systems(*systems)
  @default_systems += Array(systems).flatten
end

Instance Method Details

#after_initializeObject

Public: Callback run after the world is initialized.

This is empty by default but is present to allow plugins to tie into.

Returns nothing.



605
# File 'lib/draco.rb', line 605

def after_initialize; end

#after_tick(context, results) ⇒ Object

Public: Callback run after #tick is called.

This is empty by default but is present to allow plugins to tie into.

context - The context object of the current tick from the game engine. In DragonRuby this is args. results - The System instances that were run.

Returns nothing.



641
# File 'lib/draco.rb', line 641

def after_tick(context, results); end

#before_tick(_context) ⇒ Object

Public: Callback run before #tick is called.

context - The context object of the current tick from the game engine. In DragonRuby this is args.

Returns the systems to run during this tick.



612
613
614
615
616
617
618
# File 'lib/draco.rb', line 612

def before_tick(_context)
  systems.map do |system|
    entities = filter(system.filter)

    system.new(entities: entities, world: self)
  end
end

#component_added(entity, component) ⇒ Object

Public: Callback to run when a component is added to an existing Entity.

entity - The Entity the Component was added to. component - The Component that was added to the Entity.

Returns nothing.



649
# File 'lib/draco.rb', line 649

def component_added(entity, component); end

#component_removed(entity, component) ⇒ Object

Public: Callback to run when a component is added to an existing Entity.

entity - The Entity the Component was removed from. component - The Component that was removed from the Entity.

Returns nothing.



657
# File 'lib/draco.rb', line 657

def component_removed(entity, component); end

#filter(*components) ⇒ Object

Public: Finds all Entities that contain all of the given Components.

components - An Array of Component classes to match.

Returns an Array of matching Entities.



664
665
666
# File 'lib/draco.rb', line 664

def filter(*components)
  entities[components.flatten]
end

#inspectObject

Public: Returns a String representation of the World.



680
681
682
# File 'lib/draco.rb', line 680

def inspect
  serialize.to_s
end

#serializeObject

Public: Serializes the World to save the current state.

Returns a Hash representing the World.



671
672
673
674
675
676
677
# File 'lib/draco.rb', line 671

def serialize
  {
    class: self.class.name.to_s,
    entities: @entities.map(&:serialize),
    systems: @systems.map { |system| system.name.to_s }
  }
end

#tick(context) ⇒ Object

Public: Runs all of the Systems every tick.

context - The context object of the current tick from the game engine. In DragonRuby this is args.

Returns nothing



625
626
627
628
629
630
631
# File 'lib/draco.rb', line 625

def tick(context)
  results = before_tick(context).map do |system|
    system.call(context)
  end

  after_tick(context, results)
end

#to_sObject

Public: Returns a String representation of the World.



685
686
687
# File 'lib/draco.rb', line 685

def to_s
  serialize.to_s
end