time to rewrite now
This commit is contained in:
parent
5fac63a0de
commit
1f2105f3be
5 changed files with 2632 additions and 259 deletions
254
Cargo.lock
generated
254
Cargo.lock
generated
|
@ -2,35 +2,6 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ab_glyph"
|
||||
version = "0.2.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80179d7dd5d7e8c285d67c4a1e652972a92de7475beddfb92028c76463b13225"
|
||||
dependencies = [
|
||||
"ab_glyph_rasterizer",
|
||||
"owned_ttf_parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ab_glyph_rasterizer"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046"
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "android-activity"
|
||||
version = "0.5.2"
|
||||
|
@ -58,18 +29,6 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04"
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
|
||||
|
||||
[[package]]
|
||||
name = "as-raw-xcb-connection"
|
||||
version = "1.0.1"
|
||||
|
@ -171,18 +130,6 @@ dependencies = [
|
|||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "calloop-wayland-source"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f0ea9b9476c7fad82841a8dbb380e2eae480c21910feba80725b46931ed8f02"
|
||||
dependencies = [
|
||||
"calloop",
|
||||
"rustix",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
|
@ -447,17 +394,6 @@ dependencies = [
|
|||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.14.3"
|
||||
|
@ -691,15 +627,6 @@ dependencies = [
|
|||
"libredox",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "owned_ttf_parser"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4586edfe4c648c71797a74c84bacb32b52b212eff5dfe2bb9f2c599844023e7"
|
||||
dependencies = [
|
||||
"ttf-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.1"
|
||||
|
@ -828,19 +755,6 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
|
||||
|
||||
[[package]]
|
||||
name = "sctk-adwaita"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82b2eaf3a5b264a521b988b2e73042e742df700c4f962cde845d1541adb46550"
|
||||
dependencies = [
|
||||
"ab_glyph",
|
||||
"log",
|
||||
"memmap2",
|
||||
"smithay-client-toolkit",
|
||||
"tiny-skia",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.196"
|
||||
|
@ -876,31 +790,6 @@ version = "1.13.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
|
||||
|
||||
[[package]]
|
||||
name = "smithay-client-toolkit"
|
||||
version = "0.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60e3d9941fa3bacf7c2bf4b065304faa14164151254cd16ce1b1bc8fc381600f"
|
||||
dependencies = [
|
||||
"bitflags 2.4.2",
|
||||
"calloop",
|
||||
"calloop-wayland-source",
|
||||
"cursor-icon",
|
||||
"libc",
|
||||
"log",
|
||||
"memmap2",
|
||||
"rustix",
|
||||
"thiserror",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
"wayland-csd-frame",
|
||||
"wayland-cursor",
|
||||
"wayland-protocols",
|
||||
"wayland-protocols-wlr",
|
||||
"wayland-scanner",
|
||||
"xkeysym",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smol_str"
|
||||
version = "0.2.1"
|
||||
|
@ -941,12 +830,6 @@ dependencies = [
|
|||
"x11rb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strict-num"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.48"
|
||||
|
@ -978,31 +861,6 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tiny-skia"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6a067b809476893fce6a254cf285850ff69c847e6cfbade6a20b655b6c7e80d"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"arrayvec",
|
||||
"bytemuck",
|
||||
"cfg-if",
|
||||
"log",
|
||||
"tiny-skia-path",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tiny-skia-path"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5de35e8a90052baaaf61f171680ac2f8e925a1e43ea9d2e3a00514772250e541"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"bytemuck",
|
||||
"strict-num",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tiny-xlib"
|
||||
version = "0.2.2"
|
||||
|
@ -1048,12 +906,6 @@ version = "0.1.32"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
|
@ -1066,12 +918,6 @@ version = "1.10.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.4.0"
|
||||
|
@ -1082,12 +928,6 @@ dependencies = [
|
|||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.90"
|
||||
|
@ -1180,66 +1020,6 @@ dependencies = [
|
|||
"wayland-scanner",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wayland-csd-frame"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e"
|
||||
dependencies = [
|
||||
"bitflags 2.4.2",
|
||||
"cursor-icon",
|
||||
"wayland-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wayland-cursor"
|
||||
version = "0.31.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "71ce5fa868dd13d11a0d04c5e2e65726d0897be8de247c0c5a65886e283231ba"
|
||||
dependencies = [
|
||||
"rustix",
|
||||
"wayland-client",
|
||||
"xcursor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wayland-protocols"
|
||||
version = "0.31.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f81f365b8b4a97f422ac0e8737c438024b5951734506b0e1d775c73030561f4"
|
||||
dependencies = [
|
||||
"bitflags 2.4.2",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
"wayland-scanner",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wayland-protocols-plasma"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23803551115ff9ea9bce586860c5c5a971e360825a0309264102a9495a5ff479"
|
||||
dependencies = [
|
||||
"bitflags 2.4.2",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
"wayland-protocols",
|
||||
"wayland-scanner",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wayland-protocols-wlr"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad1f61b76b6c2d8742e10f9ba5c3737f6530b4c243132c2a2ccc8aa96fe25cd6"
|
||||
dependencies = [
|
||||
"bitflags 2.4.2",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
"wayland-protocols",
|
||||
"wayland-scanner",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wayland-scanner"
|
||||
version = "0.31.1"
|
||||
|
@ -1518,7 +1298,6 @@ version = "0.29.10"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c824f11941eeae66ec71111cc2674373c772f482b58939bb4066b642aa2ffcf"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"android-activity",
|
||||
"atomic-waker",
|
||||
"bitflags 2.4.2",
|
||||
|
@ -1532,7 +1311,6 @@ dependencies = [
|
|||
"js-sys",
|
||||
"libc",
|
||||
"log",
|
||||
"memmap2",
|
||||
"ndk",
|
||||
"ndk-sys",
|
||||
"objc2",
|
||||
|
@ -1542,16 +1320,10 @@ dependencies = [
|
|||
"raw-window-handle",
|
||||
"redox_syscall 0.3.5",
|
||||
"rustix",
|
||||
"sctk-adwaita",
|
||||
"smithay-client-toolkit",
|
||||
"smol_str",
|
||||
"unicode-segmentation",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
"wayland-protocols",
|
||||
"wayland-protocols-plasma",
|
||||
"web-sys",
|
||||
"web-time",
|
||||
"windows-sys 0.48.0",
|
||||
|
@ -1601,12 +1373,6 @@ version = "0.13.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e63e71c4b8bd9ffec2c963173a4dc4cbde9ee96961d4fcb4429db9929b606c34"
|
||||
|
||||
[[package]]
|
||||
name = "xcursor"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a0ccd7b4a5345edfcd0c3535718a4e9ff7798ffc536bb5b5a0e26ff84732911"
|
||||
|
||||
[[package]]
|
||||
name = "xkbcommon-dl"
|
||||
version = "0.4.1"
|
||||
|
@ -1625,23 +1391,3 @@ name = "xkeysym"
|
|||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "054a8e68b76250b253f671d1268cb7f1ae089ec35e195b2efb2a4e9a836d0621"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
|
|
@ -6,5 +6,5 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
softbuffer = "0.4.1"
|
||||
winit = "0.29.10"
|
||||
softbuffer = { version = "0.4.1" }
|
||||
winit = { version = "0.29.10", default-features = false, features = [ "x11", "rwh_06" ] }
|
||||
|
|
2255
broken.mir
Normal file
2255
broken.mir
Normal file
File diff suppressed because it is too large
Load diff
|
@ -50,6 +50,7 @@
|
|||
libxkbcommon
|
||||
xorg.libXi
|
||||
xorg.libXcursor
|
||||
cargo-watch
|
||||
];
|
||||
})
|
||||
];
|
||||
|
|
377
src/main.rs
377
src/main.rs
|
@ -1,6 +1,11 @@
|
|||
use std::num::NonZeroU32;
|
||||
use std::{
|
||||
num::NonZeroU32,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use game::GameState;
|
||||
use winit::{
|
||||
dpi::PhysicalSize,
|
||||
event::{Event, KeyEvent, WindowEvent},
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
keyboard::{Key, NamedKey},
|
||||
|
@ -10,7 +15,10 @@ use winit::{
|
|||
|
||||
fn main() {
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||
let window = WindowBuilder::new()
|
||||
.with_inner_size(winit::dpi::Size::Physical(PhysicalSize::new(1200, 700)))
|
||||
.build(&event_loop)
|
||||
.unwrap();
|
||||
|
||||
// ControlFlow::Poll continuously runs the event loop, even if the OS hasn't
|
||||
// dispatched any events. This is ideal for games and similar applications.
|
||||
|
@ -24,9 +32,13 @@ fn main() {
|
|||
let context = softbuffer::Context::new(window.display_handle().unwrap()).unwrap();
|
||||
let mut surface = softbuffer::Surface::new(&context, window.window_handle().unwrap()).unwrap();
|
||||
|
||||
let mut game = GameState::init();
|
||||
|
||||
event_loop
|
||||
.run(|event, elwt| {
|
||||
elwt.set_control_flow(ControlFlow::Wait);
|
||||
elwt.set_control_flow(ControlFlow::WaitUntil(
|
||||
Instant::now() + Duration::from_millis(1000 / 60),
|
||||
));
|
||||
|
||||
match event {
|
||||
Event::WindowEvent {
|
||||
|
@ -51,6 +63,8 @@ fn main() {
|
|||
// }
|
||||
|
||||
buffer.fill(0);
|
||||
game.update();
|
||||
game.render(&mut buffer, (width, height));
|
||||
|
||||
buffer.present().unwrap();
|
||||
}
|
||||
|
@ -75,3 +89,360 @@ fn main() {
|
|||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
mod game {
|
||||
use std::{convert::identity, num::NonZeroU32, time::Instant};
|
||||
|
||||
use softbuffer::Buffer;
|
||||
use winit::raw_window_handle::{DisplayHandle, WindowHandle};
|
||||
|
||||
const PADDLE_SIZE: (u32, u32) = (5, 50);
|
||||
const BALL_RADIUS: u32 = 10;
|
||||
const BALL_VEC_RIGHT_DOWN: (i16, i16) = (8, 12);
|
||||
const FIELD_SIZE: (u32, u32) = (1200, 700);
|
||||
const FIELD_BORDER_WIDTH: u32 = 5;
|
||||
const TICK_LEN: f32 = 1000. / 60.;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum BallDirection {
|
||||
RightUp,
|
||||
RightDown,
|
||||
LeftUp,
|
||||
LeftDown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum HorizontalCollisionCheck {
|
||||
None,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum VerticalCollisionCheck {
|
||||
None,
|
||||
Top,
|
||||
Bottom,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct CollisionCheck(HorizontalCollisionCheck, VerticalCollisionCheck);
|
||||
|
||||
impl CollisionCheck {
|
||||
fn check_field(obj_pos: (u32, u32), obj_size: (u32, u32)) -> Self {
|
||||
let horizontal = if obj_pos.0 == 0 {
|
||||
HorizontalCollisionCheck::Left
|
||||
} else if (obj_pos.0 + obj_size.0) >= FIELD_SIZE.0 {
|
||||
HorizontalCollisionCheck::Right
|
||||
} else {
|
||||
HorizontalCollisionCheck::None
|
||||
};
|
||||
let vertical = if obj_pos.1 == 0 {
|
||||
VerticalCollisionCheck::Top
|
||||
} else if (obj_pos.1 + obj_size.1) >= FIELD_SIZE.1 {
|
||||
VerticalCollisionCheck::Bottom
|
||||
} else {
|
||||
VerticalCollisionCheck::None
|
||||
};
|
||||
Self(horizontal, vertical)
|
||||
}
|
||||
|
||||
fn no_collisions(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
CollisionCheck(HorizontalCollisionCheck::None, VerticalCollisionCheck::None)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct MovingObject {
|
||||
prev_pos: (u32, u32),
|
||||
}
|
||||
|
||||
impl BallDirection {
|
||||
fn get_vec(&self) -> (i16, i16) {
|
||||
match self {
|
||||
BallDirection::RightDown => BALL_VEC_RIGHT_DOWN,
|
||||
BallDirection::RightUp => (BALL_VEC_RIGHT_DOWN.0, -BALL_VEC_RIGHT_DOWN.1),
|
||||
BallDirection::LeftUp => (-BALL_VEC_RIGHT_DOWN.0, BALL_VEC_RIGHT_DOWN.1),
|
||||
BallDirection::LeftDown => (-BALL_VEC_RIGHT_DOWN.0, -BALL_VEC_RIGHT_DOWN.1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GameState {
|
||||
ball_pos: (u32, u32),
|
||||
ball_direction: BallDirection,
|
||||
paddle_r_pos: u32,
|
||||
paddle_y_pos: u32,
|
||||
scores: (u32, u32),
|
||||
last_frame_time: Instant,
|
||||
}
|
||||
|
||||
impl GameState {
|
||||
pub fn init() -> Self {
|
||||
Self {
|
||||
ball_pos: (FIELD_SIZE.0 / 2, FIELD_SIZE.1 / 2),
|
||||
paddle_r_pos: FIELD_SIZE.0 / 2,
|
||||
paddle_y_pos: FIELD_SIZE.0 / 2,
|
||||
ball_direction: BallDirection::RightDown,
|
||||
scores: (0, 0),
|
||||
last_frame_time: Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
&self,
|
||||
buf: &mut Buffer<'_, DisplayHandle<'_>, WindowHandle<'_>>,
|
||||
win_size: (NonZeroU32, NonZeroU32),
|
||||
) {
|
||||
let win_size = (win_size.0.get(), win_size.1.get());
|
||||
self.render_field(buf, win_size);
|
||||
self.render_ball(buf, win_size);
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
let delta_t = self.delta_time();
|
||||
|
||||
self.last_frame_time = Instant::now();
|
||||
self.move_ball();
|
||||
}
|
||||
|
||||
fn move_ball(&mut self) {
|
||||
let vec = self.ball_direction.get_vec();
|
||||
let delta_t = self.delta_time();
|
||||
|
||||
let new_pos = (
|
||||
self.ball_pos
|
||||
.0
|
||||
.saturating_add_signed((vec.0 as f32 * delta_t) as i32)
|
||||
.clamp(0, FIELD_SIZE.0 - BALL_RADIUS),
|
||||
self.ball_pos
|
||||
.1
|
||||
.saturating_add_signed((vec.1 as f32 * delta_t) as i32)
|
||||
.clamp(0, FIELD_SIZE.1 - BALL_RADIUS),
|
||||
);
|
||||
// println!("{:?} -> {:?}", self.ball_pos, new_pos);
|
||||
self.ball_pos = new_pos;
|
||||
|
||||
let check_res = CollisionCheck::check_field(
|
||||
(
|
||||
new_pos.0.saturating_sub(BALL_RADIUS),
|
||||
new_pos.1.saturating_sub(BALL_RADIUS),
|
||||
),
|
||||
(BALL_RADIUS * 2, BALL_RADIUS * 2),
|
||||
);
|
||||
|
||||
let CollisionCheck(horizontal, vertical) = check_res;
|
||||
|
||||
self.ball_direction = match (self.ball_direction, (horizontal, vertical)) {
|
||||
(
|
||||
BallDirection::RightUp,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::Bottom),
|
||||
)
|
||||
| (
|
||||
BallDirection::RightDown,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::None),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftUp,
|
||||
(HorizontalCollisionCheck::Left, VerticalCollisionCheck::None),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftDown,
|
||||
(HorizontalCollisionCheck::Left, VerticalCollisionCheck::Top),
|
||||
) => BallDirection::RightDown,
|
||||
|
||||
(
|
||||
BallDirection::RightUp,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::None),
|
||||
)
|
||||
| (
|
||||
BallDirection::RightDown,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::Top),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftUp,
|
||||
(HorizontalCollisionCheck::Left, VerticalCollisionCheck::Top),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftDown,
|
||||
(HorizontalCollisionCheck::Left, VerticalCollisionCheck::None),
|
||||
) => BallDirection::RightUp,
|
||||
|
||||
(
|
||||
BallDirection::RightUp,
|
||||
(HorizontalCollisionCheck::Right, VerticalCollisionCheck::Bottom),
|
||||
)
|
||||
| (
|
||||
BallDirection::RightDown,
|
||||
(HorizontalCollisionCheck::Right, VerticalCollisionCheck::None),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftUp,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::None),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftDown,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::Bottom),
|
||||
) => BallDirection::LeftUp,
|
||||
|
||||
(
|
||||
BallDirection::RightUp,
|
||||
(HorizontalCollisionCheck::Right, VerticalCollisionCheck::None),
|
||||
)
|
||||
| (
|
||||
BallDirection::RightDown,
|
||||
(HorizontalCollisionCheck::Right, VerticalCollisionCheck::Top),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftUp,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::Top),
|
||||
)
|
||||
| (
|
||||
BallDirection::LeftDown,
|
||||
(HorizontalCollisionCheck::None, VerticalCollisionCheck::None),
|
||||
) => BallDirection::LeftDown,
|
||||
|
||||
other => panic!("Invalid collision: {other:#?}"),
|
||||
};
|
||||
}
|
||||
|
||||
fn delta_time(&self) -> f32 {
|
||||
Instant::now()
|
||||
.duration_since(self.last_frame_time)
|
||||
.as_millis() as f32
|
||||
/ TICK_LEN
|
||||
}
|
||||
|
||||
fn render_field(
|
||||
&self,
|
||||
buf: &mut Buffer<'_, DisplayHandle<'_>, WindowHandle<'_>>,
|
||||
win_size: (u32, u32),
|
||||
) {
|
||||
let field_pos = (
|
||||
(win_size.0 / 2).saturating_sub(FIELD_SIZE.0 / 2),
|
||||
(win_size.1 / 2).saturating_sub(FIELD_SIZE.1 / 2),
|
||||
);
|
||||
|
||||
// top border
|
||||
draw_rect(
|
||||
buf,
|
||||
win_size,
|
||||
(
|
||||
field_pos.0.saturating_sub(FIELD_BORDER_WIDTH),
|
||||
field_pos.1.saturating_sub(FIELD_BORDER_WIDTH),
|
||||
),
|
||||
(
|
||||
FIELD_SIZE.0.saturating_add(FIELD_BORDER_WIDTH * 2),
|
||||
FIELD_BORDER_WIDTH,
|
||||
),
|
||||
0xff0000,
|
||||
);
|
||||
// right border
|
||||
draw_rect(
|
||||
buf,
|
||||
win_size,
|
||||
(field_pos.0.saturating_add(FIELD_SIZE.0), field_pos.1),
|
||||
(FIELD_BORDER_WIDTH, FIELD_SIZE.1),
|
||||
0x00ff00,
|
||||
);
|
||||
// bottom border
|
||||
draw_rect(
|
||||
buf,
|
||||
win_size,
|
||||
(
|
||||
field_pos.0.saturating_sub(FIELD_BORDER_WIDTH),
|
||||
field_pos.1.saturating_add(FIELD_SIZE.1),
|
||||
),
|
||||
(
|
||||
FIELD_SIZE.0.saturating_add(FIELD_BORDER_WIDTH * 2),
|
||||
FIELD_BORDER_WIDTH,
|
||||
),
|
||||
0xff00ff,
|
||||
);
|
||||
// left border
|
||||
draw_rect(
|
||||
buf,
|
||||
win_size,
|
||||
(field_pos.0.saturating_sub(FIELD_BORDER_WIDTH), field_pos.1),
|
||||
(FIELD_BORDER_WIDTH, FIELD_SIZE.1),
|
||||
0x00ffff,
|
||||
);
|
||||
// draw midline
|
||||
draw_rect(
|
||||
buf,
|
||||
win_size,
|
||||
((win_size.0 / 2) - (FIELD_BORDER_WIDTH / 2), field_pos.1),
|
||||
(FIELD_BORDER_WIDTH, FIELD_SIZE.1),
|
||||
0x7f7f7f,
|
||||
)
|
||||
}
|
||||
fn render_ball(
|
||||
&self,
|
||||
buf: &mut Buffer<'_, DisplayHandle<'_>, WindowHandle<'_>>,
|
||||
win_size: (u32, u32),
|
||||
) {
|
||||
let field_pos = (
|
||||
(win_size.0 / 2).saturating_sub(FIELD_SIZE.0 / 2),
|
||||
(win_size.1 / 2).saturating_sub(FIELD_SIZE.1 / 2),
|
||||
);
|
||||
// let field_pos = identity((
|
||||
// (win_size.0 / 2).saturating_sub(FIELD_SIZE.0 / 2),
|
||||
// (win_size.1 / 2).saturating_sub(FIELD_SIZE.1 / 2),
|
||||
// ));
|
||||
// println!("{}, {}", field_pos.0, field_pos.1);
|
||||
|
||||
let ball_render_pos = (
|
||||
(self.ball_pos.0 + field_pos.0).saturating_sub(BALL_RADIUS),
|
||||
(self.ball_pos.1 + field_pos.1).saturating_sub(BALL_RADIUS),
|
||||
);
|
||||
|
||||
draw_rect(
|
||||
buf,
|
||||
win_size,
|
||||
ball_render_pos,
|
||||
(BALL_RADIUS * 2, BALL_RADIUS * 2),
|
||||
0xffffff,
|
||||
);
|
||||
}
|
||||
fn render_paddles(
|
||||
&self,
|
||||
buf: &mut Buffer<'_, DisplayHandle<'_>, WindowHandle<'_>>,
|
||||
win_size: (u32, u32),
|
||||
) {
|
||||
todo!()
|
||||
}
|
||||
fn render_paddle(
|
||||
&self,
|
||||
buf: &mut Buffer<'_, DisplayHandle<'_>, WindowHandle<'_>>,
|
||||
win_size: (u32, u32),
|
||||
) {
|
||||
todo!()
|
||||
}
|
||||
fn render_scores(
|
||||
&self,
|
||||
buf: &mut Buffer<'_, DisplayHandle<'_>, WindowHandle<'_>>,
|
||||
win_size: (u32, u32),
|
||||
) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_rect(
|
||||
buf: &mut Buffer<'_, DisplayHandle<'_>, WindowHandle<'_>>,
|
||||
buf_size: (u32, u32),
|
||||
pos: (u32, u32),
|
||||
size: (u32, u32),
|
||||
color: u32,
|
||||
) {
|
||||
for y in pos.1..(pos.1 + size.1) {
|
||||
for x in pos.0..(pos.0 + size.0) {
|
||||
let index = y as usize * buf_size.0 as usize + x as usize;
|
||||
if let Some(px) = buf.get_mut(index) {
|
||||
*px = color
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue