Coverage for src/retrospeedlab/games/milkhop/sheet.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-12-03 23:09 +0000

1from dataclasses import dataclass 

2 

3from retrospeedlab.games.milkhop.art import ( 

4 BARGE, 

5 BOTTLE, 

6 CART, 

7 DECK_TILE, 

8 DOOR_TILE, 

9 GOAT, 

10 HEART, 

11 MILA, 

12 MILA_FLAT, 

13 MILA_HOP, 

14 MILA_WINDUP, 

15 PATH_TILE, 

16 PUNT, 

17 QUAY_TILE, 

18 ROAD_TILE, 

19 SWAN, 

20 TRAM, 

21 Rows, 

22) 

23from retrospeedlab.games.milkhop.palette import LEGEND 

24from retrospeedlab.games.shared.font import FontSheet, install_font 

25from retrospeedlab.games.shared.sprites import stamp 

26 

27SPRITE_BANK, FONT_BANK = 0, 1 

28FONT = FontSheet(FONT_BANK, 0, 0) 

29BANK_WIDTH = 256 

30 

31 

32@dataclass(slots=True, frozen=True) 

33class Placement: 

34 name: str 

35 bank: int 

36 x: int 

37 y: int 

38 rows: Rows 

39 

40 @property 

41 def width(self) -> int: 

42 return len(self.rows[0]) 

43 

44 @property 

45 def height(self) -> int: 

46 return len(self.rows) 

47 

48 

49ARTS = ( 

50 ("Road", ROAD_TILE), 

51 ("Towpath", PATH_TILE), 

52 ("Quay", QUAY_TILE), 

53 ("Door", DOOR_TILE), 

54 ("Deck", DECK_TILE), 

55 ("Bottle", BOTTLE), 

56 ("Heart", HEART), 

57 ("Mila", MILA), 

58 ("Mila winding up", MILA_WINDUP), 

59 ("Mila hopping", MILA_HOP), 

60 ("Mila flattened", MILA_FLAT), 

61 ("Goat", GOAT), 

62 ("Swan", SWAN), 

63 ("Tram", TRAM), 

64 ("Cheese cart", CART), 

65 ("Punt", PUNT), 

66 ("Barge", BARGE), 

67) 

68 

69 

70def shelved(arts: tuple[tuple[str, Rows], ...]) -> tuple[Placement, ...]: 

71 placements: list[Placement] = [] 

72 x = y = shelf = 0 

73 for name, rows in arts: 

74 if x + len(rows[0]) > BANK_WIDTH: 

75 x, y, shelf = 0, y + shelf, 0 

76 placements.append(Placement(name, SPRITE_BANK, x, y, rows)) 

77 x, shelf = x + len(rows[0]), max(shelf, len(rows)) 

78 return tuple(placements) 

79 

80 

81PLACEMENTS = shelved(ARTS) 

82BY_NAME = {placement.name: placement for placement in PLACEMENTS} 

83SPRITES = { 

84 placement.name: (placement.bank, placement.x, placement.y, placement.width, placement.height) 

85 for placement in PLACEMENTS 

86} 

87 

88 

89def spot(name: str) -> Placement: 

90 return BY_NAME[name] 

91 

92 

93def install_sheet() -> None: 

94 for placement in PLACEMENTS: 

95 stamp(placement.bank, placement.x, placement.y, placement.rows, LEGEND) 

96 install_font(FONT)