Class: RGame::Engine::Path
- Inherits:
-
Object
- Object
- RGame::Engine::Path
- Defined in:
- lib/rgame/engine/path.rb
Overview
An ordered polyline of waypoints an entity walks along — the "road" of a tower defense level. Pure data: it holds the waypoints and the precomputed per-segment lengths, so a follower walking it at runtime allocates nothing.
Waypoints are stored flat (x0, y0, x1, y1, …) in one contiguous array rather than a
pair-object per point, and read back through scalar accessors (x_at/y_at), so
neither construction shape nor traversal leaks per-waypoint Arrays onto the hot path.
A follower (see Components::PathFollow) reads segments by index and interpolates
itself; Path never returns a coordinate pair.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
-
#distance_to(x, y) ⇒ Object
Shortest distance from the point (x, y) to the polyline — e.g.
-
#initialize(points) ⇒ Path
constructor
pointsis an Array of [x, y] waypoint pairs in walk order (construction-time, so the pair Arrays are fine here). -
#segment_length(index) ⇒ Object
Length of the segment from waypoint
ito waypointi + 1. -
#x_at(index) ⇒ Object
World coordinates of waypoint
i(0-based), as scalars (no allocation). - #y_at(index) ⇒ Object
Constructor Details
#initialize(points) ⇒ Path
points is an Array of [x, y] waypoint pairs in walk order (construction-time, so
the pair Arrays are fine here). At least two are required — a path with one point
has nowhere to walk.
20 21 22 23 24 25 26 |
# File 'lib/rgame/engine/path.rb', line 20 def initialize(points) raise ArgumentError, 'a Path needs at least two waypoints' if points.length < 2 @coords = points.flatten.freeze @count = points.length @segment_lengths, @length = build_segments end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
15 16 17 |
# File 'lib/rgame/engine/path.rb', line 15 def count @count end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
15 16 17 |
# File 'lib/rgame/engine/path.rb', line 15 def length @length end |
Instance Method Details
#distance_to(x, y) ⇒ Object
Shortest distance from the point (x, y) to the polyline — e.g. how far a spot is from the road, so a tower-defense level can mask placement cells that sit on it. Pure scalar maths, allocation-free.
39 40 41 42 43 44 45 46 |
# File 'lib/rgame/engine/path.rb', line 39 def distance_to(x, y) min = Float::INFINITY (@count - 1).times do |i| d = segment_distance(x, y, x_at(i), y_at(i), x_at(i + 1), y_at(i + 1)) min = d if d < min end min end |
#segment_length(index) ⇒ Object
Length of the segment from waypoint i to waypoint i + 1. There are
count - 1 segments, indexed 0..count-2.
34 |
# File 'lib/rgame/engine/path.rb', line 34 def segment_length(index) = @segment_lengths[index] |
#x_at(index) ⇒ Object
World coordinates of waypoint i (0-based), as scalars (no allocation).
29 |
# File 'lib/rgame/engine/path.rb', line 29 def x_at(index) = @coords[index * 2] |
#y_at(index) ⇒ Object
30 |
# File 'lib/rgame/engine/path.rb', line 30 def y_at(index) = @coords[(index * 2) + 1] |