diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-06-04 05:17:12 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-06-04 05:17:12 -0600 |
commit | e6beb7d4d3450c68ca5ebfae040f42dd82db1b31 (patch) | |
tree | ba8fae52c6f60c09f4aab2958231969ea571a7e3 /src/tools/score_manager | |
parent | fa98822a569c1efa2bebc705073c3921b9ae4217 (diff) |
add text for points when eating food
Diffstat (limited to 'src/tools/score_manager')
-rw-r--r-- | src/tools/score_manager/ScoreManager.tscn | 4 | ||||
-rw-r--r-- | src/tools/score_manager/score_manager.gd | 26 |
2 files changed, 27 insertions, 3 deletions
diff --git a/src/tools/score_manager/ScoreManager.tscn b/src/tools/score_manager/ScoreManager.tscn index 136b294..e54e089 100644 --- a/src/tools/score_manager/ScoreManager.tscn +++ b/src/tools/score_manager/ScoreManager.tscn @@ -1,6 +1,8 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://tools/score_manager/score_manager.gd" type="Script" id=1] +[ext_resource path="res://fonts/MonogramExtended.tres" type="DynamicFont" id=2] [node name="ScoreManager" type="Node2D"] script = ExtResource( 1 ) +TEXT_FONT = ExtResource( 2 ) diff --git a/src/tools/score_manager/score_manager.gd b/src/tools/score_manager/score_manager.gd index 3b5ddba..63c78a7 100644 --- a/src/tools/score_manager/score_manager.gd +++ b/src/tools/score_manager/score_manager.gd @@ -1,9 +1,13 @@ extends Node +export(Resource) var TEXT_FONT: Resource var score: int = 0 var growth: int = 0 +var point_text_time: float = 2.0 +var _point_text_fmt: String = "+%s" + func _ready(): Event.connect("food_eaten", self, "_on_food_eaten") @@ -11,8 +15,15 @@ func _ready(): func _on_food_eaten(properties: Dictionary) -> void: var points: int = properties["points"] + var location: Vector2 = properties["global_position"] + _process_points(points) + _spawn_added_score_text(points, location) + + +func _process_points(points: int) -> void: var score_to_grow: int = (growth + 1) * Global.POINTS_TO_GROW - score var amount_to_grow: int = 0 + var growth_progress: int score += points if points >= score_to_grow: amount_to_grow += 1 @@ -22,5 +33,16 @@ func _on_food_eaten(properties: Dictionary) -> void: growth += amount_to_grow Event.emit_signal("snake_add_new_segment", amount_to_grow) - score_to_grow = Global.POINTS_TO_GROW - ((growth + 1) * Global.POINTS_TO_GROW - score) - Event.emit_signal("snake_growth_progress", score_to_grow) + growth_progress = Global.POINTS_TO_GROW - ((growth + 1) * Global.POINTS_TO_GROW - score) + Event.emit_signal("snake_growth_progress", growth_progress) + + +func _spawn_added_score_text(points: int, location: Vector2) -> void: + var label: Label = Label.new() + label.text = _point_text_fmt % points + label.add_color_override("font_color", Color.red) + label.add_font_override("font", TEXT_FONT) + label.set_global_position(location) + add_child(label) + yield(get_tree().create_timer(point_text_time), "timeout") + remove_child(label) |