Class: RedBird::Stage

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

Overview

A game built with RedBird consists of a succession of Stages. A game has only a single Stage running at a time. Each Stage must have an InputDevice instance and an Array of Entity instances.

This class contains a basic set of functionalities that every stage of a game needs.

Author:

  • Frederico Linhares

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_data) ⇒ Stage

Returns a new instance of Stage.

Parameters:

  • global_data (Object)

    use this to share common data among different stages.

Author:

  • Frederico Linhares



24
25
26
27
# File 'lib/red_bird/stage.rb', line 24

def initialize(global_data)
  @global_data = global_data
  @entities = []
end

Instance Attribute Details

#entitiesArray<RedBird::Entity>

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/red_bird/stage.rb', line 18

class Stage
  attr_accessor :input_device, :entities

  # @param global_data [Object] use this to share common data among different
  #   stages.
  # @author Frederico Linhares
  def initialize(global_data)
    @global_data = global_data
    @entities = []
  end

  # Add a new entity to this stage. This method organizes the entities as
  # expected by some methods in {RedBird::Engine}.
  #
  # @param entities [Entity, Array<Entity>]
  # @author Frederico Linhares
  def add_entities(entities)
    if entities.is_a? Array then
      @entities.concat entities
    else
      @entities << entities
    end

    @entities.sort! { |a, b| a.priority <=> b.priority }
  end

  # {RedBird::Engine} calls this method before it ticks entities. Overwrite
  # this to add routines that must run before entities tick.
  #
  # @author Frederico Linhares
  def pre_tick
    # By default do nothing.
  end

  # {RedBird::Engine} calls this method after it ticks entities. Overwrite
  # this to add routines that must run after entities tick.
  #
  # @author Frederico Linhares
  def post_tick
    # By default do nothing.
  end
end

#input_deviceRedBird::InputDevice

Returns the class that inherits from Stage must defines an variable @input_device containing and instance of InputDevice.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/red_bird/stage.rb', line 18

class Stage
  attr_accessor :input_device, :entities

  # @param global_data [Object] use this to share common data among different
  #   stages.
  # @author Frederico Linhares
  def initialize(global_data)
    @global_data = global_data
    @entities = []
  end

  # Add a new entity to this stage. This method organizes the entities as
  # expected by some methods in {RedBird::Engine}.
  #
  # @param entities [Entity, Array<Entity>]
  # @author Frederico Linhares
  def add_entities(entities)
    if entities.is_a? Array then
      @entities.concat entities
    else
      @entities << entities
    end

    @entities.sort! { |a, b| a.priority <=> b.priority }
  end

  # {RedBird::Engine} calls this method before it ticks entities. Overwrite
  # this to add routines that must run before entities tick.
  #
  # @author Frederico Linhares
  def pre_tick
    # By default do nothing.
  end

  # {RedBird::Engine} calls this method after it ticks entities. Overwrite
  # this to add routines that must run after entities tick.
  #
  # @author Frederico Linhares
  def post_tick
    # By default do nothing.
  end
end

Instance Method Details

#add_entities(entities) ⇒ Object

Add a new entity to this stage. This method organizes the entities as expected by some methods in Engine.

Parameters:

Author:

  • Frederico Linhares



34
35
36
37
38
39
40
41
42
# File 'lib/red_bird/stage.rb', line 34

def add_entities(entities)
  if entities.is_a? Array then
    @entities.concat entities
  else
    @entities << entities
  end

  @entities.sort! { |a, b| a.priority <=> b.priority }
end

#post_tickObject

Engine calls this method after it ticks entities. Overwrite this to add routines that must run after entities tick.

Author:

  • Frederico Linhares



56
57
58
# File 'lib/red_bird/stage.rb', line 56

def post_tick
  # By default do nothing.
end

#pre_tickObject

Engine calls this method before it ticks entities. Overwrite this to add routines that must run before entities tick.

Author:

  • Frederico Linhares



48
49
50
# File 'lib/red_bird/stage.rb', line 48

def pre_tick
  # By default do nothing.
end