Example #1
0
        public virtual Entity Install(EntityWorld world, Entity shipEntity, Entity hardpointEntity)
        {
            var slot = hardpointEntity.GetComponent<HardpointComponent>();
            if (!hardpointEntity.IsChildOf(shipEntity)) throw new InvalidOperationException("Cannot install, ship entity does not own hardpoint entity.");
            if (slot == null) throw new InvalidOperationException("Cannot install on non-hardpoint entity.");
            var shipComponent = shipEntity.GetComponent<ShipComponent>();
            var moduleEntity = world.CreateEntity();

            var hardpointTransform = hardpointEntity.GetComponent<Transform>();
            var moduleTransform = moduleEntity.AddComponentFromPool<Transform>();
            moduleTransform.Position = Vector2.Zero;
            moduleTransform.Rotation = 0;
            moduleTransform.Scale = Vector2.One;
            moduleTransform.Origin = -hardpointTransform.Origin;            

            var scale = hardpointTransform.Scale;
            var origin = -hardpointTransform.Origin;
            if (scale.X < 0)
            {
                scale.X = Math.Abs(scale.X);
                origin.X *= -1;
            }
            if (scale.Y < 0)
            {
                scale.Y *= Math.Abs(scale.Y);
                origin.Y *= -1;
            }
            moduleTransform.Scale = scale;
            moduleTransform.Origin = origin;

            hardpointEntity.AddChild(moduleEntity);
            var previousInstalled = slot.InstalledEntity;
            if (previousInstalled != null)
            {
                previousInstalled.GetComponent<ModuleComponent>().Module.Uninstall(shipEntity, previousInstalled);
            }

            if (!string.IsNullOrWhiteSpace(PartGroupId))
            {
                ShipEntityFactory.GetShipModel(PartGroupId).CreateChildEntities(world, moduleEntity);
            }

            var moduleComponent = new ModuleComponent
            {
                HardpointEntity = hardpointEntity,
                Module = this
            };
            slot.InstalledEntity = moduleEntity;
            moduleEntity.AddComponent(moduleComponent);
            return moduleEntity;
        }