Skip to main content

quilt_watch/
lib.rs

1//! quilt-watch — the Watch primitive of Quilt.
2//! 
3//! The watch oscillates between universal and particular.
4//! The watch is the artist who chooses the medium.
5//! The watch is the witness of the work.
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Default, Serialize, Deserialize)]
10pub struct Watch {
11    pub position: (f64, f64),
12    pub velocity: (f64, f64),
13    pub mode: String,  // universal | particular | oscillation
14    pub target: Option<(f64, f64)>,
15    pub tick_count: u64,
16}
17
18impl Watch {
19    pub fn new() -> Self {
20        Self {
21            position: (0.0, 0.0),
22            velocity: (0.0, 0.0),
23            mode: "oscillation".to_string(),
24            target: None,
25            tick_count: 0,
26        }
27    }
28    pub fn follow(&mut self, target: (f64, f64)) {
29        self.target = Some(target);
30        self.mode = "particular".to_string();
31    }
32    pub fn unfollow(&mut self) {
33        self.target = None;
34        self.mode = "universal".to_string();
35    }
36    pub fn oscillate(&mut self) {
37        if self.mode == "universal" {
38            self.mode = "particular".to_string();
39        } else {
40            self.mode = "universal".to_string();
41        }
42    }
43    pub fn tick(&mut self, dt: f64) {
44        if let Some(target) = self.target {
45            self.velocity.0 += (target.0 - self.position.0) * 0.02;
46            self.velocity.1 += (target.1 - self.position.1) * 0.02;
47        }
48        self.position.0 += self.velocity.0 * dt;
49        self.position.1 += self.velocity.1 * dt;
50        self.velocity.0 *= 0.9;
51        self.velocity.1 *= 0.9;
52        self.tick_count += 1;
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    #[test]
60    fn test_watch() {
61        let mut w = Watch::new();
62        w.follow((10.0, 20.0));
63        w.tick(1.0);
64        assert!(w.position.0 > 0.0 || w.position.1 > 0.0);
65    }
66}