Grid.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. '''
  2. Created on 7 nov. 2016
  3. Game Grid
  4. @author: olinox
  5. '''
  6. from core.Cell import Cell
  7. from core.constants import GRID_GEOMETRIES
  8. from core import geometry
  9. from core.pathfinder import pathfinder
  10. class Grid(object):
  11. def __init__(self, geometry, width, height):
  12. self._geometry = None
  13. self.geometry = geometry
  14. self._width = 0
  15. self.width = width
  16. self._height = 0
  17. self.height = height
  18. self._cells = {}
  19. # properties
  20. @property
  21. def geometry(self):
  22. return self._geometry
  23. @geometry.setter
  24. def geometry(self, geometry):
  25. if not geometry in GRID_GEOMETRIES:
  26. raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
  27. self._geometry = geometry
  28. @property
  29. def width(self):
  30. return self._width
  31. @width.setter
  32. def width(self, width):
  33. if not isinstance(width, int) or not width > 0:
  34. raise ValueError("'width' has to be a strictly positive integer")
  35. self._width = width
  36. @property
  37. def height(self):
  38. return self._height
  39. @height.setter
  40. def height(self, height):
  41. if not isinstance(height, int) or not height > 0:
  42. raise ValueError("'width' has to be a strictly positive integer")
  43. self._height = height
  44. def cell(self, coord):
  45. # temporary
  46. try:
  47. return self._cells[coord]
  48. except KeyError:
  49. x, y = coord
  50. cell = Cell(self._geometry, x, y)
  51. self._cells[coord] = cell
  52. return cell
  53. # methods
  54. def cases_number(self):
  55. return self.height * self.width
  56. def in_grid(self, x, y):
  57. """return True if the coordinates are in the grid"""
  58. return (x > 0 and x <= self._width and y > 0 and y <= self._height)
  59. def line(self, x1, y1, x2, y2):
  60. return geometry.gline.line2d(self.geometry, x1, y1, x2, y2)
  61. def line3d(self, x1, y1, z1, x2, y2, z2):
  62. return geometry.gline.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
  63. def zone(self, x, y):
  64. return geometry.gzone.zone(self.geometry, x, y)
  65. def triangle(self, xa, ya, xh, yh, iAngle):
  66. return geometry.gtriangle.triangle(self.geometry, xa, ya, xh, yh, iAngle)
  67. def triangle3d(self, xa, ya, za, xh, yh, zh, iAngle):
  68. return geometry.gtriangle.triangle3d(self.geometry, xa, ya, za, xh, yh, zh, iAngle)
  69. def rect(self, x1, y1, x2, y2):
  70. return geometry.grectangle.rect(x1, y1, x2, y2)
  71. def hollow_rect(self, x1, y1, x2, y2):
  72. return geometry.grectangle.hollow_rect(x1, y1, x2, y2)
  73. def path(self, x1, y1, x2, y2):
  74. return pathfinder.path( self, (x1, y1), (x2,y2) )
  75. class HexGrid(Grid):
  76. def __init__(self, width, height):
  77. Grid.__init__(self, 5, width, height)
  78. class SquareGrid(Grid):
  79. def __init__(self, width, height):
  80. Grid.__init__(self, 4, width, height)