-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransition_scene.rb
65 lines (54 loc) · 1.61 KB
/
transition_scene.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
60
61
62
63
64
65
# frozen_string_literal: true
require 'gosu'
# TransitionScene
class TransitionScene
ROW_PADDING = 20
def initialize(text, next_scene, wait_seconds: 6,
font_size: 12, font_color: Gosu::Color::WHITE,
enable_keyboard_space: false)
@next_scene = next_scene
@text = text
@wait_seconds = wait_seconds
@font_color = font_color
@font = Gosu::Font.new(font_size, name: 'C64_Pro_Mono-STYLE.ttf')
@enable_keyboard_space = enable_keyboard_space
end
def button_down(id)
return unless id == Gosu::KbSpace && @enable_keyboard_space
@next_scene.call
end
def update
@wait_seconds -= 1
sleep(1)
@next_scene.call if @wait_seconds.negative?
@seconds_to_next_level = @wait_seconds + 1
end
def dt(text, x_pos, offset)
@font.draw_text(text, x_pos, offset, 1, 1, 1, @font_color)
end
def calculate_y
total_rows = @text.count + 1 # including the number of seconds
padded_row_height = @font.height + ROW_PADDING
text_area_height = total_rows * padded_row_height
(Game::WINDOW_HEIGHT - text_area_height) / 2
end
def calculate_x(text)
width = @font.text_width(text)
(Game::WINDOW_WIDTH - width) / 2
end
def draw
offset = calculate_y
@text.each do |t|
dt(t, calculate_x(t), offset)
offset += @font.height + ROW_PADDING
end
offset += ROW_PADDING
x = calculate_x(@seconds_to_next_level)
dt(@seconds_to_next_level, x, offset)
return unless @enable_keyboard_space
offset += ROW_PADDING * 4
msg = 'Press space to continue'
x = calculate_x(msg)
dt(msg, x, offset)
end
end