Skip to content

Commit

Permalink
Added new top-down animation controller generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed Feb 23, 2024
1 parent ab2bdc4 commit 530ae61
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ private static async Task<bool> UpdateTopDownCodePresenceInProject()
await topDownController.GenerateAndAddCsv(
firstTopDownEntity,
viewModel);

TopDownPlugin.CodeGenerators.TopDownAnimationControllerGenerator.Self.GenerateAndSave();

}
else
{
// Platformer removes here. Should we also remove top-down?

GlueCommands.Self.ProjectCommands.RemoveFromProjects(TopDownPlugin.CodeGenerators.TopDownAnimationControllerGenerator.Self.FileLocation);

}

// remove requirement for the old top-down plugin otherwise projects will get a message forever about it:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,50 @@
using System.Collections.Generic;
using System.Text;

namespace FlatRedBall.PlatformerPlugin.Generators
namespace FlatRedBall.PlatformerPlugin.Generators;

public class PlatformerAnimationControllerGenerator : Singleton<PlatformerAnimationControllerGenerator>
{
public class PlatformerAnimationControllerGenerator : Singleton<PlatformerAnimationControllerGenerator>
{
string RelativeFileLocation => "Platformer/PlatformerAnimationController.Generated.cs";
public FilePath FileLocation => GlueState.Self.CurrentGlueProjectDirectory + RelativeFileLocation;
string RelativeFileLocation => "Platformer/PlatformerAnimationController.Generated.cs";
public FilePath FileLocation => GlueState.Self.CurrentGlueProjectDirectory + RelativeFileLocation;


public void GenerateAndSave()
public void GenerateAndSave()
{

TaskManager.Self.Add(() =>
{
var contents = GenerateFileContents();

TaskManager.Self.Add(() =>
{
var contents = GenerateFileContents();
var relativeDirectory = RelativeFileLocation;

var relativeDirectory = RelativeFileLocation;
GlueCommands.Self.ProjectCommands.CreateAndAddCodeFile(relativeDirectory);

GlueCommands.Self.ProjectCommands.CreateAndAddCodeFile(relativeDirectory);
var glueProjectDirectory = GlueState.Self.CurrentGlueProjectDirectory;

var glueProjectDirectory = GlueState.Self.CurrentGlueProjectDirectory;
if (!string.IsNullOrEmpty(glueProjectDirectory))
{
var fullFile = GlueState.Self.CurrentGlueProjectDirectory + relativeDirectory;

if (!string.IsNullOrEmpty(glueProjectDirectory))
try
{
var fullFile = GlueState.Self.CurrentGlueProjectDirectory + relativeDirectory;

try
{
GlueCommands.Self.TryMultipleTimes(() =>
System.IO.File.WriteAllText(fullFile, contents));
}
catch (Exception e)
{
GlueCommands.Self.PrintError(e.ToString());
}
GlueCommands.Self.TryMultipleTimes(() =>
System.IO.File.WriteAllText(fullFile, contents));
}
catch (Exception e)
{
GlueCommands.Self.PrintError(e.ToString());
}
}

}, "Adding PlatformerAnimationConfiguration.Generated.cs to the project");
}, "Adding PlatformerAnimationConfiguration.Generated.cs to the project");


}
}

private string GenerateFileContents()
{
var toReturn =
private string GenerateFileContents()
{
var toReturn =
@"
using System.Linq;
Expand Down Expand Up @@ -127,17 +127,17 @@ public void AddLayer(PlatformerAnimationConfiguration configuration)
";

if(GlueState.Self.CurrentGlueProject.FileVersion >= (int)GlueProjectSave.GluxVersions.AnimationLayerHasName)
{
toReturn +=
@"
if(GlueState.Self.CurrentGlueProject.FileVersion >= (int)GlueProjectSave.GluxVersions.AnimationLayerHasName)
{
toReturn +=
@"
layer.Name = configuration.AnimationName;
";

}
}

toReturn +=
@"
toReturn +=
@"
layer.EveryFrameAction = () =>
{
if(!IsActive)
Expand Down Expand Up @@ -264,7 +264,6 @@ public void AddLayer(PlatformerAnimationConfiguration configuration)
}
";
return toReturn;
}
return toReturn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
using FlatRedBall.Glue.Managers;
using FlatRedBall.Glue.Plugins.ExportedImplementations;
using FlatRedBall.Glue.SaveClasses;
using FlatRedBall.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TopDownPlugin.CodeGenerators;

public class TopDownAnimationControllerGenerator : Singleton<TopDownAnimationControllerGenerator>
{
string RelativeFileLocation = "TopDown/TopDownAnimationControllerGenerator.Generated.cs";
public FilePath FileLocation => GlueState.Self.CurrentGlueProjectDirectory + RelativeFileLocation;

public void GenerateAndSave()
{

TaskManager.Self.Add(() =>
{
var contents = GenerateFileContents();

var relativeDirectory = RelativeFileLocation;

GlueCommands.Self.ProjectCommands.CreateAndAddCodeFile(relativeDirectory);

var glueProjectDirectory = GlueState.Self.CurrentGlueProjectDirectory;

if (!string.IsNullOrEmpty(glueProjectDirectory))
{
var fullFile = GlueState.Self.CurrentGlueProjectDirectory + relativeDirectory;

try
{
GlueCommands.Self.TryMultipleTimes(() =>
System.IO.File.WriteAllText(fullFile, contents));
}
catch (Exception e)
{
GlueCommands.Self.PrintError(e.ToString());
}
}

}, "Adding TopDownAnimationControllerGenerator.Generated.cs to the project");

}

private string GenerateFileContents()
{
var toReturn =
@"
using System.Linq;
namespace " + GlueState.Self.ProjectNamespace + @".TopDown
{
public enum AnimationSpeedAssignment
{
ForceTo1,
NoAssignment,
BasedOnVelocityMultiplier,
BasedOnMaxSpeedRatioMultiplier,
BasedOnInputMultiplier
}
public class TopDownAnimationConfiguration
{
public string AnimationName { get; set; }
public bool IsDirectionFacingAppended { get; set; } = true;
public float? MinVelocityAbsolute { get; set; }
public float? MaxVelocityAbsolute { get; set; }
public float? AbsoluteVelocityAnimationSpeedMultiplier { get; set; }
public float? MinMovementInputAbsolute { get; set; }
public float? MaxMovementInputAbsolute { get; set; }
public float? MaxSpeedRatioMultiplier { get; set; }
public string MovementName { get; set; }
public AnimationSpeedAssignment AnimationSpeedAssignment { get; set; }
public System.Func<bool> AdditionalPredicate;
public override string ToString()
{
return AnimationName;
}
}
public class TopDownAnimationController : FlatRedBall.Graphics.Animation.AnimationController
{
ITopDownEntity TopDownEntity;
public bool IsActive { get; set; } = true;
System.Collections.Generic.List<TopDownAnimationConfiguration> topDownAnimationConfigurations;
public System.Collections.ObjectModel.ReadOnlyCollection<TopDownAnimationConfiguration> Configurations { get; private set; }
public TopDownAnimationController(ITopDownEntity topDownEntity)
{
TopDownEntity = topDownEntity;
topDownAnimationConfigurations =
new System.Collections.Generic.List<TopDownAnimationConfiguration>();
Configurations = new System.Collections.ObjectModel.ReadOnlyCollection<TopDownAnimationConfiguration>(topDownAnimationConfigurations);
}
public TopDownAnimationConfiguration GetConfiguration(string animationName) =>
topDownAnimationConfigurations.First(item => item.AnimationName == animationName);
public void AddLayer(TopDownAnimationConfiguration configuration)
{
var layer = new FlatRedBall.Graphics.Animation.AnimationLayer();
this.Layers.Add(layer);
topDownAnimationConfigurations.Add(configuration);
";

if (GlueState.Self.CurrentGlueProject.FileVersion >= (int)GlueProjectSave.GluxVersions.AnimationLayerHasName)
{
toReturn +=
@"
layer.Name = configuration.AnimationName;
";

}

toReturn +=
@"
layer.EveryFrameAction = () =>
{
if (!IsActive)
{
return null;
}
bool shouldSet = true;
var absoluteVelocity = (float)System.Math.Sqrt(TopDownEntity.XVelocity * TopDownEntity.XVelocity + TopDownEntity.YVelocity * TopDownEntity.YVelocity);
var absoluteInput = TopDownEntity.MovementInput?.Magnitude ?? 0;
if(shouldSet && !string.IsNullOrEmpty(configuration.MovementName))
{
shouldSet = configuration.MovementName == TopDownEntity.CurrentMovement?.Name;
}
shouldSet = shouldSet && (absoluteVelocity < configuration.MinVelocityAbsolute) == false;
shouldSet = shouldSet && (absoluteVelocity > configuration.MaxVelocityAbsolute) == false;
shouldSet = shouldSet && (absoluteInput < configuration.MinMovementInputAbsolute) == false;
shouldSet = shouldSet && (absoluteInput > configuration.MaxMovementInputAbsolute) == false;
if (shouldSet && configuration.AdditionalPredicate != null)
{
shouldSet = configuration.AdditionalPredicate();
}
if (shouldSet)
{
switch (configuration.AnimationSpeedAssignment)
{
case AnimationSpeedAssignment.ForceTo1:
{
var asSprite = this.AnimatedObject as FlatRedBall.Sprite;
if (asSprite != null)
{
asSprite.AnimationSpeed = 1;
}
}
break;
case AnimationSpeedAssignment.NoAssignment:
break;
case AnimationSpeedAssignment.BasedOnVelocityMultiplier:
{
var asSprite = this.AnimatedObject as FlatRedBall.Sprite;
if (configuration.AbsoluteVelocityAnimationSpeedMultiplier != null)
{
asSprite.AnimationSpeed = configuration.AbsoluteVelocityAnimationSpeedMultiplier.Value *
absoluteVelocity;
}
}
break;
case AnimationSpeedAssignment.BasedOnMaxSpeedRatioMultiplier:
{
var asSprite = this.AnimatedObject as FlatRedBall.Sprite;
if (asSprite != null)
{
if (configuration.MaxSpeedRatioMultiplier != null)
{
if (TopDownEntity.MaxSpeed == 0)
{
asSprite.AnimationSpeed = 1;
}
else
{
asSprite.AnimationSpeed = configuration.MaxSpeedRatioMultiplier.Value * absoluteVelocity / TopDownEntity.MaxSpeed;
}
}
}
}
break;
case AnimationSpeedAssignment.BasedOnInputMultiplier:
{
var asSprite = this.AnimatedObject as FlatRedBall.Sprite;
if (asSprite != null)
{
asSprite.AnimationSpeed = TopDownEntity.MovementInput.Magnitude;
}
}
break;
}
var toReturn = configuration.AnimationName;
if (configuration.IsDirectionFacingAppended)
{
toReturn += TopDownEntity.DirectionFacing.ToString();
}
return toReturn;
}
return null;
};
}
}
}
";
return toReturn;
}

}
5 changes: 3 additions & 2 deletions FRBDK/Glue/TopDownPlugin/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TopDownEntityViewModel GetViewModel()
if (viewModel == null)
{
viewModel = new TopDownEntityViewModel();
viewModel.PropertyChanged += HandleViewModelPropertyChange;
viewModel.PropertyChanged += HandleViewModelPropertyChanged;
}

return viewModel;
Expand All @@ -76,7 +76,7 @@ private void AddTopDownGlueVariables(EntitySave entity)
// property. But we'll just codegen that for now.
}

private async void HandleViewModelPropertyChange(object sender, PropertyChangedEventArgs e)
private async void HandleViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
/////////// early out ///////////
if (ignoresPropertyChanges)
Expand Down Expand Up @@ -158,6 +158,7 @@ await TaskManager.Self.AddAsync(
AiTargetLogicCodeGenerator.Self.GenerateAndSave();
AnimationCodeGenerator.Self.GenerateAndSave();
}
TopDownAnimationControllerGenerator.Self.GenerateAndSave();
}, "Generating all top-down code");
}

Expand Down
Loading

0 comments on commit 530ae61

Please sign in to comment.