CCCPaste

Godot Basic Platform Player Example

extends KinematicBody2D

#Godot Basic Platform Player Script
#Copyright (C) 2021  Kristofer Occhipinti - Films By Kris
#http://filmsbykris.com

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation version 3 of the License.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see .

export (int) var speed = 500
export (int) var jump_speed = -500
export (int) var gravity = 800

export (float, 0, 1.0) var friction = 0.1
export (float, 0, 1.0) var acceleration = 0.25

var velocity = Vector2.ZERO
onready var sprite = $Sprite

func get_input():
	walk()
	jump()
	
func walk():
	var dir = 0
	if Input.is_action_pressed("walk_right"):
		dir += 1
		sprite.flip_h = 0
	if Input.is_action_pressed("walk_left"):
		dir -= 1
		sprite.flip_h = 1
	if dir != 0:
		velocity.x = lerp(velocity.x, dir * speed, acceleration)
	else:
		velocity.x = lerp(velocity.x, 0, friction)
	
func jump():
	if Input.is_action_just_pressed("jump"):
		if is_on_floor():
			velocity.y = jump_speed
		
func animation():
	if velocity.x > 1 || velocity.x < -1:
		sprite.play("walk")
	else:
		sprite.play("stand")
		
func _physics_process(delta):
	get_input()
	animation()
	velocity.y += gravity * delta
	velocity = move_and_slide(velocity, Vector2.UP)