Module: Omega::Client::InSystem

Included in:
Ship, Station
Defined in:
lib/omega/client/entities/cosmos.rb

Overview

Include the InSystem module in classes to define various utility methods to perform system-specific movement operations

Examples:

class Ship
  include Trackable
  include HasLocation
  include InSystem
  entity_type Manufactured::Ship
  get_method "manufactured::get_entity"
end

# issue a server side request to move ship
s = Ship.get('ship1')
s.move_to(:location => Motel::Location.new(:x => 100, :y => 200, :z => -150))

Instance Method Summary collapse

Instance Method Details

#closest(type, args = {}) ⇒ Array<Object>

Return the closest entity of the specified type.

note this will only search entities in the local registry, it does not currently call out to the server to retrieve entities

Parameters:

  • type (Symbol)

    of entity to retrieve (currently accepts :station, :resource)

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

    hash of optional arguments to use in lookup

Options Hash (args):

  • :user_owned (true, false)

    boolean indicating if we should only return entities owned by the logged in user

Returns:

  • (Array<Object>)

    entities in local registry matching criteria


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/omega/client/entities/cosmos.rb', line 149

def closest(type, args = {})
  entities = []
  if(type == :station)
    #user_owned = args[:user_owned] ? lambda { |e| e.user_id == Node.user.id } :
    #                                 lambda { |e| true }
    entities =
      Omega::Client::Station.entities.select { |e|
        e.location.parent_id == self.location.parent_id
      }.#select(&user_owned).
        sort    { |a,b| (self.location - a.location) <=>
                        (self.location - b.location) }

  elsif(type == :resource)
    entities =
      self.solar_system.asteroids.select { |ast|
        ast.resources.find { |rs| rs.quantity > 0 }
      }.flatten.sort { |a,b|
        (self.location - a.location) <=> (self.location - b.location)
      }
  end

  entities
end

#jump_to(system) ⇒ Object

Invoke a server side request to jump to the specified system

Raises the :jumped event on entity

Parameters:


214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/omega/client/entities/cosmos.rb', line 214

def jump_to(system)
  # TODO should we refresh system anyways even if its a SolarSystem instance?
  system =
    node.invoke('cosmos::get_entity',
                'with_id', system) if system.is_a?(String)

  loc    = Motel::Location.new
  loc.update self.location
  loc.parent_id = system.location.id
  RJR::Logger.info "Jumping #{self.entity.id} to #{system}"
  @entity = node.invoke 'manufactured::move_entity', self.entity.id, loc
  self.raise_event(:jumped)
end

#move_to(args, &cb) ⇒ Object

Issue server side call to move entity to specified destination, optionally registering callback to be invoked when it gets there.

note this will register a movement event callback in addition to any ones previously added / added later

Parameters:

  • args (Hash<Symbol,Object>)

    arguments to used to determine destiantion

  • cb (Callable)

    optional callback to be invoked when entity arrives at location

Options Hash (args):

  • :location (Motel::Location)

    exact location to move to

  • :destination (:closest_station, Object)

    destination to move to through which location will be inferred / extracted


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/omega/client/entities/cosmos.rb', line 184

def move_to(args, &cb)
  # TODO ignore move if we're @ destination
  loc = args[:location]
  if args.has_key?(:destination)
    if args[:destination] == :closest_station
      loc = closest(:station).location
    else
      loc = args[:destination].location
    end
  end

  nloc = Motel::Location.new(:parent_id => self.location.parent_id,
                             :x => loc.x, :y => loc.y, :z => loc.z)
  clear_handlers_for :movement
  handle :movement, (self.location - nloc), &cb unless cb.nil?
  RJR::Logger.info "Moving #{self.id} to #{nloc}"
  node.invoke 'manufactured::move_entity', self.id, nloc
end

#solar_systemCosmos::Entities::SolarSystem

Always return latest system


135
136
137
# File 'lib/omega/client/entities/cosmos.rb', line 135

def solar_system
  SolarSystem.cached(self.entity.parent_id)
end

#stop_movingObject

Invoke a server side request to stop movement


204
205
206
207
# File 'lib/omega/client/entities/cosmos.rb', line 204

def stop_moving
  RJR::Logger.info "Stopping movement of #{self.id}"
  node.invoke 'manufactured::stop_entity', self.id
end