public override ICodeBlock GenerateDestroy(ICodeBlock codeBlock, IElement element) { if (GetIfHasAttachedGumInstances(element)) { codeBlock.For("int i = gumAttachmentWrappers.Count-1; i > -1; i--") .Line("FlatRedBall.SpriteManager.RemovePositionedObject(gumAttachmentWrappers[i]);"); } return(base.GenerateDestroy(codeBlock, element)); }
private void GenerateFillCompletely(NamedObjectSave namedObjectSave, ICodeBlock codeBlock) { T Get <T>(string name) { return(namedObjectSave.Properties.GetValue <T>(name)); } var tileSize = Get <float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionTileSize)); var tileSizeString = tileSize.ToString(CultureInfo.InvariantCulture) + "f"; var leftFill = Get <float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillLeft)); var leftFillString = leftFill.ToString(CultureInfo.InvariantCulture) + "f"; var topFill = Get <float>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillTop)); var topFillString = topFill.ToString(CultureInfo.InvariantCulture) + "f"; var widthFill = Get <int>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillWidth)); var heightFill = Get <int>(nameof(TileShapeCollectionPropertiesViewModel.CollisionFillHeight)); var remainderX = leftFill % tileSize; var remainderY = topFill % tileSize; var instanceName = namedObjectSave.FieldName; codeBlock.Line($"{instanceName}.GridSize = {tileSizeString};"); //TileShapeCollectionInstance.GridSize = gridSize; codeBlock.Line($"{instanceName}.LeftSeedX = {remainderX.ToString(CultureInfo.InvariantCulture)};"); codeBlock.Line($"{instanceName}.BottomSeedY = {remainderY.ToString(CultureInfo.InvariantCulture)};"); codeBlock.Line($"{instanceName}.SortAxis = FlatRedBall.Math.Axis.X;"); //TileShapeCollectionInstance.SortAxis = FlatRedBall.Math.Axis.X; var xFor = codeBlock.For($"int x = 0; x < {widthFill}; x++"); //int(int x = 0; x < width; x++) //{ var yFor = xFor.For($"int y = 0; y < {heightFill}; y++"); // for (int y = 0; y < height; y++) // { yFor.Line( $"{instanceName}.AddCollisionAtWorld(" + $"{leftFillString} + x * {tileSize} + {tileSize} / 2.0f," + $"{topFillString} - y * {tileSize} - {tileSize} / 2.0f);"); // TileShapeCollectionInstance.AddCollisionAtWorld( // left + x * gridSize + gridSize / 2.0f, // top - y * gridSize - gridSize / 2.0f); // } //} }
private void GenerateScreenSaveUpdateDependencies(ICodeBlock codeBlock, IElement element) { // Only do top level and lists, not contents of lists, or else they might get update dependencies called 2 times //foreach (var namedObject in element.AllNamedObjects) foreach (var namedObject in element.NamedObjects) { var isEntity = namedObject.SourceType == SourceType.Entity; EntitySave entity = null; bool isList = false; if (isEntity) { entity = ObjectFinder.Self.GetEntitySave(namedObject.SourceClassType); } else if (namedObject.SourceType == SourceType.FlatRedBallType && namedObject.SourceClassType == "PositionedObjectList<T>") { var classType = namedObject.SourceClassGenericType; entity = ObjectFinder.Self.GetEntitySave(classType); if (entity != null) { isList = true; } } if (entity != null) { EntityManagementValues managementValues = Values?.EntityManagementValueList?.FirstOrDefault(item => item.Name == entity.Name); bool shouldCallUpdateDependencies = managementValues != null && managementValues.SelectedProperties.Contains("Attachment"); if (shouldCallUpdateDependencies) { if (isList) { var forBlock = codeBlock.For($"int i = 0; i < {namedObject.FieldName}.Count; i++"); forBlock.Line($"{namedObject.FieldName}[i].UpdateDependencies(currentTime);"); } else { codeBlock.Line($"{namedObject.FieldName}.UpdateDependencies(currentTime);"); } } } } }
public override ICodeBlock GenerateDestroy(ICodeBlock codeBlock, IElement element) { if (element is EntitySave) { EntitySave entitySave = (EntitySave)element; if (entitySave.IsScrollableEntityList && !string.IsNullOrEmpty(entitySave.ItemType)) { string itemTypeWithoutPath = FileManager.RemovePath(entitySave.ItemType); codeBlock .For("int i = mScrollableItems.Count - 1; i > -1; i--") .Line("mScrollableItems[i].Destroy();") .End() .Line("FlatRedBall.SpriteManager.RemovePositionedObject(mScrollableHandle);") .Line("Factories." + itemTypeWithoutPath + "Factory.Destroy();"); } } return(codeBlock); }
public static void GetDestroyForNamedObject(IElement element, NamedObjectSave namedObject, ICodeBlock codeBlock, bool forceRecycle = false) { #region EARLY OUTS if (GetShouldSkipDestroyOn(namedObject)) { return; } #endregion bool shouldRecycle = forceRecycle || (element is EntitySave && (element as EntitySave).CreatedByOtherEntities); AddIfConditionalSymbolIfNecesssary(codeBlock, namedObject); #region Update dependencies // Update April 3, 2011 // The generated code used // to call UpdateDependencies // on any contained NamedObjectSave. // We did this because we wanted to make // sure that all contained NamedObjectSaves // were updated so when the Entity is recycled, // the contained NamedObjectSaves are in the proper. // position...or at least that was how things used to // be done. Now when AddToManagers is called on an Entity, // the Entity is moved back to the origin (and unrotated). This // means that we don't need to worry about this anymore: //if ( SaveObject is EntitySave && // (SaveObject as EntitySave).CreatedByOtherEntities && // (namedObject.AttachToContainer || namedObject.AttachToCamera)) //{ // // Why do we do this? The reason is because it's possible (and likely) that the Entity that this code // // is being generated for may be moving. Any attached objects will "lag behind" until the Draw method is // // called which updates dependencies. But Entities are usually destroyed before this update happens. If the // // Entity that is destroyed is re-cycled, then all attached objects will be offset slightly. This can build up // // over time and cause issues. This should help eliminate a lot of bugs. // if (namedObject.SourceType == SourceType.Entity || // (namedObject.AssetTypeInfo != null && namedObject.AssetTypeInfo.ShouldAttach)) // { // stringBuilder.AppendLine(tabs + namedObject.InstanceName + ".UpdateDependencies(TimeManager.CurrentTime);"); // } //} #endregion AssetTypeInfo ati = AvailableAssetTypes.Self.GetAssetTypeFromRuntimeType(namedObject.InstanceType); #region If object was added to manager, remove this object from managers if (ati != null && namedObject.AddToManagers) { string removalMethod = null; if (shouldRecycle && !string.IsNullOrEmpty(ati.RecycledDestroyMethod)) { removalMethod = ati.RecycledDestroyMethod.Replace("this", namedObject.InstanceName); } else { if (ati.DestroyMethod != null) { removalMethod = ati.DestroyMethod.Replace("this", namedObject.InstanceName); } } if (!string.IsNullOrEmpty(removalMethod)) { codeBlock.If(namedObject.InstanceName + " != null") .Line(removalMethod + ";"); } } #endregion // TODO: Do we want to handle this case for pooled objects? I think so. #region If the object is an Entity, destroy the object and unload the static content if (namedObject.SourceType == SourceType.Entity) { var ifBlock = codeBlock.If(namedObject.InstanceName + " != null"); ifBlock.Line(namedObject.InstanceName + ".Destroy();"); if(!forceRecycle) { // We also detach so that if the object is recycled its Parent will be null ifBlock .Line(namedObject.InstanceName + ".Detach();"); } // Vic says: We used to manually call UnloadStaticContent on all Entities. But this // doesn't work well with inheritance. Instead, we now use the ContentManagers UnloadContent method support // GenerateStaticDestroyForType(tabs, namedObject.ClassType); } #endregion #region Special case Camera if (namedObject.SourceType == SourceType.FlatRedBallType && namedObject.SourceClassType == "Camera" && namedObject.IsNewCamera) { codeBlock.Line("FlatRedBall.SpriteManager.Cameras.Remove(" + namedObject.InstanceName + ");"); } #endregion #region Special case PositionedObjectList if (namedObject.IsList && namedObject.IsFullyDefined) { bool shouldSkip = namedObject.SetByContainer; if (!shouldSkip) { var forBlock = codeBlock.For(string.Format("int i = {0}.Count - 1; i > -1; i--", namedObject.InstanceName)); bool isEntity = ObjectFinder.Self.GetEntitySave(namedObject.SourceClassGenericType) != null; if (isEntity) { forBlock.Line(namedObject.InstanceName + "[i].Destroy();"); } else { string genericClassType = namedObject.SourceClassGenericType; AssetTypeInfo atiForListElement = AvailableAssetTypes.Self.GetAssetTypeFromRuntimeType(genericClassType); if (atiForListElement != null && atiForListElement.DestroyMethod != null) { string removalMethod = atiForListElement.DestroyMethod.Replace("this", namedObject.InstanceName + "[i]"); if (!string.IsNullOrEmpty(removalMethod)) { forBlock.Line(removalMethod + ";"); } } else { forBlock.Line(string.Format("FlatRedBall.SpriteManager.RemovePositionedObject({0}[i]);", namedObject.InstanceName)); } } string genericType = namedObject.SourceClassGenericType; if (genericType.Contains("\\")) { genericType = genericType.Substring(genericType.IndexOf("\\") + 1); } } // Vic says: We used to manually call UnloadStaticContent on all Entities. But this // doesn't work well with inheritance. Instead, we now use the ContentManagers UnloadContent method support // GenerateStaticDestroyForType(tabs, genericType); } #endregion AddEndIfIfNecessary(codeBlock, namedObject); }
private static void WriteTextInitializationLoopForScene(ICodeBlock codeBlock, string sceneName) { bool useTranslation = ProjectManager.GlueProjectSave.UsesTranslation; var forBlock = codeBlock.For(string.Format("int i = 0; i < {0}.Texts.Count; i++", sceneName)); if (useTranslation) { forBlock.Line( string.Format( "{0}.Texts[i].DisplayText = FlatRedBall.Localization.LocalizationManager.Translate({0}.Texts[i].DisplayText);", sceneName)); } forBlock.Line(string.Format("{0}.Texts[i].AdjustPositionForPixelPerfectDrawing = true;", sceneName)); }
public static void GetActivityForNamedObject(NamedObjectSave namedObjectSave, ICodeBlock codeBlock) { ///////////////////////////EARLY OUT///////////////////////////////////////////////// if ( (namedObjectSave.SetByContainer && namedObjectSave.GetContainer() is EntitySave) || namedObjectSave.IsDisabled || namedObjectSave.CallActivity == false || namedObjectSave.InstantiatedByBase || !namedObjectSave.IsFullyDefined) { return; } /////////////////////////END EARLY OUT/////////////////////////////////////////////// bool setByDerived = namedObjectSave.SetByDerived; AddIfConditionalSymbolIfNecesssary(codeBlock, namedObjectSave); if (!setByDerived) { if (namedObjectSave.Instantiate == false) { // This may be null or it may be instantiated later by the user, so we should // handle both cases: codeBlock = codeBlock.If(namedObjectSave.InstanceName + " != null"); } if (namedObjectSave.SourceType == SourceType.Entity) { // Entities need activity! codeBlock.Line(namedObjectSave.InstanceName + ".Activity();"); } else if (namedObjectSave.SourceType == SourceType.FlatRedBallType && namedObjectSave.ClassType != null && namedObjectSave.ClassType.Contains("PositionedObjectList<")) { // Now let's see if the object in the list is an entity string genericType = namedObjectSave.SourceClassGenericType; if (genericType.Contains("Entities\\")) { codeBlock.For("int i = " + namedObjectSave.InstanceName + ".Count - 1; i > -1; i--") .If("i < " + namedObjectSave.InstanceName + ".Count") .Line("// We do the extra if-check because activity could destroy any number of entities") .Line(namedObjectSave.InstanceName + "[i].Activity();"); } } } // If it's an emitter, call TimedEmit: ParticleCodeGenerator.GenerateTimedEmit(codeBlock, namedObjectSave); if (!setByDerived) { if (namedObjectSave.Instantiate == false) { // end the if-statement we started above. codeBlock = codeBlock.End(); } } AddEndIfIfNecessary(codeBlock, namedObjectSave); }
public override ICodeBlock GenerateDestroy(ICodeBlock codeBlock, IElement element) { if (element is EntitySave) { EntitySave entitySave = (EntitySave)element; if (entitySave.IsScrollableEntityList && !string.IsNullOrEmpty(entitySave.ItemType)) { string itemTypeWithoutPath = FileManager.RemovePath(entitySave.ItemType); codeBlock .For("int i = mScrollableItems.Count - 1; i > -1; i--") .Line("mScrollableItems[i].Destroy();") .End() .Line("FlatRedBall.SpriteManager.RemovePositionedObject(mScrollableHandle);") .Line("Factories." + itemTypeWithoutPath + "Factory.Destroy();"); } } return codeBlock; }