spacepaste

  1.  
  2. import warnings
  3. from types import ClassType
  4. from pandac.PandaModules import *
  5. from zc.units.baseunit import BaseUnit, strict_callback, higher_callback, loose_callback
  6. from zc.ai.empty import EmptyAi
  7. from zc.ai.pathfinder import STATE_QUEUED, STATE_FOUND, STATE_NOT_FOUND
  8. from zc.shaders import shader_manipulator
  9. NODE_MOVE_SPEED = 0.5/16
  10. # Unitstats
  11. VIEW_RADIUS = 320
  12. class ZombieFemale(BaseUnit):
  13. """
  14. Basic zombie, female variant
  15. basic unit controller without any ai to govern it
  16. will move and do stuff according to the orders depending on the issuer and type of callback
  17. (strict, higher, loose)
  18. Accepts events:
  19. on_unit_move_order_issued
  20. parameters: issuer, target, wherex, wherey
  21. on_unit_teleport_order_issued
  22. parameters: issuer, target
  23. on_unit_stop_order_issued
  24. parameters: issuer, target
  25. Sends events:
  26. on_unit_moved
  27. Sends: self
  28. on_unit_teleported
  29. Sends: self
  30. on_unit_stopped
  31. Sends: self
  32. """
  33. Models = [
  34. "models/mod.egg",
  35. ]
  36. Textures = [
  37. "textures/text.png",
  38. ]
  39. Animations = {
  40. "idle":("idle01", "idle02"),
  41. "walking-slow":("walkslow01",),
  42. }
  43. def __init__(self, game, renderer, x, y, controller, Ai=None, name=None):
  44. BaseUnit.__init__(self, game, renderer, x, y, controller, Ai, name)
  45. # consts
  46. self.VIEW_RADIUS = VIEW_RADIUS
  47. # semaphors
  48. self._moving = False
  49. self._behaviour = "zombie"
  50. self._type = "zombie_female"
  51. self._actor = False
  52. while self._actor is False:
  53. self._model = self._game.game_values["static_random"].choice(ZombieFemale.Models)
  54. self._texture = self._game._map.f(self._game.game_values["static_random"].choice(ZombieFemale.Textures), "t")
  55. self._actor = None
  56. self._siz = 6.0
  57. self._actor = shader_manipulator.new_instance_with_shaders(self,
  58. self._game._map.f,
  59. self._name,
  60. self._model,
  61. self._game.game_values["static_random"].choice(ZombieFemale.Animations["idle"]),
  62. self._texture,
  63. self._siz)
  64. self.update(all=True)
  65. # base behaviour event registration:
  66. self.regEv("on_unit_move_order_issued", self._move)
  67. self.regEv("on_unit_teleport_order_issued", self._teleport)
  68. @strict_callback
  69. def _move(self, event, issuer, target, wherex, wherey):
  70. if (wherex == self._posx) and (wherey == self._posy):
  71. return True
  72. if self._moving:
  73. self.unschedEv(self._move_unit)
  74. fpath = self._game._pathfinder.find_path(self._move_callback,
  75. (int(self._posx / 16), int(self._posy / 16)),
  76. (int(wherex / 16), int(wherey / 16)),
  77. True
  78. )
  79. fpath._store_x = wherex
  80. fpath._store_y = wherey
  81. if fpath.get_state() != STATE_QUEUED:
  82. if fpath.get_state() == STATE_NOT_FOUND:
  83. self._stop(event, issuer, target)
  84. return True
  85. else:
  86. self._move_command(fpath)
  87. return True
  88. else:
  89. # TODO: Nastavit zombie na dumanie...
  90. return True
  91. def _move_callback(self, fpath, *args):
  92. if fpath.get_state() == STATE_FOUND:
  93. self._move_command(fpath)
  94. else:
  95. self._stop(None, None, None)
  96. def _move_command(self, fpath, wtype="walking-slow"):
  97. self.trigEv("on_unit_move", self)
  98. anim = self._game.game_values["game_random"].choice(ZombieFemale.Animations[wtype])
  99. self.update_anim(anim)
  100. class CT(object):
  101. def __init__(inself):
  102. inself.x = None
  103. inself.y = None
  104. inself.w = None
  105. inself.targetx = fpath._store_x
  106. inself.targety = fpath._store_y
  107. inself.final = False
  108. current_target = CT()
  109. target_list = iter(fpath.get_path())
  110. cnode = target_list.next()
  111. current_target.x, current_target.y = cnode.get_position()
  112. current_target.w = cnode.get_weight()
  113. # TODO: Pridat pametanie si weight... alebo nieco podobne...
  114. delay = 0
  115. if current_target.w > 0:
  116. delay = NODE_MOVE_SPEED * ((1/256.0) * (256-current_target.w))
  117. elif current_target.w < 0:
  118. delay = NODE_MOVE_SPEED / ((1/256.0) * (256-current_target.w))
  119. else:
  120. delay = NODE_MOVE_SPEED
  121. self._angle = [None, None]
  122. self._moving = True # we set up semaphore
  123. self.schedEv(self._move_unit, d=delay, p_kwargs={"current_target":current_target, "target_list":target_list})
  124. def _move_unit(self, dt, current_target, target_list):
  125. cposx = int(self._posx)
  126. cposy = int(self._posy)
  127. if current_target.final:
  128. if (cposx == current_target.targetx) and (cposy == current_target.targety):
  129. # we have arrived at final node!
  130. self._angle = [None, None]
  131. self._stop(None, self._name, None)
  132. # we clear semaphore
  133. self._moving = False
  134. return True
  135. modx, mody, orient = self._get_next_position(cposx, cposy, current_target.targetx, current_target.y)
  136. self._posx = modx
  137. self._posy = mody
  138. self._orient = orient
  139. self.update()
  140. return
  141. if (cposx == current_target.x*16) and (cposy == current_target.y*16):
  142. # we have arrived at the node
  143. self._ai.arrived_at_new_position()
  144. cnode = None
  145. try:
  146. cnode = target_list.next() # we set up new target coordinates
  147. except StopIteration:
  148. current_target.final = True
  149. return
  150. else:
  151. current_target.x, current_target.y = cnode.get_position()
  152. w = cnode.get_weight()
  153. if w != current_target.w:
  154. # TODO: Vylepsit, ale asi tazko, leda animacne!
  155. delay = 0
  156. d = (w + current_target.w) / 2
  157. if d > 0:
  158. delay = NODE_MOVE_SPEED * ((1/256.0) * (256-d))
  159. elif d < 0:
  160. delay = NODE_MOVE_SPEED / ((1/256.0) * (256-d))
  161. else:
  162. delay = NODE_MOVE_SPEED
  163. self._angle = [None, None]
  164. current_target.w = w
  165. self.unschedEv(self._move_unit)
  166. self.schedEv(self._move_unit, d=delay, p_kwargs={"current_target":current_target, "target_list":target_list})
  167. self._angle = [None, None]
  168. modx, mody, orient = self._get_next_position(cposx, cposy, current_target.x*16, current_target.y*16)
  169. self._posx = modx
  170. self._posy = mody
  171. self._orient = orient
  172. self.update()
  173. @strict_callback
  174. def _teleport(self, event, issuer, target, wherex, wherey):
  175. self._actor.setPos(wherex, wherey, self._getheight(wherex, wherey))
  176. self._posx = wherex
  177. self._posy = wherey
  178. self.trigEv("on_unit_teleport", self)
  179. self._ai.arrived_at_new_position()
  180. self._stop(event, issuer, target)
  181. @loose_callback
  182. def _stop(self, event, issuer, target):
  183. self.trigEv("on_unit_stop", self)
  184. anim = self._game.game_values["game_random"].choice(ZombieFemale.Animations["idle"])
  185. self.update_anim(anim)
  186. self.unschedEv("all") # unscheduling any events pending
  187. # we clear semaphores
  188. self._moving = False
  189.