extends CharacterBody3D
const SPEED = 20
const JUMP_VELOCITY = 20
# Controles na preferencia do projeto
# move_forward(sd) _backward(w) _left(a) _right(d) jump(spaço) attack(J)
#
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var esta_pulando := false
var esta_movendo := false
var input_dir
var direction
# 1 = ESQUERA 2 = DIREITA
var virado := 0
@onready var animar := $AnimationPlayer
#funcão do jogo- procurar chamar outras funcoes
func _physics_process(delta):
pega_vetor_de_entrada()
aplica_movimento()
gravidade()
pulo()
move_and_slide()
#eu acabei escolhendo colocar sprites virados pra ambos os lados
#inverter sprites deu mais erros ainda
if is_on_floor() and virado == 2 and !esta_movendo:
esta_pulando = false
animar.play("dparado")
elif is_on_floor() and virado == 1 and !esta_movendo:
esta_pulando = false
animar.play("eparado")
if Input.is_action_just_released("move_right"):
esta_movendo = false
if Input.is_action_just_released("move_left"):
esta_movendo = false
if Input.is_action_just_released("move_backward"):
esta_movendo = false
if Input.is_action_just_released("move_forward"):
esta_movendo = false
#andar p cima
if input_dir == Vector2(0,-1) && virado == 2:
animar.play("dcorrendo")
if input_dir == Vector2(0,-1) && virado == 1:
animar.play("ecorrendo")
#andar p direita
if input_dir == Vector2(1,0) and !esta_pulando:
virado = 2
esta_movendo = true
animar.play("dcorrendo")
if esta_pulando and virado == 2:
esta_pulando = true
animar.play("dpulando")
#anda p esquerda
if input_dir == Vector2(-1,0) and !esta_pulando:
virado = 1
esta_movendo = true
animar.play("ecorrendo")
if esta_pulando and virado == 1:
esta_pulando = true
animar.play("epulando")
#andar p baixo
if input_dir == Vector2(0,1) and virado == 2:
animar.play("dcorrendo")
if input_dir == Vector2(0,1) and virado == 1:
animar.play("ecorrendo")
func gravidade():
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * 0.1 #delta
func pulo():
# Handle Jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
#até aqui o codigo estaria OK. Abaixo é extra para teste
esta_pulando = true
func pega_vetor_de_entrada():
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
func aplica_movimento():
direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)