1
0

progress.gd 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. extends Node
  2. ## What the player keeps between sessions: the level reached (saved on every win, and
  3. ## dropping by one for every Params.LOSE_HOURS away), best stars per level, and settings.
  4. const PATH := "user://progress.cfg"
  5. ## Level the next game scene starts at.
  6. var current_level := 0
  7. var _config := ConfigFile.new()
  8. func _ready() -> void:
  9. _config.load(PATH)
  10. ## Records a win that leads to [param level]. The saved level only goes up (replaying an
  11. ## old level never lowers it), but any win restarts the decay clock.
  12. func save_level(level: int) -> void:
  13. _config.set_value("progress", "level", maxi(level, saved_level()))
  14. _config.set_value("progress", "time", Time.get_unix_time_from_system())
  15. _config.save(PATH)
  16. ## The level the player continues from (after any decay applied by [method load_level]).
  17. func saved_level() -> int:
  18. return _config.get_value("progress", "level", 0)
  19. ## Best stars earned on [param level], 0 if never won.
  20. func stars(level: int) -> int:
  21. return _config.get_value("stars", str(level), 0)
  22. ## Highest level the player has won, -1 if none.
  23. func highest_won() -> int:
  24. var highest := -1
  25. if _config.has_section("stars"):
  26. for key in _config.get_section_keys("stars"):
  27. highest = maxi(highest, int(key))
  28. return highest
  29. ## Applies any decay since the last save and returns [level, hours until the next decay].
  30. func load_level() -> Array:
  31. var level: int = _config.get_value("progress", "level", 0)
  32. var last: float = _config.get_value("progress", "time", 0.0)
  33. var now := Time.get_unix_time_from_system()
  34. var period := 3600.0 * Params.LOSE_HOURS
  35. var remaining := period + last - now
  36. while remaining < 0.0 and level > 0:
  37. last += period
  38. level -= 1
  39. remaining = period + last - now
  40. _config.set_value("progress", "level", level)
  41. _config.set_value("progress", "time", last)
  42. _config.save(PATH)
  43. return [level, remaining / 3600.0]
  44. ## Keeps the best star count (1 to 3) for a level; returns true if it is a new best.
  45. func save_stars(level: int, stars: int) -> bool:
  46. var key := str(level)
  47. if stars <= _config.get_value("stars", key, 0):
  48. return false
  49. _config.set_value("stars", key, stars)
  50. _config.save(PATH)
  51. return true
  52. func total_stars() -> int:
  53. var total := 0
  54. if _config.has_section("stars"):
  55. for key in _config.get_section_keys("stars"):
  56. total += int(_config.get_value("stars", key))
  57. return total
  58. func save_best_score(score: int) -> bool:
  59. if score <= best_score():
  60. return false
  61. _config.set_value("progress", "best_score", score)
  62. _config.save(PATH)
  63. return true
  64. func best_score() -> int:
  65. return _config.get_value("progress", "best_score", 0)
  66. func music_on() -> bool:
  67. return _config.get_value("settings", "music", true)
  68. func set_music_on(on: bool) -> void:
  69. _config.set_value("settings", "music", on)
  70. _config.save(PATH)