|
| 1 | +using Obsidian.API.Entities; |
| 2 | +using Obsidian.Services; |
| 3 | +using Obsidian.WorldData; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Obsidian.Entities.Factories; |
| 11 | +internal class EntitySpawner : IEntitySpawner |
| 12 | +{ |
| 13 | + private IWorld world; |
| 14 | + |
| 15 | + private EntityType? entityType = null; |
| 16 | + |
| 17 | + private VectorF position = VectorF.Zero; |
| 18 | + private bool isBaby = false; |
| 19 | + private string? customName = null; |
| 20 | + private bool customNameVisible = false; |
| 21 | + private bool ambientPotionEffect = false; |
| 22 | + private int absorbedArrows = 0; |
| 23 | + private int absorbedStingers = 0; |
| 24 | + private bool burning = false; |
| 25 | + private bool glowing = false; |
| 26 | + |
| 27 | + public EntitySpawner(IWorld world) |
| 28 | + { |
| 29 | + this.world = world; |
| 30 | + } |
| 31 | + |
| 32 | + public IEntitySpawner WithEntityType(EntityType type) |
| 33 | + { |
| 34 | + entityType = type; |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + public IEntitySpawner AsBaby() |
| 39 | + { |
| 40 | + isBaby = true; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public IEntitySpawner AtPosition(VectorF position) |
| 45 | + { |
| 46 | + this.position = position; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public IEntitySpawner WithCustomName(string name, bool visible = true) |
| 51 | + { |
| 52 | + customName = name; |
| 53 | + customNameVisible = visible; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public IEntitySpawner WithAmbientPotionEffect(bool ambient) |
| 58 | + { |
| 59 | + ambientPotionEffect = ambient; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public IEntitySpawner WithAbsorbedArrows(int arrows) |
| 64 | + { |
| 65 | + absorbedArrows = arrows; |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public IEntitySpawner WithAbsorbedStingers(int stingers) |
| 70 | + { |
| 71 | + absorbedStingers = stingers; |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + public IEntitySpawner IsBurning() |
| 76 | + { |
| 77 | + this.burning = true; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public IEntitySpawner IsGlowing() |
| 82 | + { |
| 83 | + this.glowing = true; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public IEntity Spawn() |
| 88 | + { |
| 89 | + var packetBroadcaster = (world as World).PacketBroadcaster; |
| 90 | + |
| 91 | + // This could get sgen'd, same for the entity classes. but for now, this is fine for implementation |
| 92 | + Entity entity = entityType switch |
| 93 | + { |
| 94 | + EntityType.Pig => new Pig() |
| 95 | + { |
| 96 | + PacketBroadcaster = packetBroadcaster, |
| 97 | + World = world, |
| 98 | + }, |
| 99 | + EntityType.Horse => new Horse() |
| 100 | + { |
| 101 | + PacketBroadcaster = packetBroadcaster, |
| 102 | + World = world, |
| 103 | + }, |
| 104 | + EntityType.Llama => new Llama() |
| 105 | + { |
| 106 | + PacketBroadcaster = packetBroadcaster, |
| 107 | + World = world, |
| 108 | + }, |
| 109 | + EntityType.Donkey => new Donkey() |
| 110 | + { |
| 111 | + PacketBroadcaster = packetBroadcaster, |
| 112 | + World = world |
| 113 | + }, |
| 114 | + EntityType.SkeletonHorse => new SkeletonHorse() |
| 115 | + { |
| 116 | + PacketBroadcaster = packetBroadcaster, |
| 117 | + World = world |
| 118 | + }, |
| 119 | + EntityType.ZombieHorse => new ZombieHorse() |
| 120 | + { |
| 121 | + PacketBroadcaster = packetBroadcaster, |
| 122 | + World = world |
| 123 | + }, |
| 124 | + |
| 125 | + null => throw new InvalidOperationException("Entity type must be set"), |
| 126 | + |
| 127 | + _ => entityType.Value.IsNonLiving() ? |
| 128 | + new Entity() |
| 129 | + { |
| 130 | + PacketBroadcaster = packetBroadcaster, |
| 131 | + World = world, |
| 132 | + } : |
| 133 | + new Living() |
| 134 | + { |
| 135 | + PacketBroadcaster = packetBroadcaster, |
| 136 | + World = world |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + entity.Type = entityType.Value; |
| 141 | + entity.EntityId = Server.GetNextEntityId(); |
| 142 | + entity.Position = position; |
| 143 | + |
| 144 | + if (entity is Living living && customName != null) |
| 145 | + { |
| 146 | + living.CustomName = customName; |
| 147 | + living.CustomNameVisible = customNameVisible; |
| 148 | + living.AmbientPotionEffect = ambientPotionEffect; |
| 149 | + living.AbsorbedArrows = absorbedArrows; |
| 150 | + living.AbsorbedStingers = absorbedStingers; |
| 151 | + } |
| 152 | + |
| 153 | + if (entity is AgeableMob ageable && isBaby) |
| 154 | + { |
| 155 | + ageable.IsBaby = isBaby; |
| 156 | + } |
| 157 | + |
| 158 | + entity.Burning = burning; |
| 159 | + entity.Glowing = glowing; |
| 160 | + |
| 161 | + return (world as World).SpawnEntity(entity); |
| 162 | + } |
| 163 | +} |
0 commit comments