nodes/building.pyΒΆ

Part of Tanks of Freedom.

 1"""Building node: owns a procedural sprite + flag overlay.
 2
 3A neutral building can be captured by an infantry unit ending its move on
 4the building's cell. The HQ is the win condition: capture or destroy the
 5enemy HQ to win.
 6"""
 7
 8from __future__ import annotations
 9
10from simvx.core import Node2D, Signal
11from simvx.core.animation.sprite import Sprite2D
12
13from .data import BLDG_HQ, PLAYER_NEUTRAL
14from .textures import make_building, make_flag
15
16
17class Building(Node2D):
18    """Capturable building."""
19
20    def __init__(self, *, building_type: int, owner: int, cell: tuple[int, int], tile_map, **kwargs):
21        super().__init__(name=f"bldg_{building_type}_{cell[0]}_{cell[1]}", **kwargs)
22        self.type = building_type
23        self.owner = owner
24        self.cell = cell
25        self._tile_map = tile_map
26
27        self.is_hq = building_type == BLDG_HQ
28
29        wx, wy = tile_map.map_to_world(cell)
30        # Building art is taller than a tile; anchor its base on the diamond.
31        self.position = (wx, wy - 16)
32
33        self._sprite = Sprite2D(
34            texture=make_building(building_type, owner),
35            width=64,
36            height=72,
37            filter="nearest",
38            position=(0, -12),
39        )
40        self.add_child(self._sprite)
41
42        self._flag = Sprite2D(
43            texture=make_flag(owner),
44            width=14,
45            height=14,
46            filter="nearest",
47            position=(18, -42),
48        )
49        self.add_child(self._flag)
50
51        self.captured = Signal()  # (new_owner)
52
53    def set_owner(self, new_owner: int) -> None:
54        if new_owner == self.owner:
55            return
56        self.owner = new_owner
57        self._sprite.texture = make_building(self.type, new_owner)
58        self._flag.texture = make_flag(new_owner)
59        self.captured(new_owner)
60
61    def is_neutral(self) -> bool:
62        return self.owner == PLAYER_NEUTRAL