Guest User

Untitled

a guest
Mar 23rd, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. extends CharacterBody2D
  2.  
  3. class_name Player
  4.  
  5. @export var speed : int = 5
  6. @export var can_move : bool = true
  7. @export var health : int = 1
  8.  
  9. @onready var punch = $PunchCollision
  10. @onready var animation = $AnimationPlayer
  11.  
  12. var dir: String #where the player is moving
  13.  
  14.  
  15. # Called when the node enters the scene tree for the first time.
  16. func _ready():
  17. pass # Replace with function body.
  18.  
  19.  
  20. # Called every frame. 'delta' is the elapsed time since the previous frame.
  21. func _process(delta):
  22. if health < 1:
  23. queue_free()
  24. # move_and_slide()
  25.  
  26.  
  27.  
  28. func _physics_process(delta):
  29. var input_vector = Input.get_vector("left", "right", "up", "down")
  30. velocity += input_vector * speed * 100 * delta
  31. if can_move == true:
  32. sprite_movement()
  33. position = velocity
  34. move_and_slide()
  35.  
  36.  
  37.  
  38.  
  39. if Input.is_action_just_pressed("punch"):
  40. can_move = false
  41. animation.play("punch")
  42.  
  43.  
  44. func sprite_movement():
  45. if Input.is_action_pressed("up"):
  46. rotation_degrees = 0
  47. elif Input.is_action_pressed("down"):
  48. rotation_degrees = 180
  49. elif Input.is_action_pressed("right"):
  50. rotation_degrees = 90
  51. elif Input.is_action_pressed("left"):
  52. rotation_degrees = 270
  53.  
  54.  
  55.  
  56. func _on_punch_collision_area_entered(area):
  57. if area is Bullet:
  58. var direction : int = rotation_degrees
  59. var current_dir : int = area.rotation_degrees
  60. area.rotation_degrees = direction
  61. area.dir_vector = area.dir_vector.rotated(deg_to_rad(direction - current_dir))
  62. area.velocity = -150
  63. Eventsbus.deflected.emit(direction)
  64. area.is_deflected = true
  65. area.can_deflect = false
  66.  
Advertisement
Add Comment
Please, Sign In to add comment