1
0

progress.gd 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. func save_level(level: int) -> void:
  11. _config.set_value("progress", "level", level)
  12. _config.set_value("progress", "time", Time.get_unix_time_from_system())
  13. _config.save(PATH)
  14. ## Applies any decay since the last save and returns [level, hours until the next decay].
  15. func load_level() -> Array:
  16. var level: int = _config.get_value("progress", "level", 0)
  17. var last: float = _config.get_value("progress", "time", 0.0)
  18. var now := Time.get_unix_time_from_system()
  19. var period := 3600.0 * Params.LOSE_HOURS
  20. var remaining := period + last - now
  21. while remaining < 0.0 and level > 0:
  22. last += period
  23. level -= 1
  24. remaining = period + last - now
  25. _config.set_value("progress", "level", level)
  26. _config.set_value("progress", "time", last)
  27. _config.save(PATH)
  28. return [level, remaining / 3600.0]
  29. ## Keeps the best star count (1 to 3) for a level; returns true if it is a new best.
  30. func save_stars(level: int, stars: int) -> bool:
  31. var key := str(level)
  32. if stars <= _config.get_value("stars", key, 0):
  33. return false
  34. _config.set_value("stars", key, stars)
  35. _config.save(PATH)
  36. return true
  37. func total_stars() -> int:
  38. var total := 0
  39. if _config.has_section("stars"):
  40. for key in _config.get_section_keys("stars"):
  41. total += int(_config.get_value("stars", key))
  42. return total
  43. func save_best_score(score: int) -> bool:
  44. if score <= best_score():
  45. return false
  46. _config.set_value("progress", "best_score", score)
  47. _config.save(PATH)
  48. return true
  49. func best_score() -> int:
  50. return _config.get_value("progress", "best_score", 0)
  51. func music_on() -> bool:
  52. return _config.get_value("settings", "music", true)
  53. func set_music_on(on: bool) -> void:
  54. _config.set_value("settings", "music", on)
  55. _config.save(PATH)