Class: Cosmos::Entities::Asteroid

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

Overview

en.wikipedia.org/wiki/Asteroid

Cosmos entity residing in a solar system, may be associated with resources through ResourceSource. Primarily interacted with by Manufactured::Ship to mine the contained resources.

Constant Summary collapse

CHILD_TYPES =
[]
VALIDATE_SIZE =
proc { |s| (10...20).include?(s) }
VALIDATE_COLOR =
proc { |c| c =~ /^[a-fA-F0-9]{6}$/ }
RAND_SIZE =
proc { rand(10) + 10               }
RAND_COLOR =
proc { "%06x" % (rand * 0xffffff)  }

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

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

Constructor Details

#initialize(args = {}) ⇒ Asteroid

Cosmos::Asteroid intializer


31
32
33
34
35
36
# File 'lib/cosmos/entities/asteroid.rb', line 31

def initialize(args = {})
  init_entity(args)
  init_system_entity(args)

  attr_from_args args, :resources => []
end

Instance Attribute Details

#resourcesObject

Returns the value of attribute resources


20
21
22
# File 'lib/cosmos/entities/asteroid.rb', line 20

def resources
  @resources
end

Class Method Details

.json_create(o) ⇒ Object

Create new asteroid from json representation


88
89
90
91
# File 'lib/cosmos/entities/asteroid.rb', line 88

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

Instance Method Details

#accepts_resource?(res) ⇒ Boolean

Return boolean indicating if this asteroid can accept the specified resource.

TODO right now indiscremenantly accepts all valid resources, make this more selective

Returns:

  • (Boolean)

52
53
54
# File 'lib/cosmos/entities/asteroid.rb', line 52

def accepts_resource?(res)
  res.valid?
end

#set_resource(res) ⇒ Object

Set resource locally


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cosmos/entities/asteroid.rb', line 57

def set_resource(res)
  r = @resources.find { |r| r.material_id == res.material_id }
  if r
    # simply update quantity
    if res.quantity > 0
      r.quantity = res.quantity

    # delete resource
    else
      @resources.delete(r)
    end

    return r
  end

  # add resource
  res.entity = self
  @resources << res
  return res
end

#to_json(*a) ⇒ Object

Return json representation of asteroid


79
80
81
82
83
84
85
# File 'lib/cosmos/entities/asteroid.rb', line 79

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

#valid?Boolean

Return boolean indicating if this asteroid is valid.

Currently tests

  • base entity and system entity is valid

  • location is not moving

Returns:

  • (Boolean)

43
44
45
46
47
# File 'lib/cosmos/entities/asteroid.rb', line 43

def valid?
  entity_valid? && system_entity_valid? &&
  @location.movement_strategy.is_a?(Motel::MovementStrategies::Stopped) &&
  @resources.all? { |r| r.valid? }
end