Compare commits
27
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bf78729a1 | ||
|
|
f38e811e14 | ||
|
|
08f8931502 | ||
|
|
9852f30581 | ||
|
|
7283bf7ecf | ||
|
|
03c09101fe | ||
|
|
27fd785034 | ||
|
|
2c2ccd6770 | ||
|
|
54c458296d | ||
|
|
6c0453cce3 | ||
|
|
bda716da90 | ||
|
|
651c4713c2 | ||
|
|
b895eaa265 | ||
|
|
d7b9e571f3 | ||
|
|
85039bedcf | ||
|
|
a61395ca6a | ||
|
|
45e1843fba | ||
|
|
e4a570e17b | ||
|
|
e27482082b | ||
|
|
18d6a96b0d | ||
|
|
98a2784de2 | ||
|
|
40d06aaceb | ||
|
|
d36ef92b3e | ||
|
|
5da99ed444 | ||
|
|
90c8c9882b | ||
|
|
9ab067ae7d | ||
|
|
ac9f7fede4 |
No files matched your search
+2
-2
@@ -7,5 +7,5 @@ Cargo.lock
|
||||
|
||||
# асеты
|
||||
# assets/**/*
|
||||
!assets/shaders/
|
||||
!assets/shaders/**/*
|
||||
# !assets/shaders/
|
||||
# !assets/shaders/**/*
|
||||
+1
-1
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.18"
|
||||
bevy = "0.20"
|
||||
|
||||
[profildev]
|
||||
opt-level = 1
|
||||
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 771 KiB |
@@ -0,0 +1,84 @@
|
||||
import bevy_ui_render::ui_vertex_output::UiVertexOutput;
|
||||
|
||||
fn random (pos: vec2<f32>) -> f32 {
|
||||
return fract(sin(dot(pos.xy,
|
||||
vec2(12.9898,78.233)))*
|
||||
43758.5453123);
|
||||
}
|
||||
|
||||
fn noise (st: vec2<f32>) -> f32 {
|
||||
let i = floor(st);
|
||||
let f = fract(st);
|
||||
|
||||
// Four corners in 2D of a tile
|
||||
let a = random(i);
|
||||
let b = random(i + vec2(1.0, 0.0));
|
||||
let c = random(i + vec2(0.0, 1.0));
|
||||
let d = random(i + vec2(1.0, 1.0));
|
||||
|
||||
let u = f * f * (3.0 - 2.0 * f);
|
||||
|
||||
return mix(a, b, u.x) +
|
||||
(c - a)* u.y * (1.0 - u.x) +
|
||||
(d - b) * u.x * u.y;
|
||||
}
|
||||
|
||||
fn fbm(st: vec2<f32>) -> f32 {
|
||||
var value = 0.0;
|
||||
var amplitude = 0.5;
|
||||
var p = st;
|
||||
|
||||
for (var i: u32 = 0u; i < 6u; i += 1u) {
|
||||
value += amplitude * noise(p);
|
||||
p *= 2.0;
|
||||
amplitude *= 0.5;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
fn sp(in: f32) -> f32 {
|
||||
return min(in * 2.0, -in * 2.0 + 2.0);
|
||||
}
|
||||
|
||||
fn sp2(in: f32) -> f32 {
|
||||
return max(sp(in * 2.0), sp(in * 2.0) - 1.0);
|
||||
}
|
||||
|
||||
fn wood(uv: vec2<f32>, color: vec3<f32>, sel: bool) -> vec3<f32> {
|
||||
let b = select(color, color * 0.6, sel);
|
||||
return b;
|
||||
}
|
||||
|
||||
fn scene(uv: vec2<f32>) -> vec4<f32> {
|
||||
let cell = floor(uv * 8.0);
|
||||
|
||||
let dark_green = vec3<f32>(0.08, 0.28, 0.16);
|
||||
let light_beige = vec3<f32>(0.88, 0.84, 0.72);
|
||||
|
||||
let color = select(
|
||||
wood(uv, dark_green, sp(fbm(uv * 60.0)) > 0.9),
|
||||
wood(uv, light_beige, sp(fbm(uv * 80.0 + vec2<f32>(400.0, 400.0))) > 0.9),
|
||||
(i32(cell.x) + i32(cell.y)) % 2 == 0
|
||||
);
|
||||
|
||||
return vec4<f32>(color, 1.0);
|
||||
}
|
||||
|
||||
fn antialias(uv: vec2<f32>, delta: vec2<f32>) -> vec4<f32> {
|
||||
var color = vec4<f32>(0.0);
|
||||
|
||||
color += scene(uv + delta * vec2<f32>(-0.5, -0.5));
|
||||
color += scene(uv + delta * vec2<f32>( 0.5, -0.5));
|
||||
color += scene(uv + delta * vec2<f32>(-0.5, 0.5));
|
||||
color += scene(uv + delta * vec2<f32>( 0.5, 0.5));
|
||||
|
||||
return color * 0.25;
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fragment(in: UiVertexOutput) -> @location(0) vec4<f32> {
|
||||
let delta = fwidth(in.uv) * 0.5;
|
||||
|
||||
return antialias(in.uv, delta);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import bevy_ui_render::ui_vertex_output::UiVertexOutput;
|
||||
|
||||
@group(1) @binding(1) var combined_texture: texture_2d<f32>;
|
||||
@group(1) @binding(2) var combined_sampler: sampler;
|
||||
|
||||
fn scene(uv: vec2<f32>) -> vec4<f32> {
|
||||
let tex_color = textureSample(combined_texture, combined_sampler, uv);
|
||||
let mask = tex_color.a;
|
||||
|
||||
// Alpha cutout по маске
|
||||
if mask < 0.1 {
|
||||
discard;
|
||||
}
|
||||
|
||||
// Распаковка нормали: [0, 1] -> [-1, 1]
|
||||
// Если тени выглядят вывернутыми — поменяйте знак у G-компонента.
|
||||
let normal = normalize(vec3<f32>(
|
||||
tex_color.r * 2.0 - 1.0,
|
||||
tex_color.g * 2.0 - 1.0,
|
||||
tex_color.b * 2.0 - 1.0,
|
||||
));
|
||||
|
||||
// Направленный свет: постоянный вектор к источнику
|
||||
let light_dir = normalize(vec3<f32>(-0.5, -1.0, 0.5));
|
||||
let ndotl = max(dot(normal, light_dir), 0.0);
|
||||
|
||||
// Ambient, чтобы затенённые участки не были чёрными
|
||||
let ambient = 0.25;
|
||||
let lighting = ambient + ndotl * 0.75;
|
||||
|
||||
// Базовый цвет поверхности — например, тот же зелёный, что был у вас.
|
||||
// (или можно взять из uniform-поля материала, если добавите)
|
||||
let base_color = vec3<f32>(0.08, 0.28, 0.4);
|
||||
|
||||
let final_color = base_color * lighting;
|
||||
|
||||
return vec4<f32>(final_color, 1.0);
|
||||
}
|
||||
|
||||
fn antialias(uv: vec2<f32>, delta: vec2<f32>) -> vec4<f32> {
|
||||
var color = vec4<f32>(0.0);
|
||||
|
||||
color += scene(uv + delta * vec2<f32>(-0.5, -0.5));
|
||||
color += scene(uv + delta * vec2<f32>( 0.5, -0.5));
|
||||
color += scene(uv + delta * vec2<f32>(-0.5, 0.5));
|
||||
color += scene(uv + delta * vec2<f32>( 0.5, 0.5));
|
||||
|
||||
return color * 0.25;
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fragment(in: UiVertexOutput) -> @location(0) vec4<f32> {
|
||||
let delta = fwidth(in.uv) * 0.5;
|
||||
|
||||
return antialias(in.uv, delta);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
mod trajectory;
|
||||
pub use trajectory::*;
|
||||
|
||||
mod movement_mask;
|
||||
pub use movement_mask::mov_mask;
|
||||
|
||||
use super::Color;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Board<const N: usize> {
|
||||
layers: [[u64; 2]; N],
|
||||
}
|
||||
|
||||
impl<const N: usize> Board<N> {
|
||||
pub const fn from_layers(layers: [[u64; 2]; N]) -> Self {
|
||||
Self { layers }
|
||||
}
|
||||
pub fn apply_patch(&mut self, patch: &MaskPatch) {
|
||||
for layer in self.layers.iter_mut() {
|
||||
layer[0] &= !patch.clear_mask;
|
||||
layer[1] &= !patch.clear_mask;
|
||||
}
|
||||
|
||||
for (side, y, mask) in patch.iter() {
|
||||
let x = side.index();
|
||||
debug_assert!(x < 2 && y < N);
|
||||
if x >= 2 || y >= N {
|
||||
eprintln!(
|
||||
"Board::apply_patch: invalid set_mask coords (side={:?}, layer={}, N={})",
|
||||
side, y, N
|
||||
);
|
||||
panic!("invalid set_mask coordinates");
|
||||
}
|
||||
self.layers[y][x] |= mask;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mask(&self, x: Color, y: usize) -> u64 {
|
||||
if y >= N || x.index() >= 2 {
|
||||
return 0;
|
||||
}
|
||||
self.layers[y][x.index()]
|
||||
}
|
||||
|
||||
pub fn is_mask_zero(&self, x: Color, y: usize) -> bool {
|
||||
self.get_mask(x, y) == 0
|
||||
}
|
||||
|
||||
pub fn and_mask_is_zero(&self, x: Color, y: usize, mask: u64) -> bool {
|
||||
if y >= N || x.index() >= 2 {
|
||||
return true;
|
||||
}
|
||||
(self.layers[y][x.index()] & mask) == 0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct MaskPatch {
|
||||
pub clear_mask: u64,
|
||||
pub set_masks: [Option<(Color, usize, u64)>; 4],
|
||||
len: u8,
|
||||
}
|
||||
|
||||
impl MaskPatch {
|
||||
pub fn new(clear_mask: u64) -> Self {
|
||||
Self { clear_mask, set_masks: [None; 4], len: 0 }
|
||||
}
|
||||
|
||||
pub fn push(&mut self, side: Color, layer: usize, mask: u64) {
|
||||
debug_assert!((self.len as usize) < 4);
|
||||
self.set_masks[self.len as usize] = Some((side, layer, mask));
|
||||
self.len += 1;
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = (Color, usize, u64)> + '_ {
|
||||
self.set_masks[..self.len as usize]
|
||||
.iter()
|
||||
.map(|o| o.unwrap())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
|
||||
pub fn mov_mask(mut x: i8) -> u64{
|
||||
let mut mask: u64;
|
||||
|
||||
if x == 0 {
|
||||
return 0xffffffffffffffff;
|
||||
} else {
|
||||
if x > 0 {
|
||||
mask = 1;
|
||||
x -= 1;
|
||||
} else {
|
||||
mask = 128;
|
||||
x += 1;
|
||||
}
|
||||
}
|
||||
|
||||
while x != 0{
|
||||
if x > 0 {
|
||||
mask |= mask << 1;
|
||||
x -= 1;
|
||||
} else {
|
||||
mask |= mask >> 1;
|
||||
x += 1;
|
||||
}
|
||||
}
|
||||
|
||||
mask |= mask << 8;
|
||||
mask |= mask << 16;
|
||||
mask |= mask << 32;
|
||||
|
||||
!mask
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
pub fn trajectory(mut position: u64, mask: u64, own_position: u64, enemy_position: u64, shift: i8, mut range: Option<u8>) -> u64 {
|
||||
|
||||
let combined_mask = mask & !own_position;
|
||||
|
||||
let mut l: u64 = 0;
|
||||
|
||||
while position != 0 && range != Some(0) {
|
||||
position = shift_i8(position, shift);
|
||||
position = position & combined_mask;
|
||||
|
||||
l = l | position;
|
||||
|
||||
position = position & !enemy_position;
|
||||
|
||||
if let Some(ref mut n) = range {
|
||||
*n -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
l
|
||||
}
|
||||
|
||||
fn shift_i8(value: u64, amount: i8) -> u64{
|
||||
if amount >= 0 {
|
||||
value << amount
|
||||
} else {
|
||||
value >> (-amount)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
use std::collections::HashMap;
|
||||
use super::*;
|
||||
|
||||
pub const WHITE_WON_ZONE: u64 = 0xff << 56;
|
||||
pub const BLACK_WON_ZONE: u64 = 0xff;
|
||||
|
||||
pub const START_BOARD: Board<1> = Board::from_layers([
|
||||
[0xffff, 0xffff << 48],
|
||||
]);
|
||||
|
||||
pub struct Breakthrough{
|
||||
board: Board<1>,
|
||||
side: Color,
|
||||
legal_moves: Option<HashMap<Move, MaskPatch>>,
|
||||
}
|
||||
|
||||
impl Breakthrough {
|
||||
pub fn new() -> Self {
|
||||
let mut p = Breakthrough{
|
||||
board: START_BOARD,
|
||||
side: Color::White,
|
||||
legal_moves: None
|
||||
};
|
||||
p.refresh_legal_moves();
|
||||
p
|
||||
}
|
||||
|
||||
pub fn refresh_legal_moves(&mut self) {
|
||||
self.legal_moves = Some(self.generate_legal());
|
||||
}
|
||||
|
||||
pub fn get_board(&self) -> Board<1> {
|
||||
self.board.clone()
|
||||
}
|
||||
|
||||
fn generate_legal(&self) -> HashMap<Move, MaskPatch>{
|
||||
let mut legal: HashMap<Move, MaskPatch> = HashMap::new();
|
||||
|
||||
let white = self.board.get_mask(Color::White, 0);
|
||||
let black = self.board.get_mask(Color::Black, 0);
|
||||
|
||||
let own = match self.side {
|
||||
Color::White => white,
|
||||
Color::Black => black,
|
||||
};
|
||||
|
||||
let mut bits = own;
|
||||
while bits != 0 {
|
||||
let from = bits.trailing_zeros() as u8;
|
||||
let from_bit = 1u64 << from;
|
||||
bits &= bits - 1;
|
||||
|
||||
let mut t = self.mov_pattern(from_bit);
|
||||
while t != 0 {
|
||||
let to = t.trailing_zeros() as u8;
|
||||
let to_bit = 1u64 << to;
|
||||
t &= t - 1;
|
||||
|
||||
let mv = Move { from, to, p: Promotion::NoPromotion };
|
||||
|
||||
let mut patch = MaskPatch::new(from_bit | to_bit);
|
||||
patch.push(self.side, 0, to_bit);
|
||||
|
||||
legal.insert(mv, patch);
|
||||
}
|
||||
}
|
||||
|
||||
legal
|
||||
}
|
||||
|
||||
fn mov_pattern(&self, pos: u64) -> u64 {
|
||||
let white = self.board.get_mask(Color::White, 0);
|
||||
let black = self.board.get_mask(Color::Black, 0);
|
||||
|
||||
let (own, enemy, mvc, mvl, mvr) = match self.side {
|
||||
Color::White => (
|
||||
white, black,
|
||||
(8i8, mov_mask(0)),
|
||||
(7i8, mov_mask(-1)),
|
||||
(9i8, mov_mask(1)),
|
||||
),
|
||||
Color::Black => (
|
||||
black, white,
|
||||
(-8i8, mov_mask(0)),
|
||||
(-7i8, mov_mask(1)),
|
||||
(-9i8, mov_mask(-1)),
|
||||
),
|
||||
};
|
||||
|
||||
let mmvc = trajectory(pos, mvc.1, own | enemy, 0, mvc.0, Some(1));
|
||||
let mmvl = trajectory(pos, mvl.1, own, enemy, mvl.0, Some(1));
|
||||
let mmvr = trajectory(pos, mvr.1, own, enemy, mvr.0, Some(1));
|
||||
|
||||
mmvc | mmvr | mmvl
|
||||
}
|
||||
}
|
||||
|
||||
impl ChessEngine for Breakthrough {
|
||||
fn legal_moves(&self) -> Vec<Move> {
|
||||
self.legal_moves.clone().unwrap().keys().cloned().collect()/*.expect("cache empty; call refresh first")*/
|
||||
}
|
||||
|
||||
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors> {
|
||||
if self.status() != GameStatus::Ongoing {
|
||||
return Err(MoveErrors::GameOver);
|
||||
}
|
||||
|
||||
let patch = self
|
||||
.legal_moves
|
||||
.as_ref()
|
||||
.ok_or(MoveErrors::NoMoveStack)?
|
||||
.get(&mv)
|
||||
.ok_or(MoveErrors::InvalidMove)?;
|
||||
|
||||
self.board.apply_patch(patch);
|
||||
self.side = self.side.opposite();
|
||||
self.refresh_legal_moves();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn status(&self) -> GameStatus {
|
||||
if self.board.is_mask_zero(Color::White, 0) {
|
||||
return GameStatus::Won(Color::Black);
|
||||
}
|
||||
if self.board.is_mask_zero(Color::Black, 0) {
|
||||
return GameStatus::Won(Color::White);
|
||||
}
|
||||
if !self.board.and_mask_is_zero(Color::Black, 0, BLACK_WON_ZONE) {
|
||||
return GameStatus::Won(Color::Black);
|
||||
}
|
||||
if !self.board.and_mask_is_zero(Color::White, 0, WHITE_WON_ZONE) {
|
||||
return GameStatus::Won(Color::White);
|
||||
}
|
||||
GameStatus::Ongoing
|
||||
}
|
||||
|
||||
fn side_to_move(&self) -> Color{
|
||||
self.side
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
use super::*;
|
||||
|
||||
mod breakthrough;
|
||||
pub use breakthrough::Breakthrough;
|
||||
@@ -0,0 +1,85 @@
|
||||
pub mod board;
|
||||
pub use board::*;
|
||||
|
||||
mod engines;
|
||||
pub use engines::*;
|
||||
|
||||
pub trait ChessEngine {
|
||||
// fn new() -> Self;
|
||||
fn legal_moves(&self) -> Vec<Move>;
|
||||
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors>;
|
||||
|
||||
fn side_to_move(&self) -> Color;
|
||||
fn status(&self) -> GameStatus;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MoveErrors { GameOver, InvalidMove, NoMoveStack }
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Move{
|
||||
pub from: u8,
|
||||
pub to: u8,
|
||||
pub p: Promotion,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[repr(u8)]
|
||||
pub enum Promotion {
|
||||
NoPromotion = 0,
|
||||
Knight = 1,
|
||||
Rook = 2,
|
||||
Bishop = 3,
|
||||
Queen = 4,
|
||||
}
|
||||
|
||||
impl From<Promotion> for u8 {
|
||||
#[inline]
|
||||
fn from(p: Promotion) -> u8 {
|
||||
p as u8
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for Promotion {
|
||||
type Error = ();
|
||||
|
||||
#[inline]
|
||||
fn try_from(v: u8) -> Result<Self, ()> {
|
||||
match v {
|
||||
0 => Ok(Promotion::NoPromotion),
|
||||
1 => Ok(Promotion::Knight),
|
||||
2 => Ok(Promotion::Rook),
|
||||
3 => Ok(Promotion::Bishop),
|
||||
4 => Ok(Promotion::Queen),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Color {
|
||||
White,
|
||||
Black,
|
||||
}
|
||||
|
||||
impl Color {
|
||||
pub fn opposite(self) -> Color {
|
||||
match self {
|
||||
Color::White => Color::Black,
|
||||
Color::Black => Color::White,
|
||||
}
|
||||
}
|
||||
pub fn index(self) -> usize {
|
||||
match self {
|
||||
Color::White => 0,
|
||||
Color::Black => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum GameStatus {
|
||||
Ongoing,
|
||||
Won(Color),
|
||||
Draw,
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
mod ui_matireal;
|
||||
use ui_matireal::{BoardMaterial, PawnMaterial};
|
||||
|
||||
pub struct Cli;
|
||||
|
||||
impl Plugin for Cli {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(UiMaterialPlugin::<BoardMaterial>::default());
|
||||
app.add_plugins(UiMaterialPlugin::<PawnMaterial>::default());
|
||||
app.add_systems(Startup, setup_scene);
|
||||
// app.add_systems(Update, my_system);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn setup_scene(
|
||||
mut commands: Commands,
|
||||
mut b_materials: ResMut<Assets<BoardMaterial>>,
|
||||
mut p_materials: ResMut<Assets<PawnMaterial>>,
|
||||
asset: Res<AssetServer>,
|
||||
) {
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
let texture_handle: Handle<Image> = asset.load("pawn_combined.png");
|
||||
let texture_handle30: Handle<Image> = asset.load("pawn_combined30.png");
|
||||
|
||||
commands.spawn((
|
||||
Node {
|
||||
width: Val::Percent(100.0),
|
||||
height: Val::Percent(100.0),
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
..default()
|
||||
},
|
||||
BackgroundColor(Color::srgb(0.1, 0.1, 0.1)),
|
||||
children![
|
||||
(
|
||||
Node {
|
||||
width: Val::VMin(90.0),
|
||||
height: Val::VMin(90.0),
|
||||
aspect_ratio: Some(1.0),
|
||||
..default()
|
||||
},
|
||||
BackgroundColor(Color::srgb(0.2, 0.6, 0.8)),
|
||||
children![
|
||||
(
|
||||
Node {
|
||||
width: Val::Percent(100.0),
|
||||
height: Val::Percent(100.0),
|
||||
..default()
|
||||
},
|
||||
MaterialNode(b_materials.add(BoardMaterial {})),
|
||||
children![
|
||||
(
|
||||
Node {
|
||||
width: Val::Percent(12.5),
|
||||
height: Val::Percent(12.5),
|
||||
..default()
|
||||
},
|
||||
MaterialNode(p_materials.add(PawnMaterial {
|
||||
combined_map: texture_handle
|
||||
})),
|
||||
),
|
||||
(
|
||||
Node {
|
||||
width: Val::Percent(12.5),
|
||||
height: Val::Percent(12.5),
|
||||
..default()
|
||||
},
|
||||
MaterialNode(p_materials.add(PawnMaterial {
|
||||
combined_map: texture_handle30
|
||||
})),
|
||||
),
|
||||
]
|
||||
),
|
||||
],
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy::render::render_resource::AsBindGroup;
|
||||
use bevy::shader::ShaderRef;
|
||||
|
||||
// Пустая структура — наш флаг-материал.
|
||||
#[derive(AsBindGroup, Asset, TypePath, Debug, Clone)]
|
||||
pub struct BoardMaterial {}
|
||||
|
||||
impl UiMaterial for BoardMaterial {
|
||||
fn fragment_shader() -> ShaderRef {
|
||||
"shaders/custom_ui_material.wesl".into()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(AsBindGroup, Asset, TypePath, Debug, Clone)]
|
||||
pub struct PawnMaterial {
|
||||
#[texture(1)]
|
||||
#[sampler(2)]
|
||||
pub combined_map: Handle<Image>,
|
||||
}
|
||||
|
||||
impl UiMaterial for PawnMaterial {
|
||||
fn fragment_shader() -> ShaderRef {
|
||||
"shaders/pawn.wesl".into()
|
||||
}
|
||||
}
|
||||
+15
-82
@@ -1,96 +1,29 @@
|
||||
use bevy::{prelude::*, ui::RelativeCursorPosition};
|
||||
mod chess_api;
|
||||
use chess_api::*;
|
||||
|
||||
mod render_bitmask;
|
||||
use render_bitmask::*;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
mod game_client;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
resolution: (800, 800).into(),
|
||||
title: "local-chess".into(),
|
||||
resolution: (800, 800).into(),
|
||||
resizable: true,
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
}))
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, relative_cursor_position_system)
|
||||
.add_plugins(game_client::Cli)
|
||||
.add_systems(Startup, hello_world_system)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, assets: Res<AssetServer>) {
|
||||
commands.spawn(Camera2d);
|
||||
commands
|
||||
.spawn(Node {
|
||||
width: percent(100),
|
||||
height: percent(100),
|
||||
align_items: AlignItems::Center,
|
||||
justify_content: JustifyContent::Center,
|
||||
flex_direction: FlexDirection::Column,
|
||||
..default()
|
||||
})
|
||||
.with_children(|parent| {
|
||||
parent
|
||||
.spawn((
|
||||
Node {
|
||||
width: px(512),
|
||||
height: px(512),
|
||||
margin: UiRect::bottom(px(15)),
|
||||
..default()
|
||||
},
|
||||
ImageNode::new(assets.load("scene/chessboard.png")),
|
||||
|
||||
))
|
||||
.insert(RelativeCursorPosition::default());
|
||||
});
|
||||
|
||||
commands
|
||||
.spawn(Node {
|
||||
width: percent(100),
|
||||
height: percent(100),
|
||||
align_items: AlignItems::Center,
|
||||
justify_content: JustifyContent::Center,
|
||||
flex_direction: FlexDirection::Column,
|
||||
left: percent(-40.0),
|
||||
top: percent(-40.0),
|
||||
..default()
|
||||
})
|
||||
.with_children(|parent| {
|
||||
parent.spawn((
|
||||
Text::new("who"),
|
||||
|
||||
TextFont {
|
||||
font: assets.load("fonts/Baskerville Bold Italic.ttf").into(),
|
||||
//font_size: FontSize::Px(33.0),
|
||||
font_size: 50.0,
|
||||
..default()
|
||||
},
|
||||
TextColor(Color::srgb(1.0, 0.5, 1.0)),
|
||||
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
fn relative_cursor_position_system(rcp: Single<&RelativeCursorPosition>, text: Single<&mut Text>){
|
||||
//let ratio;
|
||||
let is_white = true;
|
||||
let mut output_text = text.into_inner();
|
||||
if let Some(pos) = rcp.normalized{
|
||||
|
||||
let mut x = ((pos.x + 0.5) * 8.0) as usize + 1;
|
||||
let mut y;
|
||||
if is_white {
|
||||
y = ((pos.y - 0.5) * -8.0 ) as usize + 1;
|
||||
} else {
|
||||
y = ((pos.y + 0.5) * 8.0) as usize + 1;
|
||||
}
|
||||
let m = ((y - 1) * 8) + x-1;
|
||||
x = (pos.x > -0.5 && pos.x < 0.5) as usize * x;
|
||||
let cx = [' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
|
||||
y = (pos.y > -0.5 && pos.y < 0.5) as usize * y;
|
||||
let cy = [' ', '1', '2', '3', '4', '5', '6', '7', '8'];
|
||||
|
||||
** output_text = format!("{}|{}|{}", cx[x], cy[y], m);
|
||||
} else {
|
||||
** output_text = " ".to_string();
|
||||
}
|
||||
fn hello_world_system() {
|
||||
println!("Hello, World!");
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum BoardColor {
|
||||
Solid(char),
|
||||
Checkerboard(char, char),
|
||||
}
|
||||
|
||||
pub fn render_u64(a: u64) {
|
||||
let mut p: u64 = 1 << 56;
|
||||
for _ in 0..8 {
|
||||
for x in 0..8 {
|
||||
if (p << x as u64) & a == 0 {
|
||||
print!("0");
|
||||
} else {
|
||||
print!("1");
|
||||
}
|
||||
}
|
||||
p = p >> 8;
|
||||
print!("\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn render_board_row(background: BoardColor, layers: &[(u64, char)], row: u8) {
|
||||
let p: u64 = 1 << 56 >> (row as u64 * 8);
|
||||
for x in 0..8 {
|
||||
if let Some((_, ch)) = layers.iter().find(|(a, _)| (p << x as u64) & a > 0) {
|
||||
print!("{}", ch)
|
||||
} else {
|
||||
match background {
|
||||
BoardColor::Solid(a) => print!("{}", a),
|
||||
BoardColor::Checkerboard(a, b) => {
|
||||
if (x + row as usize) % 2 == 0 {
|
||||
print!("{}", a)
|
||||
} else {
|
||||
print!("{}", b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
|
||||
pub fn render_board(background: BoardColor, layers: &[(u64, char)]) {
|
||||
for row in 0..8 {
|
||||
render_board_row(background.clone(), layers, row);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user