summaryrefslogtreecommitdiffhomepage
path: root/lib/felecs/scene_manager.rb
blob: af61b9c844e8e441b108e4bd9f0a95572b3f6b26 (plain)
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
66
67
68
69
# frozen_string_literal: true

module FelECS
  class Scenes
    # Allows overwriting the storage of systems, such as for clearing.
    # This method should generally only need to be used internally and
    # not by a game developer/
    # @!visibility private
    attr_writer :systems

    # How early this Scene should be executed in a list of Scenes
    attr_accessor :priority

    def priority=(priority)
      @priority = priority
      FelECS::Stage.scenes = FelECS::Stage.scenes.sort_by(&:priority)
      priority
    end

    # Create a new Scene using the name given
    # @param name [String] String format must follow requirements of a constant
    def initialize(name, priority: 0)
      self.priority = priority
      FelECS::Scenes.const_set(name, self)
    end

    # The list of Systems this Scene contains
    # @return [Array<System>]
    def systems
      @systems ||= []
    end

    # Execute all systems in this Scene, in the order of their priority
    # @return [Boolean] +true+
    def call
      systems.each(&:call)
      true
    end

    # Adds any number of Systems to this Scene
    # @return [Boolean] +true+
    def add(*systems_to_add)
      self.systems |= systems_to_add
      self.systems = systems.sort_by(&:priority)
      systems_to_add.each do |system|
        system.scenes |= [self]
      end
      true
    end

    # Removes any number of Systems from this Scene
    # @return [Boolean] +true+
    def remove(*systems_to_remove)
      self.systems -= systems_to_remove
      true
    end

    # Removes all Systems from this Scene
    # @return [Boolean] +true+
    def clear
      systems.each do |system|
        system.scenes.delete self
      end
      systems.clear
      # FelECS::Stage.update_systems_list if FelECS::Stage.scenes.include? self
      true
    end
  end
end