From 36874a535a3d5f7f2955f33e34aa1a4768b6fec1 Mon Sep 17 00:00:00 2001
From: David Luevano Alvarado <david@luevano.xyz>
Date: Sat, 4 Jun 2022 03:17:22 -0600
Subject: added hud for snake size and grow progress

---
 src/ui/hud/snake/scripts/snake_head.gd | 29 ++++++++++++++
 src/ui/hud/snake/scripts/snake_hud.gd  | 69 ++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 src/ui/hud/snake/scripts/snake_head.gd
 create mode 100644 src/ui/hud/snake/scripts/snake_hud.gd

(limited to 'src/ui/hud/snake/scripts')

diff --git a/src/ui/hud/snake/scripts/snake_head.gd b/src/ui/hud/snake/scripts/snake_head.gd
new file mode 100644
index 0000000..a414e23
--- /dev/null
+++ b/src/ui/hud/snake/scripts/snake_head.gd
@@ -0,0 +1,29 @@
+extends TextureRect
+
+enum {
+	IDLE,
+	EAT,
+	DEAD
+}
+
+var frames = {
+	IDLE: preload("res://ui/hud/snake/sprites/head1.png"),
+	EAT: preload("res://ui/hud/snake/sprites/head2.png"),
+	DEAD: preload("res://ui/hud/snake/sprites/head3.png")
+}
+
+
+func _ready():
+	texture = frames[IDLE]
+	Event.connect("food_eaten", self, "_on_food_eaten")
+	Event.connect("game_over", self, "_on_game_over")
+
+
+func _on_food_eaten(properties: Dictionary) -> void:
+	texture = frames[EAT]
+	yield(get_tree().create_timer(0.25), "timeout")
+	texture = frames[IDLE]
+
+
+func _on_game_over() -> void:
+	texture = frames[DEAD]
\ No newline at end of file
diff --git a/src/ui/hud/snake/scripts/snake_hud.gd b/src/ui/hud/snake/scripts/snake_hud.gd
new file mode 100644
index 0000000..b50b72e
--- /dev/null
+++ b/src/ui/hud/snake/scripts/snake_hud.gd
@@ -0,0 +1,69 @@
+extends HBoxContainer
+
+enum {
+	BODY1,
+	BODY2,
+	BODY3,
+	TAIL
+}
+
+var frames: Dictionary = {
+	BODY1: preload("res://ui/hud/snake/sprites/body1.png"),
+	BODY2: preload("res://ui/hud/snake/sprites/body2.png"),
+	BODY3: preload("res://ui/hud/snake/sprites/body3.png"),
+	TAIL: preload("res://ui/hud/snake/sprites/tail.png")
+}
+
+var last_tail_index: int = -1
+var last_frame: int = -1
+var frame_direction: int = 1
+
+
+func _ready():
+	Event.connect("snake_added_new_segment", self, "_on_snake_added_new_segment")
+
+
+func _on_snake_added_new_segment(type: String) -> void:
+	match type:
+		"body":
+			_add_body_frame()
+			_move_tail_frame()
+		"tail":
+			_add_tail_frame()
+
+
+func _add_body_frame() -> void:
+	var texture_rect: TextureRect = TextureRect.new()
+	texture_rect.texture = frames[_get_next_body_frame()]
+	add_child(texture_rect)
+
+
+func _add_tail_frame() -> void:
+	var texture_rect: TextureRect = TextureRect.new()
+	texture_rect.texture = frames[TAIL]
+	add_child(texture_rect)
+	last_tail_index = get_child_count() - 1
+
+
+func _move_tail_frame() -> void:
+	var child_count: int = get_child_count()
+	if child_count > Global.SNAKE_INITIAL_SEGMENTS + 1:
+		var last_child: TextureRect = get_child(last_tail_index)
+		move_child(last_child, child_count - 1)
+		last_tail_index = child_count - 1
+
+
+func _get_next_body_frame() -> int:
+	match last_frame:
+		-1:
+			last_frame = BODY2
+		BODY2:
+			last_frame += frame_direction
+		BODY3:
+			frame_direction = -1
+			last_frame += frame_direction
+		BODY1:
+			frame_direction = 1
+			last_frame += frame_direction
+
+	return last_frame
-- 
cgit v1.2.3-70-g09d2