Class: RuneRb::Game::Map::Direction

Inherits:
Object
  • Object
show all
Extended by:
Constants, System::Log
Defined in:
app/game/map/direction.rb

Overview

An object providing Directional functions to map positions and coordinates.

Since:

  • 0.9.3

Constant Summary

Constants included from Constants

Constants::DEFAULT_POSITION, Constants::DIRECTIONS, Constants::EAST, Constants::NESW, Constants::NONE, Constants::NORTH, Constants::NORTH_EAST, Constants::NORTH_WEST, Constants::REGION_TILE_LENGTH, Constants::SOUTH, Constants::SOUTH_EAST, Constants::SOUTH_WEST, Constants::VIEWABLE_REGION_RADIUS, Constants::VIEWPORT_WIDTH, Constants::WEST, Constants::WNES, Constants::WNES_DIAGONAL, Constants::X_DELTAS, Constants::Y_DELTAS

Class Method Summary collapse

Methods included from System::Log

class_name, err, err!, log, log!, symbolize_file

Class Method Details

.between(first, second) ⇒ Object

The Direction between two Positions

Parameters:

  • first (Position)

    the first Position

  • second (Position)

    the second Position

Since:

  • 0.9.3


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

def between(first, second)
  Direction.from_delta(((second[:y] - first[:y]) <=> 0),
                       ((second[:x] - first[:x]) <=> 0))
end

.diagonal?(direction) ⇒ Boolean

Is the supplied direction diagonal?

Parameters:

  • direction (Symbol)

    the direction.

Returns:

  • (Boolean)

Since:

  • 0.9.3


56
57
58
# File 'app/game/map/direction.rb', line 56

def diagonal?(direction)
  %i[SOUTH_WEST SOUTH_EAST NORTH_WEST NORTH_EAST].include?(direction)
end

.diagonal_directions(direction) ⇒ Object

The 2 directions that make up a specific direction.

Parameters:

  • direction (Symbol)

    the direction.

Since:

  • 0.9.3


44
45
46
47
48
49
50
51
52
# File 'app/game/map/direction.rb', line 44

def diagonal_directions(direction)
  case direction
  when :NORTH_EAST then [NORTH, EAST]
  when :NORTH_WEST then [NORTH, WEST]
  when :SOUTH_EAST then [SOUTH, EAST]
  when :SOUTH_WEST then [SOUTH, WEST]
  else "Unrecognized direction #{direction}!"
  end
end

.from_delta(delta_y, delta_x) ⇒ Object

Returns a direction from deltas between x and y coordinates

Parameters:

  • delta_x (Integer)

    the delta between 2 x coordinates

  • delta_y (Integer)

    the delta between 2 y coordinates

Since:

  • 0.9.3


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/game/map/direction.rb', line 19

def from_delta(delta_y, delta_x)
  case delta_y
  when 1
    case delta_x
    when 1 then NORTH_EAST
    when 0 then NORTH
    when -1 then NORTH_WEST
    end
  when -1
    case delta_x
    when 1 then SOUTH_EAST
    when 0 then SOUTH
    when -1 then SOUTH_WEST
    end
  when 0
    case delta_x
    when 1 then EAST
    when 0 then NONE
    when -1 then WEST
    end
  end
end

.orientation_for(direction) ⇒ Object

Gets the client's orientation for the direction

Parameters:

  • direction (Symbol)

    the direction.

Since:

  • 0.9.3


62
63
64
65
66
67
68
69
70
# File 'app/game/map/direction.rb', line 62

def orientation_for(direction)
  case direction
  when :WEST, :NORTH_WEST then 0
  when :NORTH, :NORTH_EAST then 1
  when :EAST, :SOUTH_EAST then 2
  when :SOUTH, :SOUTH_WEST then 3
  else 'Only a valid direction can have orientation.'
  end
end