ballin 0.1.2

A colorful interactive physics simulator with thousands of balls, but in your terminal.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Shape manager for tracking and manipulating shapes.
//!
//! The `ShapeManager` is the central coordinator for all shape operations:
//! - Creating and removing shapes
//! - Selection and manipulation
//! - Random placement with collision avoidance
//! - Physics synchronization

use rand::Rng;
use rapier2d::prelude::*;

use super::ascii_art::get_ascii_art;
use super::colliders::{create_shape_collider, remove_shape_collider, shapes_overlap};
use super::types::{Shape, ShapeType};

/// Minimum separation distance between shapes (in character cells).
const MIN_SHAPE_SEPARATION: f32 = 4.0;

/// Edge margin to avoid placing shapes too close to boundaries.
const EDGE_MARGIN: f32 = 3.0;

/// Manager for all shapes in the simulation.
///
/// Handles creation, removal, selection, and physics synchronization
/// for all shape objects.
#[derive(Debug)]
pub struct ShapeManager {
    /// All shapes in the simulation.
    shapes: Vec<Shape>,

    /// Next shape ID to assign.
    next_id: u32,

    /// Currently selected shape ID, if any.
    selected_id: Option<u32>,
}

impl Default for ShapeManager {
    fn default() -> Self {
        Self::new()
    }
}

impl ShapeManager {
    /// Creates a new empty shape manager.
    pub fn new() -> Self {
        Self {
            shapes: Vec::new(),
            next_id: 1,
            selected_id: None,
        }
    }

    /// Returns the number of shapes.
    pub fn shape_count(&self) -> usize {
        self.shapes.len()
    }

    /// Returns an iterator over all shapes.
    pub fn shapes(&self) -> impl Iterator<Item = &Shape> {
        self.shapes.iter()
    }

    /// Returns a mutable iterator over all shapes.
    pub fn shapes_mut(&mut self) -> impl Iterator<Item = &mut Shape> {
        self.shapes.iter_mut()
    }

    /// Returns a reference to the currently selected shape, if any.
    pub fn selected_shape(&self) -> Option<&Shape> {
        self.selected_id
            .and_then(|id| self.shapes.iter().find(|s| s.id() == id))
    }

    /// Returns a mutable reference to the currently selected shape, if any.
    pub fn selected_shape_mut(&mut self) -> Option<&mut Shape> {
        let selected_id = self.selected_id?;
        self.shapes.iter_mut().find(|s| s.id() == selected_id)
    }

    /// Returns the ID of the currently selected shape, if any.
    pub fn selected_id(&self) -> Option<u32> {
        self.selected_id
    }

    /// Adds a shape at the specified position.
    ///
    /// Creates the shape and its physics collider.
    ///
    /// # Arguments
    ///
    /// * `shape_type` - The type of shape to create
    /// * `x` - X position in physics coordinates
    /// * `y` - Y position in physics coordinates
    /// * `rigid_body_set` - Physics rigid body set
    /// * `collider_set` - Physics collider set
    ///
    /// # Returns
    ///
    /// The ID of the newly created shape.
    pub fn add_shape(
        &mut self,
        shape_type: ShapeType,
        x: f32,
        y: f32,
        rigid_body_set: &mut RigidBodySet,
        collider_set: &mut ColliderSet,
    ) -> u32 {
        let id = self.next_id;
        self.next_id += 1;

        let mut shape = Shape::new(id, shape_type, x, y);

        let (body_handle, collider_handle) =
            create_shape_collider(&shape, rigid_body_set, collider_set);
        shape.set_physics_handles(body_handle, collider_handle);

        self.shapes.push(shape);
        id
    }

    /// Removes a shape by ID.
    ///
    /// Also removes the physics collider.
    ///
    /// # Arguments
    ///
    /// * `id` - The shape ID to remove
    /// * `rigid_body_set` - Physics rigid body set
    /// * `collider_set` - Physics collider set
    /// * `island_manager` - Physics island manager
    /// * `impulse_joint_set` - Physics impulse joint set
    /// * `multibody_joint_set` - Physics multibody joint set
    ///
    /// # Returns
    ///
    /// `true` if the shape was found and removed.
    #[allow(clippy::too_many_arguments)]
    pub fn remove_shape(
        &mut self,
        id: u32,
        rigid_body_set: &mut RigidBodySet,
        collider_set: &mut ColliderSet,
        island_manager: &mut IslandManager,
        impulse_joint_set: &mut ImpulseJointSet,
        multibody_joint_set: &mut MultibodyJointSet,
    ) -> bool {
        if let Some(idx) = self.shapes.iter().position(|s| s.id() == id) {
            let shape = &self.shapes[idx];

            remove_shape_collider(
                shape,
                rigid_body_set,
                collider_set,
                island_manager,
                impulse_joint_set,
                multibody_joint_set,
            );

            if self.selected_id == Some(id) {
                self.selected_id = None;
            }

            self.shapes.remove(idx);
            true
        } else {
            false
        }
    }

    /// Removes all shapes.
    ///
    /// # Arguments
    ///
    /// * `rigid_body_set` - Physics rigid body set
    /// * `collider_set` - Physics collider set
    /// * `island_manager` - Physics island manager
    /// * `impulse_joint_set` - Physics impulse joint set
    /// * `multibody_joint_set` - Physics multibody joint set
    pub fn clear_all(
        &mut self,
        rigid_body_set: &mut RigidBodySet,
        collider_set: &mut ColliderSet,
        island_manager: &mut IslandManager,
        impulse_joint_set: &mut ImpulseJointSet,
        multibody_joint_set: &mut MultibodyJointSet,
    ) {
        for shape in &self.shapes {
            remove_shape_collider(
                shape,
                rigid_body_set,
                collider_set,
                island_manager,
                impulse_joint_set,
                multibody_joint_set,
            );
        }
        self.shapes.clear();
        self.selected_id = None;
    }

    /// Selects the shape at the given position.
    ///
    /// Uses physics collision detection to find the shape under the click.
    ///
    /// # Arguments
    ///
    /// * `x` - X position in physics coordinates
    /// * `y` - Y position in physics coordinates
    /// * `collider_set` - Physics collider set for hit testing
    ///
    /// # Returns
    ///
    /// The ID of the selected shape, or None if no shape was hit.
    pub fn select_at(&mut self, x: f32, y: f32, collider_set: &ColliderSet) -> Option<u32> {
        // Clear previous selection
        if let Some(old_id) = self.selected_id {
            if let Some(shape) = self.shapes.iter_mut().find(|s| s.id() == old_id) {
                shape.set_selected(false);
            }
        }

        // Find shape under click
        // Use a simple distance-based check to the shape center
        let mut best_match: Option<(u32, f32)> = None;

        for shape in &self.shapes {
            let (cx, cy) = shape.position();
            let dx = x - cx;
            let dy = y - cy;
            let dist_sq = dx * dx + dy * dy;

            // Get shape size for hit radius
            let ascii_art = get_ascii_art(shape.shape_type());
            let hit_radius = (ascii_art.width().max(ascii_art.height()) as f32 / 2.0) + 1.0;

            if dist_sq < hit_radius * hit_radius {
                match best_match {
                    Some((_, best_dist)) if dist_sq < best_dist => {
                        best_match = Some((shape.id(), dist_sq));
                    }
                    None => {
                        best_match = Some((shape.id(), dist_sq));
                    }
                    _ => {}
                }
            }
        }

        // Also check using collider point containment
        for shape in &self.shapes {
            if let Some(collider_handle) = shape.collider_handle() {
                if let Some(collider) = collider_set.get(collider_handle) {
                    let point = point![x, y];
                    let local_point = collider.position().inverse_transform_point(&point);

                    if collider.shape().contains_local_point(&local_point) {
                        // Prefer exact collider hit over distance-based
                        let (cx, cy) = shape.position();
                        let dx = x - cx;
                        let dy = y - cy;
                        let dist_sq = dx * dx + dy * dy;

                        match best_match {
                            Some((_, best_dist)) if dist_sq < best_dist => {
                                best_match = Some((shape.id(), dist_sq));
                            }
                            None => {
                                best_match = Some((shape.id(), dist_sq));
                            }
                            _ => {}
                        }
                    }
                }
            }
        }

        // Update selection
        if let Some((id, _)) = best_match {
            if let Some(shape) = self.shapes.iter_mut().find(|s| s.id() == id) {
                shape.set_selected(true);
            }
            self.selected_id = Some(id);
            Some(id)
        } else {
            self.selected_id = None;
            None
        }
    }

    /// Deselects the current shape.
    pub fn deselect(&mut self) {
        if let Some(id) = self.selected_id {
            if let Some(shape) = self.shapes.iter_mut().find(|s| s.id() == id) {
                shape.set_selected(false);
            }
        }
        self.selected_id = None;
    }

    /// Rotates the selected shape clockwise by 45 degrees.
    ///
    /// # Arguments
    ///
    /// * `rigid_body_set` - Physics rigid body set for updating transform
    ///
    /// # Returns
    ///
    /// `true` if a shape was rotated.
    pub fn rotate_selected_clockwise(&mut self, rigid_body_set: &mut RigidBodySet) -> bool {
        if let Some(shape) = self.selected_shape_mut() {
            shape.rotate_clockwise();

            // Update physics transform
            if let Some(handle) = shape.rigid_body_handle() {
                if let Some(body) = rigid_body_set.get_mut(handle) {
                    let (x, y) = shape.position();
                    let rotation = shape.rotation_radians();
                    let isometry = Isometry::new(vector![x, y], rotation);
                    body.set_position(isometry, true);
                }
            }
            true
        } else {
            false
        }
    }

    /// Rotates the selected shape counter-clockwise by 90 degrees.
    ///
    /// # Arguments
    ///
    /// * `rigid_body_set` - Physics rigid body set for updating transform
    ///
    /// # Returns
    ///
    /// `true` if a shape was rotated.
    pub fn rotate_selected_counter_clockwise(&mut self, rigid_body_set: &mut RigidBodySet) -> bool {
        if let Some(shape) = self.selected_shape_mut() {
            shape.rotate_counter_clockwise();

            // Update physics transform
            if let Some(handle) = shape.rigid_body_handle() {
                if let Some(body) = rigid_body_set.get_mut(handle) {
                    let (x, y) = shape.position();
                    let rotation = shape.rotation_radians();
                    let isometry = Isometry::new(vector![x, y], rotation);
                    body.set_position(isometry, true);
                }
            }
            true
        } else {
            false
        }
    }

    /// Moves the selected shape by the given delta.
    ///
    /// # Arguments
    ///
    /// * `dx` - X movement in physics units
    /// * `dy` - Y movement in physics units
    /// * `rigid_body_set` - Physics rigid body set for updating transform
    ///
    /// # Returns
    ///
    /// `true` if a shape was moved.
    pub fn move_selected(&mut self, dx: f32, dy: f32, rigid_body_set: &mut RigidBodySet) -> bool {
        if let Some(shape) = self.selected_shape_mut() {
            let (x, y) = shape.position();
            shape.set_position(x + dx, y + dy);

            // Update physics transform
            if let Some(handle) = shape.rigid_body_handle() {
                if let Some(body) = rigid_body_set.get_mut(handle) {
                    let (new_x, new_y) = shape.position();
                    let rotation = shape.rotation_radians();
                    let isometry = Isometry::new(vector![new_x, new_y], rotation);
                    body.set_position(isometry, true);
                }
            }
            true
        } else {
            false
        }
    }

    /// Moves the selected shape to an absolute position.
    ///
    /// # Arguments
    ///
    /// * `x` - New X position in physics units
    /// * `y` - New Y position in physics units
    /// * `rigid_body_set` - Physics rigid body set for updating transform
    ///
    /// # Returns
    ///
    /// `true` if a shape was moved.
    pub fn move_selected_to(&mut self, x: f32, y: f32, rigid_body_set: &mut RigidBodySet) -> bool {
        if let Some(shape) = self.selected_shape_mut() {
            shape.set_position(x, y);

            // Update physics transform
            if let Some(handle) = shape.rigid_body_handle() {
                if let Some(body) = rigid_body_set.get_mut(handle) {
                    let rotation = shape.rotation_radians();
                    let isometry = Isometry::new(vector![x, y], rotation);
                    body.set_position(isometry, true);
                }
            }
            true
        } else {
            false
        }
    }

    /// Cycles the selected shape's color forward.
    ///
    /// # Returns
    ///
    /// `true` if a shape's color was changed.
    pub fn cycle_selected_color_forward(&mut self) -> bool {
        if let Some(shape) = self.selected_shape_mut() {
            shape.cycle_color_forward();
            true
        } else {
            false
        }
    }

    /// Cycles the selected shape's color backward.
    ///
    /// # Returns
    ///
    /// `true` if a shape's color was changed.
    pub fn cycle_selected_color_backward(&mut self) -> bool {
        if let Some(shape) = self.selected_shape_mut() {
            shape.cycle_color_backward();
            true
        } else {
            false
        }
    }

    /// Removes the currently selected shape.
    ///
    /// # Arguments
    ///
    /// * `rigid_body_set` - Physics rigid body set
    /// * `collider_set` - Physics collider set
    /// * `island_manager` - Physics island manager
    /// * `impulse_joint_set` - Physics impulse joint set
    /// * `multibody_joint_set` - Physics multibody joint set
    ///
    /// # Returns
    ///
    /// `true` if a shape was removed.
    #[allow(clippy::too_many_arguments)]
    pub fn remove_selected(
        &mut self,
        rigid_body_set: &mut RigidBodySet,
        collider_set: &mut ColliderSet,
        island_manager: &mut IslandManager,
        impulse_joint_set: &mut ImpulseJointSet,
        multibody_joint_set: &mut MultibodyJointSet,
    ) -> bool {
        if let Some(id) = self.selected_id {
            self.remove_shape(
                id,
                rigid_body_set,
                collider_set,
                island_manager,
                impulse_joint_set,
                multibody_joint_set,
            )
        } else {
            false
        }
    }

    /// Finds a valid random position for a shape.
    ///
    /// Avoids:
    /// - Spawn zone (top 1/4 of canvas)
    /// - Edges (within EDGE_MARGIN)
    /// - Other shapes (within MIN_SHAPE_SEPARATION)
    ///
    /// # Arguments
    ///
    /// * `shape_type` - The type of shape to place
    /// * `world_width` - Physics world width
    /// * `world_height` - Physics world height
    /// * `max_attempts` - Maximum placement attempts before giving up
    ///
    /// # Returns
    ///
    /// `Some((x, y))` if a valid position was found, `None` otherwise.
    pub fn find_random_position(
        &self,
        shape_type: ShapeType,
        world_width: f32,
        world_height: f32,
        max_attempts: u32,
    ) -> Option<(f32, f32)> {
        let mut rng = rand::thread_rng();

        // Get shape dimensions for placement bounds
        let ascii_art = get_ascii_art(shape_type);
        let half_width = ascii_art.width() as f32 / 2.0;
        let half_height = ascii_art.height() as f32 / 2.0;

        // Spawn zone is top 1/4 of world (in physics coords, Y-up)
        let spawn_zone_bottom = world_height * 0.75;

        // Valid placement range (avoiding edges and spawn zone)
        let min_x = EDGE_MARGIN + half_width;
        let max_x = world_width - EDGE_MARGIN - half_width;
        let min_y = EDGE_MARGIN + half_height;
        let max_y = spawn_zone_bottom - half_height;

        // Check if there's any valid space
        if min_x >= max_x || min_y >= max_y {
            return None;
        }

        for _ in 0..max_attempts {
            let x = rng.gen_range(min_x..max_x);
            let y = rng.gen_range(min_y..max_y);

            // Create temporary shape for overlap testing
            let temp_shape = Shape::new(0, shape_type, x, y);

            // Check overlap with existing shapes
            let overlaps = self
                .shapes
                .iter()
                .any(|existing| shapes_overlap(&temp_shape, existing, MIN_SHAPE_SEPARATION));

            if !overlaps {
                return Some((x, y));
            }
        }

        None
    }

    /// Places a random shape at a valid position.
    ///
    /// # Arguments
    ///
    /// * `world_width` - Physics world width
    /// * `world_height` - Physics world height
    /// * `rigid_body_set` - Physics rigid body set
    /// * `collider_set` - Physics collider set
    ///
    /// # Returns
    ///
    /// The ID of the placed shape, or `None` if no valid position was found.
    pub fn place_random_shape(
        &mut self,
        world_width: f32,
        world_height: f32,
        rigid_body_set: &mut RigidBodySet,
        collider_set: &mut ColliderSet,
    ) -> Option<u32> {
        let mut rng = rand::thread_rng();

        let shape_types = ShapeType::all();
        let shape_type = shape_types[rng.gen_range(0..shape_types.len())];

        let position = self.find_random_position(shape_type, world_width, world_height, 100)?;

        let id = self.add_shape(
            shape_type,
            position.0,
            position.1,
            rigid_body_set,
            collider_set,
        );
        Some(id)
    }

    /// Places multiple random shapes on app load.
    ///
    /// Places 2-4 shapes with proper separation.
    ///
    /// # Arguments
    ///
    /// * `world_width` - Physics world width
    /// * `world_height` - Physics world height
    /// * `rigid_body_set` - Physics rigid body set
    /// * `collider_set` - Physics collider set
    ///
    /// # Returns
    ///
    /// The number of shapes successfully placed.
    pub fn place_initial_shapes(
        &mut self,
        world_width: f32,
        world_height: f32,
        rigid_body_set: &mut RigidBodySet,
        collider_set: &mut ColliderSet,
    ) -> usize {
        let mut rng = rand::thread_rng();
        let count = rng.gen_range(2..=4);
        let mut placed = 0;

        for _ in 0..count {
            if self
                .place_random_shape(world_width, world_height, rigid_body_set, collider_set)
                .is_some()
            {
                placed += 1;
            }
        }

        placed
    }

    /// Gets a shape by ID.
    pub fn get_shape(&self, id: u32) -> Option<&Shape> {
        self.shapes.iter().find(|s| s.id() == id)
    }

    /// Gets a mutable shape by ID.
    pub fn get_shape_mut(&mut self, id: u32) -> Option<&mut Shape> {
        self.shapes.iter_mut().find(|s| s.id() == id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_manager() {
        let manager = ShapeManager::new();
        assert_eq!(manager.shape_count(), 0);
        assert!(manager.selected_id().is_none());
    }

    #[test]
    fn test_find_random_position() {
        let manager = ShapeManager::new();

        // Should find a position in a large world
        let pos = manager.find_random_position(ShapeType::Square, 100.0, 100.0, 10);
        assert!(pos.is_some());

        let (x, y) = pos.unwrap();
        // Should be within bounds
        assert!(x > 0.0 && x < 100.0);
        assert!(y > 0.0 && y < 75.0); // Below spawn zone
    }

    #[test]
    fn test_find_random_position_small_world() {
        let manager = ShapeManager::new();

        // Should fail in a tiny world
        let pos = manager.find_random_position(ShapeType::Square, 5.0, 5.0, 10);
        assert!(pos.is_none());
    }
}