From 898877f09808691a5e5d45850d27ae85f270db16 Mon Sep 17 00:00:00 2001
From: David Luevano Alvarado <david@luevano.xyz>
Date: Thu, 2 Jun 2022 02:35:03 -0600
Subject: add food system

---
 src/entities/actors/snake/scenes/Body.tscn         |  2 +-
 src/entities/actors/snake/scenes/Head.tscn         |  7 +++----
 .../actors/snake/scripts/generic_segment.gd        | 10 ++++++++++
 src/entities/actors/snake/scripts/snake.gd         | 22 ++++++++++++++--------
 4 files changed, 28 insertions(+), 13 deletions(-)

(limited to 'src/entities/actors')

diff --git a/src/entities/actors/snake/scenes/Body.tscn b/src/entities/actors/snake/scenes/Body.tscn
index 8e4a8a4..49694c0 100644
--- a/src/entities/actors/snake/scenes/Body.tscn
+++ b/src/entities/actors/snake/scenes/Body.tscn
@@ -4,7 +4,7 @@
 [ext_resource path="res://entities/actors/snake/scripts/generic_segment.gd" type="Script" id=2]
 
 [sub_resource type="CapsuleShape2D" id=1]
-radius = 5.99999
+radius = 4.99999
 height = 4.00002
 
 [node name="BodyPathFollow" type="PathFollow2D"]
diff --git a/src/entities/actors/snake/scenes/Head.tscn b/src/entities/actors/snake/scenes/Head.tscn
index 22aa7e6..d118fbf 100644
--- a/src/entities/actors/snake/scenes/Head.tscn
+++ b/src/entities/actors/snake/scenes/Head.tscn
@@ -3,12 +3,11 @@
 [ext_resource path="res://entities/actors/snake/sprites/head.png" type="Texture" id=1]
 [ext_resource path="res://entities/actors/snake/scripts/head.gd" type="Script" id=2]
 
-[sub_resource type="CircleShape2D" id=1]
-radius = 5.0
+[sub_resource type="ConvexPolygonShape2D" id=1]
+points = PoolVector2Array( -5, 0, 5, 0, 5, -3, 3, -4, 1, -5, -1, -5, -3, -4, -5, -3 )
 
 [node name="Head" type="KinematicBody2D"]
-position = Vector2( -1, 0 )
-collision_mask = 0
+collision_mask = 262
 script = ExtResource( 2 )
 
 [node name="Sprite" type="Sprite" parent="."]
diff --git a/src/entities/actors/snake/scripts/generic_segment.gd b/src/entities/actors/snake/scripts/generic_segment.gd
index 6c65d11..e2db91d 100644
--- a/src/entities/actors/snake/scripts/generic_segment.gd
+++ b/src/entities/actors/snake/scripts/generic_segment.gd
@@ -2,6 +2,16 @@ extends PathFollow2D
 
 export(String, "body", "tail") var TYPE: String = "body"
 
+onready var _segment: Area2D = get_child(0)
+
+
+func _ready() -> void:
+	_segment.connect("body_entered", self, "_on_body_entered")
+
 
 func _physics_process(delta: float) -> void:
 	offset += Global.SNAKE_SPEED * delta
+
+
+func _on_body_entered(body: Node) -> void:
+	Event.emit_signal("snake_segment_body_entered", body)
diff --git a/src/entities/actors/snake/scripts/snake.gd b/src/entities/actors/snake/scripts/snake.gd
index 76b8bee..ead6254 100644
--- a/src/entities/actors/snake/scripts/snake.gd
+++ b/src/entities/actors/snake/scripts/snake.gd
@@ -18,9 +18,11 @@ var body_segment_queue: Array
 func _ready():
 	set_physics_process(false)
 	set_process_input(false)
-	Event.connect("snake_path_new_point", self, "_on_Head_snake_path_new_point")
-	Event.connect("snake_added_new_segment", self, "_on_Snake_snake_added_new_segment")
-	Event.connect("snake_added_initial_segments", self, "_on_Snake_snake_added_initial_segments")
+	Event.connect("snake_path_new_point", self, "_on_snake_path_new_point")
+	Event.connect("snake_added_new_segment", self, "_on_snake_added_new_segment")
+	Event.connect("snake_added_initial_segments", self, "_on_snake_added_initial_segments")
+
+	Event.connect("food_eaten", self, "_on_food_eaten")
 
 
 func _physics_process(delta: float) -> void:
@@ -42,7 +44,7 @@ func _add_new_segment() -> void:
 	var _path_length_threshold: float = body_segment_queue[0] + Global.SNAKE_SEGMENT_SIZE
 	if path.curve.get_baked_length() >= _path_length_threshold:
 		var _temp_body_segment: PathFollow2D = BODY_SEGMENT_NP.instance()
-		Event.emit_signal("snake_add_new_segment", "body")
+		Event.emit_signal("snake_adding_new_segment", "body")
 		var _new_body_offset: float = body_segment_stack.back().offset - Global.SNAKE_SEGMENT_SIZE
 
 		_temp_body_segment.offset = _new_body_offset
@@ -58,7 +60,7 @@ func _add_new_segment() -> void:
 func _add_initial_segment(type: PackedScene) -> void:
 	if path.curve.get_baked_length() >= (current_body_segments + 1.0) * Global.SNAKE_SEGMENT_SIZE:
 		var _temp_body_segment: PathFollow2D = type.instance()
-		Event.emit_signal("snake_add_new_segment", _temp_body_segment.TYPE)
+		Event.emit_signal("snake_adding_new_segment", _temp_body_segment.TYPE)
 		if _temp_body_segment.TYPE == "body":
 			body_segment_stack.append(_temp_body_segment)
 		else:
@@ -78,7 +80,7 @@ func _add_segment_to_queue() -> void:
 		body_segment_queue.append(body_segment_queue.back() + Global.SNAKE_SEGMENT_SIZE)
 
 
-func _on_Head_snake_path_new_point(coordinates: Vector2) -> void:
+func _on_snake_path_new_point(coordinates: Vector2) -> void:
 	path.curve.add_point(coordinates)
 	# update call is to draw curve as there are new points to the path's curve
 	update()
@@ -89,11 +91,15 @@ func _on_Head_snake_path_new_point(coordinates: Vector2) -> void:
 		_add_initial_segment(TAIL_SEGMENT_NP)
 
 
-func _on_Snake_snake_added_new_segment(type: String) -> void:
+func _on_snake_added_new_segment(type: String) -> void:
 	if type == "tail":
 		Event.emit_signal("snake_added_initial_segments")
 
 
-func _on_Snake_snake_added_initial_segments() -> void:
+func _on_snake_added_initial_segments() -> void:
 	set_physics_process(true)
 	set_process_input(true)
+
+
+func _on_food_eaten(type: int) -> void:
+	_add_segment_to_queue()
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2