Class: Natural20::StaticLightBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/natural_20/utils/static_light_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(battlemap) ⇒ StaticLightBuilder

Returns a new instance of StaticLightBuilder.

Parameters:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/natural_20/utils/static_light_builder.rb', line 6

def initialize(battlemap)
  @map = battlemap
  @properties = battlemap.properties
  @light_properties = @properties[:lights]
  @light_map = @properties.dig(:map, :light)
  @base_illumniation = @properties.dig(:map, :illumination) || 1.0
  @lights = []
  if @light_map && @light_properties
    @light_map.each_with_index do |row, cur_y|
      row.each_char.map(&:to_sym).each_with_index do |key, cur_x|
        next unless @light_properties[key]

        @lights << {
          position: [cur_x, cur_y]
        }.merge(@light_properties[key])
      end
    end
  end
end

Instance Attribute Details

#lightsObject (readonly)

Returns the value of attribute lights.



3
4
5
# File 'lib/natural_20/utils/static_light_builder.rb', line 3

def lights
  @lights
end

Instance Method Details

#build_mapObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/natural_20/utils/static_light_builder.rb', line 26

def build_map
  max_x, max_y = @map.size
  max_y.times.map do |y|
    max_x.times.map do |x|
      @lights.inject(@base_illumniation) do |intensity, light|
        light_pos_x, light_pos_y = light[:position]
        bright_light = light.fetch(:bright, 10) / @map.feet_per_grid
        dim_light = light.fetch(:dim, 5) / @map.feet_per_grid

        intensity + if @map.line_of_sight?(x, y, light_pos_x, light_pos_y, bright_light, false)
                      1.0
                    elsif @map.line_of_sight?(x, y, light_pos_x, light_pos_y, bright_light + dim_light, false)
                      0.5
                    else
                      0.0
                    end
      end
    end
  end.transpose
end

#light_at(pos_x, pos_y) ⇒ Float

Parameters:

  • pos_x (Integer)

Returns:

  • (Float)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/natural_20/utils/static_light_builder.rb', line 50

def light_at(pos_x, pos_y)
  (@map.entities.keys + @map.interactable_objects.keys).inject(0.0) do |intensity, entity|
    next intensity if entity.light_properties.nil?

    light = entity.light_properties
    bright_light = light.fetch(:bright, 0.0) / @map.feet_per_grid
    dim_light = light.fetch(:dim, 0.0) / @map.feet_per_grid

    next intensity if (bright_light + dim_light) <= 0.0

    light_pos_x, light_pos_y = @map.entity_or_object_pos(entity)

    intensity + if @map.line_of_sight?(pos_x, pos_y, light_pos_x, light_pos_y, bright_light, false)
                  1.0
                elsif @map.line_of_sight?(pos_x, pos_y, light_pos_x, light_pos_y, bright_light + dim_light, false)
                  0.5
                else
                  0.0
                end
  end
end