summaryrefslogtreecommitdiffhomepage
path: root/helpers/01_component.rb
blob: 580b5d87c35a00afb89c752b1ca03c6e2e532c2c (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class FelFlame
  class Helper
    class BaseComponent
      class <<self
        def signature
          @signature ||= FelFlame::Signature.create_new_signature\
            FelFlame::Helper::ComponentHelper.underscore(ancestors[0].name.split('::').last)
        end

        def data
          @data ||= []
        end

        def new(**opts)
          new_component = super

          # Generate ID
          new_id = self.data.find_index { |i| i.nil? }
          new_id = self.data.size if new_id.nil?
          new_component.id = new_id

          # Fill params
          opts.each do |key, value|
            new_component.send "#{key}=", value
          end

          # Save Component
          data[new_id] = new_component
        end

        #def add(entity_id)
        #  data[entity_id] = new
        #end

        #def delete(entity_id)
        #  data.delete entity_id
        #end
      end

      def set(**opts)
        opts.each do |key, value|
          send "#{key}=", value
        end
      end

      #def create_data(name, default = nil)
      #  #TODO: fill this out
      #end

      def get #TODO: maybe optimize removing the @ symbol
        instance_variables.each_with_object({}) do |key, final|
          final[key.to_s.delete_prefix('@').to_sym] = instance_variable_get(key)
        end
      end

      def dump #TODO: Needs to get id and stuff?
        # should return a json or hash of all data in this component
      end
    end

    class Level < FelFlame::Helper::BaseComponent
      class <<self
        def data
          @data ||= { add: [], remove: [], grid: FelFlame::Helper::Array2D.new }
        end

        def add(entity_id)
          super
          data[:add].push entity_id
        end

        def remove(entity_id)
          data[:remove].push entity_id
          super
        end
      end
    end

    class Array2D < Array
      def [](val)
        unless val.nil?
          return self[val] = [] if super.nil?
        end
        super
      end
    end

    class ArrayOfHashes < Array
      def [](val)
        unless val.nil?
          return self[val] = {} if super.nil?
        end
        super
      end
    end

    module ComponentHelper
      class <<self
        def up? char
          char == char.upcase
        end

        def down? char
          char == char.downcase
        end

        def underscore(input)
          output = input[0].downcase
          (1...(input.length - 1)).each do |iter|
            if down?(input[iter]) && up?(input[iter + 1])
              output += "#{input[iter].downcase}_"
            elsif up?(input[iter - 1]) && up?(input[iter]) && down?(input[iter + 1])
              output += "_#{input[iter].downcase}"
            else
              output += input[iter].downcase
            end
          end
          output += input[-1].downcase unless input.length == 1
          output
        end
      end
    end
  end
end