player and snake movement
This commit is contained in:
parent
4576c62e4a
commit
50e2dad3e7
7 changed files with 113 additions and 23 deletions
37
prefabs/snake_segment.gd
Normal file
37
prefabs/snake_segment.gd
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
extends AnimatableBody2D
|
||||
class_name snake_segment
|
||||
|
||||
@export var is_head : bool = false
|
||||
var direction : Vector2 = Vector2.RIGHT
|
||||
var want_direction : Vector2 = Vector2.RIGHT
|
||||
|
||||
@export var next_segment : Node2D
|
||||
var start_pos : Vector2
|
||||
var target_pos : Vector2
|
||||
var move_progress : float
|
||||
# if snake is the head, it chooses where it goes
|
||||
# if snake is not the head, it goes where the one infront went
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
start_pos = position
|
||||
get_new_target()
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
move_progress += delta
|
||||
if Input.get_vector("left","right","up","down").length() > 0:
|
||||
want_direction = Input.get_vector("left","right","up","down")
|
||||
position = lerp(start_pos,target_pos,move_progress)
|
||||
if move_progress >= 1:
|
||||
get_new_target()
|
||||
|
||||
func get_new_target():
|
||||
start_pos=target_pos
|
||||
if is_head:
|
||||
direction = want_direction
|
||||
target_pos = start_pos + (direction * 320)
|
||||
else:
|
||||
target_pos = next_segment.position
|
||||
move_progress = 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue