Skip to content

Commit

Permalink
combine collider tags into 1 enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellzbellz123 committed Jan 2, 2024
1 parent 91c1ebb commit 6fa952e
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 114 deletions.
47 changes: 7 additions & 40 deletions crates/aspenlib/src/bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use bevy_asepritesheet::animator::AnimatedSpriteBundle;
use crate::{
game::actors::{
ai::components::AICombatConfig,
attributes_stats::{CharacterStatBundle, ProjectileStats, EquipmentStats},
components::{ActorMoveState, CharacterColliderTag, ProjectileColliderTag, ProjectileTag}, combat::components::{WeaponHolder, AttackDamage, WeaponForm, WeaponColliderTag},
attributes_stats::{CharacterStatBundle, EquipmentStats, ProjectileStats},
combat::components::{AttackDamage, WeaponForm, WeaponHolder},
components::{ActorColliderType, ActorMoveState},
},
loading::{custom_assets::actor_definitions::AiSetupConfig, registry::RegistryIdentifier},
prelude::{
Expand Down Expand Up @@ -81,32 +82,15 @@ pub struct ProjectileBundle {
pub sprite_bundle: SpriteBundle,
/// projectile collisions and movement
pub rigidbody_bundle: RigidBodyBundle,
/// tag
pub tag: ProjectileTag,
}

/// collider bundle for actors
#[derive(Bundle)]
pub struct CharacterColliderBundle {
pub struct ObjectColliderBundle {
/// name of collider
pub name: Name,
/// location of collider
pub transform_bundle: TransformBundle,
/// collider shape
pub collider: Collider,
/// collision groups
pub collision_groups: CollisionGroups,
/// tag
pub tag: CharacterColliderTag,
}

/// weapon collider
#[derive(Bundle)]
pub struct WeaponColliderBundle {
/// collider name
pub name: Name,
/// collider tag
pub tag: WeaponColliderTag,
/// type of collider
pub tag: ActorColliderType,
/// collider shape
pub collider: Collider,
/// collision groups
Expand All @@ -115,23 +99,6 @@ pub struct WeaponColliderBundle {
pub transform_bundle: TransformBundle,
}

/// bundle for projectile colliders
#[derive(Bundle)]
pub struct ProjectileColliderBundle {
/// collider name
pub name: Name,
/// collider lifetime
pub ttl: TimeToLive,
/// collider transform
pub transform_bundle: TransformBundle,
/// collider shape
pub collider: Collider,
/// collision groups
pub collision_groups: CollisionGroups,
/// tag
pub tag: ProjectileColliderTag,
}

/// All Components needed for `stupid_ai` functionality
#[derive(Bundle)]
pub struct StupidAiBundle {
Expand Down Expand Up @@ -198,4 +165,4 @@ impl std::fmt::Debug for WeaponBundle {
.field("rigidbody_bundle", &self.rigidbody_bundle.rigidbody)
.finish()
}
}
}
10 changes: 4 additions & 6 deletions crates/aspenlib/src/game/actors/combat/attacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

use crate::{
bundles::{ProjectileBundle, ProjectileColliderBundle, RigidBodyBundle},
bundles::{ProjectileBundle, ObjectColliderBundle, RigidBodyBundle},
consts::{AspenCollisionLayer, ACTOR_PHYSICS_Z_INDEX, ACTOR_Z_INDEX, BULLET_SPEED_MODIFIER},
game::actors::{
attributes_stats::{Damage, ProjectileStats},
combat::components::{AttackDamage, WeaponForm},
components::{ProjectileColliderTag, ProjectileTag, TimeToLive},
components::{ActorColliderType, TimeToLive},
player::actions::ShootEvent,
},
prelude::game::AspenInitHandles,
Expand Down Expand Up @@ -78,13 +78,12 @@ pub fn create_bullet(
angular_damping: 0.1,
},
},
tag: ProjectileTag,
},
Sensor,
))
.with_children(|child| {
child.spawn((
ProjectileColliderBundle {
ObjectColliderBundle {
name: Name::new("PlayerProjectileCollider"),
transform_bundle: TransformBundle {
local: (Transform {
Expand All @@ -100,8 +99,7 @@ pub fn create_bullet(
| AspenCollisionLayer::ACTOR
| AspenCollisionLayer::PROJECTILE,
),
ttl: TimeToLive(Timer::from_seconds(2.0, TimerMode::Repeating)),
tag: ProjectileColliderTag,
tag: ActorColliderType::Projectile,
},
ActiveEvents::COLLISION_EVENTS,
));
Expand Down
16 changes: 2 additions & 14 deletions crates/aspenlib/src/game/actors/combat/components.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
use bevy::{
math::Vec2,
prelude::{Bundle, Component, Deref, DerefMut, Entity, Name, ReflectComponent},
prelude::{Component, Deref, DerefMut, Entity, ReflectComponent},
reflect::Reflect,
time::Timer,
transform::TransformBundle,
utils::hashbrown::HashMap,
};

use bevy_asepritesheet::animator::AnimatedSpriteBundle;
use bevy_rapier2d::prelude::{Collider, CollisionGroups};

use crate::{
bundles::RigidBodyBundle,
game::actors::attributes_stats::{Damage, EquipmentStats,}, loading::registry::RegistryIdentifier,
};

/// collider tag for weapons
#[derive(Debug, Component)]
pub struct WeaponColliderTag;
use crate::game::actors::attributes_stats::Damage;

/// entity that holds this weapon, and the slot that it is in
#[derive(Debug, Clone, Copy, Component, Default, Reflect, Deref, DerefMut)]
Expand Down
13 changes: 6 additions & 7 deletions crates/aspenlib/src/game/actors/combat/hit_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy_rapier2d::{prelude::*, rapier::prelude::CollisionEventFlags};

use crate::game::actors::{
attributes_stats::{DamageQueue, ProjectileStats},
components::{CharacterColliderTag, ProjectileColliderTag},
components::ActorColliderType,
};

/// detects projectile hits on player, adds hits too Player
Expand All @@ -12,24 +12,23 @@ pub fn projectile_hits(
mut cmds: Commands,
mut collision_events: EventReader<CollisionEvent>,
mut damage_queue_query: Query<&mut DamageQueue>,
character_collider_q: Query<(Entity, &Parent), (With<Collider>, With<CharacterColliderTag>)>,
projectile_collider_q: Query<(Entity, &Parent), (With<Collider>, With<ProjectileColliderTag>)>,
parented_collider_query: Query<(Entity, &Parent), (With<Collider>, With<ActorColliderType>)>,
projectile_info: Query<&ProjectileStats>,
) {
for event in collision_events.read() {
if let CollisionEvent::Started(a, b, flags) = event {
if flags.contains(CollisionEventFlags::SENSOR) {
return;
}
let hit_actor = character_collider_q
let hit_actor = parented_collider_query
.get(*b)
.or_else(|_| character_collider_q.get(*a))
.or_else(|_| parented_collider_query.get(*a))
.map(|(_collider, parent)| parent.get())
.ok();

let hitting_projectile = projectile_collider_q
let hitting_projectile = parented_collider_query
.get(*a)
.or_else(|_| projectile_collider_q.get(*b))
.or_else(|_| parented_collider_query.get(*b))
.map(|(_a, parent)| parent.get())
.ok();

Expand Down
20 changes: 9 additions & 11 deletions crates/aspenlib/src/game/actors/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ use bevy::{
prelude::{Component, Deref, DerefMut, Reflect, Timer},
};

/// tag for character collider
#[derive(Component)]
pub struct CharacterColliderTag;

/// tag for combat projectiles
#[derive(Component)]
pub struct ProjectileTag;

/// tag for projectile collider
#[derive(Debug, Component)]
pub struct ProjectileColliderTag;
/// collider tag, type of collider
#[derive(Debug, Copy, Clone, PartialEq, Eq, Reflect, Component, Default)]
#[reflect(Component)]
pub enum ActorColliderType {
#[default]
Character,
Object,
Projectile,
}

/// new type for `Timer` for use with bullet lifetimes
#[derive(Debug, Component, Default, Reflect, Deref, DerefMut)]
Expand Down
8 changes: 3 additions & 5 deletions crates/aspenlib/src/game/actors/enemies/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy_rapier2d::prelude::{
};

use crate::{
bundles::{ProjectileBundle, ProjectileColliderBundle, RigidBodyBundle},
bundles::{ProjectileBundle, ObjectColliderBundle, RigidBodyBundle},
consts::{AspenCollisionLayer, ACTOR_PHYSICS_Z_INDEX, ACTOR_Z_INDEX, BULLET_SPEED_MODIFIER},
game::actors::{
ai::components::AIShootConfig,
Expand Down Expand Up @@ -121,13 +121,12 @@ pub fn spawn_enemy_projectile(
angular_damping: 0.1,
},
},
tag: super::components::ProjectileTag,
},
Sensor,
))
.with_children(|child| {
child.spawn((
ProjectileColliderBundle {
ObjectColliderBundle {
name: Name::new("EnemyProjectileCollider"),
transform_bundle: TransformBundle {
local: (Transform {
Expand All @@ -145,8 +144,7 @@ pub fn spawn_enemy_projectile(
AspenCollisionLayer::PROJECTILE,
AspenCollisionLayer::EVERYTHING,
),
ttl: TimeToLive(Timer::from_seconds(2.0, TimerMode::Repeating)),
tag: super::components::ProjectileColliderTag,
tag: super::components::ActorColliderType::Projectile,
},
ActiveEvents::COLLISION_EVENTS,
));
Expand Down
19 changes: 11 additions & 8 deletions crates/aspenlib/src/game/actors/player/actions.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use bevy_rapier2d::geometry::{Collider, CollisionGroups};

use crate::{
game::actors::combat::components::{AttackDamage, WeaponForm},
bundles::ObjectColliderBundle,
game::actors::{combat::components::WeaponForm, components::ActorColliderType},
loading::registry::RegistryIdentifier,
prelude::{
engine::{
debug, info, warn, ActionState, BuildChildren, Commands, Entity, Event, EventWriter,
GlobalTransform, Name, Parent, Query, Transform, TransformBundle, Vec2, Vec3, With,
Without,
},
game::{
action_maps, ActorType, CurrentlyDrawnWeapon, NpcType, SpawnActorEvent,
WeaponColliderTag, WeaponHolder, WeaponSlots, WeaponSocket, TILE_SIZE,
action_maps, ActorType, CurrentlyDrawnWeapon, NpcType, SpawnActorEvent, WeaponHolder,
WeaponSlots, WeaponSocket, TILE_SIZE,
},
}, loading::registry::RegistryIdentifier, bundles::WeaponColliderBundle,
},
};

/// spawns skeleton near player if `Gameplay::DebugF1` is pressed
Expand Down Expand Up @@ -120,7 +122,7 @@ pub fn equip_closest_weapon(
&mut Transform,
&ActionState<action_maps::Gameplay>,
)>,
query_child_weapon_collider: Query<(Entity, &Parent), With<WeaponColliderTag>>,
query_child_weapon_collider: Query<(Entity, &Parent), With<ActorColliderType>>,
mut weapon_query: Query<
(Entity, &mut WeaponHolder, &mut Transform),
(Without<Parent>, Without<WeaponSocket>),
Expand Down Expand Up @@ -168,18 +170,19 @@ pub fn equip_closest_weapon(
// equipping weapon too player
if slots_full {
if drawn_weapon.is_some() {
warn!("slots full, unequipping drawn weapon");
// TODO: recreate weapon collider properly?
let weapon_ent = slot_value.unwrap();
warn!("slots full, replacing weapon");
weapon_pos.translation = Vec3 {
x: 50.0,
y: 0.0,
z: 0.0,
};
cmds.entity(weapon_ent).remove_parent();
cmds.entity(weapon_ent).with_children(|f| {
f.spawn(WeaponColliderBundle {
f.spawn(ObjectColliderBundle {
name: Name::new("DroppedWeaponCollider"),
tag: WeaponColliderTag,
tag: ActorColliderType::Object,
collider: Collider::default(),
collision_groups: CollisionGroups::default(),
transform_bundle: TransformBundle::default(),
Expand Down
14 changes: 8 additions & 6 deletions crates/aspenlib/src/game/actors/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ use bevy_mod_picking::{
};

use crate::{
bundles::CharacterColliderBundle,
bundles::ObjectColliderBundle,
consts::{actor_collider, AspenCollisionLayer, ACTOR_PHYSICS_Z_INDEX},
game::{actors::components::CharacterColliderTag, interface::StartMenu},
game::{
actors::{
combat::components::WeaponSlots,
player::movement::{camera_movement_system, update_player_velocity},
player::movement::{camera_movement_system, update_player_velocity}, components::ActorColliderType,
},
input::action_maps::PlayerBundle,
interface::StartMenu,
},
loading::{
custom_assets::actor_definitions::CharacterDefinition, registry::RegistryIdentifier,
},
loading::{custom_assets::actor_definitions::CharacterDefinition, registry::RegistryIdentifier},
AppState,
};

Expand Down Expand Up @@ -135,8 +137,8 @@ pub fn build_player_from_selected_hero(
},
))
.with_children(|child| {
child.spawn((CharacterColliderBundle {
tag: CharacterColliderTag,
child.spawn((ObjectColliderBundle {
tag: ActorColliderType::Character,
name: Name::new("PlayerCollider"),
transform_bundle: TransformBundle {
local: (Transform {
Expand Down
22 changes: 9 additions & 13 deletions crates/aspenlib/src/game/actors/spawners/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ use rand::{

use self::components::{EnemyContainerTag, EnemySpawner, SpawnActorEvent, SpawnerTimer};
use crate::{
bundles::{CharacterBundle, CharacterColliderBundle, WeaponColliderBundle},
bundles::{CharacterBundle, ObjectColliderBundle},
consts::{actor_collider, AspenCollisionLayer, ACTOR_PHYSICS_Z_INDEX, ACTOR_Z_INDEX},
game::actors::{ai::components::AiType, components::CharacterColliderTag},
game::actors::{
ai::components::{ActorType, NpcType},
components::ActorColliderType,
},
loading::{custom_assets::actor_definitions::CharacterDefinition, registry::ActorRegistry},
prelude::game::WeaponColliderTag,
AppState,
};

use super::ai::components::{ActorType, NpcType};

/// spawner components
pub mod components;
/// fn for enemy's
// mod spawn_functions_enemy;
/// fn for weapons
// mod spawn_functions_weapons;

/// spawner functionality
pub struct SpawnerPlugin;
Expand Down Expand Up @@ -194,8 +190,8 @@ fn spawn_creeps_on_event(
let collider_name =
format!("{}Collider", bundle_copy.name.clone().as_str());
let spawned_enemy = child
.spawn((CharacterColliderBundle {
tag: CharacterColliderTag,
.spawn((ObjectColliderBundle {
tag: ActorColliderType::Character,
name: Name::new(collider_name),
transform_bundle: TransformBundle {
local: (Transform {
Expand Down Expand Up @@ -276,8 +272,8 @@ fn spawn_weapon_bundle(
) {
commands.spawn(bundle_copy.clone()).with_children(|child| {
let collider_name = format!("{}Collider", bundle_copy.name.as_str());
child.spawn(WeaponColliderBundle {
tag: WeaponColliderTag,
child.spawn(ObjectColliderBundle {
tag: ActorColliderType::Object,
name: Name::new(collider_name),
collider: Collider::capsule(Vec2 { x: 0.0, y: -10.0 }, Vec2 { x: 0.0, y: 10.0 }, 2.0),
collision_groups: CollisionGroups::new(
Expand Down
Loading

0 comments on commit 6fa952e

Please sign in to comment.