Godot Version
Question
extends KinematicBody2D
#-------------Variaveis de Direção horizontal
export var Speed = 1.5
var Vel_x = 0
var jumpForce = 250
var jumpSpeed = 40
var sliding = false
var airJump = false
var airJumpBlock = false
#--------------Variaveis de Direção Vertical
export var Vel_Jump = -125 #força do pulo
var Gravity = ProjectSettings.get_setting('physics/2d/default_gravity') #força variavel de umpurrão
var velocity = Vector2.ZERO #neste caso, nn recebe nada, porem ira receber a força de gravidade
var direction = Vector2.ZERO
onready var isWall = $wallcast as RayCast2D
#----------variaveis de animacao
var sprint = false
var walking = false
var attacking = false
#--------------Funções
func Attack():
if Input.is_action_just_pressed("attack"):
attacking = true
return attacking
func Sprint():
sprint = false
#--------------------
if Input.is_action_pressed("sprint"):
if Input.is_action_pressed("left") or Input.is_action_pressed("right"):
sprint = true
#-----------------------
else:
Vel_x = 0
return sprint
func Movement():
direction.x = Input.get_action_strength("right") - Input.get_action_strength('left')
if direction.x != 0:
if Sprint():
Vel_x = direction.x * 1.5
Vel_x += Speed * direction.x
$Sprite.scale.x = direction.x
$wallcast.rotation_degrees = direction.x * -90
$"Range Damage".scale.x = direction.x
walking = true
else:
Vel_x = 0
walking = false
move_local_x(Vel_x)
return walking
#---------------------------Funções Motoras Verticais]
func _Gravity():
var is_on_floor = $RayCast2D.is_colliding()
var OnWall = $wallcast.is_colliding()
#adiciona gravidade quando nao estou no chao
if !is_on_floor:
velocity.y += Gravity * 0.08
if Input.is_action_just_pressed('jump'):
if is_on_floor:
velocity.y = Vel_Jump
if direction.x != 0 and OnWall && !is_on_floor:
velocity.y = Gravity * 0.08
if Input.is_action_just_pressed('jump'):
airJump = true
$Timer.start()
else:
pass
#aplica velocidade vertical
velocity = move_and_slide(velocity, Vector2.UP)
func LastWallJump():
var is_on_floor = $RayCast2D.is_colliding()
var OnWall = $wallcast.is_colliding()
if Input.is_action_just_pressed("jump") && OnWall && !is_on_floor:
airJump = true
#------loop principal
func _physics_process(delta):
var is_on_floor = $RayCast2D.is_colliding()
var isWall = $wallcast.is_colliding()
var func_Movement = Movement()
var func_Gravity = _Gravity()
var func_Sprint = Sprint()
var func_Attack = Attack()
if !airJumpBlock && !is_on_floor && airJump:
velocity.y = Vel_Jump * 1.5
velocity.x += jumpSpeed * -direction.x
else:
velocity.x += -velocity.x * 0.2
if isWall or is_on_floor:
if airJump == false:
airJumpBlock = false
print(airJumpBlock)
print(airJump)
#---- realizar animações câmera
if Input.is_action_just_pressed("sprint"):
$ZoomGame.play("ZoomOut")
elif Input.is_action_just_released("sprint"):
$ZoomGame.play_backwards("ZoomOut")
#---- realizar animações movimento
if func_Attack:
$AnimationPlayer.play("DownAttack")
elif !is_on_floor:
$AnimationPlayer.play("jump")
if velocity.y > 0:
$AnimationPlayer.play("air")
elif walking && !func_Sprint:
$AnimationPlayer.play("walk")
elif func_Sprint:
$AnimationPlayer.play("run")
else:
$AnimationPlayer.play("stopped")
#conequições
func _on_AnimationPlayer_animation_finished(anim_name):
if anim_name == "DownAttack":
attacking = false
func _on_Range_Damage_body_entered(body):
body.queue_free()
func _on_Timer_timeout():
airJumpBlock = true
airJump = false