Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.55 KB | Source Code | 0 0
  1. extends KinematicBody2D
  2.  
  3. # Default thrust speed.
  4. var thrust_speed : int = 20000
  5.  
  6. # Default angular speed.
  7. var angular_speed : float = PI
  8.  
  9. # Drag.
  10. var drag : int = 100
  11.  
  12. # Velocity.
  13. var velocity : Vector2 = Vector2.ZERO
  14.  
  15. # Read user input & apply.
  16. func get_input(delta):
  17.    
  18.     # Check for brake & adjust drag.
  19.     if Input.is_action_pressed("brake"):
  20.         drag = 1000
  21.     else:
  22.         drag = 100
  23.  
  24.     # Apply drag.
  25.     if velocity.x > 0:
  26.         if velocity.x >= drag: velocity.x -= drag
  27.         else: velocity.x = 0
  28.     elif velocity.x < 0:
  29.         if velocity.x <= drag: velocity.x += drag
  30.         else: velocity.x = 0
  31.     if velocity.y > 0:
  32.         if velocity.y >= drag: velocity.y -= drag
  33.         else: velocity.y = 0
  34.     elif velocity.y < 0:
  35.         if velocity.y <= drag: velocity.y += drag
  36.         else: velocity.y = 0
  37.        
  38.     # Check for guns.
  39.     if Input.is_action_pressed("left_gun"):
  40.         $LeftGun.fire()
  41.     if Input.is_action_pressed("right_gun"):
  42.         $RightGun.fire()
  43.    
  44.     # Reset direction.
  45.     var direction = 0
  46.    
  47.     # Check for turns & adjust rotation.
  48.     if Input.is_action_pressed("turn_left"):
  49.         direction = -1
  50.     if Input.is_action_pressed("turn_right"):
  51.         direction = 1
  52.     rotation += angular_speed * direction * delta
  53.    
  54.     # Check for thrust & adjust velocity.
  55.     if Input.is_action_pressed("thrust"):
  56.         velocity = Vector2.UP.rotated(rotation) * thrust_speed
  57.         $ThrustSprite.play()
  58.         $ThrustSprite.show()
  59.     else:
  60.         $ThrustSprite.hide()
  61.         $ThrustSprite.stop()
  62.        
  63.     # Apply movement.
  64.     move_and_slide(velocity * delta)
  65.  
  66. # Physics process.
  67. func _physics_process(delta):
  68.     # Receive user input.
  69.     get_input(delta)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement