Updated Animator to be usable, added logo texture/image, 'finished' Intro state, added Window#shift_state

This commit is contained in:
2021-04-19 19:13:15 +00:00
parent 676545f3c7
commit 8002708695
6 changed files with 112 additions and 30 deletions

BIN
assets/textures/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View File

@@ -1,11 +1,11 @@
module CyberarmEngine
class Animator
DEFAULT_TWEEN = :linear
def initialize(start_time:, duration:, from:, to:, &block)
def initialize(start_time:, duration:, from:, to:, tween: :linear, &block)
@start_time = start_time
@duration = duration
@from = from.dup
@to = to.dup
@tween = tween
@block = block
end
@@ -14,18 +14,18 @@ module CyberarmEngine
end
def progress
(@start_time.to_f + (Gosu.milliseconds - @start_time)) / (@start_time + @duration.to_f)
((Gosu.milliseconds - @start_time) / @duration.to_f).clamp(0.0, 1.0)
end
def complete?
progress >= 1.0
end
def transition(from, to, tween = DEFAULT_TWEEN)
def transition(from = @from, to = @to, tween = @tween)
from + (to - from) * send("tween_#{tween}", progress)
end
def color_transition(from, to, _tween = DEFAULT_TWEEN)
def color_transition(from = @from, to = @to, _tween = @tween)
r = transition(from.red, to.red)
g = transition(from.green, to.green)
b = transition(from.blue, to.blue)
@@ -34,7 +34,7 @@ module CyberarmEngine
Gosu::Color.rgba(r, g, b, a)
end
def color_hsv_transition(from, to, tween = DEFAULT_TWEEN)
def color_hsv_transition(from = @from, to = @to, tween = @tween)
hue = transition(from.hue, to.hue, tween)
saturation = transition(from.saturation, to.saturation, tween)
value = transition(from.value, to.value, tween)
@@ -49,8 +49,8 @@ module CyberarmEngine
t
end
def tween_sine(t)
Math.sin(t) * t
def tween_ease_in_out(t)
(-0.5 * (Math.cos(Math::PI * t) - 1))
end
end
end

View File

@@ -1,53 +1,126 @@
module CyberarmEngine
class IntroState < CyberarmEngine::GameState
def setup
@display_width = 800
@display_height = 600
@title_size = 56
@caption_size = 24
@title = CyberarmEngine::Text.new("", size: @title_size)
@caption = CyberarmEngine::Text.new("", size: @caption_size)
@title = CyberarmEngine::Text.new("", size: @title_size, shadow_color: 0xaa_222222)
@caption = CyberarmEngine::Text.new("", size: @caption_size, shadow_color: 0xaa_222222)
@spacer_width = 256
@spacer_height = 6
@padding = 6
@cyberarm_engine_logo = generate_proxy("CyberarmEngine", "Powered By")
@cyberarm_engine_logo = get_image "#{CYBERARM_ENGINE_ROOT_PATH}/assets/textures/logo.png"
@gosu_logo = generate_proxy("Gosu", "Game Library")
@ruby_logo = generate_proxy("Ruby", "Programming Language")
@sdl2_logo = generate_proxy("SDL2", "Simple DirectMedia Layer")
@gosu_logo = generate_proxy("Gosu", "Game Library", 0xff_111111)
@ruby_logo = generate_proxy("Ruby", "Programming Language", 0xff_880000)
@opengl_logo = generate_proxy("OpenGL", "Graphics API", 0xff_5586a4) if defined?(OpenGL)
base_time = Gosu.milliseconds
@animators = [
Animator.new(start_time: base_time += 1000, duration: 100, from: 0.0, to: 1.0, tween: :ease_in_out),
Animator.new(start_time: base_time += -500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
Animator.new(start_time: base_time += 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
Animator.new(start_time: base_time += 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
Animator.new(start_time: base_time + 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
Animator.new(start_time: Gosu.milliseconds + 250, duration: 500, from: 0.0, to: 1.0, tween: :ease_in_out) # CyberarmEngine LOGO
]
@born_time = Gosu.milliseconds
@continue_after = 5_000
end
def draw
Gosu.draw_rect(0, 0, window.width, window.height, 0xff_222222)
@cyberarm_engine_logo.draw(window.width / 2 - @cyberarm_engine_logo.width / 2, window.height / 2 - @cyberarm_engine_logo.height, 2)
scale = (@display_width - @padding * 2).to_f / @cyberarm_engine_logo.width * @animators.last.transition
@gosu_logo.draw(6, window.height - @gosu_logo.height - 6, 2)
@ruby_logo.draw(window.width / 2 - @ruby_logo.width / 2, window.height - @ruby_logo.height - 6, 2)
@sdl2_logo.draw(window.width - (@sdl2_logo.width + 6), window.height - @sdl2_logo.height - 6, 2)
@cyberarm_engine_logo.draw_rot(
window.width / 2,
(window.height) / 2 - @cyberarm_engine_logo.height / 2 - @padding * 2,
2,
0,
0.5,
0.5,
scale,
scale
)
Gosu.draw_rect(
window.width / 2 - (@display_width / 2 + @padding),
window.height / 2 - @spacer_height / 2,
@display_width + @padding,
@spacer_height * @animators[0].transition,
Gosu::Color::WHITE
)
@title.x = window.width / 2 - @title.width / 2
@title.y = (window.height / 2 + (@spacer_height / 2) + @padding) * @animators[1].transition
@title.text = "Powered By"
Gosu.clip_to(0, window.height / 2 + (@spacer_height / 2), window.width, @title.height) do
@title.draw
end
y = @title.y + @title.height * 2
Gosu.clip_to(0, y, window.width, @gosu_logo.height) do
Gosu.translate(@opengl_logo.nil? ? @ruby_logo.width / 2 : 0, 0) do
@gosu_logo.draw(
window.width.to_f / 2 - @ruby_logo.width / 2 - (@ruby_logo.width - @padding),
y * @animators[2].transition,
2
)
@ruby_logo.draw(
window.width.to_f / 2 - @ruby_logo.width / 2,
y * @animators[3].transition,
2
)
@opengl_logo&.draw(
window.width.to_f / 2 - @ruby_logo.width / 2 + (@ruby_logo.width - @padding),
y * @animators[4].transition,
2
)
end
end
end
def update
@animators.each(&:update)
return unless Gosu.milliseconds - @born_time >= @continue_after
pop_state
push_state(@options[:forward], @options[:forward_options] || {}) if @options[:forward]
end
def generate_proxy(title, caption)
def button_down(_id)
@continue_after = 0
end
def generate_proxy(title, caption, color_hint)
@title.text = title
@caption.text = caption
padding = 6
spacer_height = 6
width = @spacer_width + 2 * padding
height = @title_size + @caption_size + spacer_height + 2 * padding + spacer_height
width = @spacer_width + 2 * @padding
height = @title_size + @caption_size + @spacer_height + 2 * @padding + @spacer_height
Gosu.record(width.ceil, height.ceil) do
@title.x = (width - padding * 2) / 2 - @title.width / 2
@title.y = padding
@title.x = (width - @padding * 2) / 2 - @title.width / 2
@title.y = @padding
@title.draw
Gosu.draw_rect(0, padding + @title_size + padding, @spacer_width, spacer_height, Gosu::Color::WHITE)
Gosu.draw_rect(0, @padding + @title_size + @padding, @spacer_width, @spacer_height, Gosu::Color::WHITE)
Gosu.draw_rect(1, @padding + @title_size + @padding + 1, @spacer_width - 2, @spacer_height - 2, color_hint)
@caption.x = (width - padding * 2) / 2 - @caption.width / 2
@caption.y = padding + @title_size + padding + spacer_height + padding
@caption.x = (width - @padding * 2) / 2 - @caption.width / 2
@caption.y = @padding + @title_size + @padding + @spacer_height + @padding
@caption.draw
end
end

View File

@@ -16,6 +16,10 @@ module CyberarmEngine
window.pop_state
end
def shift_state
window.shift_state
end
def show_cursor
window.show_cursor
end

View File

@@ -149,6 +149,7 @@ module CyberarmEngine
@textobject.send(method, @text, _x + @shadow_size, _y, @z, @factor_x, @factor_y, white, :add)
@textobject.send(method, @text, _x + @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
end
@rendered_shadow.draw(@x - @shadow_size, @y - @shadow_size, @z, @factor_x, @factor_y, shadow_color)
end

View File

@@ -95,7 +95,7 @@ module CyberarmEngine
end
def previous_state
if @states.size > 1 && state = @states[@states.size - 2]
if @states.size > 1 && (state = @states[@states.size - 2])
state
end
end
@@ -104,6 +104,10 @@ module CyberarmEngine
@states.pop
end
def shift_state
@states.shift
end
# Sourced from https://gist.github.com/ippa/662583
def draw_circle(cx, cy, r, z = 9999, color = Gosu::Color::GREEN, step = 10)
0.step(360, step) do |a1|