Basic unit diversity
This commit is contained in:
parent
2f1b6e9152
commit
4c8929ac68
14 changed files with 152 additions and 45 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1740,6 +1740,7 @@ name = "card-game"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,4 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { version = "0.18.0", features = ["wav","debug"] }
|
bevy = { version = "0.18.0", features = ["wav","debug"] }
|
||||||
|
rand = "0.9.2"
|
||||||
|
|
|
||||||
BIN
assets/sprites/cards/units/doh.png
Normal file
BIN
assets/sprites/cards/units/doh.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
BIN
assets/sprites/cards/units/zlosma.png
Normal file
BIN
assets/sprites/cards/units/zlosma.png
Normal file
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.
BIN
assets/sprites/units/doh.png
Normal file
BIN
assets/sprites/units/doh.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
|
|
@ -2,11 +2,14 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
animation::transform::AnimatedTransform,
|
animation::transform::AnimatedTransform,
|
||||||
card::{Card, CardInHand, Hand},
|
card::{Card, CardInHand, CardType, Hand},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(EntityEvent)]
|
#[derive(EntityEvent)]
|
||||||
pub struct CardDropped(pub Entity);
|
pub struct CardDropped {
|
||||||
|
pub entity: Entity,
|
||||||
|
pub card_type: CardType,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct DraggableCard;
|
pub struct DraggableCard;
|
||||||
|
|
@ -58,12 +61,11 @@ pub fn card_drag(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn card_drag_end(
|
pub fn card_drag_end(event: On<Pointer<DragEnd>>, mut commands: Commands, cards: Query<&Card>) {
|
||||||
event: On<Pointer<DragEnd>>,
|
if let Ok(card) = cards.get(event.entity) {
|
||||||
mut commands: Commands,
|
commands.trigger(CardDropped {
|
||||||
cards: Query<(), With<Card>>,
|
entity: event.entity,
|
||||||
) {
|
card_type: card.card_type,
|
||||||
if let Ok(_) = cards.get(event.entity) {
|
});
|
||||||
commands.trigger(CardDropped(event.entity));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
75
src/card/draw.rs
Normal file
75
src/card/draw.rs
Normal 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);
|
||||||
|
}
|
||||||
|
|
@ -3,15 +3,25 @@ use std::f32;
|
||||||
use crate::animation::transform::AnimatedTransform;
|
use crate::animation::transform::AnimatedTransform;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use drag::*;
|
use drag::*;
|
||||||
|
pub use draw::*;
|
||||||
use playing::*;
|
use playing::*;
|
||||||
|
|
||||||
pub mod drag;
|
pub mod drag;
|
||||||
|
pub mod draw;
|
||||||
pub mod playing;
|
pub mod playing;
|
||||||
|
|
||||||
pub struct CardPlugin;
|
pub struct CardPlugin;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[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)]
|
#[derive(Component)]
|
||||||
pub struct CardInHand;
|
pub struct CardInHand;
|
||||||
|
|
@ -20,10 +30,23 @@ pub struct CardInHand;
|
||||||
pub struct Hand(Vec<Entity>);
|
pub struct Hand(Vec<Entity>);
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
pub struct CardImage(Handle<Image>);
|
pub struct CardImages {
|
||||||
|
fosma: Handle<Image>,
|
||||||
|
doh: Handle<Image>,
|
||||||
|
zlosma: Handle<Image>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Event)]
|
impl CardImages {
|
||||||
pub struct AddCard;
|
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)]
|
#[derive(EntityEvent)]
|
||||||
pub struct InsertCard(Entity);
|
pub struct InsertCard(Entity);
|
||||||
|
|
@ -55,9 +78,11 @@ fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
InheritedVisibility::VISIBLE,
|
InheritedVisibility::VISIBLE,
|
||||||
));
|
));
|
||||||
|
|
||||||
commands.insert_resource(CardImage(
|
commands.insert_resource(CardImages {
|
||||||
asset_server.load::<Image>("sprites/cards/units/fosma.png"),
|
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(
|
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>) {
|
fn card_clear(_: On<ClearCards>, mut commands: Commands, mut hand: Single<&mut Hand>) {
|
||||||
for card in &hand.0 {
|
for card in &hand.0 {
|
||||||
let entity = card.clone();
|
let entity = card.clone();
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,25 @@ pub fn card_playing_system(
|
||||||
mouse_position: Res<MousePosition>,
|
mouse_position: Res<MousePosition>,
|
||||||
textures: Res<UnitTextures>,
|
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((
|
.spawn((
|
||||||
AnimatedTransform::identity(),
|
AnimatedTransform::identity(),
|
||||||
Transform::from_translation(mouse_position.0),
|
Transform::from_translation(mouse_position.0),
|
||||||
Sprite::from_image(textures.fosma_texture()),
|
bundle,
|
||||||
))
|
))
|
||||||
.id();
|
.id();
|
||||||
if grid.try_set(mouse_position.0.truncate(), fosma) {
|
if grid.try_set(mouse_position.0.truncate(), unit) {
|
||||||
commands.entity(event.0).despawn();
|
commands.entity(event.entity).despawn();
|
||||||
} else {
|
} else {
|
||||||
commands.entity(fosma).despawn();
|
commands.entity(unit).despawn();
|
||||||
commands.entity(event.0).insert(CardInHand);
|
commands.entity(event.entity).insert(CardInHand);
|
||||||
commands.trigger(InsertCard(event.0));
|
commands.trigger(InsertCard(event.entity));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ fn setup_debug_menu(mut commands: Commands) {
|
||||||
|
|
||||||
commands.spawn(button(panel, "AddCard")).observe(
|
commands.spawn(button(panel, "AddCard")).observe(
|
||||||
|_: On<buttons::ButtonPressedEvent>, mut commands: Commands| {
|
|_: On<buttons::ButtonPressedEvent>, mut commands: Commands| {
|
||||||
commands.trigger(card::AddCard)
|
commands.trigger(card::DrawCard::random_card())
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
commands.spawn(button(panel, "ClearCards")).observe(
|
commands.spawn(button(panel, "ClearCards")).observe(
|
||||||
|
|
|
||||||
|
|
@ -10,16 +10,33 @@ impl Plugin for UnitPlugin {
|
||||||
fn unit_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
fn unit_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
commands.insert_resource(UnitTextures {
|
commands.insert_resource(UnitTextures {
|
||||||
fosma: asset_server.load("sprites/units/fosma.png"),
|
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)]
|
#[derive(Resource)]
|
||||||
pub struct UnitTextures {
|
pub struct UnitTextures {
|
||||||
fosma: Handle<Image>,
|
fosma: Handle<Image>,
|
||||||
|
doh: Handle<Image>,
|
||||||
|
zlosma: Handle<Image>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UnitTextures {
|
impl UnitTextures {
|
||||||
pub fn fosma_texture(&self) -> Handle<Image> {
|
pub fn fosma_texture(&self) -> Handle<Image> {
|
||||||
self.fosma.clone()
|
self.fosma.clone()
|
||||||
}
|
}
|
||||||
|
pub fn doh_texture(&self) -> Handle<Image> {
|
||||||
|
self.doh.clone()
|
||||||
|
}
|
||||||
|
pub fn zlosma_texture(&self) -> Handle<Image> {
|
||||||
|
self.zlosma.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue