Package horizons :: Package world :: Package building :: Module buildingresourcehandler
[hide private]
[frames] | no frames]

Source Code for Module horizons.world.building.buildingresourcehandler

 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  from horizons.messaging import ResourceProduced 
23  from horizons.world.production.producer import Producer 
24  from horizons.world.resourcehandler import ResourceHandler 
25   
26   
27 -class BuildingResourceHandler(ResourceHandler):
28 """A Resourcehandler that is also a building. 29 This class exists because we keep a list of all buildings, that provide something at the island. 30 """
31 - def __init__(self, island, **kwargs):
32 super().__init__(island=island, **kwargs) 33 self.island = island
34
35 - def initialize(self):
36 super().initialize() 37 self.__init()
38
39 - def __init(self):
40 self.island.provider_buildings.append(self) 41 if self.has_component(Producer): 42 self.get_component(Producer).add_activity_changed_listener(self._set_running_costs_to_status) 43 self.get_component(Producer).add_production_finished_listener(self.on_production_finished) 44 self._set_running_costs_to_status(None, self.get_component(Producer).is_active())
45
46 - def load(self, db, worldid):
47 super().load(db, worldid) 48 self.__init()
49
50 - def remove(self):
51 super().remove() 52 self.island.provider_buildings.remove(self) 53 if self.has_component(Producer): 54 self.get_component(Producer).remove_activity_changed_listener(self._set_running_costs_to_status) 55 self.get_component(Producer).remove_production_finished_listener(self.on_production_finished)
56
57 - def on_production_finished(self, caller, resources):
60
61 - def is_valid_tradable_resource(self, resources):
62 """ Checks if the produced resource tradable (can be carried by collectors). 63 """ 64 if not resources or not resources.keys(): 65 return False 66 67 return list(resources.keys())[0] in \ 68 self.island.session.db.get_res(only_tradeable=True, only_inventory=True)
69
70 - def _set_running_costs_to_status(self, caller, activate):
71 currently_active = self.running_costs_active() 72 if (currently_active and not activate) or (not currently_active and activate): 73 self.toggle_costs() 74 self._changed()
75 76
77 -class UnitProducerBuilding(BuildingResourceHandler):
78 """Class for building that produce units. 79 Uses a BuildingResourceHandler additionally to ResourceHandler to enable 80 building specific behavior.""" 81 pass
82