New units, new rails
This commit is contained in:
parent
809d472c56
commit
3562560a6e
7 changed files with 136 additions and 24 deletions
|
|
@ -1,41 +1,29 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
animation::transform::AnimatedTransform,
|
||||
card::{CardInHand, InsertCard, drag::CardDropped},
|
||||
grid::Grid,
|
||||
mouse_position::MousePosition,
|
||||
turns::TurnUnit,
|
||||
unit::UnitTextures,
|
||||
unit::{doh, fosma, zlosma},
|
||||
};
|
||||
|
||||
pub fn card_playing_system(
|
||||
event: On<CardDropped>,
|
||||
mut commands: Commands,
|
||||
mut grid: Single<&mut Grid>,
|
||||
grid: Single<&mut Grid>,
|
||||
mouse_position: Res<MousePosition>,
|
||||
textures: Res<UnitTextures>,
|
||||
) {
|
||||
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.position),
|
||||
bundle,
|
||||
TurnUnit,
|
||||
))
|
||||
.id();
|
||||
if grid.try_set(mouse_position.position.truncate(), unit) {
|
||||
commands.entity(event.entity).despawn();
|
||||
} else {
|
||||
commands.entity(unit).despawn();
|
||||
if grid.is_occupied(mouse_position.position.truncate()) {
|
||||
commands.entity(event.entity).insert(CardInHand);
|
||||
commands.trigger(InsertCard(event.entity));
|
||||
return;
|
||||
}
|
||||
match event.card_type {
|
||||
super::CardType::Unit(unit_type) => match unit_type {
|
||||
crate::unit::UnitType::Fosma => commands.run_system_cached(fosma::spawn_fosma),
|
||||
crate::unit::UnitType::Zlosma => commands.run_system_cached(zlosma::spawn_zlosma),
|
||||
crate::unit::UnitType::Doh => commands.run_system_cached(doh::spawn_doh),
|
||||
},
|
||||
}
|
||||
commands.entity(event.entity).despawn();
|
||||
}
|
||||
|
|
|
|||
16
src/grid.rs
16
src/grid.rs
|
|
@ -67,6 +67,22 @@ impl Grid {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_entity(&self, position: Vec2) -> Option<Entity> {
|
||||
if self.is_in_grid(position) == false {
|
||||
return None;
|
||||
}
|
||||
|
||||
match self.elements[self.indexify_position(position)] {
|
||||
CellState::Filled(entity) => Some(entity.clone()),
|
||||
CellState::Part(entity) => Some(entity.clone()),
|
||||
CellState::Empty => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_entity_in_direction(&self, position: Vec2, direction: Vec2) -> Option<Entity> {
|
||||
self.get_entity(position + direction * self.cell_size())
|
||||
}
|
||||
|
||||
pub fn try_set(&mut self, position: Vec2, entity: Entity) -> bool {
|
||||
if self.is_occupied(position) {
|
||||
return false;
|
||||
|
|
|
|||
30
src/unit/doh.rs
Normal file
30
src/unit/doh.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use crate::{
|
||||
animation::transform::AnimatedTransform,
|
||||
grid::Grid,
|
||||
mouse_position::MousePosition,
|
||||
turns::{TurnBusy, TurnState, TurnUnit},
|
||||
unit::{UnitTextures, health::Health},
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Doh;
|
||||
|
||||
pub fn spawn_doh(
|
||||
mut commands: Commands,
|
||||
mut grid: Single<&mut Grid>,
|
||||
mouse_position: Res<MousePosition>,
|
||||
unit_textures: Res<UnitTextures>,
|
||||
) {
|
||||
let doh = commands
|
||||
.spawn((
|
||||
Doh,
|
||||
Sprite::from(unit_textures.doh_texture()),
|
||||
Health::new(10),
|
||||
Transform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.),
|
||||
AnimatedTransform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.),
|
||||
TurnUnit,
|
||||
))
|
||||
.id();
|
||||
grid.try_set(mouse_position.position.truncate(), doh);
|
||||
}
|
||||
30
src/unit/fosma.rs
Normal file
30
src/unit/fosma.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use crate::{
|
||||
animation::transform::AnimatedTransform,
|
||||
grid::Grid,
|
||||
mouse_position::MousePosition,
|
||||
turns::{TurnBusy, TurnState, TurnUnit},
|
||||
unit::{UnitTextures, health::Health},
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Fosma;
|
||||
|
||||
pub fn spawn_fosma(
|
||||
mut commands: Commands,
|
||||
mut grid: Single<&mut Grid>,
|
||||
mouse_position: Res<MousePosition>,
|
||||
unit_textures: Res<UnitTextures>,
|
||||
) {
|
||||
let fosma = commands
|
||||
.spawn((
|
||||
Fosma,
|
||||
Sprite::from(unit_textures.fosma_texture()),
|
||||
Health::new(10),
|
||||
Transform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.),
|
||||
AnimatedTransform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.),
|
||||
TurnUnit,
|
||||
))
|
||||
.id();
|
||||
grid.try_set(mouse_position.position.truncate(), fosma);
|
||||
}
|
||||
13
src/unit/health.rs
Normal file
13
src/unit/health.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Health {
|
||||
pub max_hp: u32,
|
||||
pub hp: u32,
|
||||
}
|
||||
|
||||
impl Health {
|
||||
pub fn new(max_hp: u32) -> Self {
|
||||
Self { max_hp, hp: max_hp }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,10 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
pub mod doh;
|
||||
pub mod fosma;
|
||||
pub mod health;
|
||||
pub mod zlosma;
|
||||
|
||||
pub struct UnitPlugin;
|
||||
impl Plugin for UnitPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
|
|
|
|||
30
src/unit/zlosma.rs
Normal file
30
src/unit/zlosma.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use crate::{
|
||||
animation::transform::AnimatedTransform,
|
||||
grid::Grid,
|
||||
mouse_position::MousePosition,
|
||||
turns::{TurnBusy, TurnState, TurnUnit},
|
||||
unit::{UnitTextures, health::Health},
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Zlosma;
|
||||
|
||||
pub fn spawn_zlosma(
|
||||
mut commands: Commands,
|
||||
mut grid: Single<&mut Grid>,
|
||||
mouse_position: Res<MousePosition>,
|
||||
unit_textures: Res<UnitTextures>,
|
||||
) {
|
||||
let zlosma = commands
|
||||
.spawn((
|
||||
Zlosma,
|
||||
Sprite::from(unit_textures.zlosma_texture()),
|
||||
Health::new(10),
|
||||
Transform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.),
|
||||
AnimatedTransform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.),
|
||||
TurnUnit,
|
||||
))
|
||||
.id();
|
||||
grid.try_set(mouse_position.position.truncate(), zlosma);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue