Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- signal swing_sword
- @onready var main = get_node("/root/Main")
- @onready var sprite = $AnimatedSprite2D
- @onready var weapPivot = $WeaponPivot
- @onready var atkArea = $WeaponPivot/AttackArea
- @onready var atkTimer = $AttackTimer
- @export var speed := 150
- var base_speed = speed
- var move_dir
- var aim_dir
- var mouse
- var angle
- var set_angle
- var _facing = "0"
- func _ready():
- sprite.play("idle1")
- func _physics_process(_delta):
- get_input()
- move_and_slide()
- func _get_facing():
- if angle == 0:
- _facing = "0"
- elif angle == 1:
- _facing = "1"
- elif angle == 2:
- _facing = "2"
- else:
- _facing == "3"
- func get_input():
- var move_dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")
- var aim_dir = Input.get_vector("aim_left", "aim_right", "aim_up", "aim_down")
- var mouse = get_local_mouse_position()
- var angle = snappedf(mouse.angle(), PI / 2) / (PI / 2)
- angle = wrapi(int(angle), 0, 4)
- if aim_dir != Vector2.ZERO:
- weapPivot.rotation = lerp_angle(rotation, aim_dir.angle(), 1)
- else:
- weapPivot.rotation = lerpf(rotation, mouse.angle(), 1)
- velocity = move_dir.clamp(Vector2(-1, -1), Vector2(1, 1)).normalized() * speed
- if velocity.length() != 0:
- sprite.animation = "walk" + str(angle)
- else:
- sprite.animation = "idle" + str(angle)
- if Input.is_action_just_pressed("attack"):
- swing_sword.emit()
- _get_facing()
- attack()
- func attack():
- sprite.animation = "attack" + str(_facing)
- atkTimer.start(1)
- func _on_atktimer_timeout():
- sprite.play()
- if velocity.length() != 0:
- sprite.animation = "walk" + str(_facing)
- elif velocity.length() > 0 or velocity.length() < 0:
- sprite.animation = "idle" + str(_facing)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement