extends KinematicBody2D
class_name PlantFormPlayer
export var speed = 100.0 # 速度 像素/秒
export var gravity = 6000 # 重力加速度
export var jump_force = 3000.0 # 单次跳跃高度
var velocity = Vector2.ZERO # 速度向量
var dir = Vector2.ZERO # 方向
enum states {
ON_FLOOR, # 在地面上
IN_AIR # 在空中
}
var _state = states.ON_FLOOR
func _process(delta):
velocity = Vector2.ZERO
# 左右移动
dir.x = Input.get_axis("ui_left","ui_right")
velocity = dir * speed
# 实现下落
velocity.y += gravity * delta
# 跳跃
if _state == states.ON_FLOOR and Input.is_action_pressed("ui_accept"):
velocity.y = -jump_force
change_state(states.IN_AIR)
velocity = move_and_slide(velocity,Vector2.UP)
if is_on_floor():
change_state(states.ON_FLOOR)
# 更改状态
func change_state(new_state:int):
_state = new_state
# 根据不同的状态,进行一些额外配置
match _state:
states.IN_AIR:
pass
states.ON_FLOOR:
pass
extends PlantFormPlayer
func _ready():
pass