Package horizons :: Package world :: Package units :: Module groundunit
[hide private]
[frames] | no frames]

Source Code for Module horizons.world.units.groundunit

  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 weakref 
 23   
 24  from horizons.component.selectablecomponent import SelectableComponent 
 25  from horizons.constants import GAME_SPEED, WEAPONS 
 26  from horizons.util.pathfinding import PathBlockedError 
 27  from horizons.util.pathfinding.pather import SoldierPather 
 28  from horizons.world.units.weaponholder import MovingWeaponHolder 
 29   
 30  from .unit import Unit 
 31   
 32   
33 -class GroundUnit(Unit):
34 """Class representing ground unit 35 @param x: int x position 36 @param y: int y position 37 """ 38 # TODO: 39 # set these tabs in yaml as soon as there are ground units 40 # tabs = (GroundUnitOverviewTab,) 41 # enemy_tabs = (EnemyShipOverviewTab,) 42 43 pather_class = SoldierPather 44 health_bar_y = -70 45
46 - def __init__(self, x, y, **kwargs):
47 super().__init__(x=x, y=y, **kwargs) 48 self.session.world.ground_units.append(self) 49 self.session.world.ground_unit_map[self.position.to_tuple()] = weakref.ref(self)
50
51 - def remove(self):
52 super().remove() 53 self.session.world.ground_units.remove(self) 54 self.session.view.discard_change_listener(self.draw_health) 55 del self.session.world.ground_unit_map[self.position.to_tuple()]
56
57 - def _move_tick(self, resume=False):
58 del self.session.world.ground_unit_map[self.position.to_tuple()] 59 60 try: 61 super()._move_tick(resume) 62 except PathBlockedError: 63 if resume: 64 self.session.world.ground_unit_map[self.position.to_tuple()] = weakref.ref(self) 65 raise 66 67 self.session.world.ground_unit_map[self.position.to_tuple()] = weakref.ref(self) 68 self.session.world.ground_unit_map[self._next_target.to_tuple()] = weakref.ref(self)
69
70 - def load(self, db, worldid):
71 super().load(db, worldid) 72 73 # register unit in world 74 self.session.world.ground_units.append(self) 75 self.session.world.ground_unit_map[self.position.to_tuple()] = weakref.ref(self)
76 77
78 -class FightingGroundUnit(MovingWeaponHolder, GroundUnit):
79 """Weapon Holder Ground Unit"""
80 - def __init__(self, x, y, **kwargs):
81 super().__init__(x=x, y=y, **kwargs) 82 #NOTE weapons 83 self.add_weapon_to_storage(WEAPONS.SWORD) 84 self.add_weapon_to_storage(WEAPONS.CANNON) 85 names = self.session.db("SELECT name FROM groundunitnames") 86 # We need unicode strings as the name is displayed on screen. 87 self.name = [str(x[0], 'utf-8') for x in names]
88
89 - def go(self, x, y):
90 self.get_component(SelectableComponent).go(x, y) 91 self.stop_attack()
92
93 - def act_attack(self, dest):
94 """ 95 Rotates to target and acts correctly 96 """ 97 self.stop_for(GAME_SPEED.TICKS_PER_SECOND * 2) 98 facing_location = self._instance.getFacingLocation() 99 facing_coords = facing_location.getMapCoordinates() 100 facing_coords.x = dest.x 101 facing_coords.y = dest.y 102 facing_location.setMapCoordinates(facing_coords) 103 self._instance.setFacingLocation(facing_location) 104 105 if dest.distance(self.position) <= 1: 106 action = 'melee' 107 else: 108 action = 'ranged' 109 110 self.act('attack_{}'.format(action), facing_location, repeating=False) 111 self._action = 'idle'
112