models.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ########################################################################
  2. # Hello Worlds - Libre 3D RPG game.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Hello Worlds.
  6. #
  7. # Hello Worlds is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Hello Worlds is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. from core.signals import Signal
  22. class Model:
  23. def __init__(self):
  24. self.destroyed = Signal()
  25. self.changed = Signal()
  26. def destroy(self):
  27. self.destroyed.emit()
  28. class ItemModel(Model):
  29. """ Basic item model
  30. Game items and spells will extend this.
  31. """
  32. def __init__(self, name="", iconPath="assets/icons/unknown.png", desc=""):
  33. Model.__init__(self)
  34. # TODO check if iconPath exists, else set assets/icons/unknown.png
  35. self._name = name
  36. self._iconPath = iconPath
  37. self._desc = desc
  38. @property
  39. def name(self): return self._name
  40. @property
  41. def iconPath(self): return self._iconPath
  42. @property
  43. def description(self): return self._desc
  44. class StatsModel(Model):
  45. def __init__(self, rawData={}):
  46. Model.__init__(self)
  47. self.setRawData(rawData)
  48. def setRawData(self, rawData):
  49. print(rawData)
  50. self._data = {
  51. 'level' : IntModel(
  52. max=rawData.level.max,
  53. value=rawData.level.value),
  54. 'health' : IntModel(
  55. max=rawData.health.max,
  56. value=rawData.health.value),
  57. 'energy' : IntModel(
  58. max=rawData.energy.max,
  59. value=rawData.energy.value),
  60. 'strength' : 1,
  61. 'stamina' : 1,
  62. 'resistance': 2,
  63. 'reflexes' : 0.5,
  64. 'spitit' : 0,
  65. }
  66. @property
  67. def health(self): return self._data['health']
  68. @property
  69. def level(self): return self._data['level']
  70. @property
  71. def energy(self): return self._data['energy']
  72. class PlayerStatsModel(StatsModel):
  73. def __init__(self, rawData={}):
  74. StatsModel.__init__(self, rawData=rawData)
  75. @property
  76. def experience(self): return self._data['experience']
  77. def setRawData(self, rawData):
  78. StatsModel.setRawData(self, rawData)
  79. self._data.update({
  80. 'experience' : IntModel(
  81. max=rawData.experience.max,
  82. value=rawData.experience.value)
  83. })
  84. class SelectedNpcModel(Model):
  85. changed = Signal(int) # selected id
  86. def __init__(self):
  87. """ Shared by a mouse-handler and the ui
  88. """
  89. self._selectedId = None
  90. @property
  91. def selectedId(self): return self._selectedId
  92. @selectedId.setter
  93. def selectedId(self, id):
  94. self._selectedId = id
  95. self.changed.emit(id)
  96. class IntModel(Model):
  97. def __init__(self, min=0, max=100, value=0, stepSize=1):
  98. """ Used for example: character / npc -level
  99. """
  100. self.valueChanged = Signal(int)
  101. Model.__init__(self)
  102. self.__min = min
  103. self.__max = max
  104. self.__value = value
  105. self.__stepSize = stepSize
  106. @property
  107. def min(self): return self.__min
  108. @property
  109. def max(self): return self.__max
  110. @property
  111. def value(self): return self.__value
  112. @value.setter
  113. def value(self, value):
  114. self.__value = value
  115. self.valueChanged.emit(value)
  116. class FloatModel(IntModel):
  117. def __init__(self, min=0, max=100, value=0, stepSize=.01):
  118. """ Used for example: character / npc -stats
  119. """
  120. self.valueChanged = Signal(float)
  121. IntModel.__init__(self, min=min, max=max, value=value, stepSize=stepSize)