Browse Source

Small, small, refactoring + added comments

master
e-dt 4 years ago
parent
commit
f5cf81571c
  1. 31
      src/main.rs

31
src/main.rs

@ -30,7 +30,7 @@ enum ActorPosn {
x: u8,
y: u8,
},
Floating {
Floating { //This probably needs some `previous position on board' fields
x: f32,
y: f32,
held_x: f32,
@ -47,6 +47,7 @@ struct Actor {
behaviour: u8, //index into the behaviours array
}
fn make_grid(ctx: &mut Context, r: i32, c: i32, board: &[Vec<u8>]) -> GameResult<Mesh> {
let mut builder = MeshBuilder::new();
@ -60,16 +61,17 @@ fn make_grid(ctx: &mut Context, r: i32, c: i32, board: &[Vec<u8>]) -> GameResult
w: BOARD_SQUARE_SIZE,
h: BOARD_SQUARE_SIZE,
},
match board[row as usize][col as usize] {
match board[row as usize][col as usize] % 2 {
0 => Color::WHITE,
1 => Color::BLACK,
2 => Color::RED,
_ => Color::BLACK //impossible
/* 2 => Color::RED,
3 => Color::CYAN,
4 => Color::YELLOW,
5 => Color::GREEN,
6 => Color::BLUE,
7 => Color::MAGENTA,
_ => Color::BLACK,
_ => Color::BLACK,*/
},
)?;
}
@ -88,6 +90,13 @@ fn make_board(r: i32, c: i32) -> Vec<Vec<u8>> {
init
}
//I have moved this function out of the impl block for borrow checking reasons.
//This should really only borrow self.col and self.row, but as a method it borrows everything.
//As a result, I couldn't use this while iterating through actors.
//Possible solution: Wrap `col' and `row' up as some kind of Dimensions struct, then make this a method on that struct.
//Would then only borrow the dimensions.
//http://smallcultfollowing.com/babysteps/blog/2018/11/01/after-nll-interprocedural-conflicts/
fn closest_square_coords(x: f32, y: f32, col: i32, row: i32) -> (u8, u8) {
//First, normalise to within the grid.
let mut x = x;
@ -131,10 +140,9 @@ impl Apr {
})
}
fn set_colour(&mut self, r: usize, c: usize, col: u8) -> GameResult<()> {
self.board[r][c] = col;
self.should_update_grid = true;
Ok(())
fn set_colour(&mut self, r: usize, c: usize, col: u8) { //You should only modify self.board through this method.
self.board[r][c] = col; //Making a getter for self.board would sadly break borrowing in the same way that closest_square_coords does.
self.should_update_grid = true; //Similar solutions apply.
}
}
impl EventHandler for Apr {
@ -193,12 +201,7 @@ impl EventHandler for Apr {
graphics::draw(ctx, &self.grid, drawparams)?;
for actor in &self.actors {
match actor.posn {
Floating {
x,
y,
held_x: _,
held_y: _,
} => graphics::draw(ctx, &actor.image, drawparams.dest([x, y]))?,
Floating { x, y, .. } => graphics::draw(ctx, &actor.image, drawparams.dest([x, y]))?,
Board { x, y } => graphics::draw(
ctx,
&actor.image,

Loading…
Cancel
Save