Class: Cosmos::Entities::JumpGate

Inherits:
Object
  • Object
show all
Includes:
Cosmos::Entity, SystemEntity
Defined in:
lib/cosmos/entities/jump_gate.rb

Overview

Represents a link between two systems.

Reside in a SolarSystem (the jump_gate's parent) at a specified location and references another system (the endpoint). Primarily interacted with by Manufactured::Ship instances who require jump gates to travel inbetween systems

Constant Summary collapse

CHILD_TYPES =
[]
VALIDATE_SIZE =
proc { |s| true }
VALIDATE_COLOR =
proc { |c| true }
RAND_SIZE =
proc { 0        }
RAND_COLOR =
proc { ''       }

Constants included from SystemEntity

SystemEntity::PARENT_TYPE

Instance Attribute Summary collapse

Attributes included from SystemEntity

#color, #size

Attributes included from Cosmos::Entity

#children, #id, #location, #metadata, #name, #parent, #parent_id, #proxy_to

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SystemEntity

#init_system_entity, #system_entity_json, #system_entity_valid?

Methods included from Cosmos::Entity

#accepts_resource?, #add_child, #each_child, #entity_json, #entity_valid?, #has_child?, #has_children?, #init_entity, #movement_strategy=, #remove_child

Constructor Details

#initialize(args = {}) ⇒ JumpGate

Cosmos::JumpGate intializer

Parameters:

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

    hash of options to initialize jump gate with

Options Hash (args):

  • :endpoint,'endpoint' (Cosmos::SolarSystem, String)

    solar_system which jump gate connects to or its name to be looked up in the Registry


50
51
52
53
54
55
56
# File 'lib/cosmos/entities/jump_gate.rb', line 50

def initialize(args = {})
  init_entity(args)
  init_system_entity(args)
  attr_from_args args, :endpoint_id      => nil,
                       :endpoint         => nil,
                       :trigger_distance => 300  # TODO make default configurable
end

Instance Attribute Details

#endpointObject

SolarSystem system which jump gates connects to


33
34
35
# File 'lib/cosmos/entities/jump_gate.rb', line 33

def endpoint
  @endpoint
end

#endpoint_idObject

ID of system which jump gate connects to


30
31
32
# File 'lib/cosmos/entities/jump_gate.rb', line 30

def endpoint_id
  @endpoint_id
end

#trigger_distanceObject

Max distance in any direction around

gate which entities can trigger it

43
44
45
# File 'lib/cosmos/entities/jump_gate.rb', line 43

def trigger_distance
  @trigger_distance
end

Class Method Details

.json_create(o) ⇒ Object

Create new jump_gate from json representation


90
91
92
93
# File 'lib/cosmos/entities/jump_gate.rb', line 90

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

Instance Method Details

#to_json(*a) ⇒ Object

Return json representation of jump gate


80
81
82
83
84
85
86
87
# File 'lib/cosmos/entities/jump_gate.rb', line 80

def to_json(*a)
  { :json_class => self.class.name,
    :data       =>
      {:trigger_distance => @trigger_distance,
       :endpoint_id      => @endpoint_id
      }.merge(entity_json).merge(system_entity_json)
  }.to_json(*a)
end

#to_sObject

Convert jump gate to human readable string and return it


75
76
77
# File 'lib/cosmos/entities/jump_gate.rb', line 75

def to_s
  "jump_gate-#{parent_id}->#{endpoint_id}"
end

#valid?Boolean

Return boolean indicating if this jump gate is valid.

Currently tests

  • base entity and system entity is valid

  • location is not moving

  • endpoint is set to a valid Cosmos::SolarSystem

  • trigger distance is > 0

Returns:

  • (Boolean)

65
66
67
68
69
70
71
72
# File 'lib/cosmos/entities/jump_gate.rb', line 65

def valid?
  entity_valid? && system_entity_valid? &&
  @location.movement_strategy.is_a?(Motel::MovementStrategies::Stopped) &&
  !@endpoint_id.nil? &&
  (@endpoint.nil? || (@endpoint.is_a?(SolarSystem) && @endpoint.valid?)) &&
  # \@solar_system.name != @endpoint.name &&
  @trigger_distance.numeric? && @trigger_distance > 0
end