visualisation.py 8.3KB

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