diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-06-04 23:00:58 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-06-04 23:00:58 -0600 |
commit | 36abc689d783774ce4f2d7b5a1bb621d8684be45 (patch) | |
tree | a3dbed6069fbd8d9a588c7510ce581af3e847af0 /src/tools/score_manager/scripts | |
parent | 9a2bcf02c2623c8f3e8f5e74e70b3c0333790484 (diff) |
added more ui for after gameplay, generalized basic food
Diffstat (limited to 'src/tools/score_manager/scripts')
-rw-r--r-- | src/tools/score_manager/scripts/score_label.gd | 42 | ||||
-rw-r--r-- | src/tools/score_manager/scripts/score_manager.gd | 67 |
2 files changed, 109 insertions, 0 deletions
diff --git a/src/tools/score_manager/scripts/score_label.gd b/src/tools/score_manager/scripts/score_label.gd new file mode 100644 index 0000000..f1ccb9d --- /dev/null +++ b/src/tools/score_manager/scripts/score_label.gd @@ -0,0 +1,42 @@ +class_name ScoreLabel +extends MarginContainer + +onready var label: Label = $Hbox/Label +onready var timer: Timer = $Hbox/Label/Timer +onready var texture_rect: TextureRect = $Hbox/Center/VBox/TextureRect + +enum Type { + BODY_SEGMENT +} + +var texture: Dictionary = { + Type.BODY_SEGMENT: preload("res://ui/hud/progress_bars/sprites/grow_progress_icon.png") +} + +var alive_time: float = 2.0 +var fmt: String = "x%s" +var points: int + + +func _ready(): + timer.connect("timeout", self, "_on_timer_timout") + timer.wait_time = 2.0 + timer.start() + + +func set_properties(_points: int, color: Color, location: Vector2, type: int=-1) -> void: + print("Constructing") + points = _points + label.text = fmt % points + label.add_color_override("font_color", color) + set_global_position(location) + if type != -1: + texture_rect.visible = true + match type: + Type.BODY_SEGMENT: + texture_rect.texture = texture[type] + label.update() + + +func _on_timer_timout() -> void: + queue_free() diff --git a/src/tools/score_manager/scripts/score_manager.gd b/src/tools/score_manager/scripts/score_manager.gd new file mode 100644 index 0000000..27a8dd4 --- /dev/null +++ b/src/tools/score_manager/scripts/score_manager.gd @@ -0,0 +1,67 @@ +extends Node + +export(PackedScene) var SCORE_LABEL: PackedScene + +var initial_stats: Stats = SaveData.get_stats() +var stats: Stats = Stats.new() +var mutation_stats: Array = [ + Stats.new(), + Stats.new(), + Stats.new() +] + +var last_snake_pos: Vector2 = Vector2.ZERO + + +func _ready(): + Event.connect("game_over", self, "_on_game_over") + Event.connect("food_eaten", self, "_on_food_eaten") + Event.connect("snake_path_new_point", self, "_on_snake_path_new_point") + + +func _on_food_eaten(properties: Dictionary) -> void: + var points: int = properties["points"] + var location: Vector2 = properties["global_position"] + var amount_to_grow: int = _process_points(points) + _spawn_added_score_text(points, location) + _spawn_added_segment_text(amount_to_grow) + + +func _process_points(points: int) -> int: + var score_to_grow: int = (stats.segments + 1) * Global.POINTS_TO_GROW - stats.points + var amount_to_grow: int = 0 + var growth_progress: int + stats.points += points + if points >= score_to_grow: + amount_to_grow += 1 + points -= score_to_grow + # maybe be careful with this + amount_to_grow += points / Global.POINTS_TO_GROW + stats.segments += amount_to_grow + Event.emit_signal("snake_add_new_segment", amount_to_grow) + + growth_progress = Global.POINTS_TO_GROW - ((stats.segments + 1) * Global.POINTS_TO_GROW - stats.points) + Event.emit_signal("snake_growth_progress", growth_progress) + return amount_to_grow + + +func _spawn_added_score_text(points: int, location: Vector2) -> void: + var label: ScoreLabel = SCORE_LABEL.instance() + add_child(label) + label.set_properties(points, Color.red, location) + + +func _spawn_added_segment_text(amount: int) -> void: + if amount > 0: + var label: ScoreLabel = SCORE_LABEL.instance() + add_child(label) + label.set_properties(amount, Color.green, last_snake_pos, ScoreLabel.Type.BODY_SEGMENT) + + +func _on_snake_path_new_point(coordinates: Vector2) -> void: + last_snake_pos = coordinates + + +func _on_game_over() -> void: + print("Show comparison.") + Event.emit_signal("display_stats", initial_stats, stats, mutation_stats) |