summaryrefslogtreecommitdiff
path: root/2026/games_submissions/rpc2dot0/src/computer.rs
blob: e25fb651393ef1ff57f19f5f03fef6eb0d536148 (plain)
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
use crate::constants::*;
use crate::disquette::Disquette;
use crate::game::Game;
use crate::math::ContainsPoint;
use crate::math::Rectangle;
use crate::random::RandomState;

pub mod os;

use os::Os;
use teleia::context;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComputerPhase {
    Off,
    Booting,
    On,
    ShutDown,
}

impl From<ComputerPhase> for &'static str {
    fn from(value: ComputerPhase) -> Self {
        match value {
            ComputerPhase::Off => "Off",
            ComputerPhase::Booting => "Booting",
            ComputerPhase::On => "On",
            ComputerPhase::ShutDown => "ShutDown",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PowerButtonAction {
    On,
    Off,
}

impl From<PowerButtonAction> for &'static str {
    fn from(value: PowerButtonAction) -> Self {
        match value {
            PowerButtonAction::On => "On",
            PowerButtonAction::Off => "Off",
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Computer {
    pub coord: glam::Vec2,
    pub phase: ComputerPhase,
    pub disquette: Disquette,
    pub os: Os,
}

impl Computer {
    pub fn new(
        logical_width: f32,
        logical_height: f32,
        rng: &mut RandomState,
        difficulty: f32,
    ) -> Self {
        const HALF_W: f32 = COMPUTER_WIDTH / 2.0;
        const HALF_H: f32 = COMPUTER_HEIGHT / 2.0;
        let x = rng.next_range(HALF_W, logical_width - HALF_W);
        let y = rng.next_range(HALF_H, logical_height - HALF_H);
        log::info!("eatyourgreens:: Computer new at ({}, {}) ", x, y);
        let mut temp = Self {
            coord: glam::Vec2::new(x, y),
            phase: ComputerPhase::Off,
            disquette: Disquette {
                coord: glam::Vec2::ZERO,
                vel: glam::Vec2::ZERO,
                size: 0.0,
                phase: crate::disquette::DisquettePhase::Free,
            },
            os: Os::from(ComputerPhase::Off),
        };

        let hitzone = temp.hit_zone(difficulty);
        temp.disquette = Disquette::new(logical_width, logical_height, rng, Some(hitzone));
        temp
    }

    pub fn disquette_inserted(&self) -> bool {
        self.disquette.is_inserted()
    }

    pub fn boot(&mut self) {
        log::info!("eatyourgreens:: computer boot");
        self.phase = ComputerPhase::Booting;
        self.os = Os::from(self.phase);
    }

    pub fn on(&mut self, ctx: &context::Context) {
        log::info!("eatyourgreens:: computer on");
        self.phase = ComputerPhase::On;
        self.os.set(self.phase);
        self.os.os_play(&ctx);
    }

    pub fn shutdown(&mut self) {
        log::info!("eatyourgreens:: computer shutdown");
        self.phase = ComputerPhase::Off;
        self.os.set(self.phase);
    }

    pub fn already_used(&self) -> bool {
        self.phase != ComputerPhase::Off
    }

    //
    // hitzone calculation
    //
    // computer pos is (x, y)so its center.
    // first find the top-left corner of the computer bbox.
    // second define "hitzone" as the inner rectangle offset from that top-left corner.

    // us diffculity to shrink the area, so user msut aim better at higher difficulties

    //   topleft(x -width/2 , y-height/2)
    //      +------------------------------------------+  ---
    //      |                                          |   |
    //      |     TRIGGER_OFFSET_Y                     |   |
    //      |     v                                    |   | COMPUTER_HEIGHT
    //      | -> +----------------------------+ ---    |   |
    //      | ^  |        diff_y              |  |     |   |
    //      | |  | -> +---------------+ ---   |  |     |   |
    //      | |  | ^  |               |  |    |  | TRIGGER_HEIGHT
    //      | |  | |  | Actual Hit    |actual_|  |     |   |
    //      | |  | |  | Zone (shrunk) |height |  |     |   |
    //      | |  | |  +---------------+ ---   |  |     |   |
    //      | |  | diff_x                     |  |     |   |
    //      | |  +----------------------------+ ---    |   |
    //      | |                                        |   |
    //      | TRIGGER_OFFSET_X                         |   |
    //      +------------------------------------------+  ---
    //      |--------------COMPUTER_WIDTH--------------|

    pub fn hit_zone(&self, difficulty: f32) -> Rectangle {
        let topleft = self.coord - glam::Vec2::new(COMPUTER_WIDTH / 2.0, COMPUTER_HEIGHT / 2.0);

        let size_multiplier = 1.0 / difficulty.max(1.0);
        let actual_size = glam::Vec2::new(TRIGGER_WIDTH, TRIGGER_HEIGHT) * size_multiplier;

        let diff = (glam::Vec2::new(TRIGGER_WIDTH, TRIGGER_HEIGHT) - actual_size) / 2.0;

        let trigger_pos = topleft + glam::Vec2::new(TRIGGER_OFFSET_X, TRIGGER_OFFSET_Y) + diff;

        Rectangle::from_min_max(trigger_pos, trigger_pos + actual_size)
    }

    pub fn check_power_button(
        &self,
        point: glam::Vec2,
        difficulty: f32,
    ) -> Option<PowerButtonAction> {
        let topleft = self.coord - glam::Vec2::new(COMPUTER_WIDTH / 2.0, COMPUTER_HEIGHT / 2.0);
        let pb_pos = topleft + glam::Vec2::new(POWER_BUTTON_OFFSET_X, POWER_BUTTON_OFFSET_Y);
        let pb_size = glam::Vec2::new(POWER_BUTTON_WIDTH, POWER_BUTTON_HEIGHT);

        let pb_rect = Rectangle::from_min_max(pb_pos, pb_pos + pb_size);

        if pb_rect.contains(point) {
            if difficulty > 1.0 {
                let half_width = POWER_BUTTON_WIDTH / 2.0;
                if point.x < pb_pos.x + half_width {
                    Some(PowerButtonAction::Off)
                } else {
                    Some(PowerButtonAction::On)
                }
            } else {
                Some(PowerButtonAction::On)
            }
        } else {
            None
        }
    }

    pub fn contains_disquette(&self, disquette: &Disquette, difficulty: f32) -> bool {
        let hitzone = self.hit_zone(difficulty);
        hitzone.contains(disquette.coord)
    }
}