Class: AdventureRL::Quadtree

Inherits:
Mask show all
Includes:
Helpers::MethodHelper
Defined in:
lib/AdventureRL/Quadtree.rb

Constant Summary collapse

DEFAULT_SETTINGS =
Settings.new(
  max_objects: 4,
  position: {
    x: 0,
    y: 0
  },
  size: {
    width:  960,
    height: 540
  },
  origin: {
    x: :left,
    y: :top
  }
)

Constants inherited from Mask

Mask::MASKS

Constants inherited from Point

Point::POINTS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mask

#assign_to, #assigned_to?, #collides_with?, #collides_with_hash?, #collides_with_mask?, #collides_with_point?, #get_assigned, #get_center, #get_corner, #get_layer, #get_mask, #get_origin, #get_real_center, #get_real_corner, #get_real_side, #get_real_sides, #get_side, #get_sides, #get_size, #has_layer?, #has_mask?, #set_layer, #set_size

Methods inherited from Point

#assign_to, #assigned_to?, #collides_with?, #collides_with_hash?, #collides_with_mask?, #collides_with_point?, #get_assigned, #get_layer, #get_point, #get_position, #get_real_point, #get_real_position, #has_layer?, #has_point?, #keys, #move_by, #set_layer, #set_position, #values, #x, #y

Constructor Details

#initialize(settings = {}) ⇒ Quadtree

Returns a new instance of Quadtree.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/AdventureRL/Quadtree.rb', line 30

def initialize settings = {}
  @settings = DEFAULT_SETTINGS.merge(Quadtree.get_default_settings).merge(settings)
  super @settings
  @max_objects     = @settings.get :max_objects
  @quadtrees = {
    top_left:     nil,
    top_right:    nil,
    bottom_left:  nil,
    bottom_right: nil
  }
  @objects = []
  add_object [@settings.get(:objects)].flatten.compact
end

Class Method Details

.get_default_settingsObject



21
22
23
24
25
26
27
28
# File 'lib/AdventureRL/Quadtree.rb', line 21

def self.get_default_settings
  window = Window.get_window
  return Settings.new(
    position: (window ? window.get_position : DEFAULT_SETTINGS.get(:window, :position) || DEFAULT_SETTINGS[:position]),
    size:     (window ? window.get_size     : DEFAULT_SETTINGS.get(:window, :size)     || DEFAULT_SETTINGS[:size]),
    origin:   (window ? window.get_origin   : DEFAULT_SETTINGS.get(:window, :origin)   || DEFAULT_SETTINGS[:origin]),
  )
end

Instance Method Details

#add_object(object) ⇒ Object Also known as: add

Add the given Mask object(s) into the Quadtree, and split into smaller quadtrees if necessary.



46
47
48
49
50
51
52
# File 'lib/AdventureRL/Quadtree.rb', line 46

def add_object object
  objects = [object].flatten
  objects.each do |obj|
    validate_object_has_mask_or_point obj
    add_object_to_quadtree obj
  end
end

#add_object_to_quadtree(object) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/AdventureRL/Quadtree.rb', line 55

def add_object_to_quadtree object
  return false  unless (collides_with? object)
  return false  if     (@objects.include? object)

  if (@objects.size < @max_objects)
    @objects << object
    return true
  end

  split_quadtrees  unless (has_quadtrees?)

  return get_quadtrees.map do |quadtree|
    next quadtree.add_object_to_quadtree(object)
  end .any?
end

#collides?(object) ⇒ Boolean

Returns true if the given object collides with any other object and false if not.

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/AdventureRL/Quadtree.rb', line 73

def collides? object
  validate_object_has_mask_or_point object
  return collides_for?(object)
end

#collides_for?(object) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/AdventureRL/Quadtree.rb', line 78

def collides_for? object
  return false  unless (collides_with? object)
  return (
    @objects.any? do |obj|
      next obj != object && obj.collides_with?(object)
    end ||
    get_quadtrees.any? do |quadtree|
      next quadtree.collides_for?(object)
    end
  )
end

#get_colliding_objects(object) ⇒ Object

Returns all objects, that collide with object.



91
92
93
94
# File 'lib/AdventureRL/Quadtree.rb', line 91

def get_colliding_objects object
  validate_object_has_mask_or_point object
  return get_colliding_objects_for(object)
end

#get_colliding_objects_for(object) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/AdventureRL/Quadtree.rb', line 96

def get_colliding_objects_for object
  colliding_objects = []
  return colliding_objects  unless (collides_with? object)
  colliding_objects.concat(@objects.select do |obj|
    next obj != object && obj.collides_with?(object)
  end)
  get_quadtrees.each do |quadtree|
    colliding_objects.concat quadtree.get_colliding_objects_for(object)
  end
  return colliding_objects
end

#remove_object(object) ⇒ Object

Remove the given object(s) (single or multiple) from the Quadtree (or any children).



125
126
127
128
129
130
131
132
133
# File 'lib/AdventureRL/Quadtree.rb', line 125

def remove_object object
  objects = [object].flatten
  objects.each do |obj|
    @objects.delete obj
    get_quadtrees.each do |quadtree|
      quadtree.remove_object obj
    end
  end
end

#resetObject

Reset this and all child Quadtrees. Removes all stored objects.



110
111
112
113
# File 'lib/AdventureRL/Quadtree.rb', line 110

def reset
  @objects.clear
  get_quadtrees.each &:reset
end

#reset_object(object) ⇒ Object

Remove and (try to) re-add the given object(s) (single or multiple).



116
117
118
119
120
121
122
# File 'lib/AdventureRL/Quadtree.rb', line 116

def reset_object object
  objects = [object].flatten
  objects.each do |obj|
    @objects.delete obj
    add_object_to_quadtree obj
  end
end