Skip to content

Commit

Permalink
init cell value
Browse files Browse the repository at this point in the history
  • Loading branch information
foxzool committed Dec 12, 2024
1 parent a72d2d8 commit a26ef7c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 53 deletions.
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::logic::Player;
use crate::board::Player;
use crate::GameState;

mod game_control;
Expand Down
64 changes: 25 additions & 39 deletions src/logic.rs → src/board.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::actions::Actions;
use crate::logic::position::CellPosition;
use crate::board::cell_state::{CellValue, FixedCell};
use crate::board::position::CellPosition;
use crate::GameState;
use bevy::color::palettes::basic::{BLACK, GRAY};
use bevy::color::palettes::css;
use bevy::prelude::*;
use sudoku::board::CellState;
use sudoku::strategy::StrategySolver;
use sudoku::Sudoku;

Expand All @@ -21,12 +23,12 @@ pub struct SudokuManager {
}

/// This plugin handles player related stuff like movement
/// Player logic is only active during the State `GameState::Playing`
/// Player board 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(),
(spawn_board, init_cells).chain(),
)
.add_systems(Update, move_player.run_if(in_state(GameState::Playing)));
}
Expand Down Expand Up @@ -97,7 +99,7 @@ fn spawn_board(mut commands: Commands, asset_server: Res<AssetServer>) {
// CellsLayout,
)).with_children(|builder| {
// 生成九个宫格
for block_index in (0..9) {
for block_index in 0..9 {
builder.spawn((Node {
height: Val::Percent(100.0),
aspect_ratio: Some(1.0),
Expand All @@ -111,7 +113,7 @@ fn spawn_board(mut commands: Commands, asset_server: Res<AssetServer>) {
},
BackgroundColor(GRAY.into())
)).with_children(|builder| {
for bi in (0..9) {
for bi in 0..9 {
let cell = block_index * 9 + bi;
builder.spawn((
CellPosition::new(cell),
Expand Down Expand Up @@ -170,51 +172,35 @@ fn spawn_board(mut commands: Commands, asset_server: Res<AssetServer>) {
});
}

#[derive(Component)]
struct CellsLayout;

#[derive(Component)]
struct ControlLayout;

fn spawn_cells(
mut commands: Commands,
layout: Single<Entity, With<CellsLayout>>,
asset_server: Res<AssetServer>,
) {
fn init_cells(mut commands: Commands, cell_position: Query<(Entity, &CellPosition)>) {
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);
'l: for (index, cell_state) in solver.grid_state().into_iter().enumerate() {
let cell_value = CellValue(cell_state);

for (entity, cell_position) in cell_position.iter() {
if cell_position.0 == index as u8 {
match &cell_value.0 {
// 如果一开始就是数字,那么这个格子是固定的
CellState::Digit(_) => {
commands.entity(entity).insert(FixedCell).insert(cell_value);
}
CellState::Candidates(_) => {
commands.entity(entity).insert(cell_value);
}
}

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()
},
Outline {
width: Val::Px(1.),
color: Color::srgb_u8(97, 97, 97),
..default()
},
// BackgroundColor(BISQUE.into())
))
.with_children(|builder| {
builder.spawn((
Node::default(),
BackgroundColor(bevy::color::palettes::css::BISQUE.into()),
));
});
});
continue 'l;
}
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/board/cell_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use bevy::prelude::*;

use sudoku::board::CellState;

/// 格子的值
#[derive(Component, Debug, Deref, DerefMut)]
pub struct CellValue(pub CellState);

#[derive(Component)]
pub struct FixedCell;
File renamed without changes.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

mod actions;
mod audio;
mod board;
mod loading;
mod menu;
mod logic;

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

use bevy::app::App;
#[cfg(debug_assertions)]
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::*;

// This example game uses States to separate logic
// This example game uses States to separate board
// See https://bevy-cheatbook.github.io/programming/states.html
// Or https://github.com/bevyengine/bevy/blob/main/examples/ecs/state.rs
#[derive(States, Default, Clone, Eq, PartialEq, Debug, Hash)]
enum GameState {
// During the loading State the LoadingPlugin will load our assets
#[default]
Loading,
// During this State the actual game logic is executed
// During this State the actual game board is executed
Playing,
// Here the menu is drawn and waiting for player interaction
Menu,
Expand Down
2 changes: 1 addition & 1 deletion src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_kira_audio::AudioSource;
pub struct LoadingPlugin;

/// This plugin loads all assets using [`AssetLoader`] from a third party bevy plugin
/// Alternatively you can write the logic to load assets yourself
/// Alternatively you can write the board to load assets yourself
/// If interested, take a look at <https://bevy-cheatbook.github.io/features/assets.html>
impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
Expand Down
8 changes: 0 additions & 8 deletions src/logic/cell_state.rs

This file was deleted.

0 comments on commit a26ef7c

Please sign in to comment.