Skip to content

Commit

Permalink
Setup cards to use config and added some initial cards
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirezmike committed Jul 23, 2024
1 parent f07121b commit 85b522d
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 55 deletions.
27 changes: 27 additions & 0 deletions assets/config/card.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

(
card_texture_path: "image/card/yellow_card.png",
cards: {
BasicStep: CardInfo(
name: "Basic Step",
description: "A-one and a-two and a-step.",
texture_path: "image/card/step.png",
),
DoubleBeat: CardInfo(
name: "Double Beat",
description: "Two beats, rapid fire!",
texture_path: "image/card/double_beat.png",
),
CounterPoint: CardInfo(
name: "Counter Point",
description: "Notes that move apart synchronously",
texture_path: "image/card/counter_point.png",
),
Splits: CardInfo(
name: "Splits",
description: "Split in two!",
texture_path: "image/card/splits.png",
),

}
)
4 changes: 2 additions & 2 deletions assets/config/deck_dock.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
minimum_card_distance: 16,
card_width: 20,
dock_height: 20.0,
max_card_scale: 5,
min_card_scale: 1,
max_card_scale: 1,
min_card_scale: 0.1,
)
Binary file added assets/image/card/counter_point.aseprite
Binary file not shown.
Binary file added assets/image/card/counter_point.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/card/double_beat.aseprite
Binary file not shown.
Binary file added assets/image/card/double_beat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/card/splits.aseprite
Binary file not shown.
Binary file added assets/image/card/splits.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/card/step.aseprite
Binary file not shown.
Binary file added assets/image/card/step.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image/card/yellow_card.aseprite
Binary file not shown.
Binary file added assets/image/card/yellow_card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 68 additions & 32 deletions src/game/card.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,89 @@
use bevy::asset::embedded_asset;
use bevy::ecs::system::SystemId;
use bevy::ecs::system::SystemState;
use bevy::prelude::*;
use bevy::utils::HashMap;
use pyri_state::prelude::*;
use serde::Deserialize;
use serde::Serialize;

use crate::screen::Screen;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
// TODO: setup correct asset loading for cards
embedded_asset!(app, "cards/sample_card.png");
app.configure::<ConfigHandle<CardConfig>>();
}

app.add_systems(
StateFlush,
Screen::Playing.on_edge(exit_playing, enter_playing),
);
#[derive(Asset, Reflect, Serialize, Deserialize)]
pub struct CardConfig {
pub card_texture_path: String,
#[serde(skip)]
pub card_texture: Handle<Image>,
cards: HashMap<CardKey, CardInfo>,
}

#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub enum CardKey {
Placeholder,
#[derive(Asset, Reflect, Serialize, Deserialize)]
struct CardInfo {
name: String,
description: String,
texture_path: String,
#[serde(skip)]
texture: Handle<Image>,
}

impl Config for CardConfig {
const PATH: &'static str = "config/card.ron";
const EXTENSION: &'static str = "card.ron";

fn on_load(&mut self, world: &mut World) {
let id = world.register_system(basic_attack);
let mut system_state = SystemState::<Res<AssetServer>>::new(world);
let asset_server = system_state.get_mut(world);

self.card_texture = asset_server.load(&self.card_texture_path);
for card in self.cards.values_mut() {
card.texture = asset_server.load(&card.texture_path);
}

let cards = self.cards.iter().map(|(key, value)| {
(
*key,
Card {
display_name: value.name.clone(),
description: value.description.clone(),
action: id,
texture: value.texture.clone(),
},
)
});

world.insert_resource(CardStorage(cards.collect()));
}

fn is_ready(&self, asset_server: &AssetServer) -> bool {
asset_server.is_loaded_with_dependencies(&self.card_texture)
&& self
.cards
.values()
.all(|x| asset_server.is_loaded_with_dependencies(&x.texture))
}
}

// TODO: Remove this `allow` later.
#[allow(dead_code)]
pub struct Card {
pub path: String,
pub display_name: String,
pub description: String,
pub texture: Handle<Image>,
pub action: SystemId<Entity>,
}

#[derive(Reflect, Eq, PartialEq, Hash, Copy, Clone, Serialize, Deserialize)]
pub enum CardKey {
BasicStep,
DoubleBeat,
CounterPoint,
Splits,
}

#[derive(Resource, Deref, DerefMut)]
pub struct CardStorage(pub HashMap<CardKey, Card>);

// TODO: Move this into a sub-folder of storing different attacks?
fn basic_attack(In(_): In<Entity>) {}

fn enter_playing(world: &mut World) {
let id = world.register_system(basic_attack);
world.insert_resource(CardStorage(
[(
CardKey::Placeholder,
Card {
path: "embedded://blobo_party/game/cards/sample_card.png".to_string(),
action: id,
},
)]
.into(),
));
}

fn exit_playing(mut _commands: Commands) {
// TODO: despawn cards?
}
14 changes: 6 additions & 8 deletions src/game/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use crate::screen::Screen;
pub(super) fn plugin(app: &mut App) {
app.add_systems(
Update,
Screen::Playing.on_update((execute_queued_cards
.in_set(UpdateSet::Update)
.run_if(on_step(4)),)),
Screen::Playing.on_update(
execute_queued_cards
.in_set(UpdateSet::Update)
.run_if(resource_added::<CardStorage>.and_then(on_step(4))),
),
);
}

Expand All @@ -37,11 +39,7 @@ impl Deck {

pub fn create_deck(mut entity: EntityWorldMut) {
entity.insert(Deck {
cards: vec![
CardKey::Placeholder,
CardKey::Placeholder,
CardKey::Placeholder,
],
cards: vec![],
next_card: 0,
});
}
Expand Down
40 changes: 27 additions & 13 deletions src/game/deck_dock.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use bevy::prelude::*;
use bevy::render::texture::ImageLoaderSettings;
use bevy::render::texture::ImageSampler;
use leafwing_input_manager::common_conditions::action_just_pressed;
use pyri_state::prelude::*;
use serde::Deserialize;
use serde::Serialize;

use crate::core::UpdateSet;
use crate::game::card::CardConfig;
use crate::game::card::CardKey;
use crate::game::card::CardStorage;
use crate::game::step::on_step;
Expand Down Expand Up @@ -97,7 +96,7 @@ struct CardPosition(usize);

fn add_card(mut added_card_event_writer: EventWriter<AddCardEvent>) {
added_card_event_writer.send(AddCardEvent {
card: CardKey::Placeholder,
card: CardKey::BasicStep,
index: 0,
});
}
Expand Down Expand Up @@ -126,22 +125,37 @@ fn handle_added_cards(
}

fn visual_card(mut entity: EntityWorldMut, card_key: CardKey) {
let asset_server = entity.world().resource::<AssetServer>();
let config_handle = entity.world().resource::<ConfigHandle<CardConfig>>();
let config = r!(entity
.world()
.resource::<Assets<CardConfig>>()
.get(&config_handle.0),);

entity
.insert((ImageBundle {
style: Style {
position_type: PositionType::Absolute,
..default()
},
transform: Transform::from_scale(Vec3::splat(0.1)),
image: UiImage::new(config.card_texture.clone()),
..default()
},))
.with_children(|children| {
children.spawn_with(move |e: EntityWorldMut| add_icon(e, card_key));
});
}

fn add_icon(mut entity: EntityWorldMut, card_key: CardKey) {
let card_storage = entity.world().resource::<CardStorage>();
let card_path = card_storage[&card_key].path.clone();
let card = &card_storage[&card_key];

entity.insert((ImageBundle {
style: Style {
position_type: PositionType::Absolute,
position_type: PositionType::Relative,
..default()
},
transform: Transform::from_scale(Vec3::splat(5.0)),
image: UiImage::new(asset_server.load_with_settings(
card_path,
|settings: &mut ImageLoaderSettings| {
settings.sampler = ImageSampler::linear();
},
)),
image: UiImage::new(card.texture.clone()),
..default()
},));
}
Expand Down

0 comments on commit 85b522d

Please sign in to comment.