use crate::{Mat, Vect};
use std::f32::consts::PI;
use crate::math::rgba::RGBA;
use crate::render::batch::{Target, VertexData};
use crate::render::particle::system::Particle;
pub struct SymmetricShape {
points: Vec<Vect>,
buffer: Vec<f32>,
indices: Vec<u32>,
}
impl Clone for SymmetricShape {
#[inline]
fn clone(&self) -> Self {
Self{
points: self.points.clone(),
buffer: self.buffer.clone(),
indices: self.indices.clone(),
}
}
}
impl SymmetricShape {
#[inline]
pub fn new(edges: usize, rotation: f32) -> SymmetricShape {
let mut points = Vec::with_capacity(edges);
for i in 0..edges{
points.push(Vect::unit(PI*2f32/edges as f32 * i as f32 + rotation));
}
let mut indices = Vec::with_capacity(edges * 3);
for i in 0..(edges as u32 - 1) {
indices.extend(&[0, i + 1, i + 2])
}
indices.extend(&[0, edges as u32, 1]);
SymmetricShape{
points,
buffer: Vec::with_capacity(edges * 6 + 6),
indices
}
}
#[inline]
pub fn draw<T: Target>(&mut self, target: &mut T, transform: &Mat, inner_color: &RGBA, outer_color: &RGBA) {
self.buffer.clear();
self.buffer.extend(&[transform.c.x, transform.c.y]);
self.buffer.extend(inner_color);
for p in self.points.iter() {
let prj = transform.prj(*p);
self.buffer.extend(&[prj.x, prj.y]);
self.buffer.extend(outer_color);
}
target.append(&self.buffer, &self.indices, 6, None, None, None);
}
}
impl Particle for SymmetricShape {
fn draw<T: Target>(&mut self, target: &mut T, position: Vect, rotation: f32, scale: f32, color: &RGBA) {
self.draw(target, &Mat::new(position,Vect::new(scale, scale), rotation), color, color);
}
}
pub struct Triangle {
points: [Vect; 3],
buff: [f32; 18],
pos: Vect,
}
impl Triangle {
pub const PATTERN: [u32; 3] = [0, 1, 2];
pub fn new(points: &[Vect; 3]) -> Self {
Self{ points: points.clone(), buff: [0f32; 18], pos: Vect::ZERO }
}
pub fn draw<T: Target>(&mut self, target: &mut T, transform: &Mat, color: &RGBA) {
for (mut i, pos) in self.points.iter().enumerate() {
self.pos = transform.prj(*pos);
i *= 6;
self.buff[i+0] = self.pos.x;
self.buff[i+1] = self.pos.y;
self.buff[i+2..i+6].copy_from_slice(color);
}
target.append(&self.buff, &Self::PATTERN, 6, None, None, None);
}
}
impl Particle for Triangle {
fn draw<T: Target>(&mut self, target: &mut T, position: Vect, rotation: f32, scale: f32, color: &RGBA) {
self.draw(target, &Mat::new(position, Vect::new(scale, scale), rotation), color);
}
}