@ -1,3 +1,13 @@
//A NOTE TO WHOMSOEVER READS THIS FILE
//EVERYTHING THAT TOUCHES THE BOARD STRUCT IS FUCKED SINCE IT GOES IN ROW-COLUMN AND EVERYTHING ELSE GOES IN COLUMN-ROW
//REFACTORING IS A MUST
//PLEASE LOOK AT ALL USES OF BOARD STRUCT WHEN YOU DO THIS
//a double note
//I JUST REFACTORED IT
//BUT IM KEEPING THIS COMMENT TIL YOU MOTHERFUCKERS READ IT
//BECAUSE I WANT YOU TO KNOW THE PAIN I HAVE FELT
use ggez ::event ::EventHandler ;
use ggez ::graphics ::{ self , Color , DrawMode , Mesh , MeshBuilder } ;
use ggez ::input ::mouse ::{ self , MouseButton } ;
@ -19,20 +29,25 @@ pub struct Apr {
behaviours : Vec < behaviour ::Behaviour > ,
}
pub fn make_grid ( ctx : & mut Context , r : u8 , c : u8 , board : & [ Vec < u8 > ] ) -> GameResult < Mesh > {
pub fn make_grid (
ctx : & mut Context ,
xlen : u8 ,
ylen : u8 ,
board : & [ Vec < ( u8 , u8 ) > ] ,
) -> GameResult < Mesh > {
let mut builder = MeshBuilder ::new ( ) ;
for row in 0 . . r {
for col in 0 . . c {
for x in 0 . . xlen {
for y in 0 . . ylen {
builder . rectangle (
DrawMode ::fill ( ) ,
graphics ::Rect {
x : board ::START_X + ( board ::BOARD_SQUARE_SIZE * f32 ::from ( col ) ) ,
y : board ::START_Y + ( board ::BOARD_SQUARE_SIZE * f32 ::from ( row ) ) ,
x : board ::START_X + ( board ::BOARD_SQUARE_SIZE * f32 ::from ( x ) ) ,
y : board ::START_Y + ( board ::BOARD_SQUARE_SIZE * f32 ::from ( y ) ) ,
w : board ::BOARD_SQUARE_SIZE ,
h : board ::BOARD_SQUARE_SIZE ,
} ,
match board [ row as usize ] [ col as usize ] % 2 {
match board [ x as usize ] [ y as usize ] . 0 % 2 {
0 = > Color ::WHITE ,
1 = > Color ::BLACK ,
_ = > Color ::BLACK , //impossible
@ -45,82 +60,37 @@ pub fn make_grid(ctx: &mut Context, r: u8, c: u8, board: &[Vec<u8>]) -> GameResu
}
impl Apr {
pub fn new ( _ctx : & mut Context , r : u8 , c : u8 ) -> GameResult < Apr > {
pub fn new ( _ctx : & mut Context , xlen : u8 , ylen : u8 ) -> GameResult < Apr > {
// Load/create resources such as images here.
let board = board ::make_board ( r , c ) ;
let board = board ::make_board ( xlen , ylen ) ;
let mut actors : Vec < actor ::Actor > = Vec ::new ( ) ;
let mut behaviours : Vec < behaviour ::Behaviour > = Vec ::new ( ) ;
for i in 0 . . r {
let mut behaviours : Vec < behaviour ::Behaviour > = vec ! [ behaviour ::Behaviour {
moves : vec ! [ behaviour ::MovePattern {
movement : ( 0 , - 1 ) ,
amount : 1 ,
} ] ,
} ] ; //pawn can move ONE forward, ONCE. not really the best test
for i in 0 . . xlen {
// white side pawns
actors . push ( actor ::Actor ::new (
_ctx ,
"/pawn.png" ,
i ,
1 ,
behaviours . iter ( ) . len ( ) as u8 ,
) ) ;
behaviours . push ( behaviour ::Behaviour ::new (
vec ! [ ( 1 , 1 ) ] ,
vec ! [ ( 1 , - 1 ) , ( - 1 , - 1 ) ] ,
) ) ;
}
for i in 0 . . = 1 {
// white side knights
actors . push ( actor ::Actor ::new (
_ctx ,
"/pawn.png" ,
i ,
0 ,
behaviours . iter ( ) . len ( ) as u8 ,
) ) ;
behaviours . push ( behaviour ::Behaviour ::new (
vec ! [ ( 1 , 2 ) ] ,
vec ! [ ( 1 , - 1 ) , ( - 1 , - 1 ) ] ,
) ) ;
}
for i in 0 . . r {
// black side pawns
actors . push ( actor ::Actor ::new (
_ctx ,
"/pawn2.png" ,
i ,
c - 2 ,
behaviours . iter ( ) . len ( ) as u8 ,
) ) ;
behaviours . push ( behaviour ::Behaviour ::new (
vec ! [ ( 1 , 1 ) ] ,
vec ! [ ( 1 , 1 ) , ( - 1 , 1 ) ] ,
) ) ;
}
for i in 0 . . = 1 {
// black side knights
actors . push ( actor ::Actor ::new (
_ctx ,
"/pawn2.png" ,
i ,
c - 1 ,
behaviours . iter ( ) . len ( ) as u8 ,
) ) ;
behaviours . push ( behaviour ::Behaviour ::new (
vec ! [ ( 1 , 2 ) ] ,
vec ! [ ( 1 , 1 ) , ( - 1 , 1 ) ] ,
) ) ;
actors . push ( actor ::Actor ::new ( _ctx , "/pawn.png" , i , 6 , 0 ) ) ;
}
actors . push ( actor ::Actor ::new ( _ctx , "/pawn.png" , 4 , 4 , 0 ) ) ; //blocking pawn for testing
Ok ( Apr {
grid : make_grid ( _ctx , r , c , board . board . as_slice ( ) ) ? ,
grid : make_grid ( _ctx , xlen , ylen , board . board . as_slice ( ) ) ? ,
board ,
should_update_grid : false ,
actors ,
dragging : false ,
behaviours , //the pawn can go TWO diagonally and to the right and only upwards (4-tile-corner leaper pawn behaviour)
behaviours ,
} )
}
#[ allow(dead_code) ]
pub fn set_colour ( & mut self , r : usize , c : usize , clr : u8 ) {
pub fn set_colour ( & mut self , x : usize , y : usize , clr : u8 ) {
//You should only modify self.board through this method.
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 . board . board [ x ] [ y ] . 0 = 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.
}
}
@ -143,12 +113,20 @@ impl EventHandler for Apr {
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 ) ) {
if ! self . behaviours [ usize ::from ( actor . behaviour ) ] . validate_dest (
( prev_x , prev_y ) ,
( x , y ) ,
& self . board ,
) {
actor . posn = actor ::ActorPosn ::BoardPosn {
x : prev_x ,
y : prev_y ,
} ;
} else {
self . board . board [ usize ::from ( prev_y ) ] [ usize ::from ( prev_x ) ] . 1 = 255 ;
self . board . board [ usize ::from ( y ) ] [ usize ::from ( x ) ] . 1 = index as u8 ; //only 255 actors allowed? should be fine (laughing) (we look back at this in 10 years and die)
//[x][y] or [y][x]?? i think its [y][x] since row-column
//update, eveyrthing is fucked
actor . posn = actor ::ActorPosn ::BoardPosn { x , y } ;
}
}
@ -194,8 +172,8 @@ impl EventHandler for Apr {
if self . should_update_grid {
self . grid = make_grid (
ctx ,
self . board . row ,
self . board . col ,
self . board . xlen ,
self . board . ylen ,
self . board . board . as_slice ( ) ,
) ? ;
self . should_update_grid = false ;
@ -210,7 +188,7 @@ impl EventHandler for Apr {
ctx ,
& actor . image ,
drawparams . dest ( [
board ::START_X + board ::BOARD_SQUARE_SIZE * f32 ::from ( x ) ,
board ::START_X + board ::BOARD_SQUARE_SIZE * f32 ::from ( x ) , //i might have to kill myself
board ::START_Y + board ::BOARD_SQUARE_SIZE * f32 ::from ( y ) ,
] ) ,
) ? ,