Class: Chingu::GameStates::Edit
Overview
Premade game state for chingu - A simple pause state. Pause whenever with:
push_game_state(Chingu::GameStates::Pause)
requires the global $window set to the instance of Gosu::Window (automaticly handled if you use Chingu::Window)
Constant Summary
Helpers::GFX::CIRCLE_STEP
Instance Attribute Summary
#game_objects, #game_state_manager, #options
#input_clients
Instance Method Summary
collapse
#button_down, #button_up, #close, #close_game, #draw_trait, has_trait, has_traits, #setup_trait, #to_s, #to_sym, #trait_options, #update_trait
included
#input, #input=
#add_input_client, #dispatch_action, #dispatch_button_down, #dispatch_button_up, #dispatch_input_for, #remove_input_client
#add_game_object, #game_objects, #game_objects_of_class, #load_game_objects, #remove_game_object
#clear_game_states, #current_game_state, #pop_game_state, #previous_game_state, #push_game_state, #switch_game_state, #transitional_game_state
#draw_circle, #draw_rect, #fill, #fill_gradient, #fill_rect
Constructor Details
#initialize(options = {}) ⇒ Edit
Returns a new instance of Edit.
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/chingu/game_states/edit.rb', line 33
def initialize(options = {})
super
@grid = options[:grid]
@classes = options[:classes] || []
@color = Gosu::Color.new(200,0,0,0)
@red = Gosu::Color.new(0xFFFF0000)
@white = Gosu::Color.new(0xFFFFFFFF)
@selected_game_object = nil
self.input = { :left_mouse_button => :left_mouse_button,
:released_left_mouse_button => :released_left_mouse_button,
:delete => :destroy_selected_game_objects,
:backspace => :destroy_selected_game_objects,
:e => :save_and_quit,
:s => :save,
:esc => :quit,
:"1" => :create_object_1,
:"2" => :create_object_2,
:"3" => :create_object_3,
:"4" => :create_object_4,
:"5" => :create_object_5,
}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
If we’re editing a game state with automaticly called special methods, the following takes care of those.
217
218
219
|
# File 'lib/chingu/game_states/edit.rb', line 217
def method_missing(symbol, *args)
previous_game_state.__send__(symbol, *args)
end
|
Instance Method Details
#create_object_1 ⇒ Object
74
|
# File 'lib/chingu/game_states/edit.rb', line 74
def create_object_1; create_object_nr(0); end
|
#create_object_2 ⇒ Object
75
|
# File 'lib/chingu/game_states/edit.rb', line 75
def create_object_2; create_object_nr(1); end
|
#create_object_3 ⇒ Object
76
|
# File 'lib/chingu/game_states/edit.rb', line 76
def create_object_3; create_object_nr(2); end
|
#create_object_4 ⇒ Object
77
|
# File 'lib/chingu/game_states/edit.rb', line 77
def create_object_4; create_object_nr(3); end
|
#create_object_5 ⇒ Object
78
|
# File 'lib/chingu/game_states/edit.rb', line 78
def create_object_5; create_object_nr(4); end
|
#create_object_nr(number) ⇒ Object
70
71
72
|
# File 'lib/chingu/game_states/edit.rb', line 70
def create_object_nr(number)
@classes[number].create(:x => $window.mouse_x, :y => $window.mouse_y, :parent => previous_game_state) if @classes[number]
end
|
#destroy_selected_game_objects ⇒ Object
135
136
137
|
# File 'lib/chingu/game_states/edit.rb', line 135
def destroy_selected_game_objects
selected_game_objects.each(&:destroy)
end
|
#draw ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/chingu/game_states/edit.rb', line 82
def draw
previous_game_state.draw
$window.draw_quad( 0,0,@color,
$window.width,0,@color,
$window.width,100,@color,
0,100,@color,10)
super
selected_game_objects.each do |game_object|
draw_rect(game_object.bounding_box.inflate(2,2), @red, 999)
end
$window.draw_triangle( $window.mouse_x, $window.mouse_y, @white,
$window.mouse_x, $window.mouse_y + 10, @white,
$window.mouse_x + 10, $window.mouse_y + 10, @white, 9999)
end
|
#game_object_at(x, y) ⇒ Object
173
174
175
176
177
|
# File 'lib/chingu/game_states/edit.rb', line 173
def game_object_at(x, y)
previous_game_state.game_objects.select do |game_object|
game_object.respond_to?(:bounding_box) && game_object.bounding_box.collide_point?(x,y)
end.first
end
|
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/chingu/game_states/edit.rb', line 139
def left_mouse_button
@left_mouse_button = true
x = $window.mouse_x / $window.factor
y = $window.mouse_y / $window.factor
@text.text = "Click @ #{x} / #{y}"
selected_game_objects.each do |game_object|
game_object.options[:selected] = false
end
@selected_game_object = game_object_at(x, y)
if @selected_game_object
@text.text = "#{@text.text} : #{@selected_game_object.class.to_s}"
@selected_game_object.options[:selected] = true
@mouse_x_offset = @selected_game_object.x - $window.mouse_x
@mouse_y_offset = @selected_game_object.y - $window.mouse_y
end
end
|
#quit ⇒ Object
209
210
211
|
# File 'lib/chingu/game_states/edit.rb', line 209
def quit
pop_game_state(:setup => false)
end
|
168
169
170
171
|
# File 'lib/chingu/game_states/edit.rb', line 168
def released_left_mouse_button
@left_mouse_button = false
@selected_game_object = false
end
|
#save ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/chingu/game_states/edit.rb', line 180
def save
require 'yaml'
objects = []
previous_game_state.game_objects.each do |game_object|
objects << {game_object.class.to_s =>
{
:x => game_object.x,
:y => game_object.y,
:angle => game_object.angle,
:zorder => game_object.zorder,
:factor_x => game_object.factor_x,
:factor_y => game_object.factor_y,
:center_x => game_object.center_x,
:center_y => game_object.center_y,
}
}
end
File.open(@filename, 'w') do |out|
YAML.dump(objects, out)
end
end
|
#save_and_quit ⇒ Object
204
205
206
207
|
# File 'lib/chingu/game_states/edit.rb', line 204
def save_and_quit
save
quit
end
|
#selected_game_objects ⇒ Object
131
132
133
|
# File 'lib/chingu/game_states/edit.rb', line 131
def selected_game_objects
previous_game_state.game_objects.select { |o| o.options[:selected] }
end
|
#setup ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/chingu/game_states/edit.rb', line 57
def setup
name = if defined?(previous_game_state.filename)
previous_game_state.filename
else
"#{previous_game_state.class.to_s.downcase}.yml"
end
@filename = File.join($window.root, name)
@title = Text.create("File: #{@filename}", :x => 5, :y => 10)
@title.text += " - Grid: #{@grid}" if @grid
@title2 = Text.create("(1-10) Create object at mouse pos (DEL) Delete selected object (S) Save (E) Save and Quit (ESC) Quit without saving", :x => 5, :y => 30)
@text = Text.create("", :x => 5, :y => 50)
end
|
#update ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/chingu/game_states/edit.rb', line 114
def update
super
if @left_mouse_button && @selected_game_object
@selected_game_object.x = ($window.mouse_x + @mouse_x_offset) / $window.factor
@selected_game_object.y = ($window.mouse_y + @mouse_y_offset) / $window.factor
@selected_game_object.x -= @selected_game_object.x % @grid[0]
@selected_game_object.y -= @selected_game_object.y % @grid[1]
if @selected_game_object.respond_to?(:bounding_box)
@selected_game_object.bounding_box.x = @selected_game_object.x
@selected_game_object.bounding_box.y = @selected_game_object.y
end
end
end
|