Skip to content

Commit

Permalink
feat: restore energy after some time
Browse files Browse the repository at this point in the history
  • Loading branch information
myin142 committed Aug 15, 2023
1 parent 934cbc6 commit 244bb3a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
27 changes: 27 additions & 0 deletions godot/shared/DateTime.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class_name DateTime

var _time := 0

static func now():
return DateTime.new(Time.get_unix_time_from_system())

func _init(t):
if typeof(t) == TYPE_STRING:
_time = Time.get_unix_time_from_datetime_string(t)
elif typeof(t) == TYPE_INT:
_time = t
elif typeof(t) == TYPE_FLOAT:
_time = int(t)

func get_time():
return _time

func get_diff_in_minutes(dt: DateTime):
var diffSec = dt.get_time() - get_time()
return int(diffSec / 60.0)

func add_minutes(m: int):
return DateTime.new(_time + m * 60)

func _to_string():
return Time.get_datetime_string_from_unix_time(_time)
40 changes: 38 additions & 2 deletions godot/src/Energy.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
class_name Energy
extends Node

@export var max_energy := 5
@onready var energy := max_energy : set = _set_energy

@export var restore_minutes := 60

var _logger = Logger.new("Energy")
var _last_used

func get_last_used_time():
return _last_used.get_time()

func set_last_used_time(time: int):
_last_used = DateTime.new(time)

func _set_energy(v):
energy = v
energy = min(v, max_energy)
GameManager.energy_updated.emit(energy)

func use_energy():
func use_energy(datetime: DateTime = DateTime.now()):
if energy > 0:
if _last_used == null:
_last_used = datetime
_logger.debug("Set last used time to %s" % _last_used)
self.energy -= 1
return true
return false

func restore(now: DateTime = DateTime.now()):
if _last_used == null:
if energy < max_energy:
_last_used = now
return

var diff_min = _last_used.get_diff_in_minutes(now)
if diff_min < restore_minutes:
return

var restore_count = int(diff_min / restore_minutes)
self.energy += restore_count
_logger.debug("Restoring %s energy" % restore_count)

if energy >= max_energy:
_last_used = null
return

_last_used = _last_used.add_minutes(restore_count * restore_minutes)
_logger.debug("New last used time %s " % _last_used)
6 changes: 5 additions & 1 deletion godot/src/game_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var points := 0

func _ready():
_load_game()
energy.restore()

func _exit_tree():
_save_game()
Expand All @@ -29,7 +30,8 @@ func get_energy():

func _save_game():
save_manager.save_to_slot(SAVE_SLOT, {
"energy": get_energy()
"energy": get_energy(),
"energyUsedTime": energy.get_last_used_time()
})

func _load_game():
Expand All @@ -39,3 +41,5 @@ func _load_game():

if "energy" in data:
energy.energy = data.energy
if "energyUsedTime" in data:
energy.set_last_used_time(data.energyUsedTime)
10 changes: 10 additions & 0 deletions godot/tests/DateTimeTest.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends UnitTest

func test_create_from_iso_string():
var dt = DateTime.new('2020-01-01T00:00:00Z')
assert_eq(dt.get_time(), 1577836800)

func test_get_diff_in_minutes():
var dt = DateTime.new('2020-01-01T00:10:00')
assert_eq(dt.get_diff_in_minutes(DateTime.new('2020-01-01T00:20:00')), 10)
assert_eq(dt.get_diff_in_minutes(DateTime.new('2020-01-01T00:00:00')), -10)
45 changes: 45 additions & 0 deletions godot/tests/EnergyTest.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
extends UnitTest

var energy: Energy

func before_each():
energy = Energy.new()
energy.max_energy = 5
add_child_autofree(energy)

func test_restore_energy():
energy.restore_minutes = 5

energy.use_energy(DateTime.new('2020-01-01T00:00:00Z'))
assert_eq(energy.energy, 4)

energy.restore(DateTime.new('2020-01-01T00:04:00Z'))
assert_eq(energy.energy, 4)

energy.restore(DateTime.new('2020-01-01T00:05:00Z'))
assert_eq(energy.energy, 5)

func test_restore_multiple_energy():
energy.restore_minutes = 5

energy.use_energy(DateTime.new('2020-01-01T00:00:00Z'))
energy.use_energy(DateTime.new('2020-01-01T00:01:00Z'))
energy.use_energy(DateTime.new('2020-01-01T00:02:00Z'))
energy.use_energy(DateTime.new('2020-01-01T00:03:00Z'))
assert_eq(energy.energy, 1)


energy.restore(DateTime.new('2020-01-01T00:12:00Z'))
assert_eq(energy.energy, 3)

energy.restore(DateTime.new('2020-01-01T00:16:00Z'))
assert_eq(energy.energy, 4)

energy.restore(DateTime.new('2020-01-01T00:19:00Z'))
assert_eq(energy.energy, 4)

energy.restore(DateTime.new('2020-01-01T00:20:00Z'))
assert_eq(energy.energy, 5)

energy.restore(DateTime.new('2020-01-01T00:30:00Z'))
assert_eq(energy.energy, 5)

0 comments on commit 244bb3a

Please sign in to comment.