use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use crate::prelude::CollisionLayer;
#[derive(Bundle, Default)]
pub struct RayCastBundle {
pub ray: RayCast,
pub collision_layer: CollisionLayer,
}
#[derive(Debug, Clone, Reflect, Serialize, Deserialize, Component)]
pub struct RayCast {
pub offset: Vec2,
pub cast: Vec2,
pub collide_with_static: bool,
#[serde(skip_serializing, skip_deserializing)]
pub collision: Option<RayCastCollision>,
}
impl Default for RayCast {
fn default() -> Self {
Self::new(Vec2::new(0.0,-100.0))
}
}
#[derive(Debug, Clone, Copy, Reflect, Serialize, Deserialize)]
pub struct RayCastCollision {
pub collision_point: Vec2,
pub entity: Entity,
pub is_static: bool,
}
impl RayCast {
pub fn new(cast: Vec2) -> Self {
RayCast {
offset: Vec2::ZERO,
cast,
collide_with_static: true,
collision: None,
}
}
pub fn with_offset(
mut self,
offset: Vec2,
) -> Self {
self.offset = offset;
self
}
pub fn with_static(
mut self,
collide_with_static: bool,
) -> Self {
self.collide_with_static = collide_with_static;
self
}
pub fn get_collision(&self) -> Option<RayCastCollision> {
self.collision
}
}