tilemap.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from typing import cast, Any, Optional
  17. from flexlay.tilemap_layer import TilemapLayer
  18. from flexlay.field import Field
  19. from flexlay.math import Point, Pointf, Size, Rectf
  20. from flexlay.graphic_context import GraphicContext
  21. from flexlay.util.sexpr_reader import get_value_from_tree
  22. from flexlay.util.sexpr_writer import SExprWriter
  23. from flexlay.objmap_tilemap_object import ObjMapTilemapObject
  24. from supertux.tileset import SuperTuxTileset
  25. class SuperTuxTileMap:
  26. @staticmethod
  27. def from_sexpr(data: Any) -> 'SuperTuxTileMap':
  28. # Load position from path, node and then x, ys
  29. x = 0
  30. y = 0
  31. path = get_value_from_tree(["path"], data, None)
  32. if path:
  33. node = get_value_from_tree(["node"], path, None)
  34. if node:
  35. x = get_value_from_tree(["x", "_"], node, None)
  36. y = get_value_from_tree(["y", "_"], node, None)
  37. name = get_value_from_tree(["name", "_"], data, "")
  38. z_pos = get_value_from_tree(["z-pos", "_"], data, 0)
  39. solid = get_value_from_tree(["solid", "_"], data, False)
  40. result = SuperTuxTileMap(z_pos, solid)
  41. width = get_value_from_tree(["width", "_"], data, 20)
  42. height = get_value_from_tree(["height", "_"], data, 15)
  43. result.draw_target = get_value_from_tree(["draw-target", "_"], data, "")
  44. result.speed = get_value_from_tree(["speed", "_"], data, 1.0)
  45. result.speed_y = get_value_from_tree(["speed-y", "_"], data, 1.0)
  46. result.alpha = get_value_from_tree(["alpha", "_"], data, 1.0)
  47. assert SuperTuxTileset.current is not None
  48. result.tilemap_layer = TilemapLayer(SuperTuxTileset.current, width, height)
  49. result.tilemap_layer.set_data(get_value_from_tree(["tiles"], data, []))
  50. result.tilemap_layer.metadata = result
  51. result.pos = Point(x, y)
  52. result.tilemap_layer.name = name
  53. return result
  54. @staticmethod
  55. def from_size(width: int, height: int, name: str, z_pos: float = 0, solid: bool = False) -> 'SuperTuxTileMap':
  56. result = SuperTuxTileMap(z_pos, solid)
  57. assert SuperTuxTileset.current is not None
  58. result.tilemap_layer = TilemapLayer(SuperTuxTileset.current, width, height)
  59. result.tilemap_layer.metadata = result
  60. result.tilemap_layer.name = name
  61. return result
  62. def __init__(self, z_pos: float = 0, solid: bool = False) -> None:
  63. self.solid: bool = solid
  64. self.draw_target: str = ""
  65. self.z_pos: float = z_pos
  66. self.speed: float = 1.0
  67. self.speed_y: float = 1.0
  68. self.alpha: float = 1.0
  69. self.pos: Point = Point(0, 0)
  70. self.tilemap_layer: Optional[TilemapLayer] = None
  71. @property
  72. def name(self) -> str:
  73. assert self.tilemap_layer is not None
  74. return self.tilemap_layer.name
  75. def write(self, writer: SExprWriter, objmap_tilemap_object: ObjMapTilemapObject) -> None:
  76. assert self.tilemap_layer is not None
  77. writer.begin_list("tilemap")
  78. writer.write_bool("solid", self.solid)
  79. if self.draw_target:
  80. writer.write_string("draw-target", self.draw_target)
  81. if self.speed != 1.0:
  82. writer.write_float("speed", self.speed)
  83. if self.speed_y != 1.0:
  84. writer.write_float("speed-y", self.speed_y)
  85. if self.alpha != 1.0:
  86. writer.write_float("alpha", self.alpha)
  87. writer.write_float("z-pos", self.z_pos)
  88. if self.tilemap_layer.name:
  89. writer.write_string("name", self.tilemap_layer.name)
  90. if self.pos and (self.pos.x != 0 or self.pos.y != 0):
  91. writer.begin_list("path")
  92. writer.begin_list("node")
  93. writer.write_int("x", self.pos.x)
  94. writer.write_int("y", self.pos.y)
  95. writer.end_list("node")
  96. writer.end_list("path")
  97. writer.write_int("width", self.tilemap_layer.width)
  98. writer.write_int("height", self.tilemap_layer.height)
  99. writer.write_field("tiles", self.tilemap_layer.field)
  100. writer.end_list()
  101. def get_bounding_rect(self) -> Rectf:
  102. assert self.tilemap_layer is not None
  103. return self.tilemap_layer.get_bounding_rect()
  104. def has_bounding_rect(self) -> bool:
  105. assert self.tilemap_layer is not None
  106. return self.tilemap_layer.has_bounding_rect()
  107. def draw(self, gc: GraphicContext) -> None:
  108. assert self.tilemap_layer is not None
  109. self.tilemap_layer.draw(gc, self.pos)
  110. def world2tile(self, p: Pointf) -> Point:
  111. assert self.tilemap_layer is not None
  112. return self.tilemap_layer.world2tile(p)
  113. def get_tileset(self) -> SuperTuxTileset:
  114. assert self.tilemap_layer is not None
  115. assert isinstance(self.tilemap_layer.get_tileset(), SuperTuxTileset)
  116. return cast(SuperTuxTileset, self.tilemap_layer.get_tileset())
  117. def get_data(self) -> list[int]:
  118. assert self.tilemap_layer is not None
  119. return self.tilemap_layer.get_data()
  120. def set_data(self, data: list[int]) -> None:
  121. assert self.tilemap_layer is not None
  122. self.tilemap_layer.set_data(data)
  123. def resize(self, size: Size, pos: Point) -> None:
  124. assert self.tilemap_layer is not None
  125. self.tilemap_layer.resize(size, pos)
  126. @property
  127. def field(self) -> Field:
  128. assert self.tilemap_layer is not None
  129. return self.tilemap_layer.field
  130. @property
  131. def width(self) -> int:
  132. assert self.tilemap_layer is not None
  133. return self.tilemap_layer.width
  134. @property
  135. def height(self) -> int:
  136. assert self.tilemap_layer is not None
  137. return self.tilemap_layer.height
  138. # EOF #