PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/worst_case/worst_case/entities.py

http://worst-case.googlecode.com/
Python | 91 lines | 65 code | 21 blank | 5 comment | 5 complexity | afe1ae36aea4dbc6664062703577793e MD5 | raw file
  1. import vec2
  2. import gfx
  3. ANIMATION_RUN = 0, (0, 1, 2), 1
  4. ANIMATION_WORK = 1, (0, 1, 2, 1), 1
  5. ANIMATION_DEATH = 2, (0, 1, 2), 0
  6. ANIMATION_HELICOPTER = 0, (0, 1, 2), 1
  7. ANIMATION_WATER_FROM_HELICOPTER = 0, (0, 1, 2), 1
  8. class SingleWorker(object):
  9. def __init__(self, spriteSheet, pos):
  10. self.anim = gfx.AnimatedSprite(spriteSheet)
  11. self.pos = vec2.Vec2(pos)
  12. self.target = vec2.Vec2(pos)
  13. self.isDead = False
  14. self.anim.set_position(self.pos, 0)
  15. def tick(self, surface):
  16. # determine facing:
  17. facing = self.pos.x < self.target.x
  18. if self.isDead:
  19. # worker is dead:
  20. self.anim.set_animation(*ANIMATION_DEATH)
  21. facing = None
  22. elif self.pos == self.target:
  23. # worker is on target pos:
  24. self.anim.set_animation(*ANIMATION_WORK)
  25. facing = None
  26. elif self.pos.distance(self.target) <= 1:
  27. # worker is moving to target pos (and reaching it this turn):
  28. self.anim.set_animation(*ANIMATION_WORK)
  29. self.pos = self.target
  30. else:
  31. # worker is moving to target pos (and reaching it this turn):
  32. self.anim.set_animation(*ANIMATION_RUN)
  33. self.pos += (self.target - self.pos).setlength(1)
  34. intpos = int(self.pos.x), int(self.pos.y)
  35. self.anim.set_position(intpos, facing)
  36. self.anim.draw_frame(surface)
  37. def move_to(self, target):
  38. self.target = vec2.Vec2(target)
  39. def die(self):
  40. self.isDead = True
  41. self.target = self.pos # the dead go nowhere anymore
  42. class Helicopter(object):
  43. def __init__(self, spriteSheet, waterSpriteSheet, pos):
  44. self.spriteSheet = spriteSheet
  45. self.anim = gfx.AnimatedSprite(spriteSheet)
  46. self.anim.set_animation(*ANIMATION_HELICOPTER)
  47. self.anim.set_position(pos, 0)
  48. self.anim.frameDelay = 6
  49. self.pos = pos
  50. self.waterAnim = gfx.AnimatedSprite(waterSpriteSheet)
  51. self.waterAnim.set_animation(*ANIMATION_WATER_FROM_HELICOPTER)
  52. def tick(self, surface):
  53. x, y = self.pos
  54. self.anim.set_position((x, y), 0)
  55. if x > 100:
  56. xWater = x + 87 -223 - 5 # these offsets are based on image layouts
  57. yWater = y + 227 -5
  58. self.waterAnim.set_position((xWater, yWater), 0)
  59. self.waterAnim.draw_frame(surface)
  60. self.anim.draw_frame(surface)
  61. self.pos = ((x+1) % 800, y)
  62. class ReactorBuilding(object):
  63. def __init__(self, images, pos):
  64. self.images = images
  65. self.state = 0
  66. self.pos = pos
  67. def set_state(self, state):
  68. self.state = state
  69. def draw(self, surface):
  70. surface.blit(self.images[self.state], self.pos)