summaryrefslogtreecommitdiff
path: root/src/entities/actors/snake/scripts/head.gd
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-01 03:27:11 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-01 03:27:11 -0600
commit2c5d588bc593929a96f0e58d7d1ab52f269add55 (patch)
tree17c3cd8f5a880197734fc1530766f01eec5ea425 /src/entities/actors/snake/scripts/head.gd
parentdaa89fdeccc46e1ae871651eeedde47808ac03d1 (diff)
make working basic snake, refactor code and made gif recorder its separate thing
Diffstat (limited to 'src/entities/actors/snake/scripts/head.gd')
-rw-r--r--src/entities/actors/snake/scripts/head.gd32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/entities/actors/snake/scripts/head.gd b/src/entities/actors/snake/scripts/head.gd
index ea28f74..448802e 100644
--- a/src/entities/actors/snake/scripts/head.gd
+++ b/src/entities/actors/snake/scripts/head.gd
@@ -1,30 +1,36 @@
-extends Node2D
+extends KinematicBody2D
-var speed: float = Global.SNAKE_SPEED
-var rot_speed: float = Global.SNAKE_ROT_SPEED
-var position_update_interval: float = Global.SNAKE_POSITION_UPDATE_INTERVAL
+enum {
+ LEFT=-1,
+ RIGHT=1
+}
+var velocity: Vector2 = Vector2.ZERO
var _direction: Vector2 = Vector2.UP
var _time_elapsed: float = 0.0
-func _process(delta: float) -> void:
+func _physics_process(delta: float) -> void:
if Input.is_action_pressed("move_left"):
- # _direction = _direction.rotated(deg2rad(-ROT_SPEED))
- rotate(deg2rad(-rot_speed * delta))
+ _rotate_to(LEFT)
if Input.is_action_pressed("move_right"):
- # _direction = _direction.rotated(deg2rad(ROT_SPEED))
- rotate(deg2rad(rot_speed * delta))
+ _rotate_to(RIGHT)
- move_local_x(_direction.x * speed * delta)
- move_local_y(_direction.y * speed * delta)
+ velocity = _direction * Global.SNAKE_SPEED
+ # not sure if needed, worked wonders when using a Node2D instead of KB2D
+ velocity = move_and_slide(velocity)
_handle_time_elapsed(delta)
+func _rotate_to(direction: int) -> void:
+ rotate(deg2rad(direction * Global.SNAKE_ROT_SPEED * get_physics_process_delta_time()))
+ _direction = _direction.rotated(deg2rad(direction * Global.SNAKE_ROT_SPEED * get_physics_process_delta_time()))
+
+
# 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)
+ if _time_elapsed >= Global.SNAKE_POSITION_UPDATE_INTERVAL:
+ Event.emit_signal("snake_path_new_point", global_position)
_time_elapsed = 0.0
_time_elapsed += delta