sprite.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Any, Optional
  17. import os
  18. import logging
  19. from flexlay.sprite import Sprite
  20. from flexlay.util.sexpr import sexpr_read_from_file
  21. from flexlay.util.sexpr_reader import get_value_from_tree
  22. class SuperTuxSpriteAction:
  23. def __init__(self) -> None:
  24. self.name: Optional[str] = None
  25. self.image: Optional[str] = None
  26. self.x_offset: int = 0
  27. self.y_offset: int = 0
  28. def parse(self, sexpr: Any) -> None:
  29. self.name = get_value_from_tree(["name", "_"], sexpr, "default")
  30. self.x_offset = get_value_from_tree(["x-offset", "_"], sexpr, 0)
  31. self.y_offset = get_value_from_tree(["y-offset", "_"], sexpr, 0)
  32. # we only parse the first image for now as we don't have support for
  33. # animation in flexlay anyway
  34. self.image = get_value_from_tree(["images", "_"], sexpr, 0)
  35. class SuperTuxSprite:
  36. def __init__(self) -> None:
  37. self.actions: dict[str, SuperTuxSpriteAction] = {}
  38. self.actions_default: Optional[SuperTuxSpriteAction] = None
  39. self.basedir: str = ""
  40. @staticmethod
  41. def from_png_file(filename: str) -> 'SuperTuxSprite':
  42. sprite = SuperTuxSprite()
  43. action = SuperTuxSpriteAction()
  44. action.name = "default"
  45. action.image = filename
  46. sprite.actions["default"] = action
  47. return sprite
  48. @staticmethod
  49. def from_sprite_file(filename: str) -> 'SuperTuxSprite':
  50. sprite = SuperTuxSprite()
  51. tree: Any = sexpr_read_from_file(filename)[0]
  52. if tree is None:
  53. raise Exception("Error: Couldn't load: '%s'" % filename)
  54. sprite.basedir = os.path.dirname(filename)
  55. for i in tree[1:]:
  56. if i[0] == "action":
  57. action = SuperTuxSpriteAction()
  58. action.parse(i[1:])
  59. assert action.name is not None
  60. sprite.actions[action.name] = action
  61. if sprite.actions_default is None or action.name == "default":
  62. sprite.actions_default = action
  63. else:
  64. logging.warning("Unknown symbol '%s' in sprite '%s'" % (i[0], filename))
  65. return sprite
  66. @staticmethod
  67. def from_file(filename: str) -> 'SuperTuxSprite':
  68. if filename[-4:] == ".png":
  69. return SuperTuxSprite.from_png_file(filename)
  70. elif filename[-7:] == ".sprite":
  71. return SuperTuxSprite.from_sprite_file(filename)
  72. else:
  73. raise Exception("Unsupported sprite format '%s'" % filename)
  74. def get_sprite(self, action_name: str = "default") -> Optional[Sprite]:
  75. action = self.actions.get(action_name, self.actions_default)
  76. if action is None:
  77. logging.info("Kaputt:" + str(self.actions))
  78. return None
  79. else:
  80. assert action.image is not None
  81. sprite = Sprite.from_file(os.path.join(self.basedir, action.image))
  82. # FIXME:
  83. # sprite.set_frame_offset(0, Point(action.x_offset, action.y_offset))
  84. return sprite
  85. # EOF #