summaryrefslogtreecommitdiff
path: root/src/tools/score_manager
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-04 23:00:58 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-04 23:00:58 -0600
commit36abc689d783774ce4f2d7b5a1bb621d8684be45 (patch)
treea3dbed6069fbd8d9a588c7510ce581af3e847af0 /src/tools/score_manager
parent9a2bcf02c2623c8f3e8f5e74e70b3c0333790484 (diff)
added more ui for after gameplay, generalized basic food
Diffstat (limited to 'src/tools/score_manager')
-rw-r--r--src/tools/score_manager/ScoreManager.tscn8
-rw-r--r--src/tools/score_manager/scenes/ScoreLabel.tscn46
-rw-r--r--src/tools/score_manager/scenes/ScoreManager.tscn8
-rw-r--r--src/tools/score_manager/score_manager.gd48
-rw-r--r--src/tools/score_manager/scripts/score_label.gd42
-rw-r--r--src/tools/score_manager/scripts/score_manager.gd67
6 files changed, 163 insertions, 56 deletions
diff --git a/src/tools/score_manager/ScoreManager.tscn b/src/tools/score_manager/ScoreManager.tscn
deleted file mode 100644
index e54e089..0000000
--- a/src/tools/score_manager/ScoreManager.tscn
+++ /dev/null
@@ -1,8 +0,0 @@
-[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/scenes/ScoreLabel.tscn b/src/tools/score_manager/scenes/ScoreLabel.tscn
new file mode 100644
index 0000000..365aff1
--- /dev/null
+++ b/src/tools/score_manager/scenes/ScoreLabel.tscn
@@ -0,0 +1,46 @@
+[gd_scene load_steps=4 format=2]
+
+[ext_resource path="res://fonts/MonogramExtended.tres" type="DynamicFont" id=1]
+[ext_resource path="res://tools/score_manager/scripts/score_label.gd" type="Script" id=2]
+[ext_resource path="res://ui/stats_display/sprites/sep_2x2.png" type="Texture" id=3]
+
+[node name="ScoreLabel" type="MarginContainer"]
+margin_bottom = 13.0
+script = ExtResource( 2 )
+
+[node name="Hbox" type="HBoxContainer" parent="."]
+margin_right = 16.0
+margin_bottom = 13.0
+custom_constants/separation = 2
+
+[node name="Label" type="Label" parent="Hbox"]
+margin_right = 12.0
+margin_bottom = 13.0
+custom_colors/font_color_shadow = Color( 0, 0, 0, 1 )
+custom_fonts/font = ExtResource( 1 )
+text = "x1"
+
+[node name="Timer" type="Timer" parent="Hbox/Label"]
+one_shot = true
+
+[node name="Center" type="CenterContainer" parent="Hbox"]
+margin_left = 14.0
+margin_right = 16.0
+margin_bottom = 13.0
+
+[node name="VBox" type="VBoxContainer" parent="Hbox/Center"]
+margin_top = 5.0
+margin_right = 2.0
+margin_bottom = 7.0
+custom_constants/separation = 0
+
+[node name="Sep" type="TextureRect" parent="Hbox/Center/VBox"]
+margin_right = 2.0
+margin_bottom = 2.0
+texture = ExtResource( 3 )
+
+[node name="TextureRect" type="TextureRect" parent="Hbox/Center/VBox"]
+visible = false
+margin_top = 4.0
+margin_right = 4.0
+margin_bottom = 4.0
diff --git a/src/tools/score_manager/scenes/ScoreManager.tscn b/src/tools/score_manager/scenes/ScoreManager.tscn
new file mode 100644
index 0000000..c80439b
--- /dev/null
+++ b/src/tools/score_manager/scenes/ScoreManager.tscn
@@ -0,0 +1,8 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://tools/score_manager/scripts/score_manager.gd" type="Script" id=1]
+[ext_resource path="res://tools/score_manager/scenes/ScoreLabel.tscn" type="PackedScene" id=4]
+
+[node name="ScoreManager" type="Node2D"]
+script = ExtResource( 1 )
+SCORE_LABEL = ExtResource( 4 )
diff --git a/src/tools/score_manager/score_manager.gd b/src/tools/score_manager/score_manager.gd
deleted file mode 100644
index 63c78a7..0000000
--- a/src/tools/score_manager/score_manager.gd
+++ /dev/null
@@ -1,48 +0,0 @@
-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")
-
-
-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
- points -= score_to_grow
- # maybe be careful with this
- amount_to_grow += points / Global.POINTS_TO_GROW
- growth += amount_to_grow
- Event.emit_signal("snake_add_new_segment", amount_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)
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)