DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

deck-builder/client/src/plugins/game/card/mod.rs -rw-r--r-- 6.6 KiB
106a55afJonni Liljamo chore: remove old-client-for-ref 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use bevy::prelude::*;
use bevy_mod_picking::prelude::*;
use bevy_picking_rapier::RapierPickTarget;
use bevy_rapier3d::prelude::*;
use bevy_text_mesh::prelude::*;

use crate::game_status::Card;

pub struct CardPlugin;

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

// NOTE: kind of like using enum variants
pub mod visual_card_kind {
    use bevy::prelude::*;

    #[derive(Component)]
    pub struct Normal;
    #[derive(Component)]
    pub struct Supply(pub usize);
    #[derive(Component)]
    pub struct Hand(pub usize);
}

#[derive(Component, Clone)]
pub struct ClickedCard;

#[derive(Component)]
pub struct VisualCard {
    pub card: Card,
}

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

impl Default for VisualCard {
    fn default() -> Self {
        Self {
            card: Card {
                name: "".to_string(),
                short_details: vec![],
                long_details: vec![],
                cost: 0,
                actions: vec![],
                to_be_trashed: false,
            },
        }
    }
}

#[derive(Bundle)]
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,
}

impl Default for VisualCardBundle {
    fn default() -> Self {
        Self {
            visual_card: VisualCard::default(),
            transform: Transform {
                translation: Vec3 {
                    x: 0.,
                    y: 0.,
                    z: 0.,
                },
                /// defaults to being rotated 90 degress on X, so flat on the
                /// table.
                rotation: Quat::from_euler(EulerRot::XYZ, -1.571, 0., 0.),
                ..Default::default()
            },
            global_transform: GlobalTransform::default(),
            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 {
    font_normal: Handle<TextMeshFont>,
    font_medium: Handle<TextMeshFont>,
    pub font_bold: Handle<TextMeshFont>,
    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 {
            font_normal: asset_server.load("fonts/FiraMono-Regular.ttf#mesh"),
            font_medium: asset_server.load("fonts/FiraMono-Medium.ttf#mesh"),
            font_bold: asset_server.load("fonts/FiraMono-Bold.ttf#mesh"),
            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).insert((
            PickableBundle::default(),
            RapierPickTarget::default(),
            OnPointer::<Click>::target_insert(ClickedCard),
        ));
        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()
            },));

            // card name
            parent.spawn(TextMeshBundle {
                text_mesh: TextMesh {
                    text: card.card.name.clone(),
                    style: TextMeshStyle {
                        font: card_data.font_medium.clone(),
                        font_size: SizeUnit::NonStandard(5.),
                        color: Color::rgb(0.2, 0.2, 0.2),
                        ..Default::default()
                    },
                    ..Default::default()
                },
                transform: Transform::from_xyz(-0.3, 0.2, 0.),
                ..Default::default()
            });

            // card cost
            parent.spawn(TextMeshBundle {
                text_mesh: TextMesh {
                    text: card.card.cost.to_string(),
                    style: TextMeshStyle {
                        font: card_data.font_bold.clone(),
                        font_size: SizeUnit::NonStandard(7.),
                        color: Color::rgb(1., 1., 1.),
                        ..Default::default()
                    },
                    ..Default::default()
                },
                transform: Transform::from_xyz(0.2, -0.575, 0.),
                ..Default::default()
            });

            parent.spawn(TextMeshBundle {
                text_mesh: TextMesh {
                    text: card.card.short_details.join("\n"),
                    style: TextMeshStyle {
                        font: card_data.font_normal.clone(),
                        font_size: SizeUnit::NonStandard(4.),
                        color: Color::rgb(1., 1., 1.),
                        ..Default::default()
                    },
                    size: TextMeshSize {
                        width: SizeUnit::NonStandard(42.),
                        ..Default::default()
                    },
                    ..Default::default()
                },
                transform: Transform::from_xyz(-0.25, 0.075, 0.),
                ..Default::default()
            });
        });
    }
}