DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

7c803038d19246e23c41eafe8d5e27ac512f6111 — Jonni Liljamo 1 year, 7 months ago 04c4fdc
feat(client): add background to cards
1 files changed, 63 insertions(+), 3 deletions(-)

M client/src/plugins/game/card/mod.rs
M client/src/plugins/game/card/mod.rs => client/src/plugins/game/card/mod.rs +63 -3
@@ 14,7 14,10 @@ use crate::game_status::Card;
pub struct CardPlugin;

impl Plugin for CardPlugin {
    fn build(&self, app: &mut App) {}
    fn build(&self, app: &mut App) {
        app.init_resource::<VisualCardData>()
            .add_system(on_spawn_card);
    }
}

// NOTE: kind of like using enum variants


@@ 33,7 36,7 @@ pub struct VisualCard {
}

impl VisualCard {
    pub const WIDTH: f32 = 0.35;
    pub const ASPECT_RATIO: f32 = 5. / 8.;
}

impl Default for VisualCard {


@@ 55,6 58,8 @@ pub struct VisualCardBundle {
    pub visual_card: VisualCard,
    pub transform: Transform,
    pub global_transform: GlobalTransform,
    pub visibility: Visibility,
    pub computed_visibility: ComputedVisibility,
    pub collider: Collider,
    pub rigid_body: RigidBody,
}


@@ 75,8 80,63 @@ impl Default for VisualCardBundle {
                ..Default::default()
            },
            global_transform: GlobalTransform::default(),
            collider: Collider::cuboid(VisualCard::WIDTH, VisualCard::WIDTH * 1.6, 0.01),
            visibility: Visibility::default(),
            computed_visibility: ComputedVisibility::default(),
            collider: Collider::cuboid(VisualCard::ASPECT_RATIO / 2., VisualCard::ASPECT_RATIO, 0.01),
            rigid_body: RigidBody::Dynamic,
        }
    }
}

#[derive(Resource)]
pub struct VisualCardData {
    mesh: Handle<Mesh>,
    card_background: Handle<StandardMaterial>,
}

impl FromWorld for VisualCardData {
    fn from_world(world: &mut World) -> Self {
        let world = world.cell();
        let mut meshes = world.resource_mut::<Assets<Mesh>>();
        let mut materials = world.resource_mut::<Assets<StandardMaterial>>();
        let asset_server = world.resource::<AssetServer>();

        let card_background_material = StandardMaterial {
            unlit: true,
            alpha_mode: AlphaMode::Blend,
            base_color_texture: Some(asset_server.load("card_background.png")),
            ..Default::default()
        };

        Self {
            mesh: meshes.add(
                      shape::Quad {
                          size: Vec2::new(VisualCard::ASPECT_RATIO / 2., VisualCard::ASPECT_RATIO),
                          ..Default::default()
                      }
                      .into(),
                      ),
            card_background: materials.add(card_background_material),
        }
    }
}

fn on_spawn_card(
    mut commands: Commands,
    card_data: Res<VisualCardData>,
    new_cards: Query<(Entity, &VisualCard), Added<VisualCard>>,
) {
    for (entity, card) in &new_cards {
        commands.entity(entity).with_children(|parent| {
            parent.spawn(PbrBundle {
                material: card_data.card_background.clone(),
                mesh: card_data.mesh.clone(),
                transform: Transform {
                    scale: Vec3::new(2., 2., 1.),
                    ..Default::default()
                },
                ..Default::default()
            });
        });
    }
}