summaryrefslogtreecommitdiff
path: root/src/entities/actors/snake
diff options
context:
space:
mode:
Diffstat (limited to 'src/entities/actors/snake')
-rw-r--r--src/entities/actors/snake/Snake.tscn23
-rw-r--r--src/entities/actors/snake/body_segment/1x1.pngbin0 -> 82 bytes
-rw-r--r--src/entities/actors/snake/body_segment/1x1.png.import35
-rw-r--r--src/entities/actors/snake/body_segment/BodySegment.tscn9
-rw-r--r--src/entities/actors/snake/head/Head.tscn10
-rw-r--r--src/entities/actors/snake/head/head.gd37
-rw-r--r--src/entities/actors/snake/snake.gd90
7 files changed, 144 insertions, 60 deletions
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
--- /dev/null
+++ b/src/entities/actors/snake/body_segment/1x1.png
Binary files 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)