Class: Motel::MovementStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/motel/movement_strategy.rb

Overview

A MovementStrategy is registered with a Location and used by the Registry to update the location's coordinates in accordance to the algorithm and parameters of the strategy.

This is the base class that defines the movement strategy interface, subclasses should be defined to update the location in various manners. Each should implement the #move method to update the location passed to it along with the number of seconds since move was last invoked.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ MovementStrategy

MovementStrategy initializer

Parameters:

  • args (Hash) (defaults to: {})

    hash of options to initialize movement strategy with

Options Hash (args):

  • :step_delay (Float, Integer)

    base step delay which runner will wait before moving entity


27
28
29
# File 'lib/motel/movement_strategy.rb', line 27

def initialize(args = {})
   attr_from_args args, :step_delay => 1
end

Instance Attribute Details

#step_delayObject

The minimum number of seconds the runner should wait before invoking move


21
22
23
# File 'lib/motel/movement_strategy.rb', line 21

def step_delay
  @step_delay
end

Class Method Details

.json_create(o) ⇒ Object

Create new movement strategy from json representation


60
61
62
# File 'lib/motel/movement_strategy.rb', line 60

def self.json_create(o)
  new(o['data'])
end

Instance Method Details

#change?(loc) ⇒ Boolean

Return bool indicating if we should change movement strategy

Returns:

  • (Boolean)

37
38
39
# File 'lib/motel/movement_strategy.rb', line 37

def change?(loc)
  false
end

#move(location, elapsed_seconds) ⇒ Object

Moves the given location, specifying the number of seconds which have elapsed since move was last called


43
44
# File 'lib/motel/movement_strategy.rb', line 43

def move(location, elapsed_seconds)
end

#to_json(*a) ⇒ Object

Convert movement strategy to json representation and return it


52
53
54
55
56
57
# File 'lib/motel/movement_strategy.rb', line 52

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       => { :step_delay => step_delay }
  }.to_json(*a)
end

#to_sObject

Convert movement strategy to human readable string and return it


47
48
49
# File 'lib/motel/movement_strategy.rb', line 47

def to_s
  "movement_strategy-#{self.class.to_s}"
end

#valid?Boolean

Validate movement strategy, default to false, must be subclassed

Returns:

  • (Boolean)

32
33
34
# File 'lib/motel/movement_strategy.rb', line 32

def valid?
  false
end