Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created May 28, 2024 20:58
Show Gist options
  • Save amirrajan/93245bba1d84c3b8b607c8bb4f42be67 to your computer and use it in GitHub Desktop.
Save amirrajan/93245bba1d84c3b8b607c8bb4f42be67 to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit - Scene Management With Fade To Black
class RootScene < Scene
attr :game
def initialize
@game = Game.new
$game = @game
@world_scene = WorldScene.new
@embark_scene = EmbarkScene.new
@world_event_scene = WorldEventScene.new
@battle_scene = BattleScene.new
@game_over_scene = GameOverScene.new
@loot_scene = LootScene.new
@cutscene_scene = CutsceneScene.new
@alert_scene = AlertScene.new
@mini_world_scene = MiniWorldScene.new
@mini_world_event_scene = MiniWorldEventScene.new
@mini_world_loot_scene = MiniWorldLootScene.new
$root_scene = self
end
def defaults
state.scene ||= :embark_scene
state.scene_at ||= 0
state.fade_at ||= 0
end
def bg_prefab
{ x: Grid.allscreen_x,
y: Grid.allscreen_y,
w: Grid.allscreen_w,
h: Grid.allscreen_h,
path: :solid,
r: 255,
g: 255,
b: 255,
a: 255 }
end
def fade_prefab
start_time = state.fade_at
duration = 30
spline = [
[ 0, 0.25, 0.75, 1.0],
[1.0, 0.75, 0.25, 0]
]
if Kernel.tick_count < 15
current_progress = 1
else
current_progress = Easing.ease_spline(start_time, Kernel.tick_count, duration, spline)
end
a = 255 * current_progress
{ x: Grid.allscreen_x,
y: Grid.allscreen_y,
w: Grid.allscreen_w,
h: Grid.allscreen_h,
path: :solid,
r: 0,
g: 0,
b: 0,
a: a }
end
def tick
defaults
inputs.clear if state.queued_scene
scene_before_tick = state.scene
outputs.background_color = [0, 0, 0]
outputs.primitives << bg_prefab
scene_tick
outputs.primitives << fade_prefab
if state.scene != scene_before_tick
raise "Scene change is not allowed during tick, set next_scene instead"
end
if state.next_scene
if state.queued_scene
state.next_scene = nil
state.next_scene_metadata = nil
else
state.queued_scene = state.next_scene
state.queued_scene_at = Kernel.tick_count
state.queued_scene_metadata = state.next_scene_metadata
state.queued_scene_callback = state.next_scene_callback
state.next_scene = nil
state.fade_at = Kernel.tick_count
end
end
if state.queued_scene_at && state.queued_scene_at.elapsed_time >= 15 && state.queued_scene
state.scene = state.queued_scene
state.scene_at = Kernel.tick_count
state.scene_metadata = state.queued_scene_metadata
state.queued_scene_callback.call if state.queued_scene_callback
state.queued_scene = nil
state.queued_scene_at = nil
state.queued_scene_metadata = nil
state.queued_scene_callback = nil
end
end
def scenes
[
@embark_scene,
@world_scene,
@world_event_scene,
@battle_scene,
@game_over_scene,
@loot_scene,
@cutscene_scene,
@alert_scene,
@mini_world_scene,
@mini_world_event_scene,
@mini_world_loot_scene
]
end
def scene_current
scenes.find { |s| s.id == state.scene }
end
def scene_set_args
scene = scene_current
if !scene
raise "Scene with id of #{state.scene} was not found."
end
scene.game = @game
scene.args = args
scene.metadata = state.scene_metadata || {}
$scene = scene_current
end
def scene_tick
scene_set_args
if state.scene_at == Kernel.tick_count - 1
scene_current.will_appear
end
scene_current.tick
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment