TheFlipSide/Scripts/player.gd

41 lines
1.4 KiB
GDScript

extends CharacterBody2D
@onready var tile_layer : TileMapLayer = $"../TileMap"
@export var acceleration : float = 50
@export var braking : float = 20
@export var move_speed : float = 100
var move_input : float
func _physics_process(delta: float) -> void:
#Horizontal movement.
move_input = Input.get_axis("move_left", "move_right")
if move_input != 0:
velocity.x = lerp(velocity.x, move_input * move_speed, acceleration * delta)
else:
velocity.x = lerp(velocity.x, 0.0, braking * delta)
#Vertical movement.
velocity.y += al_globals.gravity * delta
if Input.is_action_just_pressed("flip"):
al_globals.gravity *= -1
move_and_slide() #Allow physics control.
spike_detection() #Checks for tiles with the "Spike" custom data enabled.
func spike_detection() -> void:
#Adapted from Godot forum post.
#Get current tile, and then put the data from it into a variable.
var tile_coord = tile_layer.local_to_map(tile_layer.to_local(global_position))
var tile_data = tile_layer.get_cell_tile_data(tile_coord)
#If it has data, check for it. If it has the "spike" custom data, ensure the gravity is set back to normal, and then reset the level.
if tile_data:
var custom_data = tile_data.get_custom_data("spike")
if custom_data == true:
if al_globals.gravity < 0:
al_globals.gravity *= -1
get_tree().change_scene_to_file("res://Scenes/main.tscn")