diff --git a/src/actor.rs b/src/actor.rs index e86968a..44f5eec 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -12,8 +12,8 @@ pub enum ActorPosn { y: f32, held_x: f32, held_y: f32, - prev_x: u8, - prev_y: u8, + prev_x: u8, + prev_y: u8, }, } diff --git a/src/apr.rs b/src/apr.rs index cf5f7ff..9c58f3e 100644 --- a/src/apr.rs +++ b/src/apr.rs @@ -53,12 +53,18 @@ impl Apr { for i in 0..r { // white side actors.push(actor::Actor::new(_ctx, "/pawn.png", i, 1, 0)); - behaviours.push(behaviour::Behaviour::new(vec![(-1, -1), (1, -1)], [0, 0, 1, 0])); + behaviours.push(behaviour::Behaviour::new( + vec![(-1, -1), (1, -1)], + [0, 0, 1, 0], + )); } for i in 0..r { // black side actors.push(actor::Actor::new(_ctx, "/pawn2.png", i, c - 2, 0)); - behaviours.push(behaviour::Behaviour::new(vec![(1, 1), (-1, 1)], [1, 0, 0, 0])); + behaviours.push(behaviour::Behaviour::new( + vec![(1, 1), (-1, 1)], + [1, 0, 0, 0], + )); } Ok(Apr { grid: make_grid(_ctx, r, c, board.board.as_slice())?, @@ -88,16 +94,23 @@ impl EventHandler for Apr { y: _, held_x, held_y, - prev_x, - prev_y, + prev_x, + prev_y, } => { if !mouse::button_pressed(ctx, MouseButton::Left) { // no long clicking and dragging let (x, y) = self.board.closest_square_coords(posn.x, posn.y); - if self.dragging { // released, so now we check destination - if !self.behaviours[index].validate_dest((prev_x, prev_y), (x, y)) { actor.posn = actor::ActorPosn::BoardPosn { x: prev_x, y: prev_y }; } - else { actor.posn = actor::ActorPosn::BoardPosn { x, y }; } - } + if self.dragging { + // released, so now we check destination + if !self.behaviours[index].validate_dest((prev_x, prev_y), (x, y)) { + actor.posn = actor::ActorPosn::BoardPosn { + x: prev_x, + y: prev_y, + }; + } else { + actor.posn = actor::ActorPosn::BoardPosn { x, y }; + } + } self.dragging = false; } else { actor.posn = actor::ActorPosn::FloatingPosn { @@ -105,8 +118,8 @@ impl EventHandler for Apr { y: posn.y + held_y, held_x, held_y, - prev_x, - prev_y, + prev_x, + prev_y, }; } } @@ -122,8 +135,8 @@ impl EventHandler for Apr { y, held_x: x - posn.x, held_y: y - posn.y, - prev_x: posx, - prev_y: posy, + prev_x: posx, + prev_y: posy, }; } } diff --git a/src/behaviour.rs b/src/behaviour.rs index fb5ab56..cf8490c 100644 --- a/src/behaviour.rs +++ b/src/behaviour.rs @@ -1,24 +1,29 @@ pub struct Behaviour { pub moves: Vec<(i8, i8)>, //this is a placeholder - pub dirs: [u8; 4], // up right down left + pub dirs: [u8; 4], // up right down left } impl Behaviour { - pub fn new(m: Vec<(i8, i8)>, d: [u8; 4]) -> Behaviour { - Behaviour { - moves: m, - dirs: d, - } - } - - pub fn validate_dest(&self, origin: (u8, u8), destination: (u8, u8)) -> bool { // original plan was to use match but i felt for loops would work better since we dont yet know the extent of moves vector - //println!("origin: ({}, {}), destination: ({}, {})", origin.0, origin.1, destination.0, destination.1); - for moveset in self.moves.iter() { - for dir in self.dirs.iter() { // mind blanking need to add something to confirm which direction we are iterating through - if (((origin.0 as i8) + (*dir as i8) * moveset.0) as u8, ((origin.1 as i8) - (*dir as i8) * moveset.1) as u8) == destination { return true; } // flip the second component of calculation because the grid works from top left isntead of bot left like a piss - } - } - return false; - } + pub fn new(m: Vec<(i8, i8)>, d: [u8; 4]) -> Behaviour { + Behaviour { moves: m, dirs: d } + } + + pub fn validate_dest(&self, origin: (u8, u8), destination: (u8, u8)) -> bool { + // original plan was to use match but i felt for loops would work better since we dont yet know the extent of moves vector + //println!("origin: ({}, {}), destination: ({}, {})", origin.0, origin.1, destination.0, destination.1); + for moveset in self.moves.iter() { + for dir in self.dirs.iter() { + // mind blanking need to add something to confirm which direction we are iterating through + if ( + ((origin.0 as i8) + (*dir as i8) * moveset.0) as u8, + ((origin.1 as i8) - (*dir as i8) * moveset.1) as u8, + ) == destination + { + return true; + } // flip the second component of calculation because the grid works from top left isntead of bot left like a piss + } + } + return false; + } } -// ok remember wghen i said in chat i didnt think enough on the directions concept. well that was pretty fucked up. \ No newline at end of file +// ok remember wghen i said in chat i didnt think enough on the directions concept. well that was pretty fucked up. diff --git a/src/main.rs b/src/main.rs index fda676b..c3f80d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ mod behaviour; mod board; fn main() -> GameResult<()> { - let mut cb = ContextBuilder::new("apruebo", "s1m7u and e-dt"); + let mut cb = ContextBuilder::new("apruebo", "s1m7u, e-dt and silverember"); if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") { let mut path = path::PathBuf::from(manifest_dir);