Class: Manufactured::Loot

Inherits:
Object show all
Includes:
Entity::HasCargo, Entity::InSystem
Defined in:
lib/manufactured/loot.rb

Overview

Free floating groups of items (resources/etc) in a Cosmos::Entities::SolarSystem which Ships can retrieve if within collection_distance

Instance Attribute Summary collapse

Attributes included from Entity::HasCargo

#cargo_capacity, #resources, #transfer_distance

Attributes included from Entity::InSystem

#location, #solar_system, #system_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Entity::HasCargo

#add_resource, #can_accept?, #can_transfer?, #cargo_empty?, #cargo_full?, #cargo_quantity, #cargo_space, #remove_resource, #resources_valid?

Methods included from Entity::InSystem

#movement_strategy, #movement_strategy=

Constructor Details

#initialize(args = {}) ⇒ Loot

Loot initializer

Options Hash (args):

  • :id,'id' (String)

    id to assign to the loot

  • :location,'location' (Motel::Location)

    location of the loot in the solar system


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/manufactured/loot.rb', line 26

def initialize(args = {})
  args[:location] =
    Motel::Location.new :coordinates => [0,0,1],
                        :orientation => [1,0,0]  unless args.has_key?(:location) ||
                                                        args.has_key?('location')

  attr_from_args args, :id                   => nil,
                       :resources            =>  [],
                       :location             => nil,
                       :system_id            => nil,
                       :solar_system         => nil,
                       :transfer_distance    =>  25,
                       :cargo_capacity       => 100

  @location.movement_strategy =
    args[:movement_strategy] if args.has_key?(:movement_strategy)
end

Instance Attribute Details

#idObject

Unique string id of the loot


20
21
22
# File 'lib/manufactured/loot.rb', line 20

def id
  @id
end

Class Method Details

.json_create(o) ⇒ Object

Create new loot from json representation


87
88
89
90
# File 'lib/manufactured/loot.rb', line 87

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

Instance Method Details

#alive?Boolean

Just for compatability for now, always return true


65
66
67
# File 'lib/manufactured/loot.rb', line 65

def alive?
  true
end

#to_json(*a) ⇒ Object

Convert loot to json representation and return it


70
71
72
73
74
75
76
77
78
79
# File 'lib/manufactured/loot.rb', line 70

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      {:id => id,
       :location => @location,
       :system_id => @system_id,
       :resources => @resources }
  }.to_json(*a)
end

#to_sObject

Convert loot to human readable string and return it


82
83
84
# File 'lib/manufactured/loot.rb', line 82

def to_s
  "loot-#{@id}"
end

#valid?Boolean

Return boolean indicating if this loot is valid

Tests the various attributes of the Loot, returning true if everything is consistent, else false.

Current tests

  • id is set to a valid (non-empty) string

  • location is set to a Motel::Location

  • location movement strategy is stopped

  • solar system is set to Cosmos::SolarSystem


54
55
56
57
58
59
60
61
62
# File 'lib/manufactured/loot.rb', line 54

def valid?
  !@id.nil? && @id.is_a?(String) && @id != "" &&

  !@location.nil? && @location.is_a?(Motel::Location) &&
   @location.movement_strategy == Motel::MovementStrategies::Stopped.instance &&

  !@system_id.nil? &&
  (@solar_system.nil? || @solar_system.is_a?(Cosmos::Entities::SolarSystem))
end