Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

90 righe
2.3 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, ShipOrbiting, 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 ship_orbiting = ShipOrbiting::new();
  29. let entity = world
  30. .create_entity()
  31. .marked::<<WorldPersistence as Persistence>::Marker>()
  32. .with(player_owned)
  33. .with(fleet_owned)
  34. .with(position)
  35. .with(ship)
  36. .with(ship_orbiting)
  37. .build();
  38. Ok(entity)
  39. }
  40. pub fn player(mut self, value: Entity) -> Self {
  41. self.player = Some(value);
  42. self
  43. }
  44. pub fn fleet(mut self, value: Entity) -> Self {
  45. self.fleet = Some(value);
  46. self
  47. }
  48. pub fn position<V: Into<Vector2f>>(mut self, value: V) -> Self {
  49. self.position = Some(value.into());
  50. self
  51. }
  52. pub fn direction<V: Into<Vector2f>>(mut self, value: V) -> Self {
  53. self.position = Some(value.into());
  54. self
  55. }
  56. pub fn type_(mut self, value: ShipType) -> Self {
  57. self.type_ = Some(value);
  58. self
  59. }
  60. }
  61. impl Default for ShipBuilder {
  62. fn default() -> Self {
  63. Self {
  64. player: None,
  65. fleet: None,
  66. position: None,
  67. type_: None,
  68. direction: Vector2f::new(random::<f32>() - 0.5, random::<f32>() - 0.5).normalize(),
  69. }
  70. }
  71. }