|
|
|
|
@ -1,9 +1,10 @@
|
|
|
|
|
use crate::{
|
|
|
|
|
game::{Board, BoardIndex, Piece},
|
|
|
|
|
game::{Board, BoardIndex, Piece, SelectedTile},
|
|
|
|
|
prelude::*,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SCALE: f32 = 80.0;
|
|
|
|
|
const SCALE: f32 = 4.0;
|
|
|
|
|
const TILE_SIZE: f32 = 16.0;
|
|
|
|
|
|
|
|
|
|
pub struct Display2dPlugin;
|
|
|
|
|
|
|
|
|
|
@ -21,6 +22,8 @@ impl Plugin for Display2dPlugin {
|
|
|
|
|
.run_if(resource_exists::<SpriteSheet>())
|
|
|
|
|
.run_if(resource_changed::<Board>()) // TODO: run_if(in_state(Display2d))
|
|
|
|
|
.run_if(any_with_component::<BoardIndex>()),
|
|
|
|
|
cursor_position.run_if(in_state(GameState::Display2d)),
|
|
|
|
|
selected.run_if(resource_changed::<SelectedTile>()),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(OnEnter(GameState::Display2d), (activate, draw_board));
|
|
|
|
|
@ -60,7 +63,7 @@ fn load_spritesheet(
|
|
|
|
|
) {
|
|
|
|
|
let atlas = TextureAtlas::from_grid(
|
|
|
|
|
server.load("sprites.png"),
|
|
|
|
|
Vec2::new(16.0, 16.0),
|
|
|
|
|
Vec2::new(TILE_SIZE, TILE_SIZE),
|
|
|
|
|
5,
|
|
|
|
|
1,
|
|
|
|
|
None,
|
|
|
|
|
@ -77,7 +80,11 @@ fn initialize_board(sprite_sheet: Option<Res<SpriteSheet>>, mut commands: Comman
|
|
|
|
|
commands
|
|
|
|
|
.spawn((
|
|
|
|
|
SpatialBundle {
|
|
|
|
|
transform: Transform::from_xyz(-SCALE * 3.5, -SCALE * 1.5, 0.0),
|
|
|
|
|
transform: Transform::from_xyz(
|
|
|
|
|
-SCALE * TILE_SIZE * 7.0 / 2.0, // TODO: WHY???
|
|
|
|
|
-SCALE * TILE_SIZE * 3.0 / 2.0, // Why 7 and 3??
|
|
|
|
|
0.0,
|
|
|
|
|
),
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
Board2d,
|
|
|
|
|
@ -88,8 +95,9 @@ fn initialize_board(sprite_sheet: Option<Res<SpriteSheet>>, mut commands: Comman
|
|
|
|
|
let y = i / 8;
|
|
|
|
|
let s = (x % 2) ^ (y % 2);
|
|
|
|
|
|
|
|
|
|
let transform = Transform::from_scale(Vec3::splat(5.0))
|
|
|
|
|
.with_translation(Vec3::new(SCALE * x as f32, SCALE * y as f32, 0.0));
|
|
|
|
|
let transform = Transform::from_scale(Vec3::splat(SCALE)).with_translation(
|
|
|
|
|
Vec3::new(SCALE * 16.0 * x as f32, SCALE * 16.0 * y as f32, 0.0),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let sprite = TextureAtlasSprite::new(s);
|
|
|
|
|
|
|
|
|
|
@ -168,3 +176,55 @@ fn activate(
|
|
|
|
|
*visibility = Visibility::Visible;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cursor_position(
|
|
|
|
|
mut events: EventReader<CursorMoved>,
|
|
|
|
|
sprite_q: Query<(
|
|
|
|
|
&TextureAtlasSprite,
|
|
|
|
|
&Handle<TextureAtlas>,
|
|
|
|
|
&GlobalTransform,
|
|
|
|
|
&BoardIndex,
|
|
|
|
|
)>,
|
|
|
|
|
camera_q: Query<(&Camera, &GlobalTransform), With<Camera2d>>,
|
|
|
|
|
atlases: Res<Assets<TextureAtlas>>,
|
|
|
|
|
mut selected: ResMut<SelectedTile>,
|
|
|
|
|
) {
|
|
|
|
|
events.iter().for_each(|CursorMoved { position, .. }| {
|
|
|
|
|
if let Some(position) = camera_q
|
|
|
|
|
.iter()
|
|
|
|
|
.find_map(|(camera, transform)| camera.viewport_to_world_2d(transform, *position))
|
|
|
|
|
{
|
|
|
|
|
let idx = sprite_q.iter().find_map(
|
|
|
|
|
|(TextureAtlasSprite { index, anchor, .. }, handle, transform, board_index)| {
|
|
|
|
|
// Implementation credit goes to the sprite bevy_mod_picking backend
|
|
|
|
|
// TODO: Upstream changes
|
|
|
|
|
let pos = transform.translation();
|
|
|
|
|
|
|
|
|
|
let size = {
|
|
|
|
|
let sprite_size = atlases
|
|
|
|
|
.get(handle)
|
|
|
|
|
.map(|atlas| atlas.textures.get(*index).expect("Get this rect"))
|
|
|
|
|
.expect("get this rect")
|
|
|
|
|
.size();
|
|
|
|
|
let (transform_scale, _, _) = transform.to_scale_rotation_translation();
|
|
|
|
|
sprite_size * transform_scale.truncate()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let center = pos.truncate() - (anchor.as_vec() * size);
|
|
|
|
|
let rect = Rect::from_center_half_size(center, size / 2.0);
|
|
|
|
|
|
|
|
|
|
rect.contains(position).then_some(board_index)
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
if selected.idx != idx.cloned() {
|
|
|
|
|
selected.idx = idx.cloned();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn selected(selected: Res<SelectedTile>) {
|
|
|
|
|
if selected.idx.is_some() {
|
|
|
|
|
info!("Selected Tile: {:?}", selected.idx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|