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

Source Code for Package horizons.world.building

  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  import logging 
 23   
 24  from fife import fife 
 25   
 26  import horizons.globals 
 27  from horizons.i18n import gettext as T 
 28  from horizons.util.loaders.actionsetloader import ActionSetLoader 
 29  from horizons.world.ingametype import IngameType 
 30  from horizons.world.production.producer import Producer 
 31   
 32   
33 -class BuildingClass(IngameType):
34 log = logging.getLogger('world.building') 35 36 basepackage = 'horizons.world.building.' 37 classstring = 'Building[{id}]' 38
39 - def __new__(self, db, id, yaml_data):
40 return super().__new__(self, id, yaml_data)
41
42 - def __init__(self, db, id, yaml_data):
43 """ 44 Final loading for the building class. Load a lot of attributes for the building classes 45 @param id: building id. 46 @param db: DbReader 47 """ 48 super().__init__(id, yaml_data) 49 50 self.settler_level = yaml_data['tier'] 51 self.tooltip_text = self._strip_translation_marks(yaml_data['tooltip_text']) 52 self.size = (int(yaml_data['size_x']), int(yaml_data['size_y'])) 53 self.width = self.size[0] 54 self.height = self.size[1] 55 self.inhabitants = int(yaml_data['inhabitants']) 56 self.costs = yaml_data['buildingcosts'] 57 self.running_costs = yaml_data['cost'] 58 self.running_costs_inactive = yaml_data['cost_inactive'] 59 self.has_running_costs = bool(self.running_costs) 60 self.show_status_icons = yaml_data.get('show_status_icons', True) 61 self.translucent = yaml_data.get('translucent', False) 62 # for mines: on which deposit is it buildable 63 self.buildable_on_deposit_type = None 64 try: 65 component_template = self.get_component_template(Producer) 66 self.buildable_on_deposit_type = component_template.get('is_mine_for') 67 except KeyError: 68 pass
69
70 - def __str__(self):
71 return "Building[{id}]({name})".format(id=self.id, name=self.name)
72
73 - def _loadObject(cls):
74 """Loads building from the db. 75 """ 76 cls.log.debug("Loading building %s", cls.id) 77 try: 78 cls._real_object = horizons.globals.fife.engine.getModel().createObject(str(cls.id), 'building') 79 except fife.NameClash: 80 cls.log.debug("Already loaded building %s", cls.id) 81 cls._real_object = horizons.globals.fife.engine.getModel().getObject(str(cls.id), 'building') 82 return 83 all_action_sets = ActionSetLoader.get_sets() 84 85 # cls.action_sets looks like this: {tier1: {set1: None, set2: preview2, ..}, ..} 86 for action_set_list in cls.action_sets.values(): 87 for action_set in action_set_list: # set1, set2, ... 88 for action_id in all_action_sets[action_set]: # idle, move, ... 89 cls._do_load(all_action_sets, action_set, action_id)
90
91 - def _do_load(cls, all_action_sets, action_set, action_id):
92 params = {'id': action_set, 'action': action_id} 93 action = cls._real_object.createAction('{action}_{id}'.format(**params)) 94 fife.ActionVisual.create(action) 95 for rotation in all_action_sets[action_set][action_id]: 96 params['rot'] = rotation 97 assert rotation == 45 or rotation == 135 or rotation == 225 or rotation == 315, "Bad rotation for action_set {id}: {rot} for action: {action}".format(**params) 98 path = '{id}+{action}+{rot}'.format(**params) 99 anim = horizons.globals.fife.animationloader.loadResource(path) 100 action.get2dGfxVisual().addAnimation(int(rotation), anim) 101 action.setDuration(anim.getDuration())
102
103 - def get_tooltip(self):
104 """Returns tooltip text of a building class. 105 ATTENTION: This text is automatically translated when loaded 106 already. DO NOT wrap the return value of this method in T()! 107 @return: string tooltip_text 108 """ 109 # You usually do not need to change anything here when translating 110 tooltip = T("{building}: {description}") 111 return tooltip.format(building=self._name, 112 description=self.tooltip_text)
113