From b76b6ba02bc36fb27add64e4c27afde7f6fd1acc Mon Sep 17 00:00:00 2001 From: iamsosmart19 Date: Fri, 31 Dec 2021 18:11:34 +1100 Subject: [PATCH 1/2] Refactored "make actor" into Actor::new() --- src/actor.rs | 11 +++++++++++ src/apr.rs | 12 ++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index d78f6f4..48e1bd3 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -1,4 +1,5 @@ use ggez::graphics; +use ggez::Context; #[path = "board.rs"] mod board; @@ -23,3 +24,13 @@ pub struct Actor { pub posn: ActorPosn, pub behaviour: u8, //index into the behaviours array } + +impl Actor { + pub fn new(ctx: &mut Context, img_path: &str, _x: u8, _y: u8, b: u8) -> Actor { + Actor { + image: graphics::Image::new(ctx, img_path).unwrap(), + posn: ActorPosn::BoardPosn { x: _x, y: _y }, + behaviour: b, + } + } +} diff --git a/src/apr.rs b/src/apr.rs index 4146928..8748faa 100644 --- a/src/apr.rs +++ b/src/apr.rs @@ -52,11 +52,11 @@ impl Apr { let mut actors: Vec = Vec::new(); for i in 0..r { // white side - actors.push(Apr::make_actor(_ctx, "/pawn.png", i, 0, 0)); + actors.push(actor::Actor::new(_ctx, "/pawn.png", i, 0, 0)); } for i in 0..r { // black side - actors.push(Apr::make_actor(_ctx, "/pawn2.png", i, c - 1, 0)); + actors.push(actor::Actor::new(_ctx, "/pawn2.png", i, c - 1, 0)); } Ok(Apr { grid: make_grid(_ctx, r, c, board.board.as_slice())?, @@ -70,14 +70,6 @@ impl Apr { }) } - pub fn make_actor(ctx: &mut Context, img_path: &str, _x: u8, _y: u8, b: u8) -> actor::Actor { - actor::Actor { - image: graphics::Image::new(ctx, img_path).unwrap(), - posn: actor::ActorPosn::BoardPosn { x: _x, y: _y }, - behaviour: b, - } - } - pub fn set_colour(&mut self, r: usize, c: usize, col: u8) { //You should only modify self.board through this method. self.board.board[r][c] = col; //Making a getter for self.board would sadly break borrowing in the same way that closest_square_coords does. From ec31d8976a38e0f31582bb82b8ce3871db0a45fb Mon Sep 17 00:00:00 2001 From: iamsosmart19 Date: Fri, 31 Dec 2021 18:12:53 +1100 Subject: [PATCH 2/2] Renamed argument from col to clr --- src/apr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/apr.rs b/src/apr.rs index 8748faa..750f799 100644 --- a/src/apr.rs +++ b/src/apr.rs @@ -70,9 +70,9 @@ impl Apr { }) } - pub fn set_colour(&mut self, r: usize, c: usize, col: u8) { + pub fn set_colour(&mut self, r: usize, c: usize, clr: u8) { //You should only modify self.board through this method. - self.board.board[r][c] = col; //Making a getter for self.board would sadly break borrowing in the same way that closest_square_coords does. + self.board.board[r][c] = clr; //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. } }