1use crate::activity::{Activity, MotionKind};
6use crate::graph::CHARACTER_BOX;
7use crate::graph::{CharacterTrack, Motion, SpriteInstance, SpriteKind};
8use crate::math::pm;
9use crate::recipe::SceneRecipe;
10
11const CHARACTER_MAX_FRACTION: f64 = 0.09;
13const SAFE_MARGIN: f64 = 0.06;
15
16pub fn sprite_scale(short_side: u32, sprite_h: u32) -> u32 {
19 ((CHARACTER_MAX_FRACTION * f64::from(short_side) / f64::from(sprite_h)).floor() as u32).max(1)
20}
21
22#[derive(Clone, Copy, Debug, PartialEq)]
24pub struct LightSource {
25 pub cx: f64,
26 pub cy: f64,
27 pub strength: f64,
29}
30
31#[derive(Clone, Debug, PartialEq)]
33pub struct FocalLayout {
34 pub scale: u32,
35 pub x0: i32,
37 pub x1: i32,
38 pub height: i32,
40 pub props: Vec<SpriteInstance>,
42 pub unattended: Vec<SpriteInstance>,
44 pub character: Option<CharacterTrack>,
45 pub light: Option<LightSource>,
46 pub embers_at: Option<(f64, f64)>,
48}
49
50fn prop_kind(id: &str) -> Option<SpriteKind> {
51 match id {
52 "campfire" => Some(SpriteKind::Campfire),
53 "log_seat" => Some(SpriteKind::LogSeat),
54 "telescope" => Some(SpriteKind::Telescope),
55 "lantern" => Some(SpriteKind::Lantern),
56 _ => None,
57 }
58}
59
60pub fn layout(
62 recipe: &SceneRecipe,
63 activity: Option<&Activity>,
64 w: u32,
65 h: u32,
66 ground_y: i32,
67 cell_w: u32,
68) -> FocalLayout {
69 let comp = &recipe.composition;
70 let wf = f64::from(w);
71 let scale = sprite_scale(w.min(h), CHARACTER_BOX.1);
72 let s = scale as i32;
73 let (cw, ch) = (
74 (CHARACTER_BOX.0 * scale) as i32,
75 (CHARACTER_BOX.1 * scale) as i32,
76 );
77 let gap = 3 * s;
78 let (safe_l, safe_r) = ((SAFE_MARGIN * wf) as i32, ((1.0 - SAFE_MARGIN) * wf) as i32);
79
80 let Some(activity) = activity else {
81 let cx = (pm(comp.focus_x_pm) * wf).round() as i32;
83 return FocalLayout {
84 scale,
85 x0: cx - cw / 2,
86 x1: cx + cw / 2,
87 height: ch,
88 props: Vec::new(),
89 unattended: Vec::new(),
90 character: None,
91 light: None,
92 embers_at: None,
93 };
94 };
95
96 let mut sides: Vec<(SpriteKind, bool, &str)> = Vec::new();
98 let mut next_left = comp.prop_left;
99 for (id, def) in &activity.props {
100 let Some(kind) = prop_kind(id) else { continue };
101 let left = match def.attach.as_str() {
102 "left_of_clawd" => true,
103 "right_of_clawd" => false,
104 _ => {
105 let l = next_left;
106 next_left = !next_left;
107 l
108 }
109 };
110 sides.push((kind, left, id.as_str()));
111 }
112 let side_width = |left: bool| -> i32 {
113 sides
114 .iter()
115 .filter(|(_, l, _)| *l == left)
116 .map(|(k, _, _)| (k.map().width * scale) as i32 + gap)
117 .sum()
118 };
119
120 let mut pace_half = match activity.motion.kind {
123 MotionKind::Pace => (pm(comp.pace_half_pm) * f64::from(w.min(h))) as i32,
124 _ => 0,
125 };
126 let fixed = cw + side_width(true) + side_width(false);
127 pace_half = pace_half.min(((safe_r - safe_l - fixed) / 2).max(0));
128 let half_band = pace_half + cw / 2;
129 let group_w = fixed + 2 * pace_half;
130
131 let mut cx = (pm(comp.focus_x_pm) * wf).round() as i32;
133 let left_edge = cx - half_band - side_width(true);
134 if left_edge < safe_l {
135 cx += safe_l - left_edge;
136 } else if left_edge + group_w > safe_r {
137 cx -= left_edge + group_w - safe_r;
138 }
139
140 let mut props = Vec::new();
141 let mut light = None;
142 let mut embers_at = None;
143 let (mut left_cursor, mut right_cursor) = (cx - half_band - gap, cx + half_band + gap);
144 let mut height = ch;
145 for (kind, left, id) in &sides {
146 let map = kind.map();
147 let (pw, ph) = ((map.width * scale) as i32, (map.height * scale) as i32);
148 let x = if *left {
149 left_cursor -= pw;
150 let x = left_cursor;
151 left_cursor -= gap;
152 x
153 } else {
154 let x = right_cursor;
155 right_cursor += pw + gap;
156 x
157 };
158 let y = ground_y - ph;
159 height = height.max(ph);
160 props.push(SpriteInstance {
161 kind: *kind,
162 x,
163 y,
164 scale,
165 mirror: !*left,
166 });
167 let pcx = f64::from(x) + f64::from(pw) * 0.5;
168 match *id {
169 "campfire" if light.is_none() => {
170 light = Some(LightSource {
171 cx: pcx,
172 cy: f64::from(y) + f64::from(ph) * 0.6,
173 strength: 1.0,
174 });
175 embers_at = Some((pcx, f64::from(y + s)));
176 }
177 "lantern" if light.is_none() => {
178 light = Some(LightSource {
179 cx: pcx,
180 cy: f64::from(y) + f64::from(ph) * 0.55,
181 strength: 0.55,
182 });
183 }
184 _ => {}
185 }
186 }
187
188 let mut unattended = Vec::new();
190 for id in &activity.unattended.props {
191 if activity.props.contains_key(id) {
192 continue;
193 }
194 if let Some(kind) = prop_kind(id) {
195 let map = kind.map();
196 let (pw, ph) = ((map.width * scale) as i32, (map.height * scale) as i32);
197 unattended.push(SpriteInstance {
198 kind,
199 x: cx - pw / 2,
200 y: ground_y - ph,
201 scale,
202 mirror: comp.prop_left,
203 });
204 }
205 }
206
207 let motion = match activity.motion.kind {
208 MotionKind::Pace if pace_half > 0 => {
209 let span = 2 * pace_half;
210 let px_per_s = f64::from(comp.motion_speed_cc) / 100.0 * f64::from(cell_w);
211 let period_ms = ((2.0 * f64::from(span) / px_per_s.max(1.0)) * 1000.0).round() as u64;
212 Motion::Pace {
213 x_min: cx - pace_half - cw / 2,
214 x_max: cx + pace_half - cw / 2,
215 period_ms: period_ms.max(1000),
216 }
217 }
218 _ => Motion::Stationary { x: cx - cw / 2 },
219 };
220
221 FocalLayout {
222 scale,
223 x0: left_cursor + gap,
224 x1: right_cursor - gap,
225 height,
226 props,
227 unattended,
228 character: Some(CharacterTrack {
229 y: ground_y - ch,
230 scale,
231 motion,
232 face_left: comp.prop_left,
233 }),
234 light,
235 embers_at,
236 }
237}