farmer.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import pygame
  2. from tiles import *
  3. from variables import *
  4. from world import World
  5. from player import *
  6. class Farmer:
  7. def __init__(self, x: int, y: int, world: World, player: Player, display: pygame.display):
  8. """Un fermier, avec ses coordonnées (x, y)"""
  9. self.display = display
  10. self.x: int = x
  11. self.y: int = y
  12. self.world: World = world
  13. self.player = player
  14. self.neighbors_list: list = self.neighbors(self.x, self.y, self.world)
  15. self.origin: Tile = self.find_current_tile()
  16. self.destination: Tile = world.pos_list[0]
  17. def draw(self):
  18. """Affiche le fermier"""
  19. image = pygame.transform.scale(FARMER, (self.world.tile_width, self.world.tile_height))
  20. self.display.blit(image, (self.x - self.world.tile_width // 2, self.y - self.world.tile_height // 2))
  21. def neighbors(self, x: int, y: int, world: World) -> list:
  22. """Prend la position d'une tuile ((x, y), qui n'est pas son centre), et renvoie la position des tuiles adjacentess dans une liste"""
  23. x_centre = x
  24. y_centre = y
  25. res = []
  26. top_tile = (x_centre, y_centre - world.gap - world.tile_height)
  27. bottom_tile = (x_centre, y_centre + world.gap + world.tile_height)
  28. left_high_tile = (x_centre - world.gap - 3 * world.tile_width // 4, y_centre - world.gap // 2 - world.tile_height // 2)
  29. left_bottom_tile = (x_centre - world.gap - 3 * world.tile_width // 4, y_centre + world.gap // 2 + world.tile_height // 2)
  30. right_high_tile = (x_centre + world.gap + 3 * world.tile_width // 4, y_centre - world.gap // 2 - world.tile_height // 2)
  31. right_bottom_tile = (x_centre + world.gap + 3 * world.tile_width // 4, y_centre + world.gap // 2 + world.tile_height // 2)
  32. res.append(top_tile)
  33. res.append(bottom_tile)
  34. res.append(left_high_tile)
  35. res.append(left_bottom_tile)
  36. res.append(right_high_tile)
  37. res.append(right_bottom_tile)
  38. return res
  39. def next_tile(self) -> tuple:
  40. """Prend la tuile à atteindre et renvoie les coordonnées de la tuile sur laquelle le fermier doit aller pour s'en rapprocher le plus"""
  41. res = (0, 0)
  42. x_nest = self.destination["x"]
  43. y_nest = self.destination["y"]
  44. min = WINDOW_WIDTH ** 2 * 2
  45. for tuile in self.neighbors_list:
  46. x, y = tuile
  47. x = abs(x - x_nest)
  48. y = abs(y - y_nest)
  49. hypo = x ** 2 + y ** 2
  50. if min > hypo :
  51. min = hypo
  52. res = tuile
  53. return(res)
  54. def find_current_tile(self):
  55. """Renvoie la tuile sur laquelle se trouve le fermier"""
  56. found = False
  57. i = 0
  58. for tile in self.world.pos_list:
  59. if SPACE > abs(tile["x"] - self.x) and SPACE > abs(tile["y"] - self.y):
  60. found = True
  61. if found:
  62. return tile
  63. else:
  64. print("Aucune tuile trouvée")
  65. def is_at_nest(self) -> bool:
  66. """Renvoie True si le fermier est au nidn False sinon"""
  67. res = False
  68. if self.find_current_tile() == self.world.pos_list[0]:
  69. res = True
  70. return res
  71. def is_at_origin(self) -> bool:
  72. """Renvoie True si le fermier est au nidn False sinon"""
  73. res = False
  74. if self.find_current_tile() == self.origin:
  75. res = True
  76. return res
  77. def change_destination(self) -> None:
  78. """Change la tuile destination du fermier lorsqu'il touche le nid"""
  79. if self.is_at_nest() :
  80. self.destination = self.origin
  81. elif self.is_at_origin():
  82. self.destination = {"x": self.x * 2, "y": self.y * 2}