Module: Chingu::Traits::Asynchronous

Defined in:
lib/chingu/traits/asynchronous.rb

Overview

A chingu trait providing an asynchronous tasks queue

For example: class Robot < GameObject traits :asynchronous # robot stuff end # later, controlling your robot… robot.async do |q| q.tween(5000, :x => 1024, :y => 64) q.call :explode end

Will move the robot asynchronously from its current location to (1024, 64), then blow it up. The async method returns immediately after adding tasks to the queue.

The first task on the queue is processed each tick during the update phase, then removed from the queue when it is deemed finished. What constitutes “finished” is determined by the particular subclass of BasicTask.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks



53
54
55
# File 'lib/chingu/traits/asynchronous.rb', line 53

def tasks
  @tasks
end

Instance Method Details

#asyncObject

Add a set of tasks to the task queue to be executed asynchronously. If no block is given, returns the TaskBuilder that would have been passed to the block.



73
74
75
76
77
78
79
80
# File 'lib/chingu/traits/asynchronous.rb', line 73

def async
  builder = Chingu::Async::TaskBuilder.new(@tasks)
  if block_given?
    yield builder
  else
    builder
  end
end

#setup_trait(options) ⇒ Object

Setup



58
59
60
61
# File 'lib/chingu/traits/asynchronous.rb', line 58

def setup_trait(options)
  @tasks = Chingu::Async::TaskList.new
  super
end

#update_traitObject



63
64
65
66
# File 'lib/chingu/traits/asynchronous.rb', line 63

def update_trait
  @tasks.update self
  super
end