diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-05-31 03:49:28 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-05-31 03:49:28 -0600 |
commit | bd197168c39551c3456099ed2a39375a8442483f (patch) | |
tree | adeafb80b75e3566918f35141c2392af590897c3 /src/entities | |
parent | 2838046928db956b7712d24dfb63ee45fbc4d050 (diff) |
working snake generation and path following, minor refactor
Diffstat (limited to 'src/entities')
15 files changed, 104 insertions, 175 deletions
diff --git a/src/entities/actors/snake/Snake.tscn b/src/entities/actors/snake/Snake.tscn deleted file mode 100644 index 5548f40..0000000 --- a/src/entities/actors/snake/Snake.tscn +++ /dev/null @@ -1,39 +0,0 @@ -[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="BodySegment" parent="." instance=ExtResource( 2 )] -position = Vector2( 0, 16 ) -PREV_SEGMENT_NP = NodePath("../Head") - -[node name="BodySegment2" parent="." instance=ExtResource( 2 )] -position = Vector2( 0, 32 ) -PREV_SEGMENT_NP = NodePath("../BodySegment") - -[node name="BodySegment3" parent="." instance=ExtResource( 2 )] -position = Vector2( 0, 48 ) -PREV_SEGMENT_NP = NodePath("../BodySegment2") - -[node name="BodySegment4" parent="." instance=ExtResource( 2 )] -position = Vector2( 0, 64 ) -PREV_SEGMENT_NP = NodePath("../BodySegment3") - -[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/BodySegment.tscn b/src/entities/actors/snake/body_segment/BodySegment.tscn deleted file mode 100644 index 4bf02b0..0000000 --- a/src/entities/actors/snake/body_segment/BodySegment.tscn +++ /dev/null @@ -1,28 +0,0 @@ -[gd_scene load_steps=3 format=2] - -[ext_resource path="res://entities/actors/snake/sprites/segment.png" type="Texture" id=1] -[ext_resource path="res://entities/actors/snake/body_segment/body_segment.gd" type="Script" id=2] - -[node name="BodySegment" type="Node2D"] -script = ExtResource( 2 ) - -[node name="Sprite" type="Sprite" parent="."] -texture = ExtResource( 1 ) - -[node name="DirectionLine" type="Line2D" parent="."] -visible = false -points = PoolVector2Array( 0, 0, 0, -32 ) -width = 2.0 -default_color = Color( 1, 0, 0, 1 ) - -[node name="DirectionToPreviousLine" type="Line2D" parent="."] -visible = false -points = PoolVector2Array( 0, 0, 0, -32 ) -width = 2.0 -default_color = Color( 0, 0, 1, 1 ) - -[node name="PrevPivot" type="Node2D" parent="."] -position = Vector2( 0, -8 ) - -[node name="NextPivot" type="Node2D" parent="."] -position = Vector2( 0, 8 ) diff --git a/src/entities/actors/snake/body_segment/body_segment.gd b/src/entities/actors/snake/body_segment/body_segment.gd deleted file mode 100644 index eabede3..0000000 --- a/src/entities/actors/snake/body_segment/body_segment.gd +++ /dev/null @@ -1,27 +0,0 @@ -extends Node2D - -export(NodePath) var PREV_SEGMENT_NP: NodePath - -onready var prev_segment: Node2D = get_node(PREV_SEGMENT_NP) -onready var prev_segment_next_pivot: Node2D = prev_segment.get_node("NextPivot") -onready var _prev_pivot: Node2D = $PrevPivot -onready var _next_pivot: Node2D = $NextPivot - -var rot_speed: float = Global.SNAKE_SEGMENT_ROT_SPEED - -var prev_segment_next_pivot_to_center: Vector2 -var _center_to_prev_pivot: Vector2 -var _angle_between_segments: float - - -func _process(delta: float) -> void: - prev_segment_next_pivot_to_center = prev_segment_next_pivot.global_position - prev_segment.global_position - _center_to_prev_pivot = _prev_pivot.global_position - global_position - - _angle_between_segments = _center_to_prev_pivot.angle_to(prev_segment_next_pivot_to_center) - if _angle_between_segments > 0.0: - rotate(deg2rad(-rot_speed * delta)) - else: - rotate(deg2rad(rot_speed * delta)) - - global_position = prev_segment_next_pivot.global_position - _center_to_prev_pivot diff --git a/src/entities/actors/snake/scenes/Body.tscn b/src/entities/actors/snake/scenes/Body.tscn new file mode 100644 index 0000000..93020b2 --- /dev/null +++ b/src/entities/actors/snake/scenes/Body.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://entities/actors/snake/sprites/body.png" type="Texture" id=1] +[ext_resource path="res://entities/actors/snake/scripts/generic_segment.gd" type="Script" id=2] + +[node name="BodyPathFollow" type="PathFollow2D"] +loop = false +script = ExtResource( 2 ) + +[node name="Body" type="Node2D" parent="."] +rotation = 1.5708 + +[node name="Sprite" type="Sprite" parent="Body"] +texture = ExtResource( 1 ) diff --git a/src/entities/actors/snake/head/Head.tscn b/src/entities/actors/snake/scenes/Head.tscn index a77cbef..2dc1666 100644 --- a/src/entities/actors/snake/head/Head.tscn +++ b/src/entities/actors/snake/scenes/Head.tscn @@ -1,13 +1,10 @@ [gd_scene load_steps=3 format=2] [ext_resource path="res://entities/actors/snake/sprites/head.png" type="Texture" id=1] -[ext_resource path="res://entities/actors/snake/head/head.gd" type="Script" id=2] +[ext_resource path="res://entities/actors/snake/scripts/head.gd" type="Script" id=2] [node name="HeadPoint" type="Node2D"] script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] texture = ExtResource( 1 ) - -[node name="NextPivot" type="Node2D" parent="."] -position = Vector2( 0, 8 ) diff --git a/src/entities/actors/snake/scenes/Snake.tscn b/src/entities/actors/snake/scenes/Snake.tscn new file mode 100644 index 0000000..cb1eb2a --- /dev/null +++ b/src/entities/actors/snake/scenes/Snake.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=6 format=2] + +[ext_resource path="res://entities/actors/snake/scenes/Head.tscn" type="PackedScene" id=1] +[ext_resource path="res://entities/actors/snake/scenes/Body.tscn" type="PackedScene" id=2] +[ext_resource path="res://entities/actors/snake/scripts/snake.gd" type="Script" id=3] +[ext_resource path="res://entities/actors/snake/scenes/Tail.tscn" type="PackedScene" id=4] + +[sub_resource type="Curve2D" id=1] +_data = { +"points": PoolVector2Array( ) +} + +[node name="Snake" type="Node2D"] +script = ExtResource( 3 ) +BODY_SEGMENT_NP = ExtResource( 2 ) +TAIL_SEGMENT_NP = ExtResource( 4 ) + +[node name="Path" type="Path2D" parent="."] +curve = SubResource( 1 ) + +[node name="Head" parent="." instance=ExtResource( 1 )] diff --git a/src/entities/actors/snake/scenes/Tail.tscn b/src/entities/actors/snake/scenes/Tail.tscn new file mode 100644 index 0000000..bb30c42 --- /dev/null +++ b/src/entities/actors/snake/scenes/Tail.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://entities/actors/snake/sprites/tail.png" type="Texture" id=1] +[ext_resource path="res://entities/actors/snake/scripts/generic_segment.gd" type="Script" id=2] + +[node name="TailPathFollow" type="PathFollow2D"] +loop = false +script = ExtResource( 2 ) +TYPE = "tail" + +[node name="Tail" type="Node2D" parent="."] +rotation = 1.5708 + +[node name="Sprite" type="Sprite" parent="Tail"] +texture = ExtResource( 1 ) diff --git a/src/entities/actors/snake/scripts/generic_segment.gd b/src/entities/actors/snake/scripts/generic_segment.gd new file mode 100644 index 0000000..ec34f43 --- /dev/null +++ b/src/entities/actors/snake/scripts/generic_segment.gd @@ -0,0 +1,8 @@ +extends PathFollow2D + +export(String, "body", "tail") var TYPE: String = "body" +var speed: float = Global.SNAKE_SPEED + + +func _process(delta: float) -> void: + offset += speed * delta
\ No newline at end of file diff --git a/src/entities/actors/snake/head/head.gd b/src/entities/actors/snake/scripts/head.gd index 96707b7..ea28f74 100644 --- a/src/entities/actors/snake/head/head.gd +++ b/src/entities/actors/snake/scripts/head.gd @@ -8,10 +8,6 @@ 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)) diff --git a/src/entities/actors/snake/scripts/snake.gd b/src/entities/actors/snake/scripts/snake.gd new file mode 100644 index 0000000..e604011 --- /dev/null +++ b/src/entities/actors/snake/scripts/snake.gd @@ -0,0 +1,42 @@ +class_name Snake +extends Node2D + +export(PackedScene) var BODY_SEGMENT_NP: PackedScene +export(PackedScene) var TAIL_SEGMENT_NP: PackedScene + +onready var path: Path2D = $Path + +var current_body_segments: int = 0 +var max_body_segments: int = 1 +var body_segment_size: float = 16.0 + + +func _ready(): + Event.connect("new_curve_point", self, "_on_Head_new_curve_point") + + +func _draw() -> void: + if path.curve.get_baked_points().size() >= 2: + draw_polyline(path.curve.get_baked_points(), Color.aquamarine, 1, true) + + +func add_point_to_curve(coordinates: Vector2) -> void: + path.curve.add_point(coordinates) + # update call is to draw curve as there are new points + update() + + if current_body_segments < max_body_segments: + add_snake_segment(BODY_SEGMENT_NP) + elif current_body_segments == max_body_segments: + add_snake_segment(TAIL_SEGMENT_NP) + + +func add_snake_segment(type: PackedScene) -> void: + if path.curve.get_baked_length() >= (current_body_segments + 1.0) * body_segment_size: + var _temp_body_segment: PathFollow2D = type.instance() + path.add_child(_temp_body_segment) + current_body_segments += 1 + + +func _on_Head_new_curve_point(coordinates: Vector2) -> void: + add_point_to_curve(coordinates) diff --git a/src/entities/actors/snake/snake.gd b/src/entities/actors/snake/snake.gd deleted file mode 100644 index fb817da..0000000 --- a/src/entities/actors/snake/snake.gd +++ /dev/null @@ -1,35 +0,0 @@ -class_name Snake -extends Node2D - -export(PackedScene) var BODY_SEGMENT_NP: PackedScene - -onready var head: Node2D = $Head -onready var path: Path2D = $Path -onready var path_follow: PathFollow2D = $Path/PathFollow -onready var curve: Curve2D = Curve2D.new() - -var speed: float = Global.SNAKE_SPEED - - -func _ready(): - Event.connect("new_curve_point", self, "_on_Head_new_curve_point") - path.curve = curve - - -func _process(delta: float) -> void: - path_follow.offset = path_follow.offset + speed * delta - - -func _draw() -> void: - if path.curve.get_baked_points().size() >= 2: - draw_polyline(path.curve.get_baked_points(), Color.aquamarine, 1, true) - - -func add_point_to_curve(coordinates: Vector2) -> void: - path.curve.add_point(coordinates) - # update call is to draw curve - update() - - -func _on_Head_new_curve_point(coordinates: Vector2) -> void: - add_point_to_curve(coordinates) diff --git a/src/entities/actors/snake/sprites/1x1.png b/src/entities/actors/snake/sprites/1x1.png Binary files differdeleted file mode 100644 index 89d23b0..0000000 --- a/src/entities/actors/snake/sprites/1x1.png +++ /dev/null diff --git a/src/entities/actors/snake/sprites/segment.png b/src/entities/actors/snake/sprites/body.png Binary files differindex ebef3b4..ebef3b4 100644 --- a/src/entities/actors/snake/sprites/segment.png +++ b/src/entities/actors/snake/sprites/body.png diff --git a/src/entities/actors/snake/sprites/1x1.png.import b/src/entities/actors/snake/sprites/body.png.import index f3df488..31a844f 100644 --- a/src/entities/actors/snake/sprites/1x1.png.import +++ b/src/entities/actors/snake/sprites/body.png.import @@ -2,15 +2,15 @@ importer="texture" type="StreamTexture" -path="res://.import/1x1.png-a365c2cbbf23daa7097bc2296bfd3da2.stex" +path="res://.import/body.png-01afb246ae03e839b63ac17a1e318df3.stex" metadata={ "vram_texture": false } [deps] -source_file="res://entities/actors/snake/sprites/1x1.png" -dest_files=[ "res://.import/1x1.png-a365c2cbbf23daa7097bc2296bfd3da2.stex" ] +source_file="res://entities/actors/snake/sprites/body.png" +dest_files=[ "res://.import/body.png-01afb246ae03e839b63ac17a1e318df3.stex" ] [params] diff --git a/src/entities/actors/snake/sprites/segment.png.import b/src/entities/actors/snake/sprites/segment.png.import deleted file mode 100644 index fb8abf6..0000000 --- a/src/entities/actors/snake/sprites/segment.png.import +++ /dev/null @@ -1,35 +0,0 @@ -[remap] - -importer="texture" -type="StreamTexture" -path="res://.import/segment.png-63caac6c6161fc5c022cf02c07309ac1.stex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://entities/actors/snake/sprites/segment.png" -dest_files=[ "res://.import/segment.png-63caac6c6161fc5c022cf02c07309ac1.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 |