186 lines
7.6 KiB
GDScript
186 lines
7.6 KiB
GDScript
extends SceneTree
|
|
|
|
## Pruebas reales de instanciacion, independencia, actualizacion y fisica.
|
|
## Ejecutar tras importar: godot --headless --path . --script res://tests/test_level_kit_runtime.gd
|
|
## Estas pruebas NO se ejecutan mediante unittest de Python.
|
|
|
|
var _failures: int = 0
|
|
|
|
|
|
func _initialize() -> void:
|
|
call_deferred("_run")
|
|
|
|
|
|
func _run() -> void:
|
|
await _verify_terrain()
|
|
await _verify_props_and_signs()
|
|
await _verify_one_way_physics()
|
|
await _verify_presets()
|
|
await _verify_levels()
|
|
if _failures == 0:
|
|
print("OK LEVEL KIT: superficies, independencia, props, carteles, one-way, presets y niveles")
|
|
quit(0 if _failures == 0 else 1)
|
|
|
|
|
|
## Un cambio de tamano no debe alterar otras instancias ni separar arte y fisica.
|
|
func _verify_terrain() -> void:
|
|
var packed: PackedScene = load("res://world/terrain/ground.tscn") as PackedScene
|
|
var first: TerrainPiece = packed.instantiate() as TerrainPiece
|
|
var second: TerrainPiece = packed.instantiate() as TerrainPiece
|
|
root.add_child(first)
|
|
root.add_child(second)
|
|
first.dimensions = Vector2(736.0, 160.0)
|
|
await process_frame
|
|
await process_frame
|
|
var first_shape: RectangleShape2D = first.get_node("CollisionShape2D").shape as RectangleShape2D
|
|
var second_shape: RectangleShape2D = second.get_node("CollisionShape2D").shape as RectangleShape2D
|
|
_check(first_shape != second_shape, "Las formas fisicas no pueden compartirse entre instancias")
|
|
_check(first_shape.size == Vector2(736.0, 160.0), "La colision debe seguir Dimensions")
|
|
_check(second_shape.size == Vector2(512.0, 256.0), "Editar una pieza no debe redimensionar otra")
|
|
_check((first.get_node("Texture") as Sprite2D).region_rect.size == first.dimensions, "La textura debe seguir Dimensions")
|
|
_check((first.get_node("Top") as Sprite2D).position == -first.dimensions * 0.5, "El borde superior debe conservar su anclaje")
|
|
first.collision_enabled = false
|
|
await process_frame
|
|
await process_frame
|
|
_check(first.collision_layer == 0 and (first.get_node("CollisionShape2D") as CollisionShape2D).disabled, "Una superficie decorativa no debe bloquear")
|
|
_check(second.collision_layer == 1, "Desactivar una pieza no debe afectar a la otra")
|
|
first.queue_free()
|
|
second.queue_free()
|
|
await process_frame
|
|
|
|
|
|
## Colision y profundidad son independientes; el texto pertenece a cada cartel.
|
|
func _verify_props_and_signs() -> void:
|
|
var packed: PackedScene = load("res://world/props/barrel.tscn") as PackedScene
|
|
var barrel: SceneryProp = packed.instantiate() as SceneryProp
|
|
var other: SceneryProp = packed.instantiate() as SceneryProp
|
|
root.add_child(barrel)
|
|
root.add_child(other)
|
|
barrel.draw_layer = 10
|
|
barrel.collision_dimensions = Vector2(40.0, 60.0)
|
|
await process_frame
|
|
await process_frame
|
|
_check(barrel.z_index == 10, "El barril debe poder dibujarse delante")
|
|
_check((barrel.get_node("Body") as StaticBody2D).collision_layer == 1, "Cambiar profundidad no debe eliminar colision")
|
|
var shape: RectangleShape2D = barrel.get_node("Body/CollisionShape2D").shape as RectangleShape2D
|
|
var other_shape: RectangleShape2D = other.get_node("Body/CollisionShape2D").shape as RectangleShape2D
|
|
_check(shape != other_shape and other_shape.size == Vector2(54.0, 76.0), "Las colisiones de decoracion deben ser independientes")
|
|
barrel.collision_enabled = false
|
|
await process_frame
|
|
await process_frame
|
|
_check((barrel.get_node("Body") as StaticBody2D).collision_layer == 0, "El barril decorativo debe atravesarse")
|
|
_check(barrel.z_index == 10, "Desactivar colision no debe cambiar profundidad")
|
|
|
|
var sign_scene: PackedScene = load("res://world/signs/sign_post.tscn") as PackedScene
|
|
var sign: Node2D = sign_scene.instantiate() as Node2D
|
|
root.add_child(sign)
|
|
sign.set("text", "TEST\nSECOND LINE")
|
|
sign.set("font_size", 18)
|
|
await process_frame
|
|
await process_frame
|
|
var label: Label = sign.get_node("Text") as Label
|
|
_check(label.text == "TEST\nSECOND LINE", "El cartel debe mostrar el texto editado")
|
|
_check(label.get_theme_font_size("font_size") == 18, "El cartel debe actualizar el tamano de letra")
|
|
sign.queue_free()
|
|
barrel.queue_free()
|
|
other.queue_free()
|
|
await process_frame
|
|
|
|
|
|
## Comprueba el comportamiento de la fisica, no solo el flag guardado en la escena.
|
|
func _verify_one_way_physics() -> void:
|
|
var packed: PackedScene = load("res://world/terrain/platform.tscn") as PackedScene
|
|
var platform: TerrainPiece = packed.instantiate() as TerrainPiece
|
|
platform.position = Vector2(300.0, 100.0)
|
|
root.add_child(platform)
|
|
var body := CharacterBody2D.new()
|
|
body.position = Vector2(300.0, 170.0)
|
|
body.collision_layer = 2
|
|
body.collision_mask = 1
|
|
var collision := CollisionShape2D.new()
|
|
var shape := RectangleShape2D.new()
|
|
shape.size = Vector2(20.0, 20.0)
|
|
collision.shape = shape
|
|
body.add_child(collision)
|
|
root.add_child(body)
|
|
await physics_frame
|
|
await physics_frame
|
|
# La velocidad depende del paso de fisica del motor; no se cambia su configuracion.
|
|
var frames: int = ceili(float(Engine.physics_ticks_per_second) * 0.5)
|
|
for _frame: int in range(frames):
|
|
await physics_frame
|
|
body.velocity = Vector2(0.0, -300.0)
|
|
body.move_and_slide()
|
|
_check(body.position.y < 70.0, "La plataforma debe atravesarse desde abajo")
|
|
for _frame: int in range(frames):
|
|
await physics_frame
|
|
body.velocity = Vector2(0.0, 300.0)
|
|
body.move_and_slide()
|
|
_check(body.is_on_floor(), "El cuerpo debe aterrizar sobre la plataforma")
|
|
_check(absf(body.position.y - 80.0) < 2.0, "El aterrizaje debe coincidir con la cara superior")
|
|
body.queue_free()
|
|
platform.queue_free()
|
|
await process_frame
|
|
|
|
|
|
## Carga recursivamente las piezas y verifica que los objetos tengan icono real.
|
|
func _verify_presets() -> void:
|
|
for path: String in _scene_paths("res://world") + _scene_paths("res://pickups/variants"):
|
|
var packed: PackedScene = load(path) as PackedScene
|
|
_check(packed != null, "No se pudo cargar " + path)
|
|
if packed == null:
|
|
continue
|
|
var instance: Node = packed.instantiate()
|
|
root.add_child(instance)
|
|
await process_frame
|
|
if instance is Pickup:
|
|
_check((instance as Pickup).pickup_data != null, "Objeto sin datos: " + path)
|
|
_check((instance.get_node("Sprite2D") as Sprite2D).texture != null, "Objeto sin icono: " + path)
|
|
instance.queue_free()
|
|
await process_frame
|
|
|
|
|
|
## F6 debe funcionar sin un menu o singleton que inicialice antes el escenario.
|
|
func _verify_levels() -> void:
|
|
for path: String in [
|
|
"res://levels/templates/level_template.tscn",
|
|
"res://levels/examples/scenery_catalog.tscn",
|
|
"res://levels/level_01.tscn",
|
|
"res://levels/level_00.tscn",
|
|
]:
|
|
var packed: PackedScene = load(path) as PackedScene
|
|
_check(packed != null, "No se pudo cargar el nivel " + path)
|
|
if packed == null:
|
|
continue
|
|
var level: LevelRoot = packed.instantiate() as LevelRoot
|
|
root.add_child(level)
|
|
current_scene = level
|
|
await physics_frame
|
|
await physics_frame
|
|
var camera: Camera2D = level.get_node("Player/Camera2D") as Camera2D
|
|
_check(camera.limit_right == ceili(level.camera_bounds.end.x), "La camara debe respetar Camera Bounds en " + path)
|
|
if path.ends_with("level_template.tscn"):
|
|
var player: CharacterBody2D = level.get_node("Player") as CharacterBody2D
|
|
for _frame: int in range(6):
|
|
await physics_frame
|
|
_check(player.is_on_floor(), "El jugador de la plantilla debe comenzar apoyado en el suelo")
|
|
current_scene = null
|
|
level.queue_free()
|
|
await process_frame
|
|
|
|
|
|
func _scene_paths(folder: String) -> Array[String]:
|
|
var result: Array[String] = []
|
|
for file_name: String in DirAccess.get_files_at(folder):
|
|
if file_name.ends_with(".tscn"):
|
|
result.append(folder.path_join(file_name))
|
|
for directory: String in DirAccess.get_directories_at(folder):
|
|
result.append_array(_scene_paths(folder.path_join(directory)))
|
|
return result
|
|
|
|
|
|
func _check(condition: bool, message: String) -> void:
|
|
if not condition:
|
|
_failures += 1
|
|
push_error("Level Kit: " + message)
|