visualisation.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from xyworld.display.object.pygame.PygameImage import PygameImage
  2. from xyworld.display.object.pygame.DirectionnedImage import DirectionnedImage
  3. from intelligine.synergy.object.Bug import Bug
  4. from intelligine.synergy.object.Food import Food
  5. from intelligine.synergy.object.ant.Ant import Ant
  6. from intelligine.sandbox.colored.BlueAnt import BlueAnt
  7. from intelligine.sandbox.colored.RedAnt import RedAnt
  8. from intelligine.sandbox.colored.GreenAnt import GreenAnt
  9. from intelligine.synergy.object.Rock import Rock
  10. from intelligine.synergy.object.ant.Egg import Egg
  11. from os import getcwd
  12. from intelligine.cst import PREVIOUS_DIRECTION
  13. # TODO: Analyser les procedes ici pour proposer des outils dans le framework
  14. ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  15. food = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/food.png')
  16. dead_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_ant.png')
  17. red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/red_ant.png')
  18. green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/green_ant.png')
  19. blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/blue_ant.png')
  20. dead_red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_red_ant.png')
  21. dead_green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_green_ant.png')
  22. dead_blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_blue_ant.png')
  23. bug = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  24. rock = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/rock.png')
  25. egg = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg.png')
  26. eggc2 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c2.png')
  27. eggc3 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c3.png')
  28. eggc4 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c4.png')
  29. eggc5 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c5.png')
  30. eggc7 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c7.png')
  31. from intelligine.synergy.object.ant.PheromonExploration import PheromonExploration
  32. from intelligine.synergy.object.ant.PheromonHome import PheromonHome
  33. phee = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/phee.png')
  34. pheh = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/pheh.png')
  35. dir_phee = DirectionnedImage(phee)
  36. dir_pheh = DirectionnedImage(pheh)
  37. directions_ant = DirectionnedImage(ant)
  38. directions_red_ant = DirectionnedImage(red_ant)
  39. directions_blue_ant = DirectionnedImage(blue_ant)
  40. directions_green_ant = DirectionnedImage(green_ant)
  41. def phee_direction(phee, context):
  42. return dir_phee.get_for_direction(phee.get_direction())
  43. def pheh_direction(pheh, context):
  44. return dir_pheh.get_for_direction(pheh.get_direction())
  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. 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. def blue_ant_direction(bug, context):
  62. if bug.get_life_points() <= 0:
  63. return dead_blue_ant
  64. try:
  65. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  66. except KeyError:
  67. previous_direction = 14
  68. return directions_blue_ant.get_for_direction(previous_direction)
  69. def green_ant_direction(bug, context):
  70. if bug.get_life_points() <= 0:
  71. return dead_green_ant
  72. try:
  73. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  74. except KeyError:
  75. previous_direction = 14
  76. return directions_green_ant.get_for_direction(previous_direction)
  77. def for_position(position, objects, context):
  78. # TODO: DEV TMP: refact, etc
  79. eggs = []
  80. for obj in objects:
  81. if isinstance(obj, Egg):
  82. eggs.append(obj)
  83. if len(eggs) == 2:
  84. return (eggc2, eggs)
  85. if len(eggs) == 3:
  86. return (eggc3, eggs)
  87. if len(eggs) == 4:
  88. return (eggc4, eggs)
  89. if len(eggs) > 4:
  90. return (eggc7, eggs)
  91. return (None, [])
  92. visualisation = {
  93. 'window': {},
  94. 'callbacks': {
  95. 'position': for_position
  96. },
  97. 'objects': {
  98. RedAnt: {
  99. 'default': red_ant,
  100. 'callbacks': [red_ant_direction]
  101. },
  102. BlueAnt: {
  103. 'default': blue_ant,
  104. 'callbacks': [blue_ant_direction]
  105. },
  106. GreenAnt: {
  107. 'default': green_ant,
  108. 'callbacks': [green_ant_direction]
  109. },
  110. Ant: {
  111. 'default': ant,
  112. 'callbacks': [bug_direction]
  113. },
  114. Bug: {
  115. 'default': bug,
  116. 'callbacks': [bug_direction]
  117. },
  118. Egg: {
  119. 'default': egg
  120. },
  121. Rock: {
  122. 'default': rock
  123. },
  124. Food: {
  125. 'default': food
  126. },
  127. PheromonExploration: {
  128. 'default': phee,
  129. 'callbacks': [phee_direction]
  130. },
  131. PheromonHome: {
  132. 'default': pheh,
  133. 'callbacks': [pheh_direction]
  134. },
  135. }
  136. }