Class: SFML::Scene

Inherits:
Object
  • Object
show all
Extended by:
InputActions, Keybindings
Includes:
InputQueries
Defined in:
lib/sfml/scene.rb

Overview

A chunk of game state with its own lifecycle — menu, gameplay, game-over, settings overlay, etc. The host SFML::App keeps a current_scene and forwards update / draw / on_event / on_resize to it. Switch between scenes with app.switch_to (or switch_to from inside a scene, which delegates).

class TitleScene < SFML::Scene
on_key :enter, :start_game

def setup
  @gui = SFML::GUI::App.new(window: window, stylesheet: SHEET)
  # ... build UI ...
end

def update(dt) = @gui.update(dt)
def draw       = @gui.draw

def start_game
  switch_to PlayScene
end
end

class PlayScene < SFML::Scene
# …
end

class MyApp < SFML::App
initial_scene TitleScene   # auto-switches to it on app setup
end

SFML::Scene carries its own on_key DSL — scene-level bindings shadow app-level bindings while the scene is active.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InputActions

action, action_bindings

Methods included from Keybindings

key_handlers, on_key

Methods included from InputQueries

#action_pressed?, #axis

Constructor Details

#initialize(app) ⇒ Scene

Returns a new instance of Scene.



46
47
48
# File 'lib/sfml/scene.rb', line 46

def initialize(app)
  @app = app
end

Instance Attribute Details

#app ⇒ Object (readonly) Also known as: host

The Host SFML::App.



43
44
45
# File 'lib/sfml/scene.rb', line 43

def app
  @app
end

Instance Method Details

#draw ⇒ Object

Called every frame after #update.



74
# File 'lib/sfml/scene.rb', line 74

def draw; end

#height ⇒ Object

Returns the height.



55
# File 'lib/sfml/scene.rb', line 55

def height = @app.height

#on_event(_event) ⇒ Object

Called for every event the framework didn't auto-handle (closed, resized, and on_key bindings). Override to handle game-specific input patterns.



79
# File 'lib/sfml/scene.rb', line 79

def on_event(_event); end

#on_resize(_width, _height) ⇒ Object

Window resize. Default: no-op.



82
# File 'lib/sfml/scene.rb', line 82

def on_resize(_width, _height); end

#setup ⇒ Object

Called once when the host app activates this scene (replacing whatever scene was active before). Build per-scene state here.



68
# File 'lib/sfml/scene.rb', line 68

def setup; end

#switch_to(scene_or_class) ⇒ Object

Switch the host app to a new scene from inside this one. Accepts a Scene class (instantiated with app) or an already- constructed Scene instance.



60
61
62
# File 'lib/sfml/scene.rb', line 60

def switch_to(scene_or_class)
  @app.switch_to(scene_or_class)
end

#teardown ⇒ Object

Called once when the host app switches away (or when the app loop exits while this scene is active). Free per-scene resources here.



87
# File 'lib/sfml/scene.rb', line 87

def teardown; end

#update(_dt) ⇒ Object

Called every frame the scene is active and the app isn't paused.



71
# File 'lib/sfml/scene.rb', line 71

def update(_dt); end

#width ⇒ Object

Returns the width.



53
54
# File 'lib/sfml/scene.rb', line 53

def width  = @app.width
# Returns the height.

#window ⇒ Object

Convenience accessors that match SFML::App's.



51
52
# File 'lib/sfml/scene.rb', line 51

def window = @app.window
# Returns the width.