Grid.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. '''
  2. Created on 7 nov. 2016
  3. Game Grid
  4. @author: olinox
  5. '''
  6. from core import geometry
  7. from core.Cell import Cell
  8. from core.geometry import gline, gzone, gtriangle, grectangle
  9. from core.pathfinder import pathfinder
  10. class Grid(object):
  11. def __init__(self, grid_shape, width, height):
  12. self._grid_shape = None
  13. self.grid_shape = grid_shape
  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 grid_shape(self):
  22. return self._grid_shape
  23. @grid_shape.setter
  24. def grid_shape(self, grid_shape):
  25. if not grid_shape in geometry.GRID_GEOMETRIES:
  26. raise ValueError("'grid_shape' has to be a value from GRID_GEOMETRIES")
  27. self._grid_shape = grid_shape
  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 gline.line2d(self.grid_shape, x1, y1, x2, y2)
  61. def line3d(self, x1, y1, z1, x2, y2, z2):
  62. return gline.line3d(self.grid_shape, x1, y1, z1, x2, y2, z2)
  63. def zone(self, x, y, radius):
  64. return gzone.zone(self.grid_shape, x, y, radius)
  65. def triangle(self, xa, ya, xh, yh, iAngle):
  66. return gtriangle.triangle(self.grid_shape, xa, ya, xh, yh, iAngle)
  67. def triangle3d(self, xa, ya, za, xh, yh, zh, iAngle):
  68. return gtriangle.triangle3d(self.grid_shape, xa, ya, za, xh, yh, zh, iAngle)
  69. def rect(self, x1, y1, x2, y2):
  70. return grectangle.rect(x1, y1, x2, y2)
  71. def hollow_rect(self, x1, y1, x2, y2):
  72. return 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)