From 0aa26dc19da1c8907cd69d18b423c33f351d3f2f Mon Sep 17 00:00:00 2001
From: David Luevano Alvarado <david@luevano.xyz>
Date: Mon, 30 May 2022 02:05:19 -0600
Subject: working path2d with only the head, need to figure out the segments
 part

---
 assets/1x1.aseprite                                | Bin 0 -> 562 bytes
 assets/1x1.png                                     | Bin 0 -> 82 bytes
 src/Main.tscn                                      |   9 ++-
 src/entities/actors/snake/Snake.tscn               |  23 ++++++
 src/entities/actors/snake/body_segment/1x1.png     | Bin 0 -> 82 bytes
 .../actors/snake/body_segment/1x1.png.import       |  35 ++++++++
 .../actors/snake/body_segment/BodySegment.tscn     |   9 +++
 src/entities/actors/snake/head/Head.tscn           |  10 +++
 src/entities/actors/snake/head/head.gd             |  37 +++++++++
 src/entities/actors/snake/snake.gd                 |  90 +++++++--------------
 src/event.gd                                       |   3 +
 src/icons/game.png.import                          |   6 +-
 src/project.godot                                  |  42 +++++++++-
 13 files changed, 199 insertions(+), 65 deletions(-)
 create mode 100644 assets/1x1.aseprite
 create mode 100644 assets/1x1.png
 create mode 100644 src/entities/actors/snake/Snake.tscn
 create mode 100644 src/entities/actors/snake/body_segment/1x1.png
 create mode 100644 src/entities/actors/snake/body_segment/1x1.png.import
 create mode 100644 src/entities/actors/snake/body_segment/BodySegment.tscn
 create mode 100644 src/entities/actors/snake/head/Head.tscn
 create mode 100644 src/entities/actors/snake/head/head.gd
 create mode 100644 src/event.gd

diff --git a/assets/1x1.aseprite b/assets/1x1.aseprite
new file mode 100644
index 0000000..783a8c1
Binary files /dev/null and b/assets/1x1.aseprite differ
diff --git a/assets/1x1.png b/assets/1x1.png
new file mode 100644
index 0000000..89d23b0
Binary files /dev/null and b/assets/1x1.png differ
diff --git a/src/Main.tscn b/src/Main.tscn
index 70a653e..321c365 100644
--- a/src/Main.tscn
+++ b/src/Main.tscn
@@ -1,3 +1,10 @@
-[gd_scene format=2]
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://entities/actors/snake/Snake.tscn" type="PackedScene" id=1]
 
 [node name="Main" type="Node2D"]
+
+[node name="Snake" parent="." instance=ExtResource( 1 )]
+
+[node name="Camera" type="Camera2D" parent="."]
+current = true
diff --git a/src/entities/actors/snake/Snake.tscn b/src/entities/actors/snake/Snake.tscn
new file mode 100644
index 0000000..98d106c
--- /dev/null
+++ b/src/entities/actors/snake/Snake.tscn
@@ -0,0 +1,23 @@
+[gd_scene load_steps=5 format=2]
+
+[ext_resource path="res://entities/actors/snake/head/Head.tscn" type="PackedScene" id=1]
+[ext_resource path="res://entities/actors/snake/body_segment/BodySegment.tscn" type="PackedScene" id=2]
+[ext_resource path="res://entities/actors/snake/snake.gd" type="Script" id=3]
+
+[sub_resource type="Curve2D" id=1]
+_data = {
+"points": PoolVector2Array(  )
+}
+
+[node name="Snake" type="Node2D"]
+script = ExtResource( 3 )
+BODY_SEGMENT_NP = ExtResource( 2 )
+
+[node name="Head" parent="." instance=ExtResource( 1 )]
+
+[node name="Path" type="Path2D" parent="."]
+curve = SubResource( 1 )
+
+[node name="PathFollow" type="PathFollow2D" parent="Path"]
+loop = false
+lookahead = 1.0
diff --git a/src/entities/actors/snake/body_segment/1x1.png b/src/entities/actors/snake/body_segment/1x1.png
new file mode 100644
index 0000000..89d23b0
Binary files /dev/null and b/src/entities/actors/snake/body_segment/1x1.png differ
diff --git a/src/entities/actors/snake/body_segment/1x1.png.import b/src/entities/actors/snake/body_segment/1x1.png.import
new file mode 100644
index 0000000..ec15366
--- /dev/null
+++ b/src/entities/actors/snake/body_segment/1x1.png.import
@@ -0,0 +1,35 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/1x1.png-6e89afd7f217fca93b741eefef4d6779.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://entities/actors/snake/body_segment/1x1.png"
+dest_files=[ "res://.import/1x1.png-6e89afd7f217fca93b741eefef4d6779.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=false
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=false
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+process/normal_map_invert_y=false
+stream=false
+size_limit=0
+detect_3d=false
+svg/scale=1.0
diff --git a/src/entities/actors/snake/body_segment/BodySegment.tscn b/src/entities/actors/snake/body_segment/BodySegment.tscn
new file mode 100644
index 0000000..065a42d
--- /dev/null
+++ b/src/entities/actors/snake/body_segment/BodySegment.tscn
@@ -0,0 +1,9 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://entities/actors/snake/body_segment/1x1.png" type="Texture" id=1]
+
+[node name="BodySegment" type="Sprite"]
+modulate = Color( 0, 1, 0, 1 )
+position = Vector2( 0, 16 )
+scale = Vector2( 32, 32 )
+texture = ExtResource( 1 )
diff --git a/src/entities/actors/snake/head/Head.tscn b/src/entities/actors/snake/head/Head.tscn
new file mode 100644
index 0000000..790e2f0
--- /dev/null
+++ b/src/entities/actors/snake/head/Head.tscn
@@ -0,0 +1,10 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://icons/game.png" type="Texture" id=1]
+[ext_resource path="res://entities/actors/snake/head/head.gd" type="Script" id=2]
+
+[node name="HeadPoint" type="Node2D"]
+script = ExtResource( 2 )
+
+[node name="Sprite" type="Sprite" parent="."]
+texture = ExtResource( 1 )
diff --git a/src/entities/actors/snake/head/head.gd b/src/entities/actors/snake/head/head.gd
new file mode 100644
index 0000000..222d091
--- /dev/null
+++ b/src/entities/actors/snake/head/head.gd
@@ -0,0 +1,37 @@
+extends Node2D
+
+# export(float, 1.0, 1000.0, 1.0) var SPEED: float = 100.0
+# export(float, 1.0, 1000.0, 1.0) var ROT_SPEED: float = 200.0
+# export(float, 0.01, 1.0, 0.01) var POSITION_UPDATE_INTERVAL: float = 0.01
+var speed: float
+var rot_speed: float
+var position_update_interval: float
+
+var direction: Vector2 = Vector2.UP
+var _time_elapsed: float = 0.0
+
+
+func _ready():
+	Event.emit_signal("new_curve_point", global_position)
+
+
+func _process(delta: float) -> void:
+	if Input.is_action_pressed("move_left"):
+		# direction = direction.rotated(deg2rad(-ROT_SPEED))
+		rotate(deg2rad(-rot_speed * delta))
+	if Input.is_action_pressed("move_right"):
+		# direction = direction.rotated(deg2rad(ROT_SPEED))
+		rotate(deg2rad(rot_speed * delta))
+
+	move_local_x(direction.x * speed * delta)
+	move_local_y(direction.y * speed * delta)
+
+	_handle_time_elapsed(delta)
+
+
+# using a timer is not recommended for < 0.01
+func _handle_time_elapsed(delta: float) -> void:
+	if _time_elapsed >= position_update_interval:
+		Event.emit_signal("new_curve_point", global_position)
+		_time_elapsed = 0.0
+	_time_elapsed += delta
diff --git a/src/entities/actors/snake/snake.gd b/src/entities/actors/snake/snake.gd
index 553309b..7da829c 100644
--- a/src/entities/actors/snake/snake.gd
+++ b/src/entities/actors/snake/snake.gd
@@ -1,73 +1,43 @@
 class_name Snake
-extends KinematicBody2D
+extends Node2D
 
-export(float, 10.0, 1000.0, 10.0) var SPEED: float = 100.0
+export(float, 1.0, 1000.0, 1.0) var SPEED: float = 100.0
+export(float, 1.0, 1000.0, 1.0) var ROT_SPEED: float = 200.0
+export(float, 0.01, 1.0, 0.01) var POSITION_UPDATE_INTERVAL: float = 0.01
+export(PackedScene) var BODY_SEGMENT_NP: PackedScene
 
-var velocity: Vector2 = Vector2.ZERO
+onready var head: Node2D = $Head
+onready var path: Path2D = $Path
+onready var path_follow: PathFollow2D = $Path/PathFollow
+onready var curve: Curve2D = Curve2D.new()
 
-enum {
-	NULL=-1,
-	UP,
-	DOWN,
-	LEFT,
-	RIGHT
-}
 
-var direction: Dictionary = {
-	UP: Vector2.UP,
-	DOWN: Vector2.DOWN,
-	LEFT: Vector2.LEFT,
-	RIGHT: Vector2.RIGHT
-}
+func _ready():
+	Event.connect("new_curve_point", self, "_on_Head_new_curve_point")
+	path.curve = curve
+	head.speed = SPEED
+	head.rot_speed = ROT_SPEED
+	head.position_update_interval = POSITION_UPDATE_INTERVAL
+	set_process(false)
 
-var _direction_state: int = UP
-var _input_direction: int = NULL
 
+func _process(delta: float) -> void:
+	path_follow.set_offset(path_follow.get_offset() + SPEED * delta)
 
-func _physics_process(delta: float) -> void:
-	_input_direction = _get_direction_input()
 
-	match _direction_state:
-		NULL:
-			print("No input detected, not changing direction.")
-		UP:
-			if _input_direction == LEFT or _input_direction == RIGHT:
-				_change_direction_to(_input_direction)
-		DOWN:
-			if _input_direction == LEFT or _input_direction == RIGHT:
-				_change_direction_to(_input_direction)
-		LEFT:
-			if _input_direction == UP or _input_direction == DOWN:
-				_change_direction_to(_input_direction)
-		RIGHT:
-			if _input_direction == UP or _input_direction == DOWN:
-				_change_direction_to(_input_direction)
+func _draw() -> void:
+	if path.curve.get_baked_points().size() >= 2:
+		draw_polyline(path.curve.get_baked_points(), Color.aquamarine, 1, true)
 
-	move_and_slide(SPEED * direction[_direction_state], Vector2.ZERO, false, 4, PI/4, false)
 
+func add_point_to_curve(coordinates: Vector2) -> void:
+	path.curve.add_point(coordinates)
+	# need at least 2 points to enable processing (sprite move)
+	if not is_processing() and path.curve.get_baked_points().size() >= 2:
+		set_process(true)
+	# update call is to draw curve
+	update()
 
-func _change_direction_to(new_direction: int) -> void:
-	_direction_state = new_direction
 
-	match _direction_state:
-		UP:
-			rotation = 0
-		DOWN:
-			rotation = PI
-		LEFT:
-			rotation = -PI/2
-		RIGHT:
-			rotation = PI/2
-
-
-func _get_direction_input() -> int:
-	if Input.is_action_pressed("move_up"):
-		return UP
-	elif Input.is_action_pressed("move_down"):
-		return DOWN
-	elif Input.is_action_pressed("move_left"):
-		return LEFT
-	elif Input.is_action_pressed("move_right"):
-		return RIGHT
-
-	return NULL
+func _on_Head_new_curve_point(coordinates: Vector2) -> void:
+	add_point_to_curve(coordinates)
diff --git a/src/event.gd b/src/event.gd
new file mode 100644
index 0000000..845de39
--- /dev/null
+++ b/src/event.gd
@@ -0,0 +1,3 @@
+extends Node
+
+signal new_curve_point(coordinates)
\ No newline at end of file
diff --git a/src/icons/game.png.import b/src/icons/game.png.import
index de0f547..b57be49 100644
--- a/src/icons/game.png.import
+++ b/src/icons/game.png.import
@@ -20,16 +20,16 @@ compress/hdr_mode=0
 compress/bptc_ldr=0
 compress/normal_map=0
 flags/repeat=0
-flags/filter=true
+flags/filter=false
 flags/mipmaps=false
 flags/anisotropic=false
 flags/srgb=2
-process/fix_alpha_border=true
+process/fix_alpha_border=false
 process/premult_alpha=false
 process/HDR_as_SRGB=false
 process/invert_color=false
 process/normal_map_invert_y=false
 stream=false
 size_limit=0
-detect_3d=true
+detect_3d=false
 svg/scale=1.0
diff --git a/src/project.godot b/src/project.godot
index a78075e..016d03d 100644
--- a/src/project.godot
+++ b/src/project.godot
@@ -13,16 +13,33 @@ _global_script_classes=[ {
 "class": "Main",
 "language": "GDScript",
 "path": "res://main.gd"
+}, {
+"base": "Node2D",
+"class": "Snake",
+"language": "GDScript",
+"path": "res://entities/actors/snake/snake.gd"
 } ]
 _global_script_class_icons={
-"Main": ""
+"Main": "",
+"Snake": ""
 }
 
 [application]
 
 config/name="GoGodotJam3"
+run/main_scene="res://Main.tscn"
 config/icon="res://icons/game.png"
 
+[autoload]
+
+Event="*res://event.gd"
+
+[debug]
+
+gdscript/warnings/unused_argument=false
+gdscript/warnings/unused_signal=false
+gdscript/warnings/return_value_discarded=false
+
 [gui]
 
 common/drop_mouse_on_gui_input_disabled=true
@@ -35,6 +52,29 @@ texture={
 "process/fix_alpha_border": false
 }
 
+[input]
+
+move_up={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":87,"unicode":0,"echo":false,"script":null)
+ ]
+}
+move_down={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":83,"unicode":0,"echo":false,"script":null)
+ ]
+}
+move_left={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":65,"unicode":0,"echo":false,"script":null)
+ ]
+}
+move_right={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":68,"unicode":0,"echo":false,"script":null)
+ ]
+}
+
 [physics]
 
 common/enable_pause_aware_picking=true
-- 
cgit v1.2.3-70-g09d2