use super::SceneData;
use crate::render::{DynamicModel, Mesh, Quad, Renderer, TrailDrawer, TrailVertex};
use common::comp::{object, Body, Pos, Vel};
use common_base::span;
use specs::{Entity as EcsEntity, Join, WorldExt};
use std::collections::HashMap;
use vek::*;
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
struct MeshKey {
entity: EcsEntity,
is_main_weapon: bool,
}
#[derive(Default)]
pub struct TrailMgr {
entity_meshes: HashMap<MeshKey, (Mesh<TrailVertex>, usize)>,
pos_cache: HashMap<EcsEntity, Pos>,
offset: usize,
dynamic_model: Option<DynamicModel<TrailVertex>>,
model_len: u32,
}
const TRAIL_DYNAMIC_MODEL_SIZE: usize = 15;
const TRAIL_SHRINKAGE: f32 = 0.8;
impl TrailMgr {
pub fn maintain(&mut self, renderer: &mut Renderer, scene_data: &SceneData) {
span!(_guard, "maintain", "TrailMgr::maintain");
if scene_data.weapon_trails_enabled {
let ecs = scene_data.state.ecs();
for (entity, body, vel, pos) in (
&ecs.entities(),
&ecs.read_storage::<Body>(),
&ecs.read_storage::<Vel>(),
&ecs.read_storage::<Pos>(),
)
.join()
{
const MIN_SPEED: f32 = 15.0;
if vel.0.magnitude_squared() > MIN_SPEED.powi(2)
&& matches!(
body,
Body::Object(
object::Body::Arrow
| object::Body::MultiArrow
| object::Body::ArrowSnake
| object::Body::ArrowTurret
| object::Body::ArrowClay
| object::Body::BoltBesieger,
)
)
{
let last_pos = *self.pos_cache.entry(entity).or_insert(*pos);
let offset = self.offset;
let quad_mesh = self.entity_mesh_or_insert(entity, true);
const THICKNESS: f32 = 0.05;
let p1 = pos.0;
let p2 = p1 + Vec3::unit_z() * THICKNESS;
let p4 = last_pos.0;
let p3 = p4 + Vec3::unit_z() * THICKNESS;
let vertex = |p: Vec3<f32>| TrailVertex {
pos: p.into_array(),
};
let quad = Quad::new(vertex(p1), vertex(p2), vertex(p3), vertex(p4));
quad_mesh.replace_quad(offset * 4, quad);
self.pos_cache.insert(entity, *pos);
}
}
self.offset = (self.offset + 1) % TRAIL_DYNAMIC_MODEL_SIZE;
self.entity_meshes.values_mut().for_each(|(mesh, _)| {
let vertices = mesh.vertices_mut_vec();
let last_offset =
(self.offset + TRAIL_DYNAMIC_MODEL_SIZE - 1) % TRAIL_DYNAMIC_MODEL_SIZE;
let next_offset = (self.offset + 1) % TRAIL_DYNAMIC_MODEL_SIZE;
for i in 0..TRAIL_DYNAMIC_MODEL_SIZE {
let [b, c, a, d] = [0, 1, 2, 3].map(|offset| i * 4 + offset);
vertices[a] = if i == next_offset {
vertices[b]
} else {
vertices[a] * TRAIL_SHRINKAGE + vertices[b] * (1.0 - TRAIL_SHRINKAGE)
};
if i != last_offset {
vertices[d] =
vertices[d] * TRAIL_SHRINKAGE + vertices[c] * (1.0 - TRAIL_SHRINKAGE);
}
}
let zero = TrailVertex::zero();
mesh.replace_quad(self.offset * 4, Quad::new(zero, zero, zero, zero));
});
self.entity_meshes
.retain(|_, (_mesh, last_updated)| *last_updated != self.offset);
let mut big_mesh = Mesh::new();
self.entity_meshes
.values()
.filter(|(mesh, _)| mesh.iter().any(|vert| *vert != TrailVertex::zero()))
.for_each(|(mesh, _)| big_mesh.push_mesh(mesh));
if big_mesh.is_empty() {
let zero = TrailVertex::zero();
big_mesh.push_quad(Quad::new(zero, zero, zero, zero));
}
if self.dynamic_model.as_ref().map_or(0, |model| model.len()) < big_mesh.len() {
self.dynamic_model = Some(
renderer
.create_dynamic_model(big_mesh.len() + TRAIL_DYNAMIC_MODEL_SIZE * 4 * 10),
);
}
if let Some(dynamic_model) = &self.dynamic_model {
renderer.update_model(dynamic_model, &big_mesh, 0);
}
self.model_len = big_mesh.len() as u32;
} else {
self.entity_meshes.clear();
self.dynamic_model = None;
}
}
pub fn render<'a>(&'a self, drawer: &mut TrailDrawer<'_, 'a>, scene_data: &SceneData) {
span!(_guard, "render", "TrailMgr::render");
if scene_data.weapon_trails_enabled {
if let Some(dynamic_model) = &self.dynamic_model {
drawer.draw(dynamic_model.submodel(0..self.model_len))
}
}
}
pub fn entity_mesh_or_insert(
&mut self,
entity: EcsEntity,
is_main_weapon: bool,
) -> &mut Mesh<TrailVertex> {
let key = MeshKey {
entity,
is_main_weapon,
};
&mut self
.entity_meshes
.entry(key)
.and_modify(|(_mesh, offset)| *offset = self.offset)
.or_insert((Self::default_trail_mesh(), self.offset))
.0
}
fn default_trail_mesh() -> Mesh<TrailVertex> {
let mut mesh = Mesh::new();
let zero = TrailVertex::zero();
for _ in 0..TRAIL_DYNAMIC_MODEL_SIZE {
mesh.push_quad(Quad::new(zero, zero, zero, zero));
}
mesh
}
pub fn offset(&self) -> usize { self.offset }
}