Grid.py 3.2 KB

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