Class: Draco::System

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

Overview

Public: Systems contain the logic of the game. The System runs on each tick and manipulates the Entities in the World.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities: [], world: nil) ⇒ System

Public: Initializes a new System.

entities - The Entities to operate on (default: []). world - The World running the System (default: nil).



445
446
447
448
449
# File 'lib/draco.rb', line 445

def initialize(entities: [], world: nil)
  @entities = entities
  @world = world
  after_initialize
end

Instance Attribute Details

#entitiesObject

Public: Returns an Array of Entities that match the filter.



406
407
408
# File 'lib/draco.rb', line 406

def entities
  @entities
end

#worldObject

Public: Returns the World this System is running in.



409
410
411
# File 'lib/draco.rb', line 409

def world
  @world
end

Class Method Details

.filter(*components) ⇒ Object

Public: Adds the given Components to the default filter of the System.

Returns the current filter.



414
415
416
417
418
419
420
# File 'lib/draco.rb', line 414

def self.filter(*components)
  components.each do |component|
    @filter << component
  end

  @filter
end

.inherited(sub) ⇒ Object

Internal: Resets the fuilter for each class that inherits System.

sub - The class that is inheriting Entity.

Returns nothing.



427
428
429
430
# File 'lib/draco.rb', line 427

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

.Tag(name) ⇒ Object

Public: Creates a tag Component. If the tag already exists, return it.

name - The string or symbol name of the component.

Returns a class with subclass Draco::Component.



437
438
439
# File 'lib/draco.rb', line 437

def self.Tag(name) # rubocop:disable Naming/MethodName
  Draco::Tag(name)
end

Instance Method Details

#after_initializeObject

Public: Callback run after the system is initialized.

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

Returns nothing.



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

def after_initialize; end

#after_tick(context) ⇒ Object

Public: Callback run after #tick is called.

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

Returns nothing.



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

def after_tick(context); end

#before_tick(context) ⇒ Object

Public: Callback run before #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`.

Returns nothing.



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

def before_tick(context); end

#call(context) ⇒ Object

Public: Runs the system tick function.

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

Returns nothing.



463
464
465
466
467
468
# File 'lib/draco.rb', line 463

def call(context)
  before_tick(context)
  tick(context)
  after_tick(context)
  self
end

#inspectObject

Public: Returns a String representation of the System.



507
508
509
# File 'lib/draco.rb', line 507

def inspect
  serialize.to_s
end

#serializeObject

Public: Serializes the System to save the current state.

Returns a Hash representing the System.



498
499
500
501
502
503
504
# File 'lib/draco.rb', line 498

def serialize
  {
    class: self.class.name.to_s,
    entities: entities.map(&:serialize),
    world: world ? world.serialize : nil
  }
end

#tick(context) ⇒ Object

Public: Runs the System logic for the current game engine tick.

This is where the logic is implemented and it should be overriden for each System.

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

Returns nothing



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

def tick(context); end

#to_sObject

Public: Returns a String representation of the System.



512
513
514
# File 'lib/draco.rb', line 512

def to_s
  serialize.to_s
end