public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { //bool exists1 = Testing.manager.EntityExistsGlobaly(Testing.entityID); var protoEntity = new ProtoEntity(); //StarObject (Entity) reader.Read(); // PropertyName ID reader.Read(); // Actual ID protoEntity.Guid = serializer.Deserialize <Guid>(reader); // Deserialize the ID //bool exists2 = Testing.manager.EntityExistsGlobaly(Testing.entityID); // Deserialize the dataBlobs reader.Read(); // PropertyName DATABLOB while (reader.TokenType == JsonToken.PropertyName) { var typestring = "Pulsar4X.ECSLib." + (string)reader.Value; Type dataBlobType = Type.GetType(typestring); reader.Read(); // StartObject (dataBlob) //bool exists3 = Testing.manager.EntityExistsGlobaly(Testing.entityID); BaseDataBlob dataBlob = (BaseDataBlob)serializer.Deserialize(reader, dataBlobType); // EndObject (dataBlob) //bool exists4 = Testing.manager.EntityExistsGlobaly(Testing.entityID); protoEntity.SetDataBlob(dataBlob); reader.Read(); // PropertyName DATABLOB OR EndObject (Entity) //bool exists5 = Testing.manager.EntityExistsGlobaly(Testing.entityID); } //bool exists6 = Testing.manager.EntityExistsGlobaly(Testing.entityID); return(protoEntity); }
public static ProtoEntity Create(Guid guid, IEnumerable <BaseDataBlob> dataBlobs = null) { var protoEntity = new ProtoEntity { DataBlobs = EntityManager.BlankDataBlobList(), Guid = guid }; if (dataBlobs == null) { return(protoEntity); } foreach (BaseDataBlob dataBlob in dataBlobs) { protoEntity.SetDataBlob(dataBlob); } return(protoEntity); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var protoEntity = new ProtoEntity(); //StarObject (Entity) reader.Read(); // PropertyName Guid reader.Read(); // Actual Guid protoEntity.Guid = serializer.Deserialize <Guid>(reader); // Deserialize the Guid // Deserialize the dataBlobs reader.Read(); // PropertyName DATABLOB while (reader.TokenType == JsonToken.PropertyName) { Type dataBlobType = Type.GetType("Pulsar4X.ECSLib." + (string)reader.Value); reader.Read(); // StartObject (dataBlob) BaseDataBlob dataBlob = (BaseDataBlob)serializer.Deserialize(reader, dataBlobType); // EndObject (dataBlob) protoEntity.SetDataBlob(dataBlob); reader.Read(); // PropertyName DATABLOB OR EndObject (Entity) } return(protoEntity); }
public static Entity CreateShip(Entity classEntity, EntityManager systemEntityManager, Entity ownerFaction, Vector3 pos, StarSystem starsys, string shipName = null) { // @todo replace ownerFaction with formationDB later. Now ownerFaction used just to add name // @todo: make sure each component design and component instance is unique, not duplicated ProtoEntity protoShip = classEntity.Clone(); ShipInfoDB shipInfoDB = protoShip.GetDataBlob <ShipInfoDB>(); shipInfoDB.ShipClassDefinition = classEntity.Guid; if (shipName == null) { shipName = "Ship Name"; } NameDB nameDB = new NameDB(shipName); nameDB.SetName(ownerFaction.Guid, shipName); protoShip.SetDataBlob(nameDB); OrderableDB orderableDB = new OrderableDB(); protoShip.SetDataBlob(orderableDB); PositionDB position = new PositionDB(pos, starsys.Guid); protoShip.SetDataBlob(position); protoShip.SetDataBlob(new DesignInfoDB(classEntity)); //replace the ships references to the design's specific instances with shiny new specific instances ComponentInstancesDB classInstances = classEntity.GetDataBlob <ComponentInstancesDB>(); Entity shipEntity = new Entity(systemEntityManager, ownerFaction.Guid, protoShip); shipEntity.RemoveDataBlob <ComponentInstancesDB>(); shipEntity.SetDataBlob(new ComponentInstancesDB()); if (shipEntity.HasDataBlob <FireControlAbilityDB>()) { shipEntity.RemoveDataBlob <FireControlAbilityDB>(); } foreach (var designKVP in classInstances.DesignsAndComponentCount) { for (int i = 0; i < designKVP.Value; i++) { Entity newInstance = ComponentInstanceFactory.NewInstanceFromDesignEntity(designKVP.Key, ownerFaction.Guid, systemEntityManager); EntityManipulation.AddComponentToEntity(shipEntity, newInstance); } } FactionOwnerDB factionOwner = ownerFaction.GetDataBlob <FactionOwnerDB>(); factionOwner.SetOwned(shipEntity); ComponentInstancesDB shipComponentInstanceDB = shipEntity.GetDataBlob <ComponentInstancesDB>(); //TODO: do this somewhere else, recalcprocessor maybe? foreach (var design in shipComponentInstanceDB.GetDesignsByType(typeof(SensorReceverAtbDB))) { foreach (var instance in shipComponentInstanceDB.GetComponentsBySpecificDesign(design.Guid)) { var sensor = design.GetDataBlob <SensorReceverAtbDB>(); DateTime nextDatetime = shipEntity.Manager.ManagerSubpulses.StarSysDateTime + TimeSpan.FromSeconds(sensor.ScanTime); shipEntity.Manager.ManagerSubpulses.AddEntityInterupt(nextDatetime, new SensorScan().TypeName, instance.OwningEntity); } } ReCalcProcessor.ReCalcAbilities(shipEntity); return(shipEntity); }
public static Entity CreateShip(Entity classEntity, EntityManager systemEntityManager, Entity ownerFaction, Vector4 pos, StarSystem starsys, string shipName = null) { // @todo replace ownerFaction with formationDB later. Now ownerFaction used just to add name // @todo: make sure each component design and component instance is unique, not duplicated ProtoEntity protoShip = classEntity.Clone(); ShipInfoDB shipInfoDB = protoShip.GetDataBlob <ShipInfoDB>(); shipInfoDB.ShipClassDefinition = classEntity.Guid; if (shipName == null) { shipName = "Ship Name"; } NameDB nameDB = new NameDB(shipName); nameDB.SetName(ownerFaction, shipName); protoShip.SetDataBlob(nameDB); OrderableDB orderableDB = new OrderableDB(); protoShip.SetDataBlob(orderableDB); PositionDB position = new PositionDB(pos, starsys.Guid); protoShip.SetDataBlob(position); protoShip.SetDataBlob(new DesignInfoDB(classEntity)); Entity shipEntity = new Entity(systemEntityManager, protoShip); new OwnedDB(ownerFaction, shipEntity); //replace the ships references to the design's specific instances with shiny new specific instances ComponentInstancesDB componentInstances = shipEntity.GetDataBlob <ComponentInstancesDB>(); var newSpecificInstances = new PrIwObsDict <Entity, PrIwObsList <Entity> >(); foreach (var kvp in componentInstances.SpecificInstances) { newSpecificInstances.Add(kvp.Key, new PrIwObsList <Entity>()); for (int i = 0; i < kvp.Value.Count; i++) { var ownerdb = ownerFaction.GetDataBlob <FactionOwnerDB>(); newSpecificInstances[kvp.Key].Add(ComponentInstanceFactory.NewInstanceFromDesignEntity(kvp.Key, ownerFaction, ownerdb, systemEntityManager)); } } componentInstances.SpecificInstances = newSpecificInstances; foreach (var componentType in shipEntity.GetDataBlob <ComponentInstancesDB>().SpecificInstances) { int numComponents = componentType.Value.Count; componentType.Value.Clear(); for (int i = 0; i < numComponents; i++) { EntityManipulation.AddComponentToEntity(shipEntity, componentType.Key); } foreach (var componentInstance in componentType.Value) { // Set the parent/owning Entity to the shipEntity AttributeToAbilityMap.AddAbility(shipEntity, componentType.Key, componentInstance); //TODO: do this somewhere else, recalcprocessor maybe? if (componentInstance.HasDataBlob <SensorReceverAtbDB>()) { var sensor = componentInstance.GetDataBlob <SensorReceverAtbDB>(); DateTime nextDatetime = shipEntity.Manager.ManagerSubpulses.SystemLocalDateTime + TimeSpan.FromSeconds(sensor.ScanTime); shipEntity.Manager.ManagerSubpulses.AddEntityInterupt(nextDatetime, new SensorScan().TypeName, componentInstance); } } } ReCalcProcessor.ReCalcAbilities(shipEntity); return(shipEntity); }
public static Entity CreateShip(Entity classEntity, EntityManager systemEntityManager, Entity ownerFaction, Vector4 pos, StarSystem starsys, string shipName = null) { // @todo replace ownerFaction with formationDB later. Now ownerFaction used just to add name // @todo: make sure each component design and component instance is unique, not duplicated ProtoEntity protoShip = classEntity.Clone(); ShipInfoDB shipInfoDB = protoShip.GetDataBlob <ShipInfoDB>(); shipInfoDB.ShipClassDefinition = classEntity.Guid; if (shipName == null) { shipName = "Ship Name"; } NameDB nameDB = new NameDB(shipName); protoShip.SetDataBlob(nameDB); var OwnedDB = new OwnedDB(ownerFaction); protoShip.SetDataBlob(OwnedDB); PositionDB position = new PositionDB(pos, starsys.Guid); protoShip.SetDataBlob(position); protoShip.SetDataBlob(new DesignInfoDB(classEntity)); Entity shipEntity = new Entity(systemEntityManager, protoShip); //replace the ships references to the design's specific instances with shiny new specific instances ComponentInstancesDB componentInstances = shipEntity.GetDataBlob <ComponentInstancesDB>(); var newSpecificInstances = new PrIwObsDict <Entity, PrIwObsList <Entity> >(); foreach (var kvp in componentInstances.SpecificInstances) { newSpecificInstances.Add(kvp.Key, new PrIwObsList <Entity>()); for (int i = 0; i < kvp.Value.Count; i++) { newSpecificInstances[kvp.Key].Add(ComponentInstanceFactory.NewInstanceFromDesignEntity(kvp.Key, ownerFaction)); } } componentInstances.SpecificInstances = newSpecificInstances; foreach (var componentType in shipEntity.GetDataBlob <ComponentInstancesDB>().SpecificInstances) { int numComponents = componentType.Value.Count; componentType.Value.Clear(); for (int i = 0; i < numComponents; i++) { EntityManipulation.AddComponentToEntity(shipEntity, componentType.Key); } foreach (var componentInstance in componentType.Value) { // Set the parent/owning Entity to the shipEntity AttributeToAbilityMap.AddAbility(shipEntity, componentType.Key, componentInstance); } } ReCalcProcessor.ReCalcAbilities(shipEntity); return(shipEntity); }