diff --git a/Cargo.lock b/Cargo.lock index 0302996..441ff75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1740,6 +1740,7 @@ name = "card-game" version = "0.1.0" dependencies = [ "bevy", + "rand", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4c09dd2..f2a892a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] bevy = { version = "0.18.0", features = ["wav","debug"] } +rand = "0.9.2" diff --git a/assets/sprites/cards/units/doh.png b/assets/sprites/cards/units/doh.png new file mode 100644 index 0000000..2fc229f Binary files /dev/null and b/assets/sprites/cards/units/doh.png differ diff --git a/assets/sprites/cards/units/zlosma.png b/assets/sprites/cards/units/zlosma.png new file mode 100644 index 0000000..196ac64 Binary files /dev/null and b/assets/sprites/cards/units/zlosma.png differ diff --git a/assets/sprites/field.png~ b/assets/sprites/field.png~ deleted file mode 100644 index 87a4c09..0000000 Binary files a/assets/sprites/field.png~ and /dev/null differ diff --git a/assets/sprites/sources/cards.kra b/assets/sprites/sources/cards.kra index 3fd2568..5050d2e 100644 Binary files a/assets/sprites/sources/cards.kra and b/assets/sprites/sources/cards.kra differ diff --git a/assets/sprites/sources/units.kra b/assets/sprites/sources/units.kra index e03f101..3db7539 100644 Binary files a/assets/sprites/sources/units.kra and b/assets/sprites/sources/units.kra differ diff --git a/assets/sprites/units/doh.png b/assets/sprites/units/doh.png new file mode 100644 index 0000000..03d7164 Binary files /dev/null and b/assets/sprites/units/doh.png differ diff --git a/src/card/drag.rs b/src/card/drag.rs index fdc7af7..de37079 100644 --- a/src/card/drag.rs +++ b/src/card/drag.rs @@ -2,11 +2,14 @@ use bevy::prelude::*; use crate::{ animation::transform::AnimatedTransform, - card::{Card, CardInHand, Hand}, + card::{Card, CardInHand, CardType, Hand}, }; #[derive(EntityEvent)] -pub struct CardDropped(pub Entity); +pub struct CardDropped { + pub entity: Entity, + pub card_type: CardType, +} #[derive(Component)] pub struct DraggableCard; @@ -58,12 +61,11 @@ pub fn card_drag( } } -pub fn card_drag_end( - event: On>, - mut commands: Commands, - cards: Query<(), With>, -) { - if let Ok(_) = cards.get(event.entity) { - commands.trigger(CardDropped(event.entity)); +pub fn card_drag_end(event: On>, mut commands: Commands, cards: Query<&Card>) { + if let Ok(card) = cards.get(event.entity) { + commands.trigger(CardDropped { + entity: event.entity, + card_type: card.card_type, + }); } } diff --git a/src/card/draw.rs b/src/card/draw.rs new file mode 100644 index 0000000..d911aaa --- /dev/null +++ b/src/card/draw.rs @@ -0,0 +1,75 @@ +use bevy::prelude::*; +use rand::random_range; + +use crate::{ + animation::transform::AnimatedTransform, + card::{Card, CardImages, CardInHand, Hand, drag::DraggableCard}, + unit::UnitType, +}; + +#[derive(Event)] +pub struct DrawCard { + card_type: Option, +} + +impl DrawCard { + pub fn random_card() -> Self { + Self { card_type: None } + } + pub fn new(card_type: Option) -> Self { + Self { card_type } + } +} + +#[derive(Clone, Copy)] +pub enum CardType { + Unit(UnitType), +} + +pub(super) fn card_add( + event: On, + mut commands: Commands, + card_image: Res, + mut hand: Single<&mut Hand>, +) { + let sprite: Handle; + let card_type: CardType; + + card_type = match &event.card_type { + Some(event_card_type) => event_card_type.clone(), + None => match random_range(0..3) { + 0 => CardType::Unit(UnitType::Fosma), + 1 => CardType::Unit(UnitType::Zlosma), + 2 => CardType::Unit(UnitType::Doh), + _ => CardType::Unit(UnitType::Fosma), + }, + }; + + match &card_type { + CardType::Unit(unit_type) => match unit_type { + UnitType::Fosma => { + sprite = card_image.fosma_texture(); + } + UnitType::Zlosma => { + sprite = card_image.zlosma_texture(); + } + UnitType::Doh => { + sprite = card_image.doh_texture(); + } + }, + } + + let card = commands + .spawn(( + Sprite::from_image(sprite), + Transform::from_xyz(0., 0., 10.), + AnimatedTransform::from_xyz(0., 0., 10.), + Card::new(card_type.clone()), + DraggableCard, + CardInHand, + Pickable::default(), + )) + .id(); + + hand.0.push(card); +} diff --git a/src/card/mod.rs b/src/card/mod.rs index 254757b..23bae99 100644 --- a/src/card/mod.rs +++ b/src/card/mod.rs @@ -3,15 +3,25 @@ use std::f32; use crate::animation::transform::AnimatedTransform; use bevy::prelude::*; use drag::*; +pub use draw::*; use playing::*; pub mod drag; +pub mod draw; pub mod playing; pub struct CardPlugin; #[derive(Component)] -pub struct Card; +pub struct Card { + pub card_type: CardType, +} + +impl Card { + pub fn new(card_type: CardType) -> Self { + Self { card_type } + } +} #[derive(Component)] pub struct CardInHand; @@ -20,10 +30,23 @@ pub struct CardInHand; pub struct Hand(Vec); #[derive(Resource)] -pub struct CardImage(Handle); +pub struct CardImages { + fosma: Handle, + doh: Handle, + zlosma: Handle, +} -#[derive(Event)] -pub struct AddCard; +impl CardImages { + pub fn fosma_texture(&self) -> Handle { + self.fosma.clone() + } + pub fn doh_texture(&self) -> Handle { + self.doh.clone() + } + pub fn zlosma_texture(&self) -> Handle { + self.zlosma.clone() + } +} #[derive(EntityEvent)] pub struct InsertCard(Entity); @@ -55,9 +78,11 @@ fn hand_setup(mut commands: Commands, asset_server: Res) { InheritedVisibility::VISIBLE, )); - commands.insert_resource(CardImage( - asset_server.load::("sprites/cards/units/fosma.png"), - )); + commands.insert_resource(CardImages { + fosma: asset_server.load("sprites/cards/units/fosma.png"), + doh: asset_server.load("sprites/cards/units/doh.png"), + zlosma: asset_server.load("sprites/cards/units/zlosma.png"), + }); } fn card_arrange( @@ -77,27 +102,6 @@ fn card_arrange( } } -fn card_add( - _: On, - mut commands: Commands, - card_image: Res, - mut hand: Single<&mut Hand>, -) { - let card = commands - .spawn(( - Sprite::from_image(card_image.0.clone()), - Transform::from_xyz(0., 0., 10.), - AnimatedTransform::from_xyz(0., 0., 10.), - Card, - DraggableCard, - CardInHand, - Pickable::default(), - )) - .id(); - - hand.0.push(card); -} - fn card_clear(_: On, mut commands: Commands, mut hand: Single<&mut Hand>) { for card in &hand.0 { let entity = card.clone(); diff --git a/src/card/playing.rs b/src/card/playing.rs index 904e8e5..777de0e 100644 --- a/src/card/playing.rs +++ b/src/card/playing.rs @@ -17,18 +17,25 @@ pub fn card_playing_system( mouse_position: Res, textures: Res, ) { - let fosma = commands + let bundle = match event.card_type { + super::CardType::Unit(unit_type) => match unit_type { + crate::unit::UnitType::Fosma => Sprite::from_image(textures.fosma_texture()), + crate::unit::UnitType::Zlosma => Sprite::from_image(textures.zlosma_texture()), + crate::unit::UnitType::Doh => Sprite::from_image(textures.doh_texture()), + }, + }; + let unit = commands .spawn(( AnimatedTransform::identity(), Transform::from_translation(mouse_position.0), - Sprite::from_image(textures.fosma_texture()), + bundle, )) .id(); - if grid.try_set(mouse_position.0.truncate(), fosma) { - commands.entity(event.0).despawn(); + if grid.try_set(mouse_position.0.truncate(), unit) { + commands.entity(event.entity).despawn(); } else { - commands.entity(fosma).despawn(); - commands.entity(event.0).insert(CardInHand); - commands.trigger(InsertCard(event.0)); + commands.entity(unit).despawn(); + commands.entity(event.entity).insert(CardInHand); + commands.trigger(InsertCard(event.entity)); } } diff --git a/src/dev_tools/mod.rs b/src/dev_tools/mod.rs index 403ea24..dd55c03 100644 --- a/src/dev_tools/mod.rs +++ b/src/dev_tools/mod.rs @@ -58,7 +58,7 @@ fn setup_debug_menu(mut commands: Commands) { commands.spawn(button(panel, "AddCard")).observe( |_: On, mut commands: Commands| { - commands.trigger(card::AddCard) + commands.trigger(card::DrawCard::random_card()) }, ); commands.spawn(button(panel, "ClearCards")).observe( diff --git a/src/unit/mod.rs b/src/unit/mod.rs index eb3d1eb..e075b7c 100644 --- a/src/unit/mod.rs +++ b/src/unit/mod.rs @@ -10,16 +10,33 @@ impl Plugin for UnitPlugin { fn unit_setup(mut commands: Commands, asset_server: Res) { commands.insert_resource(UnitTextures { fosma: asset_server.load("sprites/units/fosma.png"), + doh: asset_server.load("sprites/units/doh.png"), + zlosma: asset_server.load("sprites/units/zlosma.png"), }); } +#[derive(PartialEq, Eq, Clone, Copy)] +pub enum UnitType { + Fosma, + Zlosma, + Doh, +} + #[derive(Resource)] pub struct UnitTextures { fosma: Handle, + doh: Handle, + zlosma: Handle, } impl UnitTextures { pub fn fosma_texture(&self) -> Handle { self.fosma.clone() } + pub fn doh_texture(&self) -> Handle { + self.doh.clone() + } + pub fn zlosma_texture(&self) -> Handle { + self.zlosma.clone() + } }