physics.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # coding: utf-8
  2. import typing
  3. from sandbox.tile.simulation.tmx import TileMap
  4. from sandbox.tile.simulation.tmx import TerrainTile
  5. from synergine2_xyz.physics import Matrixes
  6. from synergine2_xyz.physics import MoveCostComputer
  7. from synergine2_xyz.physics import TMXPhysics
  8. from synergine2_xyz.subjects import XYZSubject
  9. if False:
  10. from sandbox.tile.simulation.base import BaseSubject
  11. class TileMoveCostComputer(MoveCostComputer):
  12. def compute_move_cost(
  13. self,
  14. subject: 'BaseSubject',
  15. tile: TerrainTile,
  16. previous_node: str,
  17. next_node: str,
  18. unknown,
  19. ) -> float:
  20. # TODO: Objets/IT qui compute les couts de déplacement
  21. if not tile.property('traversable_by_man'):
  22. # TODO: revoir la lib disjkstar because les mecs traverses quand meme ...
  23. return 100
  24. return 1.0
  25. class TilePhysics(TMXPhysics):
  26. tmx_map_class = TileMap
  27. move_cost_computer_class = TileMoveCostComputer
  28. matrixes_configuration = {
  29. 'visibility': [
  30. 'height',
  31. 'opacity',
  32. ]
  33. }
  34. def get_visibility_obstacle(
  35. self,
  36. subject: XYZSubject,
  37. to_position: typing.Tuple[int, int],
  38. matrixes: Matrixes,
  39. matrix_name: str,
  40. opacity_property_name: str='opacity',
  41. ) -> typing.Union[None, typing.Tuple[int, int]]:
  42. """
  43. Return grid position obstacle if any between given subject and given to_position
  44. :param subject: Subject observer
  45. :param to_position: position to observe
  46. :param matrixes: Matrixes instance
  47. :param matrix_name: matrix name to use
  48. :param opacity_property_name: name of property containing opacity property
  49. :return: obstacle grid position or None if not
  50. """
  51. from_x, from_y = subject.position
  52. path_positions = matrixes.get_path_positions(from_=(from_x, from_y), to=to_position)
  53. path_opacities = matrixes.get_values_for_path(
  54. matrix_name,
  55. path_positions=path_positions,
  56. value_name=opacity_property_name,
  57. )
  58. # FIXME: This algo is not ok, it is for test
  59. actual_opacity = 0
  60. for i, path_opacity in enumerate(path_opacities):
  61. actual_opacity += path_opacity
  62. if actual_opacity >= 100:
  63. return path_positions[i]
  64. return None