Exemplo n.º 1
0
    private void ReadEntity(JSONObject entityObject, Entity entity)
    {
        ReadPropertiesObject(entityObject, entity);

        if (entityObject["sensor"] != null)
        {
            JSONObject sensorObject = entityObject["sensor"].AsObject;
            string     sensorName   = sensorObject["name"];
            var        sensorType   = GameScripts.FindTypeWithName(GameScripts.sensors, sensorName);
            if (sensorType == null)
            {
                warnings.Add("Unrecognized sensor: " + sensorName);
            }
            else
            {
                Sensor newSensor = (Sensor)sensorType.Create();
                ReadPropertiesObject(sensorObject, newSensor);
                entity.sensor = newSensor;
            }
        }

        if (entityObject["behaviors"] != null)
        {
            foreach (JSONNode behaviorNode in entityObject["behaviors"].AsArray)
            {
                JSONObject behaviorObject = behaviorNode.AsObject;
                string     behaviorName   = behaviorObject["name"];
                var        behaviorType   = GameScripts.FindTypeWithName(GameScripts.behaviors, behaviorName);
                if (behaviorType == null)
                {
                    warnings.Add("Unrecognized behavior: " + behaviorName);
                    continue;
                }
                EntityBehavior newBehavior = (EntityBehavior)behaviorType.Create();
                ReadPropertiesObject(behaviorObject, newBehavior);
                if (fileWriterVersion <= 5 && behaviorObject["target"] != null)
                {
                    if (behaviorObject["target"] == "activator")
                    {
                        newBehavior.target = new EntityBehavior.BehaviorTargetProperty(true);
                    }
                    else
                    {
                        newBehavior.target = new EntityBehavior.BehaviorTargetProperty(
                            new EntityReference(new System.Guid(behaviorObject["target"])));
                    }
                }
                entity.behaviors.Add(newBehavior);
            }
        }

        if (entityObject["id"] != null)
        {
            System.Guid id = new System.Guid(entityObject["id"]);
            EntityReference.AddExistingEntityId(entity, id);
        }
    }
Exemplo n.º 2
0
    private void ReadEntity(MessagePackObjectDictionary entityDict, Entity entity)
    {
        ReadPropertiesObject(entityDict, entity);

        if (entityDict.ContainsKey(FileKeys.ENTITY_SENSOR))
        {
            var    sensorDict = entityDict[FileKeys.ENTITY_SENSOR].AsDictionary();
            string sensorName = sensorDict[FileKeys.PROPOBJ_NAME].AsString();
            var    sensorType = GameScripts.FindTypeWithName(GameScripts.sensors, sensorName);
            if (sensorType == null)
            {
                warnings.Add("Unrecognized sensor: " + sensorName);
            }
            else
            {
                Sensor newSensor = (Sensor)sensorType.Create();
                ReadPropertiesObject(sensorDict, newSensor);
                entity.sensor = newSensor;
            }
        }

        if (entityDict.ContainsKey(FileKeys.ENTITY_BEHAVIORS))
        {
            foreach (var behaviorObj in entityDict[FileKeys.ENTITY_BEHAVIORS].AsList())
            {
                var    behaviorDict = behaviorObj.AsDictionary();
                string behaviorName = behaviorDict[FileKeys.PROPOBJ_NAME].AsString();
                var    behaviorType = GameScripts.FindTypeWithName(GameScripts.behaviors, behaviorName);
                if (behaviorType == null)
                {
                    warnings.Add("Unrecognized behavior: " + behaviorName);
                    continue;
                }
                EntityBehavior newBehavior = (EntityBehavior)behaviorType.Create();
                ReadPropertiesObject(behaviorDict, newBehavior);
                entity.behaviors.Add(newBehavior);
            }
        }

        if (entityDict.ContainsKey(FileKeys.ENTITY_ID))
        {
            System.Guid id = new System.Guid(entityDict[FileKeys.ENTITY_ID].AsString());
            EntityReference.AddExistingEntityId(entity, id);
        }
    }
Exemplo n.º 3
0
    private void ReadEntity(MessagePackObjectDictionary entityDict, Entity entity)
    {
        ReadPropertiesObject(entityDict, entity);

        if (entityDict.ContainsKey(FileKeys.ENTITY_SENSOR))
        {
            var    sensorDict = entityDict[FileKeys.ENTITY_SENSOR].AsDictionary();
            string sensorName = sensorDict[FileKeys.PROPOBJ_NAME].AsString();
            var    sensorType = GameScripts.FindTypeWithName(GameScripts.sensors, sensorName);
            if (sensorType == null)
            {
                warnings.Add("Unrecognized sensor: " + sensorName);
            }
            else
            {
                Sensor newSensor = (Sensor)sensorType.Create();
                ReadPropertiesObject(sensorDict, newSensor);
                entity.sensor = newSensor;
            }
        }

        if (entityDict.ContainsKey(FileKeys.ENTITY_BEHAVIORS))
        {
            foreach (var behaviorObj in entityDict[FileKeys.ENTITY_BEHAVIORS].AsList())
            {
                var    behaviorDict = behaviorObj.AsDictionary();
                string behaviorName = behaviorDict[FileKeys.PROPOBJ_NAME].AsString();
                var    behaviorType = GameScripts.FindTypeWithName(GameScripts.behaviors, behaviorName);
                if (behaviorType == null)
                {
                    warnings.Add("Unrecognized behavior: " + behaviorName);
                    continue;
                }
                EntityBehavior newBehavior = (EntityBehavior)behaviorType.Create();
                ReadPropertiesObject(behaviorDict, newBehavior);
                entity.behaviors.Add(newBehavior);
                if (newBehavior is LightBehavior light && light.halo)
                {
                    // convert halo from version 10 and earlier
                    HaloBehavior halo = new HaloBehavior();
                    halo.condition = light.condition;
                    halo.target    = light.target;
                    if (light.GetProperty("siz") is float size)
                    {
                        halo.SetProperty("siz", size);
                    }
                    if (light.GetProperty("col") is Color color &&
                        light.GetProperty("int") is float intensity)
                    {
                        halo.SetProperty("col", color * intensity / HaloComponent.INTENSITY);
                    }
                    entity.behaviors.Add(halo);
                    light.halo = false;
                }
            }
        }

        if (entityDict.ContainsKey(FileKeys.ENTITY_ID))
        {
            System.Guid id = new System.Guid(entityDict[FileKeys.ENTITY_ID].AsString());
            EntityReference.AddExistingEntityId(entity, id);
        }
    }