80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from engine.character import BodyPart, Character
|
|
from engine.defs import DefRegistry
|
|
from engine.entity import EntityPosition
|
|
from engine.map import Map
|
|
from engine.tile import Tile, TileLayerInstance
|
|
from engine.world import World
|
|
|
|
DEFS_DIR = Path(__file__).resolve().parent.parent / "resources" / "defs"
|
|
|
|
|
|
@pytest.fixture()
|
|
def registry():
|
|
return DefRegistry.load(DEFS_DIR)
|
|
|
|
|
|
def make_human(registry, **kwargs) -> Character:
|
|
return Character(species_id="human", default_body_parts=registry.new_default_body_parts("human"), **kwargs)
|
|
|
|
|
|
def test_species_def_carries_sample_abilities(registry):
|
|
species = registry.get_species_def("human")
|
|
ability_ids = {a.id for a in species.abilities}
|
|
assert ability_ids == {"leap", "melee_attack", "ranged_attack", "time_warp_sphere"}
|
|
leap = next(a for a in species.abilities if a.id == "leap")
|
|
assert leap.required_slots == ("left_leg", "right_leg")
|
|
|
|
|
|
def test_can_perform_ability_true_when_required_parts_present(registry):
|
|
character = make_human(registry)
|
|
assert character.can_perform_ability("leap", registry) is True
|
|
|
|
|
|
def test_can_perform_ability_false_when_a_required_part_is_missing(registry):
|
|
character = make_human(registry, body_parts=[BodyPart("left_leg", "human_left_leg", removed=True)])
|
|
assert character.can_perform_ability("leap", registry) is False
|
|
|
|
|
|
def test_can_perform_ability_false_for_unknown_ability(registry):
|
|
character = make_human(registry)
|
|
assert character.can_perform_ability("fly", registry) is False
|
|
|
|
|
|
def test_can_perform_ability_false_without_species(registry):
|
|
character = Character()
|
|
assert character.can_perform_ability("leap", registry) is False
|
|
|
|
|
|
def test_world_try_leap_gated_by_missing_legs(registry):
|
|
field = Map("field", 10, 10, 1)
|
|
for x in range(10):
|
|
for y in range(10):
|
|
field.set_tile(x, y, 0, Tile(flooring=TileLayerInstance("grass")))
|
|
world = World(registry=registry)
|
|
world.add_map(field)
|
|
|
|
legless = make_human(
|
|
registry,
|
|
position=EntityPosition("field", 2, 2, 0),
|
|
body_parts=[BodyPart("left_leg", "human_left_leg", removed=True)],
|
|
)
|
|
assert world.try_leap(legless, 1, 0, 0) is False
|
|
assert (legless.position.x, legless.position.y) == (2, 2) # unchanged
|
|
|
|
|
|
def test_world_try_leap_allowed_with_both_legs(registry):
|
|
field = Map("field", 10, 10, 1)
|
|
for x in range(10):
|
|
for y in range(10):
|
|
field.set_tile(x, y, 0, Tile(flooring=TileLayerInstance("grass")))
|
|
world = World(registry=registry)
|
|
world.add_map(field)
|
|
|
|
healthy = make_human(registry, position=EntityPosition("field", 2, 2, 0))
|
|
assert world.try_leap(healthy, 1, 0, 0) is True
|
|
assert (healthy.position.x, healthy.position.y) == (4, 2)
|