Skip to content

Commit

Permalink
开始添加布局
Browse files Browse the repository at this point in the history
  • Loading branch information
foxzool committed Dec 11, 2024
1 parent f339c9d commit 079d5bd
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 49 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ image = { version = "0.25", default-features = false }
## This greatly improves WGPU's performance due to its heavy use of trace! calls
log = { version = "0.4", features = ["max_level_debug", "release_max_level_warn"] }

sudoku = "0.8.0"

[build-dependencies]
embed-resource = "1"
Binary file added assets/fonts/franklin-normal-600.ttf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::math::Vec3Swizzles;
use bevy::prelude::*;

use crate::actions::game_control::{get_movement, GameControl};
use crate::player::Player;
use crate::logic::Player;
use crate::GameState;

mod game_control;
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ mod actions;
mod audio;
mod loading;
mod menu;
mod player;
mod logic;

use crate::actions::ActionsPlugin;
use crate::audio::InternalAudioPlugin;
use crate::loading::LoadingPlugin;
use crate::menu::MenuPlugin;
use crate::player::PlayerPlugin;
use crate::logic::SudokuPlugin;

use bevy::app::App;
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -40,7 +40,7 @@ impl Plugin for GamePlugin {
MenuPlugin,
ActionsPlugin,
InternalAudioPlugin,
PlayerPlugin,
SudokuPlugin,
));

#[cfg(debug_assertions)]
Expand Down
191 changes: 191 additions & 0 deletions src/logic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
use crate::actions::Actions;
use crate::logic::position::CellPosition;
use crate::GameState;
use bevy::color::palettes::basic::BLACK;
use bevy::color::palettes::css::BISQUE;
use bevy::prelude::*;
use sudoku::strategy::StrategySolver;
use sudoku::Sudoku;

mod cell_state;
mod position;

pub struct SudokuPlugin;

#[derive(Component)]
pub struct Player;

#[derive(Resource, Debug)]
pub struct SudokuManager {
pub current_sudoku: Sudoku,
}

/// This plugin handles player related stuff like movement
/// Player logic is only active during the State `GameState::Playing`
impl Plugin for SudokuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), (spawn_board, spawn_cells).chain())
.add_systems(Update, move_player.run_if(in_state(GameState::Playing)));
}
}

fn spawn_board(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/franklin-normal-600.ttf");
commands.spawn((
Node {
// 使用网格布局
display: Display::Grid,
width: Val::Percent(100.0),
height: Val::Percent(100.0),
// 网格有两列,第一列宽度为内容宽度,第二列宽度为剩余空间
grid_template_columns: vec![GridTrack::min_content(), GridTrack::flex(1.0)],
// 网格有两行,第一行高度为内容高度,第二行占据剩余空间
grid_template_rows: vec![
GridTrack::px(60.),
// GridTrack::auto(),
GridTrack::flex(1.0)
],
..default()
},
BackgroundColor(Color::WHITE),
)).with_children(|builder| {
// 顶部菜单栏
builder
.spawn(
Node {
display: Display::Grid,
// Make this node span two grid columns so that it takes up the entire top tow
grid_column: GridPlacement::span(2),
padding: UiRect::all(Val::Px(6.0)),
..default()
},
)
.with_children(|builder| {
builder.spawn((
Text::new("top-level grid"),
TextFont { font: font.clone(), ..default() },
TextColor::BLACK,
));
});

// 格子布局容器
builder
.spawn((Node {
// height: Val::Percent(100.0),
margin: UiRect::axes(Val::Px(24.0), Val::Px(0.)),
..default()
},
// BackgroundColor(RED.into()),
)).with_children(|builder| {
builder.spawn((
Node {
height: Val::Percent(100.0),
// height: Val::Px(729.0 + 8.0 + 2.0),
aspect_ratio: Some(1.0),
display: Display::Grid,

grid_template_columns: RepeatedGridTrack::flex(9, 1.0),
grid_template_rows: RepeatedGridTrack::flex(9, 1.0),
row_gap: Val::Px(1.0),
column_gap: Val::Px(1.0),
border: UiRect::all(Val::Px(2.0)),
..default()
},
BorderColor(Color::BLACK),
// BackgroundColor(Color::WHITE),
CellsLayout,
));
});


// 右侧边栏
builder
.spawn((
Node {
display: Display::Grid,
// Align content towards the start (top) in the vertical axis
align_items: AlignItems::Start,
// Align content towards the center in the horizontal axis
justify_items: JustifyItems::Center,
// Add 10px padding
padding: UiRect::all(Val::Px(10.)),
// Add an fr track to take up all the available space at the bottom of the column so that the text nodes
// can be top-aligned. Normally you'd use flexbox for this, but this is the CSS Grid example so we're using grid.
grid_template_rows: vec![GridTrack::auto(), GridTrack::auto(), GridTrack::fr(1.0)],
// Add a 10px gap between rows
row_gap: Val::Px(10.),
..default()
},
BackgroundColor(BLACK.into()),
))
.with_children(|builder| {
builder.spawn((Text::new("Sidebar"),
TextFont {
font: font.clone(),
..default()
},
));
builder.spawn((Text::new("A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely."),
TextFont {
font: font.clone(),
font_size: 13.0,
..default()
},
));
builder.spawn(Node::default());
});
});
}

#[derive(Component)]
struct CellsLayout;

#[derive(Component)]
struct ControlLayout;

fn spawn_cells(mut commands: Commands, layout: Single<Entity, With<CellsLayout>>, asset_server: Res<AssetServer>) {
let sudoku = Sudoku::generate();

let solver = StrategySolver::from_sudoku(sudoku.clone());
commands.insert_resource(SudokuManager { current_sudoku: sudoku });


for (index, cell_state) in solver.grid_state().into_iter().enumerate() {
let cell_state = cell_state::CellState(cell_state);

commands.entity(*layout).with_children(|commands| {
commands.spawn((
cell_state,
CellPosition::new(index as u8),
Node {
display: Display::Grid,
// border: UiRect::all(Val::Px(1.)),
..default()
},
BorderColor(Color::srgb_u8(18, 18, 18)),
BackgroundColor(BISQUE.into())
)).with_children(|builder| {
// builder.spawn((Node::default(), BackgroundColor(BISQUE.into())));
});
});
}
}

fn move_player(
time: Res<Time>,
actions: Res<Actions>,
mut player_query: Query<&mut Transform, With<Player>>,
) {
if actions.player_movement.is_none() {
return;
}
let speed = 150.;
let movement = Vec3::new(
actions.player_movement.unwrap().x * speed * time.delta_secs(),
actions.player_movement.unwrap().y * speed * time.delta_secs(),
0.,
);
for mut player_transform in &mut player_query {
player_transform.translation += movement;
}
}
8 changes: 8 additions & 0 deletions src/logic/cell_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use bevy::prelude::Component;

use sudoku::board::CellState as CellStateEnum;

#[derive(Component, Debug)]
pub struct CellState(pub CellStateEnum);


21 changes: 21 additions & 0 deletions src/logic/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use bevy::prelude::Component;

/// 数独格子的位置
#[derive(Component, Debug)]
pub struct CellPosition(pub u8);


impl CellPosition {
pub fn new(cell: u8) -> CellPosition {
assert!(cell < 81);
CellPosition(cell)
}

pub fn row(&self) -> u8 {
self.0 / 9
}

pub fn col(&self) -> u8 {
self.0 % 9
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn main() {
fit_canvas_to_parent: true,
// Tells wasm not to override default event handling, like F5 and Ctrl+R
prevent_default_event_handling: false,
resolution: bevy::window::WindowResolution::new(1920., 820.),
..default()
}),
..default()
Expand Down
45 changes: 0 additions & 45 deletions src/player.rs

This file was deleted.

0 comments on commit 079d5bd

Please sign in to comment.