visualisation.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.ant.Ant import Ant
  5. from intelligine.sandbox.colored.BlueAnt import BlueAnt
  6. from intelligine.sandbox.colored.RedAnt import RedAnt
  7. from intelligine.sandbox.colored.GreenAnt import GreenAnt
  8. from intelligine.synergy.object.Rock import Rock
  9. from intelligine.synergy.object.ant.Egg import Egg
  10. from os import getcwd
  11. from intelligine.cst import PREVIOUS_DIRECTION
  12. # TODO: Analyser les procedes ici pour proposer des outils dans le framework
  13. ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  14. dead_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_ant.png')
  15. red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/red_ant.png')
  16. green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/green_ant.png')
  17. blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/blue_ant.png')
  18. dead_red_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_red_ant.png')
  19. dead_green_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_green_ant.png')
  20. dead_blue_ant = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/dead_blue_ant.png')
  21. bug = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/ant.png')
  22. rock = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/rock.png')
  23. egg = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg.png')
  24. eggc2 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c2.png')
  25. eggc3 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c3.png')
  26. eggc4 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c4.png')
  27. eggc5 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c5.png')
  28. eggc7 = PygameImage.from_filepath(getcwd()+'/intelligine/display/pygame/image/egg_c7.png')
  29. directions_ant = DirectionnedImage(ant)
  30. directions_red_ant = DirectionnedImage(red_ant)
  31. directions_blue_ant = DirectionnedImage(blue_ant)
  32. directions_green_ant = DirectionnedImage(green_ant)
  33. # TODO: context donne au callbacks
  34. def bug_direction(bug, context):
  35. if bug.get_life_points() <= 0:
  36. return dead_ant
  37. try:
  38. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  39. except KeyError:
  40. previous_direction = 14
  41. return directions_ant.get_for_direction(previous_direction)
  42. def red_ant_direction(bug, context):
  43. if bug.get_life_points() <= 0:
  44. return dead_red_ant
  45. try:
  46. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  47. except KeyError:
  48. previous_direction = 14
  49. return directions_red_ant.get_for_direction(previous_direction)
  50. def blue_ant_direction(bug, context):
  51. if bug.get_life_points() <= 0:
  52. return dead_blue_ant
  53. try:
  54. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  55. except KeyError:
  56. previous_direction = 14
  57. return directions_blue_ant.get_for_direction(previous_direction)
  58. def green_ant_direction(bug, context):
  59. if bug.get_life_points() <= 0:
  60. return dead_green_ant
  61. try:
  62. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  63. except KeyError:
  64. previous_direction = 14
  65. return directions_green_ant.get_for_direction(previous_direction)
  66. def for_position(position, objects, context):
  67. # TODO: DEV TMP: refact, etc
  68. eggs = []
  69. for obj in objects:
  70. if isinstance(obj, Egg):
  71. eggs.append(obj)
  72. if len(eggs) == 2:
  73. return (eggc2, eggs)
  74. if len(eggs) == 3:
  75. return (eggc3, eggs)
  76. if len(eggs) == 4:
  77. return (eggc4, eggs)
  78. if len(eggs) > 4:
  79. return (eggc7, eggs)
  80. return (None, [])
  81. visualisation = {
  82. 'window': {},
  83. 'callbacks': {
  84. 'position': for_position
  85. },
  86. 'objects': {
  87. RedAnt: {
  88. 'default': red_ant,
  89. 'callbacks': [red_ant_direction]
  90. },
  91. BlueAnt: {
  92. 'default': blue_ant,
  93. 'callbacks': [blue_ant_direction]
  94. },
  95. GreenAnt: {
  96. 'default': green_ant,
  97. 'callbacks': [green_ant_direction]
  98. },
  99. Ant: {
  100. 'default': ant,
  101. 'callbacks': [bug_direction]
  102. },
  103. Bug: {
  104. 'default': bug,
  105. 'callbacks': [bug_direction]
  106. },
  107. Egg: {
  108. 'default': egg
  109. },
  110. Rock: {
  111. 'default': rock
  112. }
  113. }
  114. }