diff --git a/iced-tetris/Cargo.lock b/iced-tetris/Cargo.lock index e37b726..4ccbd9d 100644 --- a/iced-tetris/Cargo.lock +++ b/iced-tetris/Cargo.lock @@ -18,6 +18,15 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046" +[[package]] +name = "addr2line" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5fb1d8e4442bd405fdfd1dacb42792696b0cf9cb15882e5d097b742a676d375" +dependencies = [ + "gimli", +] + [[package]] name = "adler2" version = "2.0.0" @@ -287,6 +296,21 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -1197,6 +1221,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "gimli" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" + [[package]] name = "gl_generator" version = "0.14.0" @@ -1448,6 +1478,7 @@ dependencies = [ "iced_core", "log", "rustc-hash 2.0.0", + "tokio", "wasm-bindgen-futures", "wasm-timer", ] @@ -2147,6 +2178,15 @@ dependencies = [ "cc", ] +[[package]] +name = "object" +version = "0.36.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.20.1" @@ -2604,6 +2644,12 @@ dependencies = [ "ordered-multimap", ] +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -3038,6 +3084,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "tokio" +version = "1.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +dependencies = [ + "backtrace", + "pin-project-lite", +] + [[package]] name = "toml_datetime" version = "0.6.8" diff --git a/iced-tetris/Cargo.toml b/iced-tetris/Cargo.toml index 9856692..46b2aa8 100644 --- a/iced-tetris/Cargo.toml +++ b/iced-tetris/Cargo.toml @@ -7,7 +7,7 @@ resolver = "2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -iced = "0.13.1" +iced = { version = "0.13.1", features = ["tokio"] } chrono = "0.4.19" rand = "0.8.4" tetris-logic = { path = "../tetris-logic" } diff --git a/iced-tetris/src/main.rs b/iced-tetris/src/main.rs index ed0549e..efeb2da 100644 --- a/iced-tetris/src/main.rs +++ b/iced-tetris/src/main.rs @@ -7,12 +7,17 @@ use iced::{ use tetris_logic::{BlockGrid, MoveDirection, Tetromino}; */ -use iced::{widget::{button, text}, Element, Task}; -use tetris_logic::BlockGrid; +use iced::{ + widget::{button, text}, + Element, Subscription, Task, +}; +use tetris_logic::{BlockGrid, MoveDirection, Tetromino}; fn main() -> iced::Result { //Tetris::run(Settings::default()) - iced::application(Tetris::title, update, view).run_with(|| (Tetris::new(), Task::none())) + iced::application(Tetris::title, update, view) + .subscription(subscription) + .run_with(|| (Tetris::new(), Task::none())) } #[derive(Debug, Clone)] @@ -25,15 +30,70 @@ enum Message { Tick(chrono::DateTime), } -fn update(_state: &mut Tetris, message: Message) { +fn update(state: &mut Tetris, message: Message) { match message { - Message::Up => todo!(), - Message::Down => todo!(), - Message::Left => todo!(), - Message::Right => todo!(), - Message::Pause => todo!(), - Message::Tick(_) => todo!(), + Message::Pause => state.paused = !state.paused, + Message::Tick(_) => { + if !state.paused { + state.ticks += 1; + } + } + Message::Up => { + state.blocks.rotate_active_piece(); + } + Message::Down => { + state.blocks.move_active_piece(MoveDirection::HardDrop); + } + Message::Left => { + state.blocks.move_active_piece(MoveDirection::Left); + } + Message::Right => { + state.blocks.move_active_piece(MoveDirection::Right); + } + }; + if state.blocks.piece_currently_active() { + if state.ticks % 10 == 0 && !state.paused { + state.blocks.move_active_piece(MoveDirection::SoftDrop); + } + } else { + let piece: Tetromino = rand::random(); + state.blocks.drop_piece(piece); } + + let lines_removed = state.blocks.clear_pieces(); + state.lines_removed += lines_removed; +} + +fn subscription(_state: &Tetris) -> Subscription { + let keyboard_subscription = iced::keyboard::on_key_press(|key, _modifiers| { + use iced::keyboard::key::{Key, Named}; + match key { + Key::Named(Named::ArrowUp) => Some(Message::Up), + Key::Named(Named::ArrowDown) => Some(Message::Down), + Key::Named(Named::ArrowLeft) => Some(Message::Left), + Key::Named(Named::ArrowRight) => Some(Message::Right), + Key::Named(Named::Space) => Some(Message::Pause), + _ => None, + } + }); + /* + let keyboard_subscription = subscription::events_with(|event, status| { + if let event::Status::Captured = status { + return None; + } + + match event { + Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => { + handle_keypress(key_code) + } + _ => None, + } + }); + */ + + let time_subscription = iced::time::every(std::time::Duration::from_millis(50)) + .map(|_| Message::Tick(chrono::Local::now())); + Subscription::batch([time_subscription, keyboard_subscription]) } fn view(_state: &Tetris) -> Element { @@ -224,4 +284,3 @@ fn handle_keypress(key_code: keyboard::KeyCode) -> Option { }) } */ -