extends KinematicBody2D class_name PlayerCharacter export (int) var speed = 8 export (float) var walk_wobble_coeff = 5 var velocity = Vector2() var look_dir = 1 # right = 1, left = -1 var dialogue_active_with: Node = null func get_input(): velocity = Vector2() if Input.is_action_pressed('ui_right'): velocity.x += 1 if Input.is_action_pressed('ui_left'): velocity.x -= 1 if Input.is_action_pressed('ui_down'): velocity.y += 1 if Input.is_action_pressed('ui_up'): velocity.y -= 1 velocity = velocity.normalized() * speed func _process(_delta): # Update walking wobble var is_moving = velocity.length_squared() > .0 if is_moving: var time = OS.get_system_time_msecs() / 1000.0 var rot = 0.2 * sin(walk_wobble_coeff * time) $Sprite.set_rotation(rot) else: $Sprite.set_rotation(0) # Look at walking direction if velocity[0] > .0: look_dir = 1 elif velocity[0] < .0: look_dir = -1 $Sprite.set_scale(Vector2(look_dir, 1)) func run_dialogue(other): dialogue_active_with = other var dialogues: Array = other.get_dialogue() for d in dialogues: var name = d["name"] var body = d["body"] print("[%s] %s" % [name, body]) func _physics_process(_delta): get_input() velocity = move_and_slide(velocity) var slide_count = get_slide_count() # If there are no collisions, player must have exited dialogue if slide_count == 0: dialogue_active_with = null # If the player is colliding with a Dialogue group node and does not have an # active dialogue, run dialogue with the node for idx in slide_count: var coll = get_slide_collision(idx) if coll.collider.is_in_group("Dialogue") and dialogue_active_with == null: dialogue_active_with = coll.collider run_dialogue(coll.collider) break func change_level(num: int): var new_scene = "res://scenes/Scene" + String(num) + ".tscn" var success = get_tree().change_scene(new_scene) if success == OK: print("Changed level to " + new_scene) else: push_error("Failed to change level to " + new_scene)