Arth00r

enemyswordcodegodot

Jan 9th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.47 KB | Source Code | 0 0
  1. extends KinematicBody2D
  2.  
  3. var lifetimer = 0
  4. var thrown = false #A variable activated by the enemy script to throw the sword
  5. var velocity = Vector2.ZERO
  6. #IGNORE - For damaging the player
  7. var curr_kb_dir = Vector2(1, -0.9)
  8. var curr_kb_strength = 400
  9. var curr_damage = 1
  10.  
  11.  
  12. onready var over_parent = $Area2D.overlaps_body(get_parent())
  13.  
  14. func _physics_process(delta):
  15.    
  16.     #IGNORE - Collision with the enemy
  17.     over_parent = $Area2D.overlaps_body(get_parent())
  18.    
  19.     #IGNORE - For dealing knockback to the player
  20.     if $Sprite.flip_h == false:
  21.         curr_kb_dir.x = 1
  22.     else:
  23.         curr_kb_dir.x = -1
  24.     #--------------------------------------------
  25.    
  26.     #For rotating the sword sprite
  27.     if $Sprite.flip_h == false:
  28.         $Sprite.rotation_degrees += 30
  29.     else:
  30.         $Sprite.rotation_degrees -= 30
  31.    
  32.    
  33.    
  34.    
  35.     if thrown == true:
  36.         visible = true
  37.         $AnimationPlayer.play("active")
  38.        
  39.         #Throw it
  40.         if lifetimer < 30:
  41.             lifetimer += 1
  42.             move_and_slide(velocity)
  43.             if $Sprite.flip_h == false:
  44.                 velocity.x = 450
  45.             else:
  46.                 velocity.x = -450
  47.            
  48.         #Stop for a bit
  49.         elif lifetimer <= 45:
  50.             velocity *= 0.92
  51.             move_and_slide(velocity)
  52.             lifetimer += 1
  53.        
  54.         #Come back
  55.         else:
  56.             velocity = Vector2.ZERO
  57.             position = position.move_toward(get_parent().position, delta*lifetimer)
  58.             lifetimer += 15
  59.    
  60.     #IGNORE - Hiding the sword once it's returned to the enemy
  61.     if thrown == false:
  62.         visible = false
  63.         $AnimationPlayer.play("not active")
  64.         position = Vector2.ZERO
  65.         lifetimer = 0
  66.  
  67.  
Add Comment
Please, Sign In to add comment