Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
| 0319cb5a0e |
7 changed files with 24 additions and 149 deletions
|
|
@ -1,50 +0,0 @@
|
||||||
use bevy::prelude::{Resource, Asset};
|
|
||||||
use crate::asset_preloader::preloader::*;
|
|
||||||
use std::any::type_name;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct AssetPreloadChecker<T>
|
|
||||||
where T : Asset
|
|
||||||
{
|
|
||||||
pub data: Vec<T>,
|
|
||||||
typename: String
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AssetPreloadCheckerBuilder<T>
|
|
||||||
where T: Asset
|
|
||||||
{
|
|
||||||
data : AssetPreloadChecker<T>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Asset> AssetPreloadChecker<T>{
|
|
||||||
pub(crate) fn build(asset_preload: &mut AssetPreload) -> AssetPreloadCheckerBuilder<T>
|
|
||||||
{
|
|
||||||
let checker = AssetPreloadChecker::new();
|
|
||||||
|
|
||||||
asset_preload.0.insert(checker.typename.clone(), false);
|
|
||||||
|
|
||||||
AssetPreloadCheckerBuilder{
|
|
||||||
data: checker
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new() -> Self{
|
|
||||||
AssetPreloadChecker
|
|
||||||
{
|
|
||||||
data: Vec::new(),
|
|
||||||
typename: type_name::<T>().to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Asset> AssetPreloadCheckerBuilder<T>{
|
|
||||||
pub fn finish(self) -> AssetPreloadChecker<T> {
|
|
||||||
self.data
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn map_vec<'a, F>(&'a mut self, mut f: F)
|
|
||||||
where F: FnMut(&mut Vec<T>) + 'a{
|
|
||||||
f(&mut self.data.data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
use bevy::prelude::*;
|
|
||||||
use preloader::*;
|
|
||||||
use checker::*;
|
|
||||||
|
|
||||||
pub mod preloader;
|
|
||||||
pub mod checker;
|
|
||||||
|
|
||||||
pub(crate) fn check_preload_status(gl_preloader: Res<AssetPreload>) {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn check_loader_status<T>(loader: Res<AssetPreloadChecker<T>>)
|
|
||||||
where T: Asset{
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
use bevy::prelude::Resource;
|
|
||||||
|
|
||||||
pub enum AssetPreloadState{
|
|
||||||
Loading,
|
|
||||||
Ready
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub(crate) struct AssetPreload(pub(crate) HashMap<String,bool>);
|
|
||||||
|
|
||||||
impl AssetPreload{
|
|
||||||
pub(crate) fn new() -> Self{
|
|
||||||
AssetPreload(HashMap::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
68
src/lib.rs
68
src/lib.rs
|
|
@ -13,44 +13,16 @@ pub mod hand;
|
||||||
pub mod animal;
|
pub mod animal;
|
||||||
pub mod properties;
|
pub mod properties;
|
||||||
pub mod plugins;
|
pub mod plugins;
|
||||||
pub mod asset_preloader;
|
|
||||||
|
|
||||||
/*#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct JustUpdated;
|
pub struct JustUpdated;
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
pub struct Systems {
|
pub struct Systems {
|
||||||
pub update_hand_dimensions: SystemId
|
pub update_hand_dimensions: SystemId
|
||||||
}*/
|
|
||||||
|
|
||||||
pub fn setup(mut commands: Commands,asset_server: Res<AssetServer>) {
|
|
||||||
let hand_img: Handle<Image> = asset_server.load("sprites/hand_9s.png");
|
|
||||||
|
|
||||||
commands.spawn(Camera2d);
|
|
||||||
commands.spawn(
|
|
||||||
(
|
|
||||||
Hand{name: "Rendo".to_string()},
|
|
||||||
Sprite {
|
|
||||||
image: hand_img,
|
|
||||||
image_mode: SpriteImageMode::Sliced(TextureSlicer {
|
|
||||||
border: BorderRect::square(4.),
|
|
||||||
center_scale_mode: SliceScaleMode::Stretch,
|
|
||||||
sides_scale_mode: SliceScaleMode::Stretch,
|
|
||||||
..default()
|
|
||||||
}),
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
Transform::from_xyz(0., -300., 0.)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup_board()
|
pub fn setup(
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/*pub fn setup(
|
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
asset_server: Res<AssetServer>,
|
asset_server: Res<AssetServer>,
|
||||||
) {
|
) {
|
||||||
|
|
@ -78,12 +50,12 @@ pub fn setup_board()
|
||||||
pub fn test_setup_hand(
|
pub fn test_setup_hand(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
asset_server: Res<AssetServer>,
|
asset_server: Res<AssetServer>,
|
||||||
//systems: Res<Systems>,
|
systems: Res<Systems>,
|
||||||
query: Single<Entity, With<Hand>>
|
query: Single<Entity, With<Hand>>
|
||||||
) {
|
) {
|
||||||
let hand_id = query.into_inner();
|
let hand_id = query.into_inner();
|
||||||
for _ in 0..4 {
|
for _ in 0..4 {
|
||||||
create_card(&mut commands, &asset_server, Some(hand_id));
|
create_card(&mut commands, &asset_server, Some(hand_id), &systems);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,7 +63,7 @@ pub fn create_card(
|
||||||
commands: &mut Commands,
|
commands: &mut Commands,
|
||||||
asset_server: &Res<AssetServer>,
|
asset_server: &Res<AssetServer>,
|
||||||
hand_entity: Option<Entity>,
|
hand_entity: Option<Entity>,
|
||||||
//systems: &Res<Systems>
|
systems: &Res<Systems>
|
||||||
) {
|
) {
|
||||||
let mut card = commands.spawn((
|
let mut card = commands.spawn((
|
||||||
Card,
|
Card,
|
||||||
|
|
@ -102,31 +74,33 @@ pub fn create_card(
|
||||||
if let Some(hand) = hand_entity {
|
if let Some(hand) = hand_entity {
|
||||||
card.set_parent(hand);
|
card.set_parent(hand);
|
||||||
commands.entity(hand).insert(JustUpdated);
|
commands.entity(hand).insert(JustUpdated);
|
||||||
//commands.run_system(systems.update_hand_dimensions);
|
commands.run_system(systems.update_hand_dimensions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: replace this const with getting asset dimensions from Assets<Image>
|
||||||
|
const CARD_SPRITE_SIZE: Vec2 = Vec2::new(128., 192.);
|
||||||
|
|
||||||
pub fn update_hand_dimensions(
|
pub fn update_hand_dimensions(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
p_query: Single<(Entity, &mut Sprite, &Children), (Added<JustUpdated>, With<Hand>)>,
|
p_query: Single<(Entity, &mut Sprite, &Children), (Added<JustUpdated>, With<Hand>)>,
|
||||||
mut c_query: Query<(&mut Transform, &Sprite), (With<Card>, Without<Hand>)>,
|
mut c_query: Query<(&mut Transform, &Sprite), (With<Card>, Without<Hand>)>,
|
||||||
assets: Res<Assets<Image>>
|
|
||||||
) {
|
) {
|
||||||
let mut hand = p_query.into_inner();
|
let mut hand = p_query.into_inner();
|
||||||
let mut counter = 0.;
|
let size = hand.2.iter().count();
|
||||||
let mut hand_total_width = 0.;
|
let hor_margin = 8.;
|
||||||
let mut hand_height = 0.;
|
let ver_margin = 8.;
|
||||||
for &child_id in hand.2.iter() {
|
let sprite_size = CARD_SPRITE_SIZE;
|
||||||
|
let hand_total_width = (sprite_size.x + hor_margin) * (size as f32);
|
||||||
|
let hand_height = ver_margin * 2. + sprite_size.y;
|
||||||
|
let hand_offset = (hand_total_width - sprite_size.x - hor_margin) / 2.;
|
||||||
|
|
||||||
|
for (index, &child_id) in hand.2.iter().enumerate() {
|
||||||
let Ok(mut child) = c_query.get_mut(child_id) else { continue; };
|
let Ok(mut child) = c_query.get_mut(child_id) else { continue; };
|
||||||
let sprite_size = assets.get(child.1.image.id()).unwrap().size_f32();
|
let offset_x = (sprite_size.x + hor_margin) * (index as f32) - hand_offset;
|
||||||
let hor_margin = Vec2::new(4., 0.);
|
let offset_y = 0.;
|
||||||
let ver_margin = Vec2::new(0., 4.);
|
child.0.translation = Vec3::from((Vec2::new(offset_x, offset_y), 1.));
|
||||||
hand_total_width += sprite_size.x + hor_margin.x;
|
|
||||||
hand_height = sprite_size.y + ver_margin.y * 2.;
|
|
||||||
child.0.translation = Vec3::from((ver_margin + (sprite_size + hor_margin) * counter, 0.));
|
|
||||||
counter += 1.;
|
|
||||||
}
|
}
|
||||||
hand.1.custom_size = Some(Vec2::new(hand_total_width, hand_height));
|
hand.1.custom_size = Some(Vec2::new(hand_total_width, hand_height));
|
||||||
commands.entity(hand.0).remove::<JustUpdated>();
|
commands.entity(hand.0).remove::<JustUpdated>();
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
@ -2,7 +2,7 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
use evolution_rs::{
|
use evolution_rs::{
|
||||||
plugins::*,
|
plugins::*,
|
||||||
setup, //test_setup_hand, update_hand_dimensions
|
setup, test_setup_hand
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -16,9 +16,7 @@ use evolution_rs::{
|
||||||
// P.S. оставляю комменты в коде, потому что так надёжнее, чем сообщениями, которые можно потерять
|
// P.S. оставляю комменты в коде, потому что так надёжнее, чем сообщениями, которые можно потерять
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins((DefaultPlugins,BasePropertiesPlugin, asset_preloader::AssetPreloadPlugin))
|
.add_plugins((DefaultPlugins,BasePropertiesPlugin))
|
||||||
|
.add_systems(Startup, (setup, test_setup_hand).chain())
|
||||||
.add_systems(Startup, setup)
|
|
||||||
//.add_systems(PostStartup, (test_setup_hand, update_hand_dimensions).chain())
|
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ use bevy::prelude::{App,Plugin};
|
||||||
|
|
||||||
use crate::properties::{Property, TestProperty};
|
use crate::properties::{Property, TestProperty};
|
||||||
|
|
||||||
pub mod asset_preloader;
|
|
||||||
|
|
||||||
pub struct BasePropertiesPlugin;
|
pub struct BasePropertiesPlugin;
|
||||||
|
|
||||||
impl Plugin for BasePropertiesPlugin
|
impl Plugin for BasePropertiesPlugin
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
use bevy::prelude::*;
|
|
||||||
use crate::asset_preloader::{preloader::AssetPreload,checker::AssetPreloadChecker};
|
|
||||||
|
|
||||||
pub struct AssetPreloadPlugin;
|
|
||||||
|
|
||||||
impl Plugin for AssetPreloadPlugin {
|
|
||||||
fn build(&self, app: &mut App) {
|
|
||||||
//app.init_resource::<AssetPreloadChecker<Image>>();
|
|
||||||
let mut preload = AssetPreload::new();
|
|
||||||
let image: AssetPreloadChecker<Image> = AssetPreloadChecker::build(&mut preload).finish();
|
|
||||||
|
|
||||||
app.insert_resource(image);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue