use crate::{
entity::{EntityId, EntityManager, Transform, Sprite, Velocity, Component},
math::{Position, Size, Color},
};
pub struct EntityBuilder<'a> {
manager: &'a mut EntityManager,
entity_id: EntityId,
}
impl<'a> EntityBuilder<'a> {
pub fn new(manager: &'a mut EntityManager) -> Self {
let entity_id = manager.create_entity();
Self { manager, entity_id }
}
pub fn with_transform(self, transform: Transform) -> Self {
self.manager.add_transform(self.entity_id, transform);
self
}
pub fn with_sprite(self, sprite: Sprite) -> Self {
self.manager.add_sprite(self.entity_id, sprite);
self
}
pub fn with_velocity(self, velocity: Velocity) -> Self {
self.manager.add_velocity(self.entity_id, velocity);
self
}
pub fn at_position(self, x: f32, y: f32) -> Self {
let transform = Transform::new(Position::new(x, y));
self.with_transform(transform)
}
pub fn with_colored_square(self, color: Color, size: f32) -> Self {
let sprite = Sprite::colored_square(color, size);
self.with_sprite(sprite)
}
pub fn with_movement(self, vel_x: f32, vel_y: f32) -> Self {
let velocity = Velocity::new(vel_x, vel_y);
self.with_velocity(velocity)
}
pub fn build(self) -> EntityId {
self.entity_id
}
}
pub trait EntityManagerExt {
fn spawn(&mut self) -> EntityBuilder;
}
impl EntityManagerExt for EntityManager {
fn spawn(&mut self) -> EntityBuilder {
EntityBuilder::new(self)
}
}
impl<'a> EntityBuilder<'a> {
pub fn game_object(self, position: Position, sprite: Sprite) -> Self {
self.with_transform(Transform::new(position))
.with_sprite(sprite)
}
pub fn moving_object(self, position: Position, velocity: Velocity, sprite: Sprite) -> Self {
self.with_transform(Transform::new(position))
.with_velocity(velocity)
.with_sprite(sprite)
}
pub fn ball(self, x: f32, y: f32, vel_x: f32, vel_y: f32, color: Color, size: f32) -> Self {
self.at_position(x, y)
.with_movement(vel_x, vel_y)
.with_colored_square(color, size)
}
}