Package horizons :: Package component :: Module collectingcomponent
[hide private]
[frames] | no frames]

Source Code for Module horizons.component.collectingcomponent

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22   
 23  from horizons import entities 
 24  from horizons.component import Component 
 25  from horizons.util.pathfinding.pathnodes import ConsumerBuildingPathNodes 
 26   
 27   
28 -class CollectingComponent(Component):
29 """The CollectingBuilding class represents a object that uses collectors 30 to collect resources from other ResourceHolder objects. It is used to 31 organize Collector instances. 32 NOTE: Anything inheriting from this class must also inherit from the 33 Building class. 34 35 Base class for most producing/collecting buildings. 36 """ 37 38 NAME = 'CollectingComponent' 39 40 ## INIT/DESTRUCT
41 - def __init__(self, collectors):
42 super().__init__() 43 self.__collector_templates = collectors
44
45 - def initialize(self):
46 self.__init() 47 self.create_collector(self.__collector_templates)
48
49 - def __init(self):
50 """Part of initiation that __init__() and load() share""" 51 # list that holds the collectors that belong to this building. 52 self.__collectors = [] 53 54 self.path_nodes = ConsumerBuildingPathNodes(self.instance)
55
56 - def create_collector(self, collectors):
57 """Creates collectors for building according to db.""" 58 for collector_class, count in collectors.items(): 59 for i in range(0, count): 60 self.add_collector(collector_class)
61
62 - def add_collector(self, collector_class):
63 """Creates a collector and adds it to this building. 64 @param collector_class: unit class of collector to create 65 """ 66 collector = entities.Entities.units[collector_class](self.instance, session=self.session, owner=self.instance.owner) 67 collector.initialize()
68
69 - def remove(self):
70 # remove every non-ship collectors (those are independent) 71 for collector in self.__collectors[:]: 72 if not collector.is_ship: 73 collector.remove() 74 else: 75 collector.decouple_from_home_building() 76 #TODO remove the remove call() #2123 77 collector.remove() 78 assert not [c for c in self.__collectors] 79 super().remove() 80 self.__collectors = None 81 self.path_nodes = None
82
83 - def save(self, db):
84 super().save(db) 85 for collector in self.__collectors: 86 # collectors, that are ship (e.g. fisher ship) are viewed as independent 87 # units, and therefore managed by world. This is justified, since they survive 88 # the removal of their assigned fisher hut, and therefore require their own 89 # saving mechanism 90 if not collector.is_ship: 91 collector.save(db)
92
93 - def load(self, db, worldid):
94 super().load(db, worldid) 95 self.__init()
96 97 ## INTERFACE
98 - def add_local_collector(self, collector):
99 assert collector not in self.__collectors 100 self.__collectors.append(collector)
101
102 - def remove_local_collector(self, collector):
103 self.__collectors.remove(collector)
104
105 - def get_local_collectors(self):
106 return self.__collectors
107