You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

  1. use glc::vector::Vector2f;
  2. use rand::random;
  3. use specs::{saveload::MarkedBuilder, Builder, Entity, World, WorldExt};
  4. use crate::{
  5. components::{FleetOwned, PlayerOwned, Position, Ship, ShipType},
  6. misc::{Persistence, WorldPersistence},
  7. };
  8. use super::Error;
  9. #[derive(Clone)]
  10. pub struct ShipBuilder {
  11. player: Option<Entity>,
  12. fleet: Option<Entity>,
  13. position: Option<Vector2f>,
  14. type_: Option<ShipType>,
  15. direction: Vector2f,
  16. }
  17. impl ShipBuilder {
  18. pub fn build(&self, world: &mut World) -> Result<Entity, Error> {
  19. let player = self.player.ok_or(Error::MissingValue("player"))?;
  20. let fleet = self.fleet.ok_or(Error::MissingValue("fleet"))?;
  21. let position = self.position.ok_or(Error::MissingValue("position"))?;
  22. let type_ = self.type_.ok_or(Error::MissingValue("type"))?;
  23. let direction = self.direction;
  24. let player_owned = PlayerOwned::new(player);
  25. let fleet_owned = FleetOwned::new(fleet);
  26. let position = Position::new(position);
  27. let ship = Ship::new(type_, direction);
  28. let entity = world
  29. .create_entity()
  30. .marked::<<WorldPersistence as Persistence>::Marker>()
  31. .with(player_owned)
  32. .with(fleet_owned)
  33. .with(position)
  34. .with(ship)
  35. .build();
  36. Ok(entity)
  37. }
  38. pub fn player(mut self, value: Entity) -> Self {
  39. self.player = Some(value);
  40. self
  41. }
  42. pub fn fleet(mut self, value: Entity) -> Self {
  43. self.fleet = Some(value);
  44. self
  45. }
  46. pub fn position<V: Into<Vector2f>>(mut self, value: V) -> Self {
  47. self.position = Some(value.into());
  48. self
  49. }
  50. pub fn direction<V: Into<Vector2f>>(mut self, value: V) -> Self {
  51. self.position = Some(value.into());
  52. self
  53. }
  54. pub fn type_(mut self, value: ShipType) -> Self {
  55. self.type_ = Some(value);
  56. self
  57. }
  58. }
  59. impl Default for ShipBuilder {
  60. fn default() -> Self {
  61. Self {
  62. player: None,
  63. fleet: None,
  64. position: None,
  65. type_: None,
  66. direction: Vector2f::new(random::<f32>() - 0.5, random::<f32>() - 0.5).normalize(),
  67. }
  68. }
  69. }