Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bf78729a1 | ||
|
|
f38e811e14 | ||
|
|
08f8931502 | ||
|
|
9852f30581 | ||
|
|
7283bf7ecf |
No files matched your search
+3
-3
@@ -6,6 +6,6 @@
|
|||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
|
||||||
# асеты
|
# асеты
|
||||||
assets/**/*
|
# assets/**/*
|
||||||
!assets/shaders/
|
# !assets/shaders/
|
||||||
!assets/shaders/**/*
|
# !assets/shaders/**/*
|
||||||
@@ -4,3 +4,11 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bevy = "0.20"
|
||||||
|
|
||||||
|
[profildev]
|
||||||
|
opt-level = 1
|
||||||
|
debug = true
|
||||||
|
|
||||||
|
[profile.dev.package."*"]
|
||||||
|
opt-level = 3
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 76 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,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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
use std::io;
|
|
||||||
|
|
||||||
use crate::chess_api::Move;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub enum SquareInput {
|
|
||||||
File(u8), // столбец 0..8 (a..h)
|
|
||||||
Rank(u8), // строка 0..8 (1..8)
|
|
||||||
Cell(u8), // конкретная клетка 0..64
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SquareInput {
|
|
||||||
pub fn matches(&self, sq: u8) -> bool {
|
|
||||||
match *self {
|
|
||||||
SquareInput::File(f) => (sq % 8) == f,
|
|
||||||
SquareInput::Rank(r) => (sq / 8) == r,
|
|
||||||
SquareInput::Cell(c) => sq == c,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_square() -> SquareInput {
|
|
||||||
loop {
|
|
||||||
let mut s = String::new();
|
|
||||||
io::stdin().read_line(&mut s).unwrap();
|
|
||||||
let b = s.trim().to_lowercase().into_bytes();
|
|
||||||
|
|
||||||
match b.len() {
|
|
||||||
1 => {
|
|
||||||
if (b'a'..=b'h').contains(&b[0]) {
|
|
||||||
return SquareInput::File(b[0] - b'a');
|
|
||||||
}
|
|
||||||
if (b'1'..=b'8').contains(&b[0]) {
|
|
||||||
return SquareInput::Rank(b[0] - b'1');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
2 => {
|
|
||||||
if (b'a'..=b'h').contains(&b[0]) && (b'1'..=b'8').contains(&b[1]) {
|
|
||||||
return SquareInput::Cell((b[1] - b'1') * 8 + (b[0] - b'a'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
println!("не подходит, ещё раз:");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sq_to_string(sq: u8) -> String {
|
|
||||||
let file = (sq % 8) as u8 + b'a';
|
|
||||||
let rank = (sq / 8) as u8 + b'1';
|
|
||||||
format!("{}{}", file as char, rank as char)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn has_unique_from(items: &[Move]) -> bool {
|
|
||||||
let mut froms: Vec<u8> = items.iter().map(|x| x.from).collect();
|
|
||||||
froms.sort_unstable();
|
|
||||||
froms.windows(2).any(|w| w[0] != w[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unique_froms(items: &[Move]) -> Vec<u8> {
|
|
||||||
let mut froms: Vec<u8> = items.iter().map(|x| x.from).collect();
|
|
||||||
froms.sort_unstable();
|
|
||||||
|
|
||||||
let mut result = Vec::new();
|
|
||||||
let n = froms.len();
|
|
||||||
let mut i = 0;
|
|
||||||
while i < n {
|
|
||||||
let mut j = i + 1;
|
|
||||||
while j < n && froms[j] == froms[i] { j += 1; }
|
|
||||||
if j - i == 1 {
|
|
||||||
result.push(froms[i]);
|
|
||||||
}
|
|
||||||
i = j;
|
|
||||||
}
|
|
||||||
result
|
|
||||||
}
|
|
||||||
+19
-55
@@ -4,62 +4,26 @@ use chess_api::*;
|
|||||||
mod render_bitmask;
|
mod render_bitmask;
|
||||||
use render_bitmask::*;
|
use render_bitmask::*;
|
||||||
|
|
||||||
mod gaming;
|
use bevy::prelude::*;
|
||||||
use gaming::*;
|
|
||||||
|
mod game_client;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
App::new()
|
||||||
let mut game = Breakthrough::new();
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||||
|
primary_window: Some(Window {
|
||||||
loop {
|
title: "local-chess".into(),
|
||||||
println!("---<>---");
|
resolution: (800, 800).into(),
|
||||||
|
resizable: true,
|
||||||
let b = game.get_board();
|
..default()
|
||||||
|
}),
|
||||||
render_board(
|
..default()
|
||||||
BoardColor::Checkerboard('•', ' '),
|
}))
|
||||||
&[
|
.add_plugins(game_client::Cli)
|
||||||
(b.get_mask(Color::White, 0), '●'),
|
.add_systems(Startup, hello_world_system)
|
||||||
(b.get_mask(Color::Black, 0), '○'),
|
.run();
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
let status = game.status();
|
|
||||||
println!("side: {:?}", game.side_to_move());
|
|
||||||
if status != GameStatus::Ongoing {
|
|
||||||
println!("game over: {:?}", status);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
let moves = game.legal_moves();
|
|
||||||
|
|
||||||
let to_input = read_square();
|
|
||||||
let cand: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to)).copied().collect();
|
|
||||||
let f_cand = unique_froms(&cand);
|
|
||||||
|
|
||||||
match f_cand.clone().as_slice() {
|
|
||||||
[] => {
|
|
||||||
println!("incorekt");
|
|
||||||
}
|
|
||||||
[f_input] => {
|
|
||||||
let s: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to) && m.from == *f_input).copied().collect();
|
|
||||||
game.apply(s[0]).unwrap();
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
for i in &f_cand {
|
|
||||||
print!("{}, ", sq_to_string(*i));
|
|
||||||
}
|
|
||||||
print!("\n");
|
|
||||||
let f_input = read_square();
|
|
||||||
let k: Vec<_> = f_cand.iter().filter(|m| f_input.matches(**m)).copied().collect();
|
|
||||||
if k.len() == 1 {
|
|
||||||
let s: Vec<Move> = moves.iter().filter(|m| to_input.matches(m.to) && m.from == k[0]).copied().collect();
|
|
||||||
game.apply(s[0]).unwrap();
|
|
||||||
} else {
|
|
||||||
println!("incorekt2");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hello_world_system() {
|
||||||
|
println!("Hello, World!");
|
||||||
|
}
|
||||||
Reference in new issue
Block a user