Module: RuneRb::Game::Entity::Helpers::Movement

Included in:
Mob
Defined in:
app/game/entity/helpers/movement.rb

Overview

Since:

  • 0.9.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#localsHash (readonly)

Returns a map of local mobs and players.

Returns:

  • (Hash)

    a map of local mobs and players

Since:

  • 0.9.3


6
7
8
# File 'app/game/entity/helpers/movement.rb', line 6

def locals
  @locals
end

#movementHash (readonly)

Returns a hash containing the movement type and waypoints.

Returns:

  • (Hash)

    a hash containing the movement type and waypoints.

Since:

  • 0.9.3


3
4
5
# File 'app/game/entity/helpers/movement.rb', line 3

def movement
  @movement
end

#positionHash (readonly)

Returns the current previous positions of the mob in the virtual game map.

Returns:

  • (Hash)

    the current previous positions of the mob in the virtual game map.

Since:

  • 0.9.3


9
10
11
# File 'app/game/entity/helpers/movement.rb', line 9

def position
  @position
end

Instance Method Details

#clear_waypointsObject

Clears the queue

Since:

  • 0.9.3


28
29
30
31
32
# File 'app/game/entity/helpers/movement.rb', line 28

def clear_waypoints
  @movement[:waypoints][:next]&.clear
  @movement[:waypoints][:previous]&.clear
  reset_movement
end

#load_movementObject

Initializes and sets the movement and locals objects.

Since:

  • 0.9.3


12
13
14
15
16
17
18
19
20
# File 'app/game/entity/helpers/movement.rb', line 12

def load_movement
  @movement = { type: :TELEPORT,
                directions: { primary: RuneRb::Game::Map::DIRECTIONS[:NONE],
                              secondary: RuneRb::Game::Map::DIRECTIONS[:NONE],
                              previous: RuneRb::Game::Map::DIRECTIONS[:NORTH] },
                waypoints: { next: [],
                             previous: [] } }
  @locals = { mobs: [], players: [] }
end

#parse_movement(message) ⇒ Object

Attempts to parse movement from a message

Parameters:

Since:

  • 0.9.3


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/game/entity/helpers/movement.rb', line 53

def parse_movement(message)
  log "Received movement packet: #{message.header[:op_code]}" if RuneRb::GLOBAL[:DEBUG]
  length = message.header[:length]
  length -= 14 if message.header[:op_code] == 248

  steps = (length - 5) / 2
  return unless steps.positive?

  path = []
  first_x = message.read(:short, signed: false, mutation: :A, order: :LITTLE)
  steps.times do |itr|
    path[itr] ||= [message.read(:byte, signed: true, mutation: :STD),
                   message.read(:byte, signed: true, mutation: :STD)]
  end

  first_y = message.read(:short, signed: false, mutation: :STD, order: :LITTLE)
  @movement[:running] = message.read(:byte, signed: false, mutation: :C) == 1

  positions = []
  positions << RuneRb::Game::Map::Position.new(first_x, first_y)
  steps.times do |itr|
    positions << RuneRb::Game::Map::Position.new(path[itr][0] + first_x,
                                           path[itr][1] + first_y)
  end

  push_path(positions.flatten.compact) unless positions.empty?
end

#push_path(path) ⇒ Object

Pushes a path to the queue

Parameters:

  • path (Array)

    an array of positions that make up the path

Since:

  • 0.9.3


43
44
45
46
47
48
49
# File 'app/game/entity/helpers/movement.rb', line 43

def push_path(path)
  log "Adding path: #{RuneRb::GLOBAL[:COLOR].yellow(path.inspect)}"
  push_primary_step(path.shift) if @movement[:waypoints][:next].size.zero?
  path.each do |position|
    push_step(position)
  end
end

#reset_movementObject

Resets the movement

Since:

  • 0.9.3


88
89
90
91
92
93
# File 'app/game/entity/helpers/movement.rb', line 88

def reset_movement
  if (@movement[:waypoints][:next].empty? || @movement[:type] == :TELEPORT) && @movement[:type] != :NONE
    @movement[:type] = :STAND
  end
  @movement[:running] = false
end

#teleport(to) ⇒ Object

Teleports the Mob to a new position.

Parameters:

Since:

  • 0.9.3


36
37
38
39
# File 'app/game/entity/helpers/movement.rb', line 36

def teleport(to)
  clear_waypoints
  update(:teleport, to: to)
end

#toggle_runObject

Toggles the running mode for the queue.

Since:

  • 0.9.3


23
24
25
# File 'app/game/entity/helpers/movement.rb', line 23

def toggle_run
  @movement[:running] = true
end

#waypoint_sizeInteger

The number of waypoints left in the movement path

Returns:

  • (Integer)

    the number of steps left.

Since:

  • 0.9.3


83
84
85
# File 'app/game/entity/helpers/movement.rb', line 83

def waypoint_size
  @movement[:waypoints][:next].size
end