Basic unit diversity

This commit is contained in:
Rendo 2026-01-30 22:02:09 +05:00
commit 4c8929ac68
14 changed files with 152 additions and 45 deletions

1
Cargo.lock generated
View file

@ -1740,6 +1740,7 @@ name = "card-game"
version = "0.1.0"
dependencies = [
"bevy",
"rand",
]
[[package]]

View file

@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
bevy = { version = "0.18.0", features = ["wav","debug"] }
rand = "0.9.2"

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -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<Pointer<DragEnd>>,
mut commands: Commands,
cards: Query<(), With<Card>>,
) {
if let Ok(_) = cards.get(event.entity) {
commands.trigger(CardDropped(event.entity));
pub fn card_drag_end(event: On<Pointer<DragEnd>>, 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,
});
}
}

75
src/card/draw.rs Normal file
View file

@ -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<CardType>,
}
impl DrawCard {
pub fn random_card() -> Self {
Self { card_type: None }
}
pub fn new(card_type: Option<CardType>) -> Self {
Self { card_type }
}
}
#[derive(Clone, Copy)]
pub enum CardType {
Unit(UnitType),
}
pub(super) fn card_add(
event: On<DrawCard>,
mut commands: Commands,
card_image: Res<CardImages>,
mut hand: Single<&mut Hand>,
) {
let sprite: Handle<Image>;
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);
}

View file

@ -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<Entity>);
#[derive(Resource)]
pub struct CardImage(Handle<Image>);
pub struct CardImages {
fosma: Handle<Image>,
doh: Handle<Image>,
zlosma: Handle<Image>,
}
#[derive(Event)]
pub struct AddCard;
impl CardImages {
pub fn fosma_texture(&self) -> Handle<Image> {
self.fosma.clone()
}
pub fn doh_texture(&self) -> Handle<Image> {
self.doh.clone()
}
pub fn zlosma_texture(&self) -> Handle<Image> {
self.zlosma.clone()
}
}
#[derive(EntityEvent)]
pub struct InsertCard(Entity);
@ -55,9 +78,11 @@ fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
InheritedVisibility::VISIBLE,
));
commands.insert_resource(CardImage(
asset_server.load::<Image>("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<AddCard>,
mut commands: Commands,
card_image: Res<CardImage>,
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<ClearCards>, mut commands: Commands, mut hand: Single<&mut Hand>) {
for card in &hand.0 {
let entity = card.clone();

View file

@ -17,18 +17,25 @@ pub fn card_playing_system(
mouse_position: Res<MousePosition>,
textures: Res<UnitTextures>,
) {
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));
}
}

View file

@ -58,7 +58,7 @@ fn setup_debug_menu(mut commands: Commands) {
commands.spawn(button(panel, "AddCard")).observe(
|_: On<buttons::ButtonPressedEvent>, mut commands: Commands| {
commands.trigger(card::AddCard)
commands.trigger(card::DrawCard::random_card())
},
);
commands.spawn(button(panel, "ClearCards")).observe(

View file

@ -10,16 +10,33 @@ impl Plugin for UnitPlugin {
fn unit_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
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<Image>,
doh: Handle<Image>,
zlosma: Handle<Image>,
}
impl UnitTextures {
pub fn fosma_texture(&self) -> Handle<Image> {
self.fosma.clone()
}
pub fn doh_texture(&self) -> Handle<Image> {
self.doh.clone()
}
pub fn zlosma_texture(&self) -> Handle<Image> {
self.zlosma.clone()
}
}