Skip to content

Commit

Permalink
rotate camera on turn switch - compilation error!
Browse files Browse the repository at this point in the history
  • Loading branch information
Crazytieguy committed Nov 8, 2021
1 parent a4bf572 commit 64592ff
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 67 deletions.
55 changes: 38 additions & 17 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::pieces::*;
use bevy::prelude::*;
use bevy_mod_picking::*;
use bevy_mod_picking::{PickableBundle, PickingCamera};

use crate::pieces::{is_check_mate_on, is_check_on, Piece, PieceColor, PieceType};

pub struct Square {
pub pos: IVec2,
Expand Down Expand Up @@ -81,11 +82,16 @@ impl FromWorld for SquareMaterials {
let mut materials = world
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap();
let make_material = |r, g, b| StandardMaterial {
roughness: 0.5,
base_color: Color::rgb(r, g, b),
..Default::default()
};
SquareMaterials {
highlight_color: materials.add(Color::rgb(0.8, 0.3, 0.3).into()),
selected_color: materials.add(Color::rgb(0.9, 0.1, 0.1).into()),
black_color: materials.add(Color::rgb(0., 0.1, 0.1).into()),
white_color: materials.add(Color::rgb(1., 0.9, 0.9).into()),
highlight_color: materials.add(make_material(0.8, 0.3, 0.3)),
selected_color: materials.add(make_material(0.9, 0.1, 0.1)),
black_color: materials.add(make_material(0., 0.1, 0.1)),
white_color: materials.add(make_material(1., 0.9, 0.9)),
}
}
}
Expand All @@ -98,15 +104,30 @@ struct SelectedSquare {
struct SelectedPiece {
entity: Option<Entity>,
}
pub struct PlayerTurn(pub PieceColor);
impl Default for PlayerTurn {

pub enum StatusType {
Move,
Win,
}

pub struct GameStatus {
pub color: PieceColor,
pub status_type: StatusType,
}
impl Default for GameStatus {
fn default() -> Self {
Self(PieceColor::White)
Self {
color: PieceColor::White,
status_type: StatusType::Move,
}
}
}
impl PlayerTurn {
fn change(&mut self) {
self.0 = self.0.other();
impl GameStatus {
fn change(&mut self, pieces: &[Piece]) {
match is_check_mate_on(pieces, self.color.other()) {
false => self.color = self.color.other(),
true => self.status_type = StatusType::Win,
}
}
}

Expand Down Expand Up @@ -140,7 +161,7 @@ fn select_square(
fn select_piece(
selected_square: Res<SelectedSquare>,
mut selected_piece: ResMut<SelectedPiece>,
turn: Res<PlayerTurn>,
game_status: Res<GameStatus>,
squares_query: Query<&Square>,
pieces_query: Query<(Entity, &Piece)>,
) {
Expand All @@ -161,7 +182,7 @@ fn select_piece(
if selected_piece.entity.is_none() {
// Select the piece in the currently selected square
for (piece_entity, piece) in pieces_query.iter() {
if piece.pos == square.pos && piece.color == turn.0 {
if piece.pos == square.pos && piece.color == game_status.color {
// piece_entity is now the entity in the same square
selected_piece.entity = Some(piece_entity);
break;
Expand All @@ -174,7 +195,7 @@ fn move_piece(
mut commands: Commands,
selected_square: Res<SelectedSquare>,
selected_piece: Res<SelectedPiece>,
mut turn: ResMut<PlayerTurn>,
mut turn: ResMut<GameStatus>,
squares_query: Query<&Square>,
mut pieces_query: Query<(Entity, &mut Piece)>,
mut reset_selected_event: EventWriter<ResetSelectedEvent>,
Expand Down Expand Up @@ -219,7 +240,7 @@ fn move_piece(
// Move piece
piece.pos = square.pos;
piece.has_moved = true;
turn.change();
turn.change(&pieces_after_move);

// Check if a piece of the opposite color exists in this square and despawn it
if let Some((entity, _)) = pieces_query
Expand Down Expand Up @@ -263,7 +284,7 @@ impl Plugin for BoardPlugin {
app.init_resource::<SelectedSquare>()
.init_resource::<SelectedPiece>()
.init_resource::<SquareMaterials>()
.init_resource::<PlayerTurn>()
.init_resource::<GameStatus>()
.add_event::<ResetSelectedEvent>()
.add_startup_system(create_board.system())
.add_system(color_squares.system())
Expand Down
91 changes: 91 additions & 0 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::f32::consts::PI;

use bevy::{prelude::*, render::camera::PerspectiveProjection};
use bevy_mod_picking::PickingCameraBundle;

use crate::{board::GameStatus, pieces::PieceColor};

const ACCELERATION: f32 = PI * 2.;
const INITIAL_SPEED: f32 = 0.5;
struct CameraPosition {
yaw: f32,
}

impl Default for CameraPosition {
fn default() -> Self {
CameraPosition { yaw: -PI / 2. }
}
}

impl CameraPosition {
fn get_rotation(&self) -> Quat {
Quat::from_rotation_ypr(self.yaw, -1.3, 0.)
}

fn get_translation(&self) -> Vec3 {
Vec3::new(
3.5 * (self.yaw.sin() + 1.),
13.,
3.5 * (self.yaw.cos() + 1.),
)
}

fn get_speed(&self) -> f32 {
let theta = PI / 2. - self.yaw.abs();
f32::sqrt(2. * ACCELERATION * theta) + INITIAL_SPEED
}
}

fn setup(mut commands: Commands, camera_yaw: Res<CameraPosition>) {
commands
// Camera
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_matrix(Mat4::from_rotation_translation(
camera_yaw.get_rotation(),
camera_yaw.get_translation(),
)),
..Default::default()
})
.insert_bundle(PickingCameraBundle::default())
// Light
.commands()
.spawn_bundle(LightBundle {
transform: Transform::from_translation(Vec3::new(3.5, 10., 3.5)),
..Default::default()
});
}

fn reposition_camera(
time: Res<Time>,
game_status: Res<GameStatus>,
mut camera_position: ResMut<CameraPosition>,
mut camera_query: Query<&mut Transform, With<PerspectiveProjection>>,
) {
let target_yaw = match game_status.color {
PieceColor::White => -PI / 2.,
PieceColor::Black => PI / 2.,
};
let remaining = target_yaw - camera_position.yaw;
if remaining.abs() <= f32::EPSILON {
return;
}
let delta = camera_position.get_speed() * remaining.signum() * time.delta_seconds();
camera_position.yaw += if remaining.abs() > delta.abs() {
delta
} else {
remaining
};
if let Some(mut transform) = camera_query.iter_mut().next() {
transform.rotation = camera_position.get_rotation();
transform.translation = camera_position.get_translation();
}
}

pub struct CameraPlugin;
impl Plugin for CameraPlugin {
fn build(&self, app: &mut AppBuilder) {
app.init_resource::<CameraPosition>()
.add_startup_system(setup.system())
.add_system(reposition_camera.system());
}
}
33 changes: 8 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use bevy::prelude::*;
use bevy_mod_picking::*;
use bevy_mod_picking::{PickingCamera, PickingPlugin};
use board::BoardPlugin;
use camera::CameraPlugin;
use pieces::PiecesPlugin;
use ui::UIPlugin;

mod pieces;
use pieces::*;
mod board;
use board::*;
mod camera;
mod pieces;
mod ui;
use ui::*;

fn main() {
App::build()
Expand All @@ -24,26 +26,7 @@ fn main() {
.add_plugin(PickingPlugin)
.add_plugin(BoardPlugin)
.add_plugin(PiecesPlugin)
.add_plugin(CameraPlugin)
.add_plugin(UIPlugin)
.add_startup_system(setup.system())
.run();
}

fn setup(mut commands: Commands) {
commands
// Camera
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_matrix(Mat4::from_rotation_translation(
Quat::from_xyzw(-0.3, -0.5, -0.3, 0.5).normalize(),
Vec3::new(-3.0, 12.0, 3.5),
)),
..Default::default()
})
.insert_bundle(PickingCameraBundle::default())
// Light
.commands()
.spawn_bundle(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
});
}
6 changes: 3 additions & 3 deletions src/pieces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn color_of_square(pos: IVec2, pieces: &[Piece]) -> Option<PieceColor> {
.map(|piece| piece.color)
}

const MOVE_TIME: f32 = 0.1;
const MOVE_SPEED_CONST: f32 = 0.1;

fn move_pieces(time: Res<Time>, mut query: Query<(&mut Transform, &Piece)>) {
for (mut transform, piece) in query.iter_mut() {
Expand All @@ -192,8 +192,8 @@ fn move_pieces(time: Res<Time>, mut query: Query<(&mut Transform, &Piece)>) {
Vec3::new(piece.pos.x as f32, 0., piece.pos.y as f32) - transform.translation;

// Only move if the piece isn't already there (distance is big)
if direction.length() > 0.03 {
transform.translation += direction * time.delta_seconds() / MOVE_TIME;
if direction.length() > 0.01 {
transform.translation += direction * time.delta_seconds() / MOVE_SPEED_CONST;
}
}
}
Expand Down
31 changes: 9 additions & 22 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,17 @@ fn init_next_move_text(
}

/// Update text with the correct turn
fn update_status(
turn: Res<PlayerTurn>,
mut text_query: Query<(&mut Text, &StatusText)>,
pieces: Query<&Piece>,
) {
if !turn.is_changed() {
fn update_status(game_status: Res<GameStatus>, mut text_query: Query<(&mut Text, &StatusText)>) {
if !game_status.is_changed() {
return;
}
let pieces: Vec<_> = pieces.iter().copied().collect();
let text_value = match is_check_mate_on(&pieces, turn.0) {
true => format!(
"{} Wins!",
match turn.0 {
PieceColor::White => "Black",
PieceColor::Black => "White",
}
),
false => format!(
"Next move: {}",
match turn.0 {
PieceColor::White => "White",
PieceColor::Black => "Black",
}
),
let color_text = match game_status.color {
PieceColor::White => "White",
PieceColor::Black => "Black",
};
let text_value = match game_status.status_type {
StatusType::Win => format!("{} Wins!", color_text),
false => format!("Next move: {}", color_text),
};
if let Some((mut text, _tag)) = text_query.iter_mut().next() {
text.sections[0].value = text_value;
Expand Down

0 comments on commit 64592ff

Please sign in to comment.