Snake/prefabs/snake_segment.gd

77 lines
1.9 KiB
GDScript3
Raw Normal View History

2025-08-07 22:45:24 +10:00
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
@export var door : Node2D
2025-09-02 23:17:10 +10:00
@export var door_disabled : bool = false
2025-08-07 22:45:24 +10:00
var start_pos : Vector2
var target_pos : Vector2
var move_progress : float
var seg_id : int
2025-08-07 22:58:16 +10:00
var snake_speed : float = 1
2025-08-07 22:45:24 +10:00
# 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:
2025-08-07 22:58:16 +10:00
target_pos = position
2025-08-07 22:45:24 +10:00
start_pos = position
get_new_target()
become_real()
2025-08-07 22:45:24 +10:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
2025-08-07 22:58:16 +10:00
move_progress += delta * snake_speed
if(!Router.player_mode):
if Input.is_action_just_pressed("left"):
want_direction = Vector2.LEFT
elif Input.is_action_just_pressed("right"):
want_direction = Vector2.RIGHT
elif Input.is_action_just_pressed("up"):
want_direction = Vector2.UP
elif Input.is_action_just_pressed("down"):
want_direction = Vector2.DOWN
2025-08-07 22:45:24 +10:00
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:
2025-08-07 22:58:16 +10:00
if want_direction != direction*-1:
direction = want_direction
2025-08-07 22:45:24 +10:00
target_pos = start_pos + (direction * 320)
else:
target_pos = next_segment.position
move_progress = 0
2025-08-08 15:02:44 +10:00
func become_real():
2025-08-08 15:27:48 +10:00
collision_layer = 1
2025-08-08 15:02:44 +10:00
z_index = 10
modulate = Color.WHITE
pass
func become_fake():
#z_index = 0
#modulate = Color.DIM_GRAY
#collision_layer = 2
2025-08-08 15:02:44 +10:00
pass
2025-08-15 13:33:08 +10:00
func _on_eat_die_box_area_entered(area: Area2D) -> void:
if(area.is_in_group("apple")):
area.queue_free()
print("mmm tasty apple")
2025-08-15 15:12:02 +10:00
Router.eat_apple.emit()
2025-08-15 13:33:08 +10:00
if(area.is_in_group("wall")):
2025-09-02 23:17:10 +10:00
Router.die.emit()
if(area.is_in_group("snake")):
if (area.get_parent().is_head):
Router.die.emit()