@ -38,6 +38,12 @@ fn main() {
init_battler ,
) ,
)
. add_systems (
OnEnter ( DebuggingState ::On ) , toggle_visuals
)
. add_systems (
OnExit ( DebuggingState ::On ) , toggle_visuals
)
. add_systems (
Update ,
game_state_machine . run_if ( state_changed ::< GameState > )
@ -315,6 +321,7 @@ impl Display for LevelGoal {
#[ derive(Resource, Debug) ]
struct Visuals {
art : Art ,
mesh : Handle < Mesh > ,
material_o : Handle < ColorMaterial > ,
material_t : Handle < ColorMaterial > ,
@ -325,6 +332,12 @@ struct Visuals {
material_i : Handle < ColorMaterial > ,
}
/// Stores the art assets that may be toggled on and off for development
#[ derive(Component, Debug, Clone) ]
struct Art {
texture : Handle < Image >
}
#[ derive(Resource, Debug, Default) ]
struct Score ( usize ) ;
@ -373,36 +386,47 @@ fn init_tetris(
mut commands : Commands ,
mut meshes : ResMut < Assets < Mesh > > ,
mut materials : ResMut < Assets < ColorMaterial > > ,
server : Res < AssetServer > ,
) {
let mesh = meshes . add ( Rectangle ::new ( SCALE , SCALE ) ) ;
commands . insert_resource ( Visuals {
mesh : mesh . clone ( ) ,
art : Art {
texture : server . load ( "tetris/shape_block.png" ) ,
} ,
material_o : materials . add ( ColorMaterial {
color : YELLOW . into ( ) ,
texture : Some ( server . load ( "tetris/shape_block.png" ) ) ,
.. default ( )
} ) ,
material_t : materials . add ( ColorMaterial {
color : PURPLE . into ( ) ,
texture : Some ( server . load ( "tetris/shape_block.png" ) ) ,
.. default ( )
} ) ,
material_l : materials . add ( ColorMaterial {
color : ORANGE . into ( ) ,
texture : Some ( server . load ( "tetris/shape_block.png" ) ) ,
.. default ( )
} ) ,
material_j : materials . add ( ColorMaterial {
color : BLUE . into ( ) ,
texture : Some ( server . load ( "tetris/shape_block.png" ) ) ,
.. default ( )
} ) ,
material_s : materials . add ( ColorMaterial {
color : LIME . into ( ) ,
texture : Some ( server . load ( "tetris/shape_block.png" ) ) ,
.. default ( )
} ) ,
material_z : materials . add ( ColorMaterial {
color : RED . into ( ) ,
texture : Some ( server . load ( "tetris/shape_block.png" ) ) ,
.. default ( )
} ) ,
material_i : materials . add ( ColorMaterial {
color : AQUA . into ( ) ,
texture : Some ( server . load ( "tetris/shape_block.png" ) ) ,
.. default ( )
} ) ,
} ) ;
@ -414,8 +438,12 @@ fn init_tetris(
Mesh2d ( mesh . clone ( ) ) ,
MeshMaterial2d ( materials . add ( ColorMaterial {
color : BLACK . into ( ) ,
texture : Some ( server . load ( "tetris/bg_block.png" ) ) ,
.. default ( )
} ) ) ,
Art {
texture : server . load ( "tetris/bg_block.png" ) ,
} ,
GridPosition { x , y } ,
Transform ::from_xyz ( 0.0 , 0.0 , - 1.0 ) ,
GridBackground ,
@ -438,17 +466,23 @@ fn init_battler(
mut commands : Commands ,
mut meshes : ResMut < Assets < Mesh > > ,
mut materials : ResMut < Assets < ColorMaterial > > ,
server : Res < AssetServer > ,
) {
{
let mat = materials . add ( ColorMaterial {
color : BLUE . into ( ) ,
texture : Some ( server . load ( "tetris/placeholder_protagonist.png" ) ) ,
.. default ( )
} ) ;
let mesh = meshes . add ( Ellipse ::new ( SCALE , SCALE * 2.0 ) ) ;
let art = Art {
texture : server . load ( "tetris/placeholder_protagonist.png" ) ,
} ;
let t = Transform ::from_xyz ( - 3.0 * SCALE , 0.0 , 0.0 ) ;
commands . spawn ( (
Mesh2d ( mesh ) ,
MeshMaterial2d ( mat ) ,
art ,
t ,
BATTLER ,
Health ( 100.0 ) ,
@ -460,13 +494,18 @@ fn init_battler(
{
let mat = materials . add ( ColorMaterial {
color : RED . into ( ) ,
texture : Some ( server . load ( "tetris/placeholder_enemy.png" ) ) ,
.. default ( )
} ) ;
let art = Art {
texture : server . load ( "tetris/placeholder_enemy.png" ) ,
} ;
let mesh = meshes . add ( Ellipse ::new ( SCALE , SCALE ) ) ;
let t = Transform ::from_xyz ( 3.0 * SCALE , 0.0 , 0.0 ) ;
commands . spawn ( (
Mesh2d ( mesh ) ,
MeshMaterial2d ( mat ) ,
art ,
t ,
BATTLER ,
Health ( 100.0 ) ,
@ -955,12 +994,13 @@ fn on_add_shape_layout(
Shape ::Z = > & visuals . material_z ,
Shape ::I = > & visuals . material_i ,
} ;
let art = visuals . art . clone ( ) ;
commands
. entity ( e )
. with_related_entities ::< ShapeBlock > ( | parent | {
sl . coordinates_at ( center ) . for_each ( | gp | {
parent
. spawn ( ( mesh . clone ( ) , MeshMaterial2d ( mat . clone ( ) ) , gp. unwrap ( ) , Block , TETRIS ) )
. spawn ( ( mesh . clone ( ) , MeshMaterial2d ( mat . clone ( ) ) , art. clone ( ) , gp. unwrap ( ) , Block , TETRIS ) )
. observe ( movement ) ;
} ) ;
} ) ;
@ -1451,3 +1491,19 @@ fn check_level_fail(
}
} ) ;
}
fn toggle_visuals (
state : Res < State < DebuggingState > > ,
query : Query < ( & MeshMaterial2d < ColorMaterial > , & Art ) > ,
mut color_materials : ResMut < Assets < ColorMaterial > > ,
) {
query . iter ( ) . for_each ( | ( mm , a ) | {
let texture : Option < Handle < Image > > = match state . get ( ) {
DebuggingState ::Off = > Some ( a . texture . clone ( ) ) ,
DebuggingState ::On = > None ,
} ;
let cm = color_materials . get_mut ( mm . 0. id ( ) ) . unwrap ( ) ;
cm . texture = texture ;
} ) ;
}