Class: Amaze::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/amaze/factory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Factory

Returns a new instance of Factory.



14
15
16
17
# File 'lib/amaze/factory.rb', line 14

def initialize type
  raise "#{type} maze is not supported" unless self.class.types.include? type
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

The type of the grid



12
13
14
# File 'lib/amaze/factory.rb', line 12

def type
  @type
end

Class Method Details

.algorithmsObject

All known algorithms



60
61
62
# File 'lib/amaze/factory.rb', line 60

def self.algorithms
  %i( bt sw ab gt1 gt2 gt3 gt4 w hk rb1 rb2 )
end

.gradient_mapsObject



107
108
109
# File 'lib/amaze/factory.rb', line 107

def self.gradient_maps
  %i( red green blue monochrome cold warm gold )
end

.shapesObject

All known shapes



41
42
43
# File 'lib/amaze/factory.rb', line 41

def self.shapes
  %i( triangle diamond hexagon star )
end

.typesObject

All known maze types



7
8
9
# File 'lib/amaze/factory.rb', line 7

def self.types
  %i( delta ortho sigma upsilon polar )
end

Instance Method Details

#create_algorithm(algorithm) ⇒ Object



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
# File 'lib/amaze/factory.rb', line 64

def create_algorithm algorithm
  raise "#{algorithm} is not supported" unless self.class.algorithms.include? algorithm
  
  # Alogrithms for all mazes
  select = {
    ab:  Amaze::Algorithm::AldousBorder,
    gt1: Amaze::Algorithm::GrowingTree,
    gt2: Amaze::Algorithm::GrowingTree,
    gt3: Amaze::Algorithm::GrowingTree,
    gt4: Amaze::Algorithm::GrowingTree,
    w:   Amaze::Algorithm::Wilson,
    hk:  Amaze::Algorithm::HuntAndKill,
    rb1: Amaze::Algorithm::RecursiveBacktracker,
    rb2: Amaze::Algorithm::RecursiveBacktracker,
  }
  
  # Algorithms only for ortho mazes
  select.merge!(
    bt: Amaze::Algorithm::BinaryTree,
    sw: Amaze::Algorithm::Sidewinder
  ) if type == :ortho
  
  raise "Alogrithm not supported on #{type} maze." unless select[algorithm]
  instance = select[algorithm].new

  case algorithm
  when :gt1
    instance.configure "last from list", proc {|active| active.last }
  when :gt2
    instance.configure "random from list", proc {|active| active.sample }
  when :gt3
    instance.configure "last/random 1/1 from list", proc {|active| (rand(2) > 0) ? active.last : active.sample }
  when :gt4
    instance.configure "last/random 2/1 from list", proc {|active| (rand(3) > 0) ? active.last : active.sample }
  when :rb1
    instance.configure :stack
  when :rb2
    instance.configure :recursion
  end
  
  instance 
end

#create_ascii_formatter(*args) ⇒ Object



51
52
53
# File 'lib/amaze/factory.rb', line 51

def create_ascii_formatter *args
  Amaze::Formatter::ASCII.const_get(type.to_s.capitalize).new *args
end

#create_grid(*args) ⇒ Object



19
20
21
# File 'lib/amaze/factory.rb', line 19

def create_grid *args
  Amaze::Grid.const_get(type.to_s.capitalize).new *args
end

#create_image_formatter(*args) ⇒ Object



55
56
57
# File 'lib/amaze/factory.rb', line 55

def create_image_formatter *args
  Amaze::Formatter::Image.const_get(type.to_s.capitalize).new *args
end

#create_mask(file) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/amaze/factory.rb', line 29

def create_mask file
  case File.extname file
  when '.txt'
    Amaze::Mask.from_txt file
  when '.png'
    Amaze::Mask.from_png file
  else
    raise "Mask file of type #{File.extname(file)} is not supported."
  end
end

#create_masked_grid(file) ⇒ Object



23
24
25
26
27
# File 'lib/amaze/factory.rb', line 23

def create_masked_grid file
  klass = Amaze::Grid.const_get(type.to_s.capitalize)
  klass.prepend Amaze::MaskedGrid
  klass.new create_mask(file)
end

#create_shaped_grid(shape, *args) ⇒ Object



45
46
47
48
49
# File 'lib/amaze/factory.rb', line 45

def create_shaped_grid shape, *args
  klass = Amaze::Grid.const_get(type.to_s.capitalize)
  klass.prepend Amaze::MaskedGrid
  klass.new Amaze::Shape.const_get(shape.to_s.capitalize).new(args.first).create_mask
end

#gradient_map(name = :green) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/amaze/factory.rb', line 111

def gradient_map name=:green
  { red:
      Gradient::Map.new(
        Gradient::Point.new(0, Color::RGB.new( 95,   0,   0), 1.0), # dark red
        Gradient::Point.new(1, Color::RGB.new(255, 255, 255), 1.0), # white
      ),
    green:
      Gradient::Map.new(
        Gradient::Point.new(0, Color::RGB.new(  0,  95,   0), 1.0), # dark green
        Gradient::Point.new(1, Color::RGB.new(255, 255, 255), 1.0), # white
      ),
    blue:
      Gradient::Map.new(
        Gradient::Point.new(0, Color::RGB.new(  0,   0,  95), 1.0), # dark blue
        Gradient::Point.new(1, Color::RGB.new(255, 255, 255), 1.0), # white
      ),
    monochrome: 
      Gradient::Map.new(
        Gradient::Point.new(0, Color::RGB.new(  0,   0,   0), 1.0), # black
        Gradient::Point.new(1, Color::RGB.new(255, 255, 255), 1.0), # white
      ),
    cold: 
      Gradient::Map.new(
        Gradient::Point.new(0,    Color::RGB.new(  0,   0,  95), 1.0), # blue
        Gradient::Point.new(0.65, Color::RGB.new(  0, 191, 255), 1.0), # cyan
        Gradient::Point.new(1,    Color::RGB.new(255, 255, 255), 1.0), # white
      ),
    warm: 
      Gradient::Map.new(
        Gradient::Point.new(0,    Color::RGB.new( 95,   0,   0), 1.0), # dark red
        Gradient::Point.new(0.65, Color::RGB.new(255, 191,   0), 1.0), # yellow
        Gradient::Point.new(1,    Color::RGB.new(255, 255, 255), 1.0), # white
      ),
    gold:
      Gradient::Map.new(
        Gradient::Point.new(0,    Color::RGB.new(127,   0,   0), 1.0), # dark red
        Gradient::Point.new(0.5,  Color::RGB.new(255, 127,   0), 1.0), # light red yellow
        Gradient::Point.new(0.75, Color::RGB.new(255, 255,   0), 1.0), # yellow
        Gradient::Point.new(1,    Color::RGB.new(255, 255, 255), 1.0), # white
      ),
  }[name]
end