142 lines
5.6 KiB
GDScript
142 lines
5.6 KiB
GDScript
extends CharacterBody2D
|
|
|
|
const DAMAGE_POPUP_SCENE := preload("res://scenes/game/damage_popup.tscn")
|
|
const IMPACT_EFFECT_SCENE := preload("res://scenes/game/impact_effect.tscn")
|
|
const PLASMA_PROJECTILE_SCENE := preload("res://scenes/game/enemy_plasma_projectile.tscn")
|
|
const WEAPON_AUDIO_SCRIPT := preload("res://scripts/game/weapon_audio.gd")
|
|
const ITEM_GENERATOR := preload("res://scripts/items/item_generator.gd")
|
|
const LOOT_PICKUP := preload("res://scripts/items/loot_pickup.gd")
|
|
const LEVEL_BALANCE := preload("res://scripts/levels/level_balance.gd")
|
|
|
|
@export_enum("Normal", "Elite", "Boss") var loot_tier: int = 0
|
|
|
|
@export var max_hull := 50.0
|
|
@export var activation_range := 760.0
|
|
@export var approach_speed := 62.0
|
|
@export var approach_acceleration := 85.0
|
|
@export var turn_speed := 1.8
|
|
@export var weapon_range := 680.0
|
|
@export var weapon_cooldown := 1.5
|
|
@export var weapon_damage := 8.0
|
|
|
|
var hull := 50.0
|
|
var _player: CharacterBody2D
|
|
var _activated := false
|
|
var _sprite: Sprite2D
|
|
var _weapon_timer := 0.0
|
|
|
|
func _ready() -> void:
|
|
hull = max_hull
|
|
_sprite = $Sprite2D
|
|
add_to_group("enemies")
|
|
_weapon_timer = randf_range(0.2, weapon_cooldown)
|
|
|
|
func apply_level_settings(level: int, tier: int) -> void:
|
|
loot_tier = tier
|
|
var power := LEVEL_BALANCE.power_scale(level)
|
|
var hull_multiplier := 1.0 if tier == 0 else (2.0 if tier == 1 else 6.0)
|
|
var damage_bonus := 1.0 if tier == 0 else (1.3 if tier == 1 else 1.7)
|
|
max_hull = 50.0 * power * hull_multiplier
|
|
hull = max_hull
|
|
weapon_damage = 8.0 * power * damage_bonus
|
|
approach_speed = 62.0 * (1.0 + 0.0005 * (clampi(level, 1, 1000) - 1))
|
|
weapon_cooldown = maxf(0.55, 1.5 / (1.0 + 0.001 * (clampi(level, 1, 1000) - 1)))
|
|
|
|
func configure(player: CharacterBody2D, ship_texture: Texture2D) -> void:
|
|
_player = player
|
|
_sprite = $Sprite2D
|
|
_sprite.texture = ship_texture
|
|
var largest_dimension := maxf(ship_texture.get_width(), ship_texture.get_height())
|
|
var visual_size := 56.0 if loot_tier == 0 else (68.0 if loot_tier == 1 else 104.0)
|
|
_sprite.scale = Vector2.ONE * (visual_size / largest_dimension)
|
|
_sprite.modulate = Color("ffc785") if loot_tier == 1 else Color.WHITE
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not is_instance_valid(_player):
|
|
velocity = velocity.move_toward(Vector2.ZERO, approach_acceleration * delta)
|
|
move_and_slide()
|
|
return
|
|
|
|
var to_player := _player.global_position - global_position
|
|
if to_player.length() <= activation_range:
|
|
_activated = true
|
|
if _activated and to_player.length_squared() > 1.0:
|
|
var direction := to_player.normalized()
|
|
var desired_rotation := direction.angle() + PI * 0.5
|
|
rotation = rotate_toward(rotation, desired_rotation, turn_speed * delta)
|
|
velocity = velocity.move_toward(direction * approach_speed, approach_acceleration * delta)
|
|
_weapon_timer = maxf(0.0, _weapon_timer - delta)
|
|
if to_player.length() <= weapon_range and _weapon_timer <= 0.0:
|
|
_fire_plasma(direction)
|
|
_weapon_timer = weapon_cooldown
|
|
else:
|
|
velocity = velocity.move_toward(Vector2.ZERO, approach_acceleration * delta)
|
|
|
|
move_and_slide()
|
|
_check_player_collision()
|
|
|
|
func _check_player_collision() -> void:
|
|
for collision_index in get_slide_collision_count():
|
|
var collision := get_slide_collision(collision_index)
|
|
if collision.get_collider() == _player and _player.has_method("register_enemy_collision"):
|
|
_player.call("register_enemy_collision", global_position)
|
|
|
|
func _fire_plasma(direction: Vector2) -> void:
|
|
var projectile := PLASMA_PROJECTILE_SCENE.instantiate() as Area2D
|
|
projectile.call("configure", global_position + direction * 28.0, direction, weapon_damage)
|
|
get_parent().add_child(projectile)
|
|
# Relative to the Plasma bus: enemy shots are 4 dB quieter than player shots.
|
|
WEAPON_AUDIO_SCRIPT.play(get_parent(), global_position, ItemDefinition.WeaponKind.PLASMA, -4.0)
|
|
|
|
func take_weapon_hit(amount: float, critical: bool) -> void:
|
|
take_damage(amount, critical)
|
|
|
|
func take_damage(amount: float, critical: bool = false) -> void:
|
|
if is_queued_for_deletion() or hull <= 0.0:
|
|
return
|
|
hull = maxf(0.0, hull - maxf(0.0, amount))
|
|
_show_damage_popup(amount, critical)
|
|
if hull <= 0.0:
|
|
_reward_captain()
|
|
_drop_loot()
|
|
_spawn_destruction_effect()
|
|
queue_free()
|
|
|
|
func _reward_captain() -> void:
|
|
if is_instance_valid(_player):
|
|
GameState.captain.reward_enemy(GameState.system_level, loot_tier, float(_player.get("experience_multiplier")), float(_player.get("credits_multiplier")))
|
|
|
|
func _drop_loot() -> void:
|
|
var chance := 0.65 if loot_tier == 0 else (0.9 if loot_tier == 1 else 1.0)
|
|
if randf() > chance:
|
|
return
|
|
var pickup := Node2D.new()
|
|
pickup.set_script(LOOT_PICKUP)
|
|
pickup.set("item", ITEM_GENERATOR.generate(GameState.system_level, loot_tier))
|
|
pickup.position = position
|
|
get_parent().call_deferred("add_child", pickup)
|
|
|
|
func _spawn_destruction_effect() -> void:
|
|
var effect := IMPACT_EFFECT_SCENE.instantiate() as Node2D
|
|
effect.global_position = global_position
|
|
get_parent().add_child(effect)
|
|
effect.call("configure", true, Color(1.0, 0.36, 0.18))
|
|
|
|
func _show_damage_popup(amount: float, critical: bool = false) -> void:
|
|
if critical:
|
|
var flash := Node2D.new()
|
|
flash.set_script(preload("res://scripts/game/combat_flash.gd"))
|
|
flash.set("flash_color", Color("ffbc5c"))
|
|
flash.position = position
|
|
get_parent().add_child(flash)
|
|
var popup := DAMAGE_POPUP_SCENE.instantiate() as Label
|
|
popup.text = "%s-%s DMG" % ["KRYT! " if critical else "", _format_damage(amount)]
|
|
popup.set("is_critical", critical)
|
|
popup.global_position = global_position + Vector2(randf_range(-16.0, 16.0), -28.0)
|
|
get_parent().add_child(popup)
|
|
|
|
func _format_damage(amount: float) -> String:
|
|
if is_equal_approx(amount, roundf(amount)):
|
|
return str(int(roundf(amount)))
|
|
return "%.1f" % amount
|