Randoloot/tests/items/shield_collision_test.gd
2026-10-25 22:38:13 +01:00

75 lines
3.3 KiB
GDScript

extends Node2D
const PROJECTILE := preload("res://scenes/game/weapon_projectile.tscn")
const INTERCEPT := preload("res://scripts/game/shield_intersections.gd")
class StraightAlien extends "res://scripts/game/enemy_ship.gd":
func _laser_aim_direction(direction: Vector2) -> Vector2: return direction
func shot(kind: int) -> Area2D:
var projectile := PROJECTILE.instantiate() as Area2D
projectile.enemy_shot = true
projectile.missile_homing = false
projectile.collision_mask = 2
projectile.configure(Vector2(-150,0),Vector2.RIGHT,kind,1200,20,1000)
add_child(projectile)
projectile.set_physics_process(false)
return projectile
func _ready() -> void:
GameState.start_new_run()
var player := preload("res://scenes/game/player_ship.tscn").instantiate()
player.get_node("Camera2D").enabled = false
add_child(player)
player.set_physics_process(false)
player.get_node("WeaponController").set_physics_process(false)
player.position = Vector2.ZERO
player.absorption = Vector3.ZERO
await get_tree().physics_frame
await get_tree().physics_frame
for kind in [ItemDefinition.WeaponKind.PLASMA,ItemDefinition.WeaponKind.MISSILE,ItemDefinition.WeaponKind.GAMMA]:
player.shields = 100
var projectile := shot(kind)
projectile._physics_process(0.25)
assert(projectile.is_queued_for_deletion() and is_equal_approx(projectile.position.length(),50),"Fast projectile stops on shield perimeter")
assert(player.shields==80)
var radiation := player.get_node_or_null("RadiationStatus")
if radiation != null: radiation.set_physics_process(false)
player.shields = 100
var glancing := INTERCEPT.intercept({},Vector2(-100,40),Vector2(100,40),2,[],get_tree())
assert(glancing.collider==player and glancing.position.is_equal_approx(Vector2(-30,40)),"Shield catches glancing hits outside hull")
var enemy := preload("res://scenes/game/enemy_ship.tscn").instantiate()
enemy.set_script(StraightAlien)
enemy.position = Vector2(-250,0)
add_child(enemy)
enemy.set_physics_process(false)
enemy.weapon_kind = ItemDefinition.WeaponKind.LASER
enemy.weapon_damage = 20
enemy._player = player
enemy._combat_target = player
enemy._fire_plasma(Vector2.RIGHT)
var found := false
for child in get_children():
if child is Line2D and child.get_point_count()==2:
assert(child.get_point_position(1).is_equal_approx(Vector2(-50,0)),"Enemy laser ends at shield edge")
found = true
assert(found and player.shields==84)
player.shields = 0
player.hull = 100
var bare := shot(ItemDefinition.WeaponKind.PLASMA)
bare._physics_process(0.25)
assert(is_equal_approx(bare.position.x,-18) and player.hull==80,"Bare hull uses original collision")
player.shields = 100
var cover := StaticBody2D.new()
cover.position = Vector2(-80,0)
cover.collision_layer = 1
var shape := CollisionShape2D.new()
var circle := CircleShape2D.new()
circle.radius = 8
shape.shape = circle
cover.add_child(shape)
add_child(cover)
await get_tree().physics_frame
await get_tree().physics_frame
var blocked := shot(ItemDefinition.WeaponKind.PLASMA)
blocked.collision_mask = 3
blocked._physics_process(0.25)
assert(blocked.position.x < -80 and player.shields==100,"Closer physical cover blocks shield interception")
print("SHIELD_COLLISION_TEST: PASS (fast plasma/missiles/gamma, glancing shots, exact laser endpoint, bare hull, physical cover)")