Advertisement
thatenbykiki

Untitled

Apr 15th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2.  
  3. signal swing_sword
  4.  
  5. @onready var main = get_node("/root/Main")
  6.  
  7. @onready var sprite = $AnimatedSprite2D
  8. @onready var weapPivot = $WeaponPivot
  9. @onready var atkArea = $WeaponPivot/AttackArea
  10. @onready var atkTimer = $AttackTimer
  11.  
  12. @export var speed := 150
  13.  
  14. var base_speed = speed
  15.  
  16. var move_dir
  17. var aim_dir
  18. var mouse
  19. var angle
  20. var set_angle
  21. var _facing = "0"
  22.  
  23. func _ready():
  24.     sprite.play("idle1")
  25.  
  26.  
  27. func _physics_process(_delta):
  28.     get_input()
  29.     move_and_slide()
  30.  
  31.  
  32. func _get_facing():
  33.     if angle == 0:
  34.         _facing = "0"
  35.     elif angle == 1:
  36.         _facing = "1"
  37.     elif angle == 2:
  38.         _facing = "2"
  39.     else:
  40.         _facing == "3"
  41.  
  42.  
  43. func get_input():
  44.     var move_dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")
  45.     var aim_dir = Input.get_vector("aim_left", "aim_right", "aim_up", "aim_down")
  46.     var mouse = get_local_mouse_position()
  47.     var angle = snappedf(mouse.angle(), PI / 2) / (PI / 2)
  48.     angle = wrapi(int(angle), 0, 4)
  49.    
  50.     if aim_dir != Vector2.ZERO:
  51.         weapPivot.rotation = lerp_angle(rotation, aim_dir.angle(), 1)
  52.     else:
  53.         weapPivot.rotation = lerpf(rotation, mouse.angle(), 1)
  54.     velocity = move_dir.clamp(Vector2(-1, -1), Vector2(1, 1)).normalized() * speed
  55.    
  56.     if velocity.length() != 0:
  57.         sprite.animation = "walk" + str(angle)
  58.     else:
  59.         sprite.animation = "idle" + str(angle)
  60.    
  61.     if Input.is_action_just_pressed("attack"):
  62.         swing_sword.emit()
  63.         _get_facing()
  64.         attack()
  65.    
  66.  
  67. func attack():
  68.     sprite.animation = "attack" + str(_facing)
  69.     atkTimer.start(1)
  70.  
  71. func _on_atktimer_timeout():
  72.     sprite.play()
  73.     if velocity.length() != 0:
  74.         sprite.animation = "walk" + str(_facing)
  75.     elif velocity.length() > 0 or velocity.length() < 0:
  76.         sprite.animation = "idle" + str(_facing)
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement