Limited Space
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
ld54/nodes/npc/npc.gd

48 lines
944 B

extends CharacterBody2D
class_name Npc
const SPEED = 100
const MINIMUM_STATE_DURATION = 3
const STATES = ['panic', 'dead']
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction = 0
var rising_speed = 0
func _ready():
pick_new_direction()
$fsm.set_states(STATES)
func panic_state(delta):
if is_on_wall():
direction *= -1
var should_update = (
$fsm.state_duration >= MINIMUM_STATE_DURATION
&& ceil($fsm.state_duration) % 2 == (randi() %2)
)
if should_update:
pick_new_direction()
$fsm.state_duration = 0
velocity.x = direction
velocity.y += gravity * delta
move_and_slide()
func on_enter_dead_state():
$shape.queue_free()
func dead_state(_delta):
velocity = Vector2(0, -rising_speed * (randf_range(0.9,1)))
move_and_slide()
func die(level_speed):
$fsm.set_next_state('dead')
rising_speed = level_speed
func pick_new_direction():
direction = [1,-1][randi_range(0,1)] * SPEED