Class: SuperSimpleWorldBuilder::World

Inherits:
Object
  • Object
show all
Defined in:
lib/super_simple_world_builder/world.rb

Constant Summary collapse

DEPTH_MAX =

Number of iterations of randomly placing features on map before enlarging

30
DEPTH_INCR =

Number of rows and cols to expand map by

3

Instance Method Summary collapse

Constructor Details

#initialize(name, width, height, filler = ' ', user_features = {}) ⇒ World

Returns a new instance of World.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/super_simple_world_builder/world.rb', line 12

def initialize(name, width, height, filler=' ', user_features={})
    @name = name
    @width = width
    @height = height
    @filler = filler
    @user_features = user_features

    #initialize 2d array
    @map = Array.new(@height) { Array.new(@width, @filler) }
    
    @depth_count = 0
    @map_enlarged = false

    @features = FeatureSet::FEATURES

    #randomly defines number of each feature if random world created
    if user_features.empty?
        @features.each do |f|
            @user_features[f.name] = rand(FeatureSet::RANDOM_FEATURE_MIN..FeatureSet::RANDOM_FEATURE_MAX)
        end
    end

    add_feature_shuffler.each { |f| add_feature_to_map(f) }
end

Instance Method Details

#add_feature_shufflerObject

shuffles feature names into array to keep distribution random if map enlarged iterates over names and adds to map



39
40
41
42
43
44
45
46
# File 'lib/super_simple_world_builder/world.rb', line 39

def add_feature_shuffler
    user_features_shuffled = []
    @user_features.each do |k, v|
        v.times {user_features_shuffled << k}
    end

    user_features_shuffled.shuffle!
end

#add_feature_to_map(name) ⇒ Object



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
# File 'lib/super_simple_world_builder/world.rb', line 66

def add_feature_to_map(name)
    feature = get_feature(name)

    #randomly pick x,y coords on map
    x = rand(0..@map[0].size)
    y = rand(0..@map.size)

    #if there's enough space at the coords, add the feature to the map
    #see FeatureSet::FEATURES for dimensions and filler char
    if enough_space?(x, y, feature)
        feature.height.times do |i|
            feature.width.times do |j|
                @map[y+i][x+j] = feature.filler
            end
        end
    else
        @depth_count += 1

        #if feature couldn't be placed at coords the max amount of times then enlarge map and try again
        if @depth_count > DEPTH_MAX
            @depth_count = 0

            #increases HEIGHT of existing 2d arr by DEPTH_INCR
            #and fill with existing number of arr elements
            DEPTH_INCR.times {@map.push([])}
            DEPTH_INCR.times do |i|
                @map[0].size.times { @map[@map.size-DEPTH_INCR+i] << @filler }
            end

            #increases WIDTH of existing 2d arr by DEPTH_INCR
            @map.size.times do |i|
                DEPTH_INCR.times {@map[i] << @filler}
            end
        end

        @map_enlarged = true
        add_feature_to_map(name)
    end
end

#enough_space?(x, y, feature) ⇒ Boolean

point (x,y) indicates top left corner of feature

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/super_simple_world_builder/world.rb', line 107

def enough_space?(x, y, feature)
    result = true
    if (x + feature.width) > @map[0].size || (y + feature.height) > @map.size
        result = false
    elsif
        feature.height.times do |i|
            feature.width.times do |j|
                if @map[y+i][x+j] != @filler
                    result = false
                end
            end
        end
    end
    result
end

#export_map_to_fileObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/super_simple_world_builder/world.rb', line 130

def export_map_to_file
    File.open("#{@name}.txt", "w") do |file|
        file.puts "#{@name.center((@map[0].size*2)-1, '~')}\n\n"
        file.puts @map.map { |x| x.join(' ') }
        file.puts "\nLegend"
        file.puts "-------"
        @features.each do |f|
            file.puts "#{f.name.capitalize} (#{@user_features[f.name]})".ljust(30, ".") + " #{f.filler}"
        end
        file.puts "\n#{@name} was enlarged from #{@width}x#{@height} to #{@map[0].size}x#{@map.size} to accomodate all #{@user_features.values.reduce(:+)} features." if @map_enlarged
    end
end

#get_feature(name) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/super_simple_world_builder/world.rb', line 48

def get_feature(name)
    feature = ""
    @features.each do |f|
        if f.name == name.to_sym
            feature = f
        end
    end
    feature
end


58
59
60
61
62
63
64
# File 'lib/super_simple_world_builder/world.rb', line 58

def print_legend
    puts "Legend"
    puts "-------"
    @features.each do |f|
        puts "#{f.name.capitalize} (#{@user_features[f.name]})".ljust(30, ".") + " #{f.filler}"
    end
end


123
124
125
126
127
128
# File 'lib/super_simple_world_builder/world.rb', line 123

def print_map_to_console
    puts "#{@name.center((@map[0].size*2)-1, '~')}\n\n"
    puts @map.map { |x| x.join(' ') }
    print_legend
    puts "\n#{@name} was enlarged from #{@width}x#{@height} to #{@map[0].size}x#{@map.size} to accomodate all #{@user_features.values.reduce(:+)} features." if @map_enlarged
end