visualisation.py 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import pygame
  2. from intelligine.synergy.Colony import Colony
  3. from intelligine.synergy.Rocks import Rocks
  4. from intelligine.synergy.object.StockedFood import StockedFood
  5. from synergine.synergy.Simulation import Simulation
  6. from synergine_xyz.display.PygameImageRotate import PygameImageRotate
  7. from synergine_xyz.display.PygameVisualisation import PygameVisualisation
  8. from synergine_xyz.display.object.pygame.PygameImage import PygameImage
  9. from intelligine.synergy.object.Food import Food
  10. from intelligine.synergy.object.ant.Ant import Ant
  11. from intelligine.synergy.object.Rock import Rock
  12. from intelligine.synergy.object.ant.Egg import Egg
  13. from os import getcwd
  14. from synergine_xyz.cst import PREVIOUS_DIRECTION
  15. from synergine_xyz.tmx.TileMapConnector import TileMapConnector
  16. SURFACE_PHEROMONE_HOME = 'pheromone_home'
  17. SURFACE_PHEROMONE_EXPLORATION = 'pheromone_exploration'
  18. # TODO: Analyser les procedes ici pour proposer des outils dans le framework
  19. # ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  20. # food = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/food.png')
  21. # hole = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/hole.png')
  22. # dead_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_ant.png')
  23. # red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/red_ant.png')
  24. # green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/green_ant.png')
  25. # blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/blue_ant.png')
  26. # dead_red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_red_ant.png')
  27. # dead_green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_green_ant.png')
  28. # dead_blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_blue_ant.png')
  29. # bug = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  30. # rock = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/rock.png')
  31. egg = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg.png')
  32. eggc2 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c2.png')
  33. eggc3 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c3.png')
  34. eggc4 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c4.png')
  35. eggc5 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c5.png')
  36. eggc7 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c7.png')
  37. phee = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/phee.png')
  38. pheh = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/pheh.png')
  39. #
  40. # directions_ant = DirectionnedImage(ant)
  41. # directions_red_ant = DirectionnedImage(red_ant)
  42. # directions_blue_ant = DirectionnedImage(blue_ant)
  43. # directions_green_ant = DirectionnedImage(green_ant)
  44. #
  45. # def bug_direction(bug, context):
  46. # if bug.get_life_points() <= 0:
  47. # return dead_ant
  48. # try:
  49. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  50. # except KeyError:
  51. # previous_direction = 14
  52. # return directions_ant.get_for_direction(previous_direction)
  53. #
  54. # def red_ant_direction(bug, context):
  55. # if bug.get_life_points() <= 0:
  56. # return dead_red_ant
  57. # try:
  58. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  59. # except KeyError:
  60. # previous_direction = 14
  61. # return directions_red_ant.get_for_direction(previous_direction)
  62. #
  63. # def blue_ant_direction(bug, context):
  64. # if bug.get_life_points() <= 0:
  65. # return dead_blue_ant
  66. # try:
  67. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  68. # except KeyError:
  69. # previous_direction = 14
  70. # return directions_blue_ant.get_for_direction(previous_direction)
  71. #
  72. # def green_ant_direction(bug, context):
  73. # if bug.get_life_points() <= 0:
  74. # return dead_green_ant
  75. # try:
  76. # previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  77. # except KeyError:
  78. # previous_direction = 14
  79. # return directions_green_ant.get_for_direction(previous_direction)
  80. #
  81. #
  82. def for_position(position, objects, context):
  83. # TODO: DEV TMP: refact, etc
  84. eggs = []
  85. for obj in objects:
  86. if isinstance(obj, Egg):
  87. eggs.append(obj)
  88. if len(eggs) == 2:
  89. return (eggc2, eggs)
  90. if len(eggs) == 3:
  91. return (eggc3, eggs)
  92. if len(eggs) == 4:
  93. return (eggc4, eggs)
  94. if len(eggs) > 4:
  95. return (eggc7, eggs)
  96. return (None, [])
  97. #
  98. # visualisation = {
  99. # 'window': {},
  100. # 'callbacks': {
  101. # 'position': for_position
  102. # },
  103. # 'surfaces': {
  104. # SURFACE_PHEROMONE_EXPLORATION: {
  105. # 'default': phee,
  106. # 'callbacks': []
  107. # },
  108. # SURFACE_PHEROMONE_HOME: {
  109. # 'default': pheh,
  110. # 'callbacks': []
  111. # },
  112. # },
  113. # 'objects': {
  114. # RedAnt: {
  115. # 'default': red_ant,
  116. # 'callbacks': [red_ant_direction]
  117. # },
  118. # BlueAnt: {
  119. # 'default': blue_ant,
  120. # 'callbacks': [blue_ant_direction]
  121. # },
  122. # GreenAnt: {
  123. # 'default': green_ant,
  124. # 'callbacks': [green_ant_direction]
  125. # },
  126. # Ant: {
  127. # 'default': ant,
  128. # 'callbacks': [bug_direction]
  129. # },
  130. # Bug: {
  131. # 'default': bug,
  132. # 'callbacks': [bug_direction]
  133. # },
  134. # Egg: {
  135. # 'default': egg
  136. # },
  137. # Rock: {
  138. # 'default': rock
  139. # },
  140. # Food: {
  141. # 'default': food
  142. # },
  143. # Hole: {
  144. # 'default': hole
  145. # }
  146. # }
  147. # }
  148. #############################
  149. # Behind, new
  150. #############################
  151. map_config = {
  152. 'simulation': {
  153. 'base': Simulation
  154. },
  155. 'collection': {
  156. 'ant': Colony,
  157. 'env': Rocks # TODO: Nom plus generique
  158. },
  159. 'object': {
  160. 'ant': Ant,
  161. 'egg': Egg,
  162. 'rock': Rock,
  163. 'food': Food,
  164. 'stocked_food': StockedFood
  165. }
  166. }
  167. image_rotate = PygameImageRotate()
  168. def dead_ant_callback_container(map_connector, production_class):
  169. tile_set_id = map_connector.get_dynamic_classes().get_production_class_collection_id(production_class)
  170. pil_image = map_connector.extract_image_with_class(tile_set_id, 'dead_ant')
  171. image_bytes = pil_image.tobytes()
  172. pygame_surface = pygame.image.fromstring(image_bytes, pil_image.size, pil_image.mode)
  173. pygame_image = PygameImage(pygame_surface)
  174. def dead_ant_callback(obj, context):
  175. # TODO: obj dans le col ALIVE truc comme ça
  176. if obj.get_life_points() <= 0:
  177. return pygame_image
  178. # TODO: raise, ou dans objectVisual.get_visual tests si bool plutot que is
  179. return False
  180. return dead_ant_callback
  181. def ant_direction_modifier(obj, context, visual):
  182. try:
  183. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, obj.get_id())
  184. except KeyError:
  185. previous_direction = 14
  186. return image_rotate.get_for_direction(visual, previous_direction)
  187. def get_standard_extract_from_map(map_file_path, map_config):
  188. map_connector = TileMapConnector.from_file(map_file_path, dict(map_config))
  189. visualisation = PygameVisualisation.get_default_visualisation()
  190. simulations = map_connector.create_simulations()
  191. visualizer = PygameVisualisation(visualisation)
  192. objects_images = map_connector.extract_objects_images()
  193. visualizer.update_objects_images(objects_images)
  194. map_connector.add_object_callback_to_visualisation(visualizer, [Ant], dead_ant_callback_container)
  195. ant_production_classes = map_connector.get_dynamic_classes().get_production_classes(Ant)
  196. for ant_production_class in ant_production_classes:
  197. visualizer.add_modifier(ant_production_class, ant_direction_modifier)
  198. visualisation.update({
  199. 'callbacks': {
  200. 'position': for_position
  201. },
  202. 'surfaces': {
  203. SURFACE_PHEROMONE_EXPLORATION: {
  204. 'default': phee,
  205. 'callbacks': []
  206. },
  207. SURFACE_PHEROMONE_HOME: {
  208. 'default': pheh,
  209. 'callbacks': []
  210. },
  211. }
  212. })
  213. return simulations, visualisation