visualisation.py 8.8KB

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