Class: Chingu::GameObjectList

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

Overview

Manages a list of game objects An instance of GameObjectList is automaticly created as “game_objects” if using Chingu::Window

Instance Method Summary collapse

Constructor Details

#initializeGameObjectList

Returns a new instance of GameObjectList.



30
31
32
33
# File 'lib/chingu/game_object_list.rb', line 30

def initialize
  @game_objects = Array.new
  #@game_objects_by_class = Hash.new
end

Instance Method Details

#add_game_object(object) ⇒ Object



51
52
53
54
# File 'lib/chingu/game_object_list.rb', line 51

def add_game_object(object)
  @game_objects.push(object)
  #(@game_objects_by_class[object.class] ||= []).push(object)
end

#destroy_allObject Also known as: clear, remove_all



44
45
46
47
# File 'lib/chingu/game_object_list.rb', line 44

def destroy_all
  @game_objects.clear
  #@game_objects_of_class.clear
end

#destroy_ifObject



61
62
63
64
# File 'lib/chingu/game_object_list.rb', line 61

def destroy_if
  @game_objects.reject! { |object| yield(object) }
  #@game_objects_by_class.delete_if { |klass, object| yield(object) }
end

#drawObject



70
71
72
73
74
75
# File 'lib/chingu/game_object_list.rb', line 70

def draw
  @game_objects.each{ |object| object.visible }.each do |object| 
    object.draw_trait
    object.draw
  end
end

#eachObject



84
85
86
# File 'lib/chingu/game_object_list.rb', line 84

def each
  @game_objects.each { |object| yield object }
end

#hide!Object

Disable automatic calling of draw and draw_trait each game loop for all game objects



109
110
111
# File 'lib/chingu/game_object_list.rb', line 109

def hide!
  @game_objects.each { |object| object.hide! }
end

#of_class(klass) ⇒ Object



39
40
41
42
# File 'lib/chingu/game_object_list.rb', line 39

def of_class(klass)
  @game_objects.select { |game_object| game_object.is_a? klass }
  #@game_objects_by_class[klass] || []
end

#pause!Object

Disable automatic calling of update() and update_trait() each game loop for all game objects



95
96
97
# File 'lib/chingu/game_object_list.rb', line 95

def pause!
  @game_objects.each { |object| object.pause! }
end

#remove_game_object(object) ⇒ Object



56
57
58
59
# File 'lib/chingu/game_object_list.rb', line 56

def remove_game_object(object)
  @game_objects.delete(object)
  #@game_objects_by_class[object.class].delete(object)
end

#selectObject



88
89
90
# File 'lib/chingu/game_object_list.rb', line 88

def select
  @game_objects.select { |object| yield object }
end

#show!Object

Enable automatic calling of draw and draw_trait each game loop for all game objects



116
117
118
# File 'lib/chingu/game_object_list.rb', line 116

def show!
  @game_objects.each { |object| object.show! }
end

#sizeObject



66
67
68
# File 'lib/chingu/game_object_list.rb', line 66

def size
  @game_objects.size
end

#to_sObject



35
36
37
# File 'lib/chingu/game_object_list.rb', line 35

def to_s
  "#{@game_objects.size} game objects."
end

#unpause!Object

Enable automatic calling of update() and update_trait() each game loop for all game objects



102
103
104
# File 'lib/chingu/game_object_list.rb', line 102

def unpause!
  @game_objects.each { |object| object.unpause! }
end

#updateObject



77
78
79
80
81
82
# File 'lib/chingu/game_object_list.rb', line 77

def update
  @game_objects.select{ |object| not object.paused }.each do |object| 
    object.update_trait 
    object.update
  end
end