Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Area2D
- @export var player_index = 0
- @onready var gun_anchor = $ShipSprite/Gun/ProjAnchor
- var gem_count := 0
- var health := 100
- var can_shoot := true
- var fire_rate := 0.25
- var max_speed := 600.0
- var velocity := Vector2(0, 0)
- var boost_speed := 1500.0
- var normal_speed := 600.0
- var steering_factor := 10.0
- var direction := Vector2(0, 0)
- func _ready():
- area_entered.connect(_on_area_entered)
- set_health(health)
- func _process(delta: float) -> void:
- _input_handler()
- var viewport_size := get_viewport_rect().size
- position.x = wrapf(position.x, 0, viewport_size.x)
- position.y = wrapf(position.y, 0, viewport_size.y)
- #direction.x = Input.get_axis("move_left", "move_right")
- #direction.y = Input.get_axis("move_up", "move_down")
- direction.x = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_X)
- direction.y = Input.get_joy_axis(player_index, JOY_AXIS_LEFT_Y)
- if direction.length() > 1.0:
- direction = direction.normalized()
- var desired_velocity := max_speed * direction
- var steering_vector := desired_velocity - velocity
- velocity += steering_vector * steering_factor * delta
- position += velocity * delta
- if direction.length() > 0.0:
- get_node("ShipSprite").rotation = velocity.angle()
- func _input_handler():
- if Input.is_joy_button_pressed(player_index, JOY_BUTTON_B) or Input.is_action_just_pressed("boost"):
- max_speed = boost_speed
- get_node("BoostTimer").start()
- $SFX_Boost.play()
- if Input.is_joy_button_pressed(player_index, JOY_BUTTON_X) or Input.is_action_just_pressed("shoot") and can_shoot == true:
- shoot()
- get_node("GunTimer").start(fire_rate)
- can_shoot = false
- func _on_area_entered(area_that_entered):
- if area_that_entered.is_in_group("healing_item"):
- $SFX_Health.play()
- set_health(health + 10)
- print("health+")
- if area_that_entered.is_in_group("gem"):
- $SFX_Gem.play()
- set_gem_count(gem_count + 1)
- print("gem+")
- if area_that_entered.is_in_group("projectile"):
- $SFX_Hurt.play()
- set_health(health -5)
- print("health-")
- func set_gem_count(new_gem_count):
- gem_count = new_gem_count
- get_node("UI/GemCount").text = "x" + str(gem_count)
- func set_health(new_health):
- health = new_health
- get_node("UI/HealthBar").value = health
- func shoot():
- const BULLET = preload("res://lessons/bullet.tscn")
- var newBullet = BULLET.instantiate()
- newBullet.global_position = gun_anchor.global_position
- newBullet.global_rotation = gun_anchor.global_rotation
- gun_anchor.add_child(newBullet)
- $SFX_Shoot.play()
- func _on_boost_timer_timeout():
- max_speed = normal_speed
- func _on_gun_timer_timeout():
- can_shoot = true
Advertisement
Add Comment
Please, Sign In to add comment