Class: RuneRb::Game::Map::Position

Inherits:
Object
  • Object
show all
Defined in:
app/game/map/position.rb

Overview

A Position object provides a base tile as well as region and local tiles.

Since:

  • 0.9.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abs_x, abs_y, z = 0) ⇒ Position

Called when a new Position is created.

Parameters:

  • abs_x (Integer)

    the x coordinate.

  • abs_y (Integer)

    the y coordinate.

  • z (Integer) (defaults to: 0)

    the z coordinate.

Since:

  • 0.9.3


11
12
13
# File 'app/game/map/position.rb', line 11

def initialize(abs_x, abs_y, z = 0)
  @coordinates = { x: abs_x, y: abs_y, z: z }
end

Instance Attribute Details

#coordinatesHash (readonly)

Returns a hash wrapping the Position's absolute X and absolute Y coordinates.

Returns:

  • (Hash)

    a hash wrapping the Position's absolute X and absolute Y coordinates.

Since:

  • 0.9.3


5
6
7
# File 'app/game/map/position.rb', line 5

def coordinates
  @coordinates
end

Instance Method Details

#[](coordinate) ⇒ Object

Shorthand coordinate retrieval

Parameters:

  • coordinate (Symbol)

    the coordinate to retrieve (:x, :y, :z)

Since:

  • 0.9.3


94
95
96
# File 'app/game/map/position.rb', line 94

def [](coordinate)
  @coordinates[coordinate]
end

#[]=(coordinate, value) ⇒ Object

Shorthand coordinate assignment

Parameters:

  • coordinate (Symbol)

    the coordinate to assign (:x, :y, :z)

  • value (Integer)

    the value to assign the coordinate to.

Since:

  • 0.9.3


101
102
103
# File 'app/game/map/position.rb', line 101

def []=(coordinate, value)
  @coordinates[coordinate] = value
end

#central_region_xInteger

The X coordinate of the central region for the Position.

Returns:

  • (Integer)

    The x coordinate for the central region for the Position

Since:

  • 0.9.3


41
42
43
# File 'app/game/map/position.rb', line 41

def central_region_x
  @coordinates[:x] / 8
end

#central_region_yInteger

The Y coordinate of the central region for the Position.

Returns:

  • (Integer)

    The y coordinate of the central region for the position.

Since:

  • 0.9.3


47
48
49
# File 'app/game/map/position.rb', line 47

def central_region_y
  @coordinates[:y] / 8
end

#distance_to(other) ⇒ Object

The distance between the position and another

Parameters:

  • other (Position)

    the other position

Since:

  • 0.9.3


116
117
118
119
120
# File 'app/game/map/position.rb', line 116

def distance_to(other)
  delta_x = @coordinates[:x] - other[:x]
  delta_y = @coordinates[:y] - other[:y]
  Math.sqrt(delta_x * delta_x + delta_y * delta_y).ceil
end

#eql?(other) ⇒ Boolean

Compares this position to another

Parameters:

  • other (Position)

    the other Position

Returns:

  • (Boolean)

Since:

  • 0.9.3


17
18
19
20
21
# File 'app/game/map/position.rb', line 17

def eql?(other)
  return false unless other.is_a? Position

  @coordinates[:x] == other[:x] && @coordinates[:y] == other[:y] && @coordinates[:z] == other[:z]
end

#in_view?(other) ⇒ Boolean

Checks if other is in view of the Position

Parameters:

  • other (Position)

    the position that may or may not be in view

Returns:

  • (Boolean)

    is the distance between the Position and other greater less than 16

Since:

  • 0.9.3


86
87
88
89
90
# File 'app/game/map/position.rb', line 86

def in_view?(other)
  delta_x = @coordinates[:x] - other[:x]
  delta_y = @coordinates[:y] - other[:y]
  delta_x <= 14 && delta_x >= -15 && delta_y <= 14 && delta_y >= -15
end

#inspectObject

Since:

  • 0.9.3


122
123
124
# File 'app/game/map/position.rb', line 122

def inspect
  "[AbsX: #{@coordinates[:x]}, AbsY: #{@coordinates[:y]}, AbsZ: #{@coordinates[:z]}] || [LocX: #{local_x}, LocY: #{local_y}] || [RegX: #{central_region_x}, RegY: #{central_region_y}]"
end

#local_x(base = self) ⇒ Integer

The local x coordinate inside the region of base

Parameters:

  • base (Position) (defaults to: self)

    the base position

Returns:

  • (Integer)

    the local x coordinate relative to the base parameter

Since:

  • 0.9.3


66
67
68
# File 'app/game/map/position.rb', line 66

def local_x(base = self)
  @coordinates[:x] - base.top_left_region_x * 8
end

#local_y(base = self) ⇒ Integer

The local y coordinate inside the region of base

Parameters:

  • base (Position) (defaults to: self)

    the base position

Returns:

  • (Integer)

    the local y coordinate relative to the base parameter

Since:

  • 0.9.3


73
74
75
# File 'app/game/map/position.rb', line 73

def local_y(base = self)
  @coordinates[:y] - base.top_left_region_y * 8
end

#longest_delta(other) ⇒ Integer

The longest horizontal or vertical delta between the positions.

Parameters:

  • other (Position)

    the other position

Returns:

  • (Integer)

    the longest horizontal or vertical delta between the position.

Since:

  • 0.9.3


108
109
110
111
112
# File 'app/game/map/position.rb', line 108

def longest_delta(other)
  delta_x = @coordinates[:x] - other[:x]
  delta_y = @coordinates[:y] - other[:y]
  [delta_x, delta_y].max || delta_x || 0
end

#move(x_amount, y_amount, z_amount = 0) ⇒ Object

Moves the position by specified amounts

Since:

  • 0.9.3


32
33
34
35
36
37
# File 'app/game/map/position.rb', line 32

def move(x_amount, y_amount, z_amount = 0)
  @coordinates[:x] += x_amount
  @coordinates[:y] += y_amount
  @coordinates[:z] += z_amount
  self
end

#regional(position = self) ⇒ Object

Gets regional coordinates for a Position

Parameters:

  • position (Position) (defaults to: self)

    the Position to get regional coordinates for.

Since:

  • 0.9.3


79
80
81
# File 'app/game/map/position.rb', line 79

def regional(position = self)
  Regional.from_position(position)
end

#to(other) ⇒ Object

Updates the Position instance to that of other

Parameters:

  • other (Position)

    the other Position

Since:

  • 0.9.3


25
26
27
28
29
# File 'app/game/map/position.rb', line 25

def to(other)
  @coordinates[:x] = other[:x]
  @coordinates[:y] = other[:y]
  @coordinates[:z] = other[:z]
end

#top_left_region_xInteger

The X coordinate of the region this position is in.

Returns:

  • (Integer)

    The X coordinate of the region this position is in.

Since:

  • 0.9.3


53
54
55
# File 'app/game/map/position.rb', line 53

def top_left_region_x
  central_region_x - 6
end

#top_left_region_yInteger

The Y coordinate the region this position is in.

Returns:

  • (Integer)

    The Y coordinate of the region this position is in.

Since:

  • 0.9.3


59
60
61
# File 'app/game/map/position.rb', line 59

def top_left_region_y
  central_region_y - 6
end