Game Development Reference
In-Depth Information
8
9
def
update
10
@state
.
update
11
end
12
13
def
draw
14
@state
.
draw
15
end
16
17
def
needs_redraw?
18
@state
.
needs_redraw?
19
end
20
21
def
button_down
(
id
)
22
@state
.
button_down(
id
)
23
end
24
25
end
It has current
@state
, and all usual main loop actions are executed on that state instance.
We will add base class that all game states will extend. Let's name it
GameState
:
03-prototype/states/game_state.rb
1
class
GameState
2
3
def
self
.
switch
(new_state)
4
$window
.
state
&&
$window
.
state
.
leave
5
$window
.
state
=
new_state
6
new_state
.
enter
7
end
8
9
def
enter
10
end
11
12
def
leave
13
end
14
15
def
draw
16
end
17
18
def
update
19
end
20
21
def
needs_redraw?
22
true