Class: AdventureRL::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/AdventureRL/Point.rb

Direct Known Subclasses

Mask

Constant Summary collapse

POINTS =

This array will be filled with any created Points. Just so they won’t get garbage collected (not sure how garbage collection works).

[]

Instance Method Summary collapse

Constructor Details

#initialize(x, y, args = {}) ⇒ Point

Initialize with two arguments:

x

x position

y

y position

args = {}

Optional hash with extra options. Currently, the only valid hash key is :assign_to, to assign this Point to an object upon initialization.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/AdventureRL/Point.rb', line 14

def initialize x, y, args = {}
  POINTS << self
  @position = {
    x: x,
    y: y
  }
  @assigned_to = []
  assign_to args[:assign_to]  if (args[:assign_to])
  @layer      = nil
  @real_point = nil
end

Instance Method Details

#assign_to(object) ⇒ Object



26
27
28
29
# File 'lib/AdventureRL/Point.rb', line 26

def assign_to object
  Helpers::PipeMethods.pipe_methods_from object, to: self
  @assigned_to << object
end

#assigned_to?(object) ⇒ Boolean

Returns true if the Point has been assigned to the passed object.

Returns:

  • (Boolean)


38
39
40
# File 'lib/AdventureRL/Point.rb', line 38

def assigned_to? object
  return @assigned_to.include? object
end

#collides_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def collides_with? other
  return collides_with_mask?  other  if (defined? other.has_mask?)
  return collides_with_point? other  if (defined? other.has_point?)
  return collides_with_hash?  other  if (other.is_a?(Hash))
end

#collides_with_hash?(hash) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
# File 'lib/AdventureRL/Point.rb', line 105

def collides_with_hash? hash
  if (hash.keys.include_all?(:x, :y))
    point = Point.new hash[:x], hash[:y]
    return collides_with_point? point
  end
  return nil
end

#collides_with_mask?(mask) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/AdventureRL/Point.rb', line 97

def collides_with_mask? mask
  return mask.collides_with_point? self
end

#collides_with_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/AdventureRL/Point.rb', line 101

def collides_with_point? point
  return get_real_position == point.get_real_position
end

#get_assignedObject

Returns all objects this Point was assigned to.



32
33
34
# File 'lib/AdventureRL/Point.rb', line 32

def get_assigned
  return @assigned_to
end

#get_layerObject

Returns the parent Layer.



134
135
136
# File 'lib/AdventureRL/Point.rb', line 134

def get_layer
  return @layer
end

#get_pointObject

Returns self.



43
44
45
# File 'lib/AdventureRL/Point.rb', line 43

def get_point
  return self
end

#get_position(target = :all) ⇒ Object



59
60
61
62
63
64
# File 'lib/AdventureRL/Point.rb', line 59

def get_position target = :all
  target = target.to_sym
  return @position          if (target == :all)
  return @position[target]  if (@position.keys.include?(target))
  return nil
end

#get_real_pointObject

Returns a new Point with the real window position of this Point.



144
145
146
147
148
149
150
151
152
153
# File 'lib/AdventureRL/Point.rb', line 144

def get_real_point
  return self         unless (has_layer?)
  return @real_point  if (@real_point)
  layer_point = get_layer.get_real_corner :left, :top
  @real_point = Point.new(
    (layer_point.x + x),
    (layer_point.y + y)
  )
  return @real_point
end

#get_real_positionObject

Returns the real window position of this Point as a Hash.



156
157
158
# File 'lib/AdventureRL/Point.rb', line 156

def get_real_position
  return get_real_point.get_position
end

#has_layer?Boolean

Returns true if this Point has a parent Layer.

Returns:

  • (Boolean)


139
140
141
# File 'lib/AdventureRL/Point.rb', line 139

def has_layer?
  return !!@layer
end

#has_point?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/AdventureRL/Point.rb', line 47

def has_point?
  return true
end

#keysObject



113
114
115
116
117
118
# File 'lib/AdventureRL/Point.rb', line 113

def keys
  sorted_keys = [:x, :y]
  return @position.keys.sort do |axis|
    next sorted_keys.index axis
  end
end

#move_by(*args) ⇒ Object

Move the Point relative to the given arguments. args may be:

Two integers, representing the <tt>x</tt> and <tt>y</tt> axes, respectively.
A hash containing one or both of the keys <tt>:x</tt> and <tt>:y</tt>.


83
84
85
86
87
88
89
# File 'lib/AdventureRL/Point.rb', line 83

def move_by *args
  @real_point = nil
  incremental_position = parse_position *args
  @position[:x] += incremental_position[:x]  if (incremental_position.key? :x)
  @position[:y] += incremental_position[:y]  if (incremental_position.key? :y)
  return get_position
end

#set_layer(layer) ⇒ Object

Set the parent Layer.



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

def set_layer layer
  error(
    "Passed argument `layer' must be an instance of `Layer', but got",
    "`#{layer.inspect}:#{layer.class.name}'."
  )  unless (layer.is_a? Layer)
  @layer = layer
end

#set_position(*args) ⇒ Object Also known as: move_to

Set the new position with the given arguments. args may be:

Two integers, representing the <tt>x</tt> and <tt>y</tt> axes, respectively.
A hash containing one or both of the keys <tt>:x</tt> and <tt>:y</tt>.


70
71
72
73
74
75
76
# File 'lib/AdventureRL/Point.rb', line 70

def set_position *args
  @real_point = nil
  new_position = parse_position *args
  @position[:x] = new_position[:x]  if (new_position.key? :x)
  @position[:y] = new_position[:y]  if (new_position.key? :y)
  return get_position
end

#valuesObject



120
121
122
# File 'lib/AdventureRL/Point.rb', line 120

def values
  return @position.sort_by_keys(keys).values
end

#xObject



51
52
53
# File 'lib/AdventureRL/Point.rb', line 51

def x
  return get_position :x
end

#yObject



55
56
57
# File 'lib/AdventureRL/Point.rb', line 55

def y
  return get_position :y
end