Randoloot/tests/items/enemy_asteroid_cover_test.gd
2026-10-16 22:04:36 +02:00

82 lines
3.6 KiB
GDScript

extends Node2D
const PLAYER := preload("res://scenes/game/player_ship.tscn")
const ENEMY := preload("res://scenes/game/enemy_ship.tscn")
const PROJECTILE := preload("res://scenes/game/weapon_projectile.tscn")
const PROJECTILE_SCRIPT := preload("res://scripts/game/weapon_projectile.gd")
class AccurateEnemy:
extends "res://scripts/game/enemy_ship.gd"
func _laser_aim_direction(direction: Vector2) -> Vector2:
return direction
func _ready() -> void:
GameState.start_new_run()
var ship := PLAYER.instantiate() as CharacterBody2D
ship.name = "PlayerShip"
ship.position = Vector2(150, 0)
add_child(ship)
ship.set_physics_process(false)
ship.get_node("WeaponController").set_physics_process(false)
var enemy := ENEMY.instantiate() as CharacterBody2D
enemy.set_script(AccurateEnemy)
enemy.position = Vector2(-200, 0)
add_child(enemy)
enemy.set("_player", ship)
enemy.set_physics_process(false)
var asteroid := StaticBody2D.new()
asteroid.set_script(preload("res://scripts/levels/exploration_poi.gd"))
asteroid.set("kind", "asteroid")
asteroid.set("poi_id", "enemy-cover-test")
asteroid.set("player", ship)
add_child(asteroid)
asteroid.set_physics_process(false)
var asteroid_hp := float(asteroid.get("hull"))
var player_hp := float(ship.get("hull")) + float(ship.get("shields"))
var minerals := GameState.captain.minerals.duplicate()
await get_tree().physics_frame
await get_tree().process_frame
for kind in [ItemDefinition.WeaponKind.PLASMA, ItemDefinition.WeaponKind.MACHINE_GUN, ItemDefinition.WeaponKind.GAMMA, ItemDefinition.WeaponKind.MISSILE]:
enemy.set("weapon_kind", kind)
enemy.call("_fire_plasma", Vector2.RIGHT)
var shot: Area2D
for child in get_children():
if child is Area2D and child.get_script() == PROJECTILE_SCRIPT and not child.is_queued_for_deletion():
shot = child
assert(shot != null and (shot.collision_mask & 32) != 0)
shot.set_physics_process(false)
shot.set("damage", 10000.0)
shot.call("_physics_process", 1.0)
assert(shot.is_queued_for_deletion())
assert(shot.position.x < 0.0) # Stops at the asteroid even at high speed.
assert(float(asteroid.get("hull")) == asteroid_hp)
assert(not asteroid.get("_claimed"))
assert(float(ship.get("hull")) + float(ship.get("shields")) == player_hp)
assert(GameState.captain.minerals == minerals)
enemy.set("weapon_kind", ItemDefinition.WeaponKind.LASER)
enemy.call("_fire_plasma", Vector2.RIGHT)
var beam: Line2D
for child in get_children():
if child is Line2D:
beam = child
assert(beam != null and beam.get_point_position(1).x < 0.0)
assert(float(asteroid.get("hull")) == asteroid_hp)
assert(float(ship.get("hull")) + float(ship.get("shields")) == player_hp)
var legacy := preload("res://scenes/game/enemy_plasma_projectile.tscn").instantiate() as Area2D
legacy.call("configure", Vector2(-100, 0), Vector2.RIGHT, 10000.0)
add_child(legacy)
legacy.set_physics_process(false)
legacy.call("_physics_process", 1.0)
assert(legacy.is_queued_for_deletion())
assert(float(asteroid.get("hull")) == asteroid_hp)
# Player mining remains functional, using the existing target layer.
var player_shot := PROJECTILE.instantiate() as Area2D
player_shot.call("configure", Vector2(-100, 0), Vector2.RIGHT, ItemDefinition.WeaponKind.PLASMA, 600.0, 2.0, 500.0)
add_child(player_shot)
player_shot.set_physics_process(false)
player_shot.call("_physics_process", 1.0)
assert(player_shot.is_queued_for_deletion())
assert(is_equal_approx(float(asteroid.get("hull")), asteroid_hp - 2.0))
print("ENEMY_ASTEROID_COVER_TEST: PASS (plasma/machine gun/gamma/missile/laser, fast-shot collision, no asteroid damage/mining/rewards, player protected, legacy projectile, player mining retained)")
get_tree().quit()