Browse Source

added movement restrictions and destination validation

master
SilverEmber 4 years ago
parent
commit
f383a67b64
  1. 2
      src/actor.rs
  2. 21
      src/apr.rs
  3. 26
      src/behaviour.rs

2
src/actor.rs

@ -12,6 +12,8 @@ pub enum ActorPosn {
y: f32,
held_x: f32,
held_y: f32,
prev_x: u8,
prev_y: u8,
},
}

21
src/apr.rs

@ -49,13 +49,16 @@ impl Apr {
// Load/create resources such as images here.
let board = board::make_board(r, c);
let mut actors: Vec<actor::Actor> = Vec::new();
let mut behaviours: Vec<behaviour::Behaviour> = Vec::new();
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]));
}
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]));
}
Ok(Apr {
grid: make_grid(_ctx, r, c, board.board.as_slice())?,
@ -63,10 +66,7 @@ impl Apr {
should_update_grid: false,
actors,
dragging: false,
behaviours: vec![behaviour::Behaviour { // 4-tile-corner leaper pawn behaviour
moves: vec![(1, 1)],
dirs: (1, 0, 0, 0),
}], //the pawn can go TWO diagonally and to the right and only upwards
behaviours, //the pawn can go TWO diagonally and to the right and only upwards (4-tile-corner leaper pawn behaviour)
})
}
@ -81,18 +81,23 @@ impl Apr {
impl EventHandler for Apr {
fn update(&mut self, ctx: &mut Context) -> GameResult<()> {
let posn = mouse::position(ctx);
for actor in self.actors.iter_mut() {
for (index, actor) in self.actors.iter_mut().enumerate() {
match actor.posn {
actor::ActorPosn::FloatingPosn {
x: _,
y: _,
held_x,
held_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);
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 {
@ -100,6 +105,8 @@ impl EventHandler for Apr {
y: posn.y + held_y,
held_x,
held_y,
prev_x,
prev_y,
};
}
}
@ -115,6 +122,8 @@ impl EventHandler for Apr {
y,
held_x: x - posn.x,
held_y: y - posn.y,
prev_x: posx,
prev_y: posy,
};
}
}

26
src/behaviour.rs

@ -1,4 +1,24 @@
pub struct Behaviour {
pub moves: Vec<(u8, u8)>, //this is a placeholder
pub dirs: (u8, u8, u8, u8), // up right down left
}
pub moves: Vec<(i8, i8)>, //this is a placeholder
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;
}
}
// ok remember wghen i said in chat i didnt think enough on the directions concept. well that was pretty fucked up.
Loading…
Cancel
Save