scale icon indicating copy to clipboard operation
scale copied to clipboard

Use namespaces for Scenes

Open MatheusRich opened this issue 2 years ago • 1 comments

Hey! Thanks for creating Scale! It's been helpful on my first DR game.

One thing that didn't please me was how all the game Scenes had to live inside the same scope. I understand how handy is being able to use Scene.send(:"tick_#{args.state.scene}", args), but the way the template is set encourages people to put all the scene behavior as methods under Scene. That can cause methods from one scene leaking (or worse, overriding) other scenes! For instance:

# scenes/gameplay.rb
module Scene
  def self.tick_gameplay(args)
    update(args)
  end

  def update(args)
  end
end


# scenes/pause.rb

module Scene
  def self.tick_pause(args)
    update(args)
  end

  # this will override the update method in the gameplay scene
  def update(args)
  end
end

I propose a gentle change (so we can still use send as before), but that namespaces the specifics of each scene. Each scene is still defined in a separate file, with a corresponding tick_#{scene} method. But that method will use an inner module to define its behavior:

# scenes/gameplay.rb
module Scene
  def self.tick_gameplay(args)
    GameplayScene.tick(args)
  end

  module GameplayScene
    class << self
      def tick(args)
        # do stuff
      end

      private

      # you can define private helper methods that don't pollute the Scene namespace
    end
  end
end


# scenes/pause.rb

module Scene
  def self.tick_pause(args)
    PauseScene.tick(args)
  end

  module PauseScene
    class << self
      def tick(args)
        # do pause stuff
      end

      private

      # pause helper methods
    end
  end
end

This won't vastly change the template, but it promotes better defaults, IMO. Thoughts?

MatheusRich avatar Jan 19 '24 03:01 MatheusRich

Makes a ton of sense, I think that’d be great! Let me know if I can do anything to help support the change—contribution certainly welcome. Glad Scale has been helpful. 🙌

brettchalupa avatar Jan 20 '24 02:01 brettchalupa