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/actors/snake/scripts/head.gd | |
parent | 2838046928db956b7712d24dfb63ee45fbc4d050 (diff) |
working snake generation and path following, minor refactor
Diffstat (limited to 'src/entities/actors/snake/scripts/head.gd')
-rw-r--r-- | src/entities/actors/snake/scripts/head.gd | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/entities/actors/snake/scripts/head.gd b/src/entities/actors/snake/scripts/head.gd new file mode 100644 index 0000000..ea28f74 --- /dev/null +++ b/src/entities/actors/snake/scripts/head.gd @@ -0,0 +1,30 @@ +extends Node2D + +var speed: float = Global.SNAKE_SPEED +var rot_speed: float = Global.SNAKE_ROT_SPEED +var position_update_interval: float = Global.SNAKE_POSITION_UPDATE_INTERVAL + +var _direction: Vector2 = Vector2.UP +var _time_elapsed: float = 0.0 + + +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 |