Guest User

Battle Ships

a guest
Apr 9th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Area2D
  2.  
  3. @export var player_index = 0
  4. @onready var gun_anchor = $ShipSprite/Gun/ProjAnchor
  5.  
  6. var gem_count := 0
  7. var health := 100
  8.  
  9. var can_shoot := true
  10. var fire_rate := 0.25
  11.  
  12. var max_speed := 600.0
  13. var velocity := Vector2(0, 0)
  14. var boost_speed := 1500.0
  15. var normal_speed := 600.0
  16. var steering_factor := 10.0
  17. var direction := Vector2(0, 0)
  18.  
  19. func _ready():
  20.     area_entered.connect(_on_area_entered)
  21.     set_health(health)
  22.  
  23. func _process(delta: float) -> void:
  24.     _input_handler()
  25.     var viewport_size := get_viewport_rect().size
  26.     position.x = wrapf(position.x, 0, viewport_size.x)
  27.     position.y = wrapf(position.y, 0, viewport_size.y)
  28.    
  29.     #direction.x = Input.get_axis("move_left", "move_right")
  30.     #direction.y = Input.get_axis("move_up", "move_down")
  31.     direction.x = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_X)
  32.     direction.y = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_Y)
  33.    
  34.     if direction.length() > 1.0:
  35.         direction = direction.normalized()
  36.    
  37.     var desired_velocity := max_speed * direction
  38.     var steering_vector := desired_velocity - velocity
  39.     velocity += steering_vector * steering_factor * delta
  40.     position += velocity * delta
  41.    
  42.     if direction.length() > 0.0:
  43.         get_node("ShipSprite").rotation = velocity.angle()
  44.  
  45. func _input_handler():
  46.     if Input.is_joy_button_pressed(player_index, JOY_BUTTON_B) or Input.is_action_just_pressed("boost"):
  47.         max_speed = boost_speed
  48.         get_node("BoostTimer").start()
  49.         $SFX_Boost.play()
  50.    
  51.     if Input.is_joy_button_pressed(player_index, JOY_BUTTON_X) or Input.is_action_just_pressed("shoot") and can_shoot == true:
  52.         shoot()
  53.         get_node("GunTimer").start(fire_rate)
  54.         can_shoot = false
  55.  
  56. func _on_area_entered(area_that_entered):
  57.     if area_that_entered.is_in_group("healing_item"):
  58.         $SFX_Health.play()
  59.         set_health(health + 10)
  60.         print("health+")
  61.     if area_that_entered.is_in_group("gem"):
  62.         $SFX_Gem.play()
  63.         set_gem_count(gem_count + 1)
  64.         print("gem+")
  65.     if area_that_entered.is_in_group("projectile"):
  66.         $SFX_Hurt.play()
  67.         set_health(health -5)
  68.         print("health-")
  69.  
  70. func set_gem_count(new_gem_count):
  71.     gem_count = new_gem_count
  72.     get_node("UI/GemCount").text = "x" + str(gem_count)
  73.  
  74. func set_health(new_health):
  75.     health = new_health
  76.     get_node("UI/HealthBar").value = health
  77.  
  78. func shoot():
  79.     const BULLET = preload("res://lessons/bullet.tscn")
  80.     var newBullet = BULLET.instantiate()
  81.     newBullet.global_position = gun_anchor.global_position
  82.     newBullet.global_rotation = gun_anchor.global_rotation
  83.     gun_anchor.add_child(newBullet)
  84.     $SFX_Shoot.play()
  85.  
  86. func _on_boost_timer_timeout():
  87.     max_speed = normal_speed
  88.  
  89. func _on_gun_timer_timeout():
  90.     can_shoot = true
  91.  
Advertisement
Add Comment
Please, Sign In to add comment