Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add EnemyDestroyedEvent and EnemyDestroyedSystem #68

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/entities/consumable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn spawn_consumable(
entities: &Entities,
sprite_resource: &ReadExpect<SpriteResource>,
consumable: ConsumableEntityData,
spawn_position: Vector3<f32>,
spawn_position: &Vector3<f32>,
lazy_update: &ReadExpect<LazyUpdate>,
) {
let sprite_render = SpriteRender {
Expand All @@ -19,7 +19,7 @@ pub fn spawn_consumable(
let name = Named::new("consumable");

let mut local_transform = Transform::default();
local_transform.set_translation(spawn_position);
local_transform.set_translation(*spawn_position);

lazy_update
.create_entity(entities)
Expand Down
4 changes: 2 additions & 2 deletions src/entities/explosion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn spawn_explosion(
entities: &Entities,
sprite_resource: &ReadExpect<SpriteResource>,
sprite_number: usize,
spawn_position: Vector3<f32>,
spawn_position: &Vector3<f32>,
lazy_update: &ReadExpect<LazyUpdate>,
) -> Entity {
let frame_time: f32 = 0.1;
Expand All @@ -41,7 +41,7 @@ pub fn spawn_explosion(
let timed = TimeLimitComponent { duration };

let mut local_transform = Transform::default();
local_transform.set_translation(spawn_position);
local_transform.set_translation(*spawn_position);

lazy_update
.create_entity(entities)
Expand Down
60 changes: 60 additions & 0 deletions src/events/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use amethyst::ecs::prelude::Entity;

#[derive(Debug)]
pub struct CollisionEvent {
pub entity_a: Entity,
pub type_a: String,
pub to_velocity_x_a: f32, //velocity of the entity acting on a
pub to_velocity_y_a: f32,
pub entity_b: Entity,
pub type_b: String,
pub to_velocity_x_b: f32, //velocity of the entity acting on b
pub to_velocity_y_b: f32,
}

impl CollisionEvent {
pub fn new(
entity_a: Entity,
type_a: String,
to_velocity_x_a: f32,
to_velocity_y_a: f32,
entity_b: Entity,
type_b: String,
to_velocity_x_b: f32,
to_velocity_y_b: f32,
) -> CollisionEvent {
CollisionEvent {
entity_a,
type_a,
to_velocity_x_a,
to_velocity_y_a,
entity_b,
type_b,
to_velocity_x_b,
to_velocity_y_b,
}
}
}

#[derive(Debug)]
pub struct HitboxCollisionEvent {
pub entity_a: Entity,
pub entity_b: Entity,
}

impl HitboxCollisionEvent {
pub fn new(entity_a: Entity, entity_b: Entity) -> HitboxCollisionEvent {
HitboxCollisionEvent { entity_a, entity_b }
}
}

#[derive(Debug)]
pub struct EnemyDestroyedEvent {
pub enemy: Entity,
}

impl EnemyDestroyedEvent {
pub fn new(enemy: Entity) -> EnemyDestroyedEvent {
EnemyDestroyedEvent { enemy }
}
}
3 changes: 3 additions & 0 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod events;

pub use self::events::{CollisionEvent, EnemyDestroyedEvent, HitboxCollisionEvent};
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ use amethyst::{
utils::application_root_dir,
};

mod audio;
pub mod audio;
pub mod components;
pub mod constants;
pub mod entities;
pub mod events;
pub mod resources;
mod space_shooter;
pub mod systems;
Expand Down
53 changes: 5 additions & 48 deletions src/space_shooter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,6 @@ use amethyst::{
};
use std::f32::consts::FRAC_PI_3;

#[derive(Debug)]
pub struct CollisionEvent {
pub entity_a: Entity,
pub type_a: String,
pub to_velocity_x_a: f32, //velocity of the entity acting on a
pub to_velocity_y_a: f32,
pub entity_b: Entity,
pub type_b: String,
pub to_velocity_x_b: f32, //velocity of the entity acting on b
pub to_velocity_y_b: f32,
}

impl CollisionEvent {
pub fn new(
entity_a: Entity,
type_a: String,
to_velocity_x_a: f32,
to_velocity_y_a: f32,
entity_b: Entity,
type_b: String,
to_velocity_x_b: f32,
to_velocity_y_b: f32,
) -> CollisionEvent {
CollisionEvent {
entity_a,
type_a,
to_velocity_x_a,
to_velocity_y_a,
entity_b,
type_b,
to_velocity_x_b,
to_velocity_y_b,
}
}
}

#[derive(Debug)]
pub struct HitboxCollisionEvent {
pub entity_a: Entity,
pub entity_b: Entity,
}

impl HitboxCollisionEvent {
pub fn new(entity_a: Entity, entity_b: Entity) -> HitboxCollisionEvent {
HitboxCollisionEvent { entity_a, entity_b }
}
}

pub struct SpaceShooter {
dispatcher: Dispatcher<'static, 'static>,
}
Expand Down Expand Up @@ -142,6 +94,11 @@ impl Default for SpaceShooter {
)
.with(systems::AutoBlasterSystem, "autoblaster_system", &[])
.with(systems::ManualBlasterSystem, "manualblaster_system", &[])
.with(
systems::EnemyDestroyedSystem::default(),
"enemy_destroyed_system",
&["enemy_system"],
)
.build(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/systems/collision_detection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
components::{Enemy, Hitbox2DComponent, Motion2DComponent, Spaceship},
space_shooter::CollisionEvent,
events::CollisionEvent,
};
use amethyst::{
core::transform::Transform,
Expand Down
2 changes: 1 addition & 1 deletion src/systems/consumable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
audio::{play_sfx, Sounds},
components::{Consumable, Defense, Hitbox2DComponent, Spaceship},
constants::ARENA_MIN_Y,
space_shooter::HitboxCollisionEvent,
events::HitboxCollisionEvent,
};
use amethyst::{
assets::AssetStorage,
Expand Down
67 changes: 9 additions & 58 deletions src/systems/enemy.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
use crate::constants::ARENA_HEIGHT;
use crate::{
audio::{play_sfx, Sounds},
components::{
choose_random_name, Defense, Enemy, EnemyType, Hitbox2DComponent, Motion2DComponent,
Rigidbody,
},
constants::{ARENA_MIN_Y, EXPLOSION_Z},
entities::{spawn_consumable, spawn_explosion},
resources::{ConsumableEntityData, SpriteResource},
components::{Defense, Enemy, EnemyType, Hitbox2DComponent, Motion2DComponent, Rigidbody},
constants::ARENA_MIN_Y,
events::EnemyDestroyedEvent,
};
use amethyst::{
assets::AssetStorage,
audio::{output::Output, Source},
core::{math::Vector3, timing::Time, transform::Transform},
ecs::prelude::{
Entities, Join, LazyUpdate, Read, ReadExpect, ReadStorage, System, WriteStorage,
},
core::{timing::Time, transform::Transform},
ecs::prelude::{Entities, Join, Read, ReadStorage, System, Write, WriteStorage},
shrev::EventChannel,
};
use std::collections::HashMap;

pub struct EnemySystem;

Expand All @@ -30,12 +21,7 @@ impl<'s> System<'s> for EnemySystem {
WriteStorage<'s, Motion2DComponent>,
ReadStorage<'s, Hitbox2DComponent>,
Read<'s, Time>,
ReadExpect<'s, SpriteResource>,
ReadExpect<'s, LazyUpdate>,
Read<'s, AssetStorage<Source>>,
ReadExpect<'s, Sounds>,
Option<Read<'s, Output>>,
ReadExpect<'s, HashMap<String, ConsumableEntityData>>,
Write<'s, EventChannel<EnemyDestroyedEvent>>,
);

fn run(
Expand All @@ -48,12 +34,7 @@ impl<'s> System<'s> for EnemySystem {
mut motions,
hitboxes,
time,
sprite_resource,
lazy_update,
storage,
sounds,
audio_output,
consumable_pool,
mut enemy_destroyed_event_channel,
): Self::SystemData,
) {
for (enemy_entity, enemy_component, enemy_transform, enemy_motion, enemy_hitbox) in (
Expand Down Expand Up @@ -83,37 +64,7 @@ impl<'s> System<'s> for EnemySystem {
.delete(enemy_entity)
.expect("unable to delete entity");
} else if enemy_component.health < 0.0 {
//enemy us deleted, explosion is spawned and item dropped
let death_position = Vector3::new(
enemy_transform.translation()[0],
enemy_transform.translation()[1],
EXPLOSION_Z,
);

entities
.delete(enemy_entity)
.expect("unable to delete entity");

spawn_explosion(
&entities,
&sprite_resource,
enemy_component.explosion_sprite_idx,
death_position,
&lazy_update,
);

play_sfx(&sounds.explosion_sfx, &storage, audio_output.as_deref());

let name = choose_random_name(&enemy_component.collectables_probs);
if !name.is_empty() {
spawn_consumable(
&entities,
&sprite_resource,
consumable_pool[name].clone(),
death_position,
&lazy_update,
);
}
enemy_destroyed_event_channel.single_write(EnemyDestroyedEvent::new(enemy_entity));
}

//behavior for enemies based on its enemy_type attribute
Expand Down
2 changes: 1 addition & 1 deletion src/systems/enemy_collision_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
audio::{play_sfx, Sounds},
components::{Enemy, Motion2DComponent, Spaceship},
space_shooter::CollisionEvent,
events::CollisionEvent,
};
use amethyst::{
assets::AssetStorage,
Expand Down
92 changes: 92 additions & 0 deletions src/systems/enemy_destroyed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use crate::{
audio::{play_sfx, Sounds},
components::{choose_random_name, Enemy},
entities::{spawn_consumable, spawn_explosion},
events::EnemyDestroyedEvent,
resources::{ConsumableEntityData, SpriteResource},
};
use amethyst::{
assets::AssetStorage,
audio::{output::Output, Source},
core::transform::Transform,
ecs::prelude::{Entities, LazyUpdate, ReadExpect, ReadStorage, System},
ecs::*,
ecs::{Read, World},
shrev::{EventChannel, ReaderId},
};
use std::collections::HashMap;

#[derive(Default)]
pub struct EnemyDestroyedSystem {
event_reader: Option<ReaderId<EnemyDestroyedEvent>>,
}

impl<'s> System<'s> for EnemyDestroyedSystem {
type SystemData = (
Read<'s, EventChannel<EnemyDestroyedEvent>>,
Entities<'s>,
ReadStorage<'s, Transform>,
ReadStorage<'s, Enemy>,
ReadExpect<'s, HashMap<String, ConsumableEntityData>>,
ReadExpect<'s, SpriteResource>,
ReadExpect<'s, LazyUpdate>,
Read<'s, AssetStorage<Source>>,
ReadExpect<'s, Sounds>,
Option<Read<'s, Output>>,
);

fn setup(&mut self, world: &mut World) {
Self::SystemData::setup(world);
self.event_reader = Some(
world
.fetch_mut::<EventChannel<EnemyDestroyedEvent>>()
.register_reader(),
);
}

fn run(
&mut self,
(
enemy_destroyed_event_channel,
entities,
transforms,
enemies,
consumable_pool,
sprite_resource,
lazy_update,
storage,
sounds,
audio_output,
): Self::SystemData,
) {
for event in enemy_destroyed_event_channel.read(self.event_reader.as_mut().unwrap()) {
let enemy_transform = transforms.get(event.enemy).unwrap();
let enemy_component = enemies.get(event.enemy).unwrap();

play_sfx(&sounds.explosion_sfx, &storage, audio_output.as_deref());

spawn_explosion(
&entities,
&sprite_resource,
enemy_component.explosion_sprite_idx,
enemy_transform.translation(),
&lazy_update,
);

let name = choose_random_name(&enemy_component.collectables_probs);
if !name.is_empty() {
spawn_consumable(
&entities,
&sprite_resource,
consumable_pool[name].clone(),
enemy_transform.translation(),
&lazy_update,
);
}

entities
.delete(event.enemy)
.expect("unable to delete entity");
}
}
}