snake/
end.rs

1//! Game over screen both win and lose
2
3use std::collections::HashMap;
4use sdl2::{
5    event::Event,
6    keyboard::Scancode,
7    rect::Rect
8};
9use ycraft::{
10    collision::CollisionShape,
11    obj::{
12        ControlObjectBehavior, Frame, GameObjectBehavior, GameObjectState, Sprite
13    }, room::Room
14};
15use crate::game::{
16    Data, Fnt, Img, Rm, Snd, Spr
17};
18
19#[derive(Clone)]
20struct DeadScreen {
21    state: GameObjectState<Img, Spr, Data>,
22    change_room: bool
23}
24
25impl DeadScreen {
26    pub fn new() -> Self {
27        Self {
28            state: GameObjectState {
29                name: "dead".to_string(),
30                pos: (0.0, 0.0),
31                collider: CollisionShape::Rect { center: (320, 180), size: (640, 480) },
32                cur_spr: Spr::Dead,
33                sprs: HashMap::from([(
34                    Spr::Dead,
35                    Sprite::new(
36                        vec![Frame::new(
37                            Img::Dead, Rect::new(0, 0, 640, 360), (640, 360)
38                        )], 0.0, (0, 0)
39                    )
40                )]), custom: Data::Dead
41            }, change_room: false
42        }
43    }
44}
45
46impl GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data> for DeadScreen {
47    fn state(&self) -> GameObjectState<Img, Spr, Data> {
48        self.state.clone()
49    }
50
51    fn set_state(&mut self, new_state: &GameObjectState<Img, Spr, Data>) {
52        self.state = new_state.clone();
53    }
54
55    fn on_reset(&mut self) -> bool {
56        self.change_room = false;
57        false
58    }
59
60    fn handle_sdl_event(&mut self, event: &Event) {
61        match event {
62            Event::KeyUp { scancode, .. } if *scancode == Some(Scancode::Return) => {
63                self.change_room = true;
64            }, _ => {}
65        }
66    }
67
68    fn update(
69            &mut self, _delta: f64,
70            _ctl_objs: &Vec<Box<dyn ControlObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>,
71            _others: &Vec<Box<dyn GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>) -> (
72                Option<Rm>,
73                Vec<Box<dyn GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>
74            ) {
75        if self.change_room {
76            (Some(Rm::Title), vec![])
77        } else {
78            (None, vec![])
79        }
80    }
81}
82
83#[derive(Clone)]
84struct WinScreen {
85    state: GameObjectState<Img, Spr, Data>,
86    change_room: bool
87}
88
89impl WinScreen {
90    pub fn new() -> Self {
91        Self {
92            state: GameObjectState {
93                name: "win".to_string(),
94                pos: (0.0, 0.0),
95                collider: CollisionShape::Rect { center: (320, 180), size: (640, 480) },
96                cur_spr: Spr::Win,
97                sprs: HashMap::from([(
98                    Spr::Win,
99                    Sprite::new(
100                        vec![Frame::new(
101                            Img::Win, Rect::new(0, 0, 640, 360), (640, 360)
102                        )], 0.0, (0, 0)
103                    )
104                )]), custom: Data::Win
105            }, change_room: false
106        }
107    }
108}
109
110impl GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data> for WinScreen {
111    fn state(&self) -> GameObjectState<Img, Spr, Data> {
112        self.state.clone()
113    }
114
115    fn set_state(&mut self, new_state: &GameObjectState<Img, Spr, Data>) {
116        self.state = new_state.clone();
117    }
118
119    fn on_reset(&mut self) -> bool {
120        self.change_room = false;
121        false
122    }
123
124    fn handle_sdl_event(&mut self, event: &Event) {
125        match event {
126            Event::KeyUp { scancode, .. } if *scancode == Some(Scancode::Return) => {
127                self.change_room = true;
128            }, _ => {}
129        }
130    }
131
132    fn update(
133            &mut self, _delta: f64,
134            _ctl_objs: &Vec<Box<dyn ControlObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>,
135            _others: &Vec<Box<dyn GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>) -> (
136                Option<Rm>,
137                Vec<Box<dyn GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>
138            ) {
139        if self.change_room {
140            (Some(Rm::Title), vec![])
141        } else {
142            (None, vec![])
143        }
144    }
145}
146
147pub fn dead() -> Room<Img, Snd, Fnt, Spr, Rm, Data> {
148    Room::new(vec![ Box::new(DeadScreen::new()) ], false)
149}
150
151pub fn win() -> Room<Img, Snd, Fnt, Spr, Rm, Data> {
152    Room::new(vec![ Box::new(WinScreen::new()) ], false)
153}
154