Esempio n. 1
0
        public LevelEntityController SpawnEntity(LevelEntity entity)
        {
            var gridElem = CreateGridElement(entity.Tile);

            gridElem.name = $"GridElement_{entity.Type}";

            var entityController = InstantiateEntity(gridElem, entity).GetComponent <LevelEntityController>();

            entityController.Orientation = entity.Orientation;
            entityController.ApplyOrientation(true);
            entityController.RulesBridge = rulesBridge;
            return(entityController);
        }
Esempio n. 2
0
        public GameObject InstantiateEntity(GridElementController container, LevelEntity entity)
        {
            switch (entity.Type)
            {
            case EntityType.Emitter:
                return(SpawnEmitter(container, entity.EmitterType).gameObject);

            case EntityType.Absorber:
                return(SpawnAbsorber(container, entity.AbsorberType).gameObject);

            case EntityType.Reflector:
                return(SpawnReflector(container, entity.ReflectorType).gameObject);

            default:
                throw new Exception($"Unknown entity type {entity.Type}.");
            }
        }
Esempio n. 3
0
        public LevelData Save()
        {
            var entities = new List <LevelEntity>();

            for (int i = 0; i < Grid.transform.childCount; ++i)
            {
                var child = Grid.transform.GetChild(i);
                if (child.TryGetComponent <GridElementController>(out var gridElement))
                {
                    if (gridElement.transform.childCount == 0)
                    {
                        continue;
                    }

                    if (gridElement.tag == "EditorOnly")
                    {
                        continue;
                    }

                    var mainEntity = gridElement.transform.GetChild(0);
                    if (mainEntity.gameObject.TryGetComponent <LevelEntityController>(out var entity))
                    {
                        var serializableEntity = new LevelEntity();
                        serializableEntity.IsTiled     = true;
                        serializableEntity.Type        = entity.Type;
                        serializableEntity.Tile        = gridElement.Tile;
                        serializableEntity.Orientation = entity.Orientation;

                        if (entity.Type == EntityType.Reflector)
                        {
                            if (entity.TryGetComponent <ReflectorController>(out var reflector))
                            {
                                serializableEntity.ReflectorType = reflector.Type;
                            }
                            else
                            {
                                throw new Exception("Entity has a reflector type, but reflector controller isn't attached to it.");
                            }
                        }

                        if (entity.Type == EntityType.Absorber)
                        {
                            if (entity.TryGetComponent <AbsorberController>(out var absorber))
                            {
                                serializableEntity.AbsorberType = absorber.Type;
                            }
                            else
                            {
                                throw new Exception("Entity has a absorber type, but absorber controller isn't attached to it.");
                            }
                        }

                        if (entity.Type == EntityType.Emitter)
                        {
                            if (entity.TryGetComponent <EmitterController>(out var emitter))
                            {
                                serializableEntity.EmitterType = emitter.Type;
                            }
                            else
                            {
                                throw new Exception("Entity has a emitter type, but emitter controller isn't attached to it.");
                            }
                        }

                        entities.Add(serializableEntity);
                    }
                }
            }

            return(new LevelData()
            {
                Entities = entities
            });
        }