Guest User

Player script

a guest
Nov 23rd, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. extends CharacterBody2D
  2.  
  3. @export var move_speed: float = 300
  4. @export var starting_direction: Vector2 = Vector2(0, 0.5)
  5. @export var player_health: float = 100
  6. # parameters/Idle/blend_position
  7. @onready var animation_tree = $AnimationTree
  8. @onready var state_machine = animation_tree.get("parameters/playback")
  9. var enemy_is_attacking = false
  10. var is_not_detected = false
  11.  
  12.  
  13. func _ready():
  14. position = Vector2(512, 450)
  15. update_animation_params_player(starting_direction)
  16.  
  17.  
  18.  
  19. func _physics_process(_delta):
  20. var input_direction = Vector2(
  21. Input.get_action_strength("right") - Input.get_action_strength("left"),
  22. Input.get_action_strength("down") - Input.get_action_strength("up")
  23. )
  24. update_animation_params_player(input_direction)
  25. velocity = input_direction * move_speed
  26.  
  27. move_and_slide()
  28. pick_new_state()
  29.  
  30. func update_animation_params_player(move_input : Vector2):
  31. if(move_input != Vector2.ZERO):
  32. animation_tree.set("parameters/Walk/blend_position", move_input)
  33. animation_tree.set("parameters/Idle/blend_position", move_input)
  34.  
  35. func pick_new_state():
  36. if(velocity != Vector2.ZERO):
  37. state_machine.travel("Walk")
  38. else:
  39. state_machine.travel("Idle")
  40.  
  41. func check_detection_pos(): #Quadrants go left to right top down
  42. var firstQuad = true #Player is not in the quadrants
  43. var secondQuad = true
  44. var thirdQuad = true
  45. var fourthQuad = true
  46.  
  47. if position.y <= 344 and position.y >= 59 and position.x > 368:
  48. firstQuad = false
  49. elif position.y <= 344 and position.y >= 59 and position.x < 938:
  50. secondQuad = false
  51. elif position.y >= 344 and position.y <= 629 and position.x > 368:
  52. thirdQuad = false
  53. elif position.y >= 344 and position.y <= 629 and position.x < 938:
  54. fourthQuad = false
  55. else:
  56. return true
  57.  
  58.  
  59. func _on_bb_enemy_enemy_attacking():
  60. is_not_detected = check_detection_pos()
  61. if position == Vector2(512, 450):
  62. pass
  63. elif velocity != Vector2.ZERO: #player is moving
  64. pass
  65. elif velocity == Vector2.ZERO and is_not_detected: #player is not moving but hasn't been detected
  66. pass
  67. else:
  68. print("Enemy is attacking")
  69.  
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment