返回 CodeWhale
habitat.rs
1 //! Authored marine poses and their composition with the existing whale.
2 //! Elapsed time and the resting whale state are illustrative caller inputs.
3
4 use std::time::Duration;
5
6 use ratatui::{buffer::Buffer, layout::Rect};
7
8 use super::Entry;
9 use crate::{
10 BubbleField, Caps, DistantWhale, FishSchool, Habitat, HabitatDensity, Heading, Jellyfish,
11 MarineSnow, Message, MotionMode, Paint, Shoal, Theme, Whale, WhaleState,
12 };
13
14 fn fish(area: Rect, buf: &mut Buffer, theme: &Theme) {
15 FishSchool::new(Duration::from_millis(2_400), MotionMode::Full)
16 .density(HabitatDensity::Rich)
17 .paint(area, buf, theme);
18 }
19
20 fn jelly(area: Rect, buf: &mut Buffer, theme: &Theme) {
21 Jellyfish::new(Duration::from_millis(1_800), MotionMode::Full).paint(area, buf, theme);
22 }
23
24 fn bubbles(area: Rect, buf: &mut Buffer, theme: &Theme) {
25 BubbleField::new(Duration::from_millis(1_200), MotionMode::Full)
26 .density(HabitatDensity::Rich)
27 .paint(area, buf, theme);
28 }
29
30 fn shoal(area: Rect, buf: &mut Buffer, theme: &Theme) {
31 Shoal::new(Duration::from_millis(9_000), MotionMode::Full)
32 .density(HabitatDensity::Rich)
33 .paint(area, buf, theme);
34 }
35
36 fn snow(area: Rect, buf: &mut Buffer, theme: &Theme) {
37 MarineSnow::new(Duration::from_millis(4_000), MotionMode::Full)
38 .density(HabitatDensity::Rich)
39 .paint(area, buf, theme);
40 }
41
42 fn distant_whale(area: Rect, buf: &mut Buffer, theme: &Theme) {
43 DistantWhale::new(Duration::from_millis(40_000), MotionMode::Full)
44 .hold_visit(true)
45 .paint(area, buf, theme);
46 }
47
48 fn scene_with(area: Rect, buf: &mut Buffer, theme: &Theme, motion: MotionMode) {
49 let area = area.intersection(buf.area);
50 if area.is_empty() {
51 return;
52 }
53 Heading::new("Codewhale / clear water")
54 .meta(if motion.animates() {
55 "Native habitat"
56 } else {
57 "Reduced motion"
58 })
59 .paint(Rect { height: 1, ..area }, buf, theme);
60 let words = Rect {
61 x: area.x.saturating_add(2),
62 y: area.y.saturating_add(4),
63 width: area.width.min(32).saturating_sub(2),
64 height: area.height.saturating_sub(4).min(7),
65 }
66 .intersection(area);
67 Message::new(
68 "One ocean",
69 "A small school, a far shoal.\nOne visiting jellyfish.\nSnow, bubbles, a distant whale.",
70 )
71 .paint(words, buf, theme);
72 let whale = Rect {
73 x: area.x.saturating_add(area.width.saturating_sub(32) / 2),
74 y: area.y.saturating_add(3),
75 width: area.width.min(32),
76 height: area.height.saturating_sub(3).min(18),
77 }
78 .intersection(area);
79 Whale::new(WhaleState::Rest).paint(whale, buf, theme);
80 Habitat::new(Duration::from_millis(70_000), motion)
81 .density(HabitatDensity::Rich)
82 .hold_jellyfish_visit(true)
83 .paint(area, buf, theme);
84 }
85
86 fn scene(area: Rect, buf: &mut Buffer, theme: &Theme) {
87 scene_with(area, buf, theme, MotionMode::Full);
88 }
89
90 fn ascii(area: Rect, buf: &mut Buffer, theme: &Theme) {
91 let ascii = Theme::new(Caps {
92 ascii: true,
93 ..theme.caps()
94 })
95 .ground(theme.ground_kind());
96 scene_with(area, buf, &ascii, MotionMode::Full);
97 }
98
99 fn reduced(area: Rect, buf: &mut Buffer, theme: &Theme) {
100 scene_with(area, buf, theme, MotionMode::Reduced);
101 }
102
103 pub(crate) fn entries() -> Vec<Entry> {
104 vec![
105 Entry {
106 name: "fish-school",
107 width: 72,
108 height: 8,
109 draw: fish,
110 },
111 Entry {
112 name: "jellyfish",
113 width: 40,
114 height: 9,
115 draw: jelly,
116 },
117 Entry {
118 name: "bubble-field",
119 width: 72,
120 height: 9,
121 draw: bubbles,
122 },
123 Entry {
124 name: "shoal",
125 width: 72,
126 height: 12,
127 draw: shoal,
128 },
129 Entry {
130 name: "marine-snow",
131 width: 72,
132 height: 10,
133 draw: snow,
134 },
135 Entry {
136 name: "distant-whale",
137 width: 72,
138 height: 12,
139 draw: distant_whale,
140 },
141 Entry {
142 name: "habitat-scene",
143 width: 112,
144 height: 30,
145 draw: scene,
146 },
147 Entry {
148 name: "habitat-ascii",
149 width: 84,
150 height: 24,
151 draw: ascii,
152 },
153 Entry {
154 name: "habitat-reduced",
155 width: 112,
156 height: 30,
157 draw: reduced,
158 },
159 ]
160 }
161
161 lines RUST