generated from sakkaku-dev/godot-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restore energy after some time
- Loading branch information
Showing
5 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |