Skip to content

Commit

Permalink
Implement single unit attacking
Browse files Browse the repository at this point in the history
Fixes #24.
  • Loading branch information
Indy2222 committed Jul 5, 2022
1 parent 51ea3c8 commit 1e08d98
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 59 additions & 16 deletions crates/attacking/src/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,65 @@ pub(crate) struct AttackPlugin;

impl Plugin for AttackPlugin {
fn build(&self, app: &mut App) {
app.add_system_set_to_stage(
CoreStage::Update,
SystemSet::new()
.with_system(
update
.run_in_state(GameState::Playing)
.label(AttackingLabels::Update),
)
.with_system(
aim_and_fire
.run_in_state(GameState::Playing)
.label(AttackingLabels::Aim)
.after(AttackingLabels::Update)
.before(AttackingLabels::Fire),
),
);
app.add_event::<AttackEvent>()
.add_system_to_stage(
CoreStage::PreUpdate,
attack
.run_in_state(GameState::Playing)
.label(AttackingLabels::Attack),
)
.add_system_set_to_stage(
CoreStage::Update,
SystemSet::new()
.with_system(
update
.run_in_state(GameState::Playing)
.label(AttackingLabels::Update),
)
.with_system(
aim_and_fire
.run_in_state(GameState::Playing)
.label(AttackingLabels::Aim)
.after(AttackingLabels::Update)
.before(AttackingLabels::Fire),
),
);
}
}

pub struct AttackEvent {
attacker: Entity,
enemy: Entity,
}

impl AttackEvent {
pub fn new(attacker: Entity, enemy: Entity) -> Self {
Self { attacker, enemy }
}

fn attacker(&self) -> Entity {
self.attacker
}

fn enemy(&self) -> Entity {
self.enemy
}
}

fn attack(
mut commands: Commands,
mut events: EventReader<AttackEvent>,
cannons: Query<&LaserCannon>,
) {
for event in events.iter() {
if let Ok(cannon) = cannons.get(event.attacker()) {
// TODO constants
commands.entity(event.attacker()).insert(ChaseTarget::new(
event.enemy(),
0.3 * cannon.range(),
0.8 * cannon.range(),
));
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/attacking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub use attack::AttackEvent;
use attack::AttackPlugin;
use beam::BeamPlugin;
use bevy::{
Expand All @@ -20,7 +21,8 @@ impl PluginGroup for AttackingPluginGroup {
}

#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, SystemLabel)]
enum AttackingLabels {
pub enum AttackingLabels {
Attack,
Update,
Aim,
Fire,
Expand Down
1 change: 1 addition & 0 deletions crates/controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ de_terrain = { path = "../terrain", version = "0.1.0-dev" }
de_pathing = { path = "../pathing", version = "0.1.0-dev" }
de_spawner = { path = "../spawner", version = "0.1.0-dev" }
de_behaviour = { path = "../behaviour", version = "0.1.0-dev" }
de_attacking = { path = "../attacking", version = "0.1.0-dev" }

# Other
bevy = "0.7.0"
Expand Down
47 changes: 34 additions & 13 deletions crates/controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use bevy::{
input::{keyboard::KeyboardInput, mouse::MouseButtonInput, ElementState},
prelude::*,
};
use de_attacking::{AttackEvent, AttackingLabels};
use de_behaviour::ChaseTarget;
use de_core::{
gconfig::GameConfig,
objects::{BuildingType, MovableSolid, Playable},
player::Player,
projection::ToFlat,
};
use de_pathing::{PathQueryProps, PathTarget, UpdateEntityPath};
Expand All @@ -30,7 +33,8 @@ impl Plugin for CommandPlugin {
right_click_handler
.run_if(on_pressed(MouseButton::Right))
.label(Labels::InputUpdate)
.after(Labels::PreInputUpdate),
.after(Labels::PreInputUpdate)
.before(AttackingLabels::Attack),
)
.with_system(
left_click_handler
Expand Down Expand Up @@ -64,24 +68,41 @@ type SelectedQuery<'w, 's> =

fn right_click_handler(
mut commands: Commands,
config: Res<GameConfig>,
mut path_events: EventWriter<UpdateEntityPath>,
mut attack_events: EventWriter<AttackEvent>,
selected: SelectedQuery,
targets: Query<&Player>,
pointer: Res<Pointer>,
) {
let target = match pointer.terrain_point() {
Some(point) => point.to_flat(),
None => return,
};

for (entity, chase) in selected.iter() {
if chase.is_some() {
commands.entity(entity).remove::<ChaseTarget>();
match pointer.entity().filter(|&entity| {
targets
.get(entity)
.map(|&player| !config.is_local_player(player))
.unwrap_or(false)
}) {
Some(enemy) => {
for (attacker, _) in selected.iter() {
attack_events.send(AttackEvent::new(attacker, enemy));
}
}
None => {
let target = match pointer.terrain_point() {
Some(point) => point.to_flat(),
None => return,
};

for (entity, chase) in selected.iter() {
if chase.is_some() {
commands.entity(entity).remove::<ChaseTarget>();
}

path_events.send(UpdateEntityPath::new(
entity,
PathTarget::new(target, PathQueryProps::exact(), false),
));
path_events.send(UpdateEntityPath::new(
entity,
PathTarget::new(target, PathQueryProps::exact(), false),
));
}
}
}
}

Expand Down

0 comments on commit 1e08d98

Please sign in to comment.