134 lines
3.8 KiB
Rust
134 lines
3.8 KiB
Rust
|
|
//bevy system signatures often violate these rules
|
||
|
|
#![allow(clippy::type_complexity)]
|
||
|
|
#![allow(clippy::too_many_arguments)]
|
||
|
|
|
||
|
|
use std::{f32, time::Duration};
|
||
|
|
|
||
|
|
use avian3d::prelude::{Collider, LinearVelocity, RigidBody};
|
||
|
|
use bevy::prelude::*;
|
||
|
|
use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
||
|
|
use combat::{
|
||
|
|
Defense, Health,
|
||
|
|
projectiles::{Fire, Projectile, ProjectileLauncher, get_launch_vector},
|
||
|
|
};
|
||
|
|
use combat_damagers::ContactDamage;
|
||
|
|
use resources::Cost;
|
||
|
|
|
||
|
|
pub struct BuildingBarragerPlugin;
|
||
|
|
|
||
|
|
impl Plugin for BuildingBarragerPlugin {
|
||
|
|
fn build(&self, app: &mut App) {
|
||
|
|
app.add_systems(Startup, register).add_observer(populate);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Component)]
|
||
|
|
pub struct Barrager;
|
||
|
|
|
||
|
|
#[derive(Resource)]
|
||
|
|
struct BuildingResources {
|
||
|
|
mesh: Handle<Mesh>,
|
||
|
|
material: Handle<StandardMaterial>,
|
||
|
|
projectile_mesh: Handle<Mesh>,
|
||
|
|
projectile_material: Handle<StandardMaterial>,
|
||
|
|
projectile_collider: Collider,
|
||
|
|
info_entity: Entity,
|
||
|
|
}
|
||
|
|
|
||
|
|
const SIZE: UVec3 = UVec3::splat(2);
|
||
|
|
const PROJ_SIZE: Vec3 = Vec3::splat(0.25);
|
||
|
|
|
||
|
|
fn register(
|
||
|
|
mut commands: Commands,
|
||
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
||
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||
|
|
) {
|
||
|
|
let mesh = meshes.add(Cuboid::from_length(2.0));
|
||
|
|
let base_material = StandardMaterial {
|
||
|
|
base_color: Color::srgb_u8(0x3d, 0x95, 0xa5),
|
||
|
|
metallic: 0.2,
|
||
|
|
perceptual_roughness: 0.6,
|
||
|
|
..default()
|
||
|
|
};
|
||
|
|
let material = materials.add(base_material.clone());
|
||
|
|
let preview_material = materials.add(StandardMaterial {
|
||
|
|
alpha_mode: AlphaMode::Blend,
|
||
|
|
base_color: base_material.base_color.with_alpha(PREVIEW_ALPHA),
|
||
|
|
..base_material
|
||
|
|
});
|
||
|
|
|
||
|
|
let info = commands.spawn_empty().id();
|
||
|
|
let preview = commands
|
||
|
|
.spawn((
|
||
|
|
BuildingPreview { info },
|
||
|
|
Visibility::Hidden,
|
||
|
|
Mesh3d(mesh.clone()),
|
||
|
|
MeshMaterial3d(preview_material),
|
||
|
|
))
|
||
|
|
.id();
|
||
|
|
commands.entity(info).insert((
|
||
|
|
BuildingInfo {
|
||
|
|
preview,
|
||
|
|
size: SIZE,
|
||
|
|
unlocked: true,
|
||
|
|
},
|
||
|
|
Cost { souls: 0, bones: 0 },
|
||
|
|
Name::new("Barrager"),
|
||
|
|
));
|
||
|
|
commands.insert_resource(BuildingResources {
|
||
|
|
mesh: mesh,
|
||
|
|
material: material.clone(),
|
||
|
|
projectile_material: material,
|
||
|
|
projectile_mesh: meshes.add(Cuboid::from_size(PROJ_SIZE)),
|
||
|
|
projectile_collider: Collider::cuboid(PROJ_SIZE.x, PROJ_SIZE.y, PROJ_SIZE.z),
|
||
|
|
info_entity: info,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
fn populate(
|
||
|
|
trigger: Trigger<PopulatePlacedBuilding>,
|
||
|
|
mut commands: Commands,
|
||
|
|
res: Res<BuildingResources>,
|
||
|
|
) {
|
||
|
|
if trigger.event().info != res.info_entity {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
commands
|
||
|
|
.entity(trigger.target())
|
||
|
|
.insert((
|
||
|
|
Barrager,
|
||
|
|
Health::new(20.0),
|
||
|
|
Defense::new(7.0),
|
||
|
|
ContactDamage::new(2.0),
|
||
|
|
ProjectileLauncher::new(
|
||
|
|
Duration::from_secs(1),
|
||
|
|
50.0,
|
||
|
|
5.0,
|
||
|
|
Vec3::Y * (SIZE.y as f32 + 0.5),
|
||
|
|
Duration::from_secs(10),
|
||
|
|
),
|
||
|
|
Mesh3d(res.mesh.clone()),
|
||
|
|
MeshMaterial3d(res.material.clone()),
|
||
|
|
))
|
||
|
|
.observe(on_fire);
|
||
|
|
}
|
||
|
|
|
||
|
|
fn on_fire(trigger: Trigger<Fire>, mut commands: Commands, res: Res<BuildingResources>) {
|
||
|
|
commands.spawn((
|
||
|
|
RigidBody::Dynamic,
|
||
|
|
trigger.event().team,
|
||
|
|
Health::new(1.0),
|
||
|
|
ContactDamage::projectile(5.0, f32::INFINITY),
|
||
|
|
Transform::from_translation(trigger.event().spawn_pos),
|
||
|
|
Projectile::from(&trigger.event().launcher),
|
||
|
|
LinearVelocity(get_launch_vector(
|
||
|
|
trigger.event().spawn_pos,
|
||
|
|
trigger.event().target_pos,
|
||
|
|
5.0,
|
||
|
|
)),
|
||
|
|
res.projectile_collider.clone(),
|
||
|
|
MeshMaterial3d(res.projectile_material.clone()),
|
||
|
|
Mesh3d(res.projectile_mesh.clone()),
|
||
|
|
));
|
||
|
|
}
|