visualisation.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. def bug_direction(bug, context):
  34. if bug.get_life_points() <= 0:
  35. return dead_ant
  36. try:
  37. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  38. except KeyError:
  39. previous_direction = 14
  40. return directions_ant.get_for_direction(previous_direction)
  41. def red_ant_direction(bug, context):
  42. if bug.get_life_points() <= 0:
  43. return dead_red_ant
  44. try:
  45. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  46. except KeyError:
  47. previous_direction = 14
  48. return directions_red_ant.get_for_direction(previous_direction)
  49. def blue_ant_direction(bug, context):
  50. if bug.get_life_points() <= 0:
  51. return dead_blue_ant
  52. try:
  53. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
  54. except KeyError:
  55. previous_direction = 14
  56. return directions_blue_ant.get_for_direction(previous_direction)
  57. def green_ant_direction(bug, context):
  58. if bug.get_life_points() <= 0:
  59. return dead_green_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_green_ant.get_for_direction(previous_direction)
  65. def for_position(position, objects, context):
  66. # TODO: DEV TMP: refact, etc
  67. eggs = []
  68. for obj in objects:
  69. if isinstance(obj, Egg):
  70. eggs.append(obj)
  71. if len(eggs) == 2:
  72. return (eggc2, eggs)
  73. if len(eggs) == 3:
  74. return (eggc3, eggs)
  75. if len(eggs) == 4:
  76. return (eggc4, eggs)
  77. if len(eggs) > 4:
  78. return (eggc7, eggs)
  79. return (None, [])
  80. visualisation = {
  81. 'window': {},
  82. 'callbacks': {
  83. 'position': for_position
  84. },
  85. 'objects': {
  86. RedAnt: {
  87. 'default': red_ant,
  88. 'callbacks': [red_ant_direction]
  89. },
  90. BlueAnt: {
  91. 'default': blue_ant,
  92. 'callbacks': [blue_ant_direction]
  93. },
  94. GreenAnt: {
  95. 'default': green_ant,
  96. 'callbacks': [green_ant_direction]
  97. },
  98. Ant: {
  99. 'default': ant,
  100. 'callbacks': [bug_direction]
  101. },
  102. Bug: {
  103. 'default': bug,
  104. 'callbacks': [bug_direction]
  105. },
  106. Egg: {
  107. 'default': egg
  108. },
  109. Rock: {
  110. 'default': rock
  111. }
  112. }
  113. }