Skip to content

Commit

Permalink
Added support for logo and intro animations;
Browse files Browse the repository at this point in the history
  • Loading branch information
kdobrzynski committed Apr 14, 2021
1 parent 1dcfb82 commit d25c57c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
18 changes: 14 additions & 4 deletions Negum.Core/Loaders/AbstractLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ protected virtual IEnumerable<FileInfo> GetFiles(IEngine engine, string subdirec
protected virtual DirectoryInfo GetDirectory(IEngine engine, string subdirectoryName)
{
var rootDir = new DirectoryInfo(engine.Path);

if (string.IsNullOrWhiteSpace(subdirectoryName))
{
return rootDir;
}

var subDir = rootDir
.GetDirectories()
.FirstOrDefault(dir => dir.Name.ToLower().Equals(subdirectoryName.ToLower()));

return subDir;
}

Expand Down Expand Up @@ -66,10 +66,20 @@ protected virtual async Task<TManager> ReadManagerAsync<TManager>(FileInfo file)
where TManager : IManager =>
await this.ReadManagerAsync<TManager>(file.FullName);

protected virtual async Task<TManager> ReadManagerAsync<TManager, TReader>(FileInfo file)
where TManager : IManager
where TReader : IConfigurationReader =>
await this.ReadManagerAsync<TManager, TReader>(file.FullName);

protected virtual async Task<TManager> ReadManagerAsync<TManager>(string path)
where TManager : IManager =>
await this.ReadManagerAsync<TManager, IConfigurationWithSubsectionReader>(path);

protected virtual async Task<TManager> ReadManagerAsync<TManager, TReader>(string path)
where TManager : IManager
where TReader : IConfigurationReader
{
var configReader = NegumContainer.Resolve<IConfigurationWithSubsectionReader>();
var configReader = NegumContainer.Resolve<TReader>();
var configuration = await configReader.ReadAsync(path);

return (TManager) NegumContainer.Resolve<TManager>().UseConfiguration(configuration);
Expand Down
2 changes: 2 additions & 0 deletions Negum.Core/Loaders/IDataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public async Task<IData> LoadAsync(IEngine engine)
{
var motifLogoFullPath = this.FindFile(motifFile.Directory, motifLogoPath);
data.LogoManager = await this.ReadManagerAsync<IStoryboardManager>(motifLogoFullPath);
data.LogoAnimationManager = await this.ReadManagerAsync<IAnimationManager, IAnimationReader>(motifLogoFullPath);
}

var motifIntroPath = data.MotifManager.Files.IntroStoryboardDefinition;
Expand All @@ -72,6 +73,7 @@ public async Task<IData> LoadAsync(IEngine engine)
{
var motifIntroFullPath = this.FindFile(motifFile.Directory, motifIntroPath);
data.IntroManager = await this.ReadManagerAsync<IStoryboardManager>(motifIntroFullPath);
data.IntroAnimationManager = await this.ReadManagerAsync<IAnimationManager, IAnimationReader>(motifIntroFullPath);
}

return data;
Expand Down
4 changes: 2 additions & 2 deletions Negum.Core/Managers/IManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public TManagerSection GetSection<TManagerSection>(string sectionName)
public IEnumerable<TManagerSection> GetSections<TManagerSection>(string sectionName)
where TManagerSection : IManagerSection =>
this.Config
.Where(section => section.Name.Equals(sectionName))
.Where(section => section.Name.ToLower().Equals(sectionName.ToLower()))
.Select(section => this.GetNewManagerSection(section.Name, section))
.Cast<TManagerSection>()
.ToList();

public IEnumerable<TManagerSection> GetSubsections<TManagerSection>(string sectionName)
where TManagerSection : IManagerSection =>
this.Config
.FirstOrDefault(section => section.Name.StartsWith(sectionName))
.FirstOrDefault(section => section.Name.ToLower().StartsWith(sectionName.ToLower()))
.Subsections
.Select(subsection => this.GetNewManagerSection(subsection.Name, subsection))
.Cast<TManagerSection>()
Expand Down
12 changes: 12 additions & 0 deletions Negum.Core/Models/Data/IData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,20 @@ public interface IData
/// </summary>
IStoryboardManager LogoManager { get; }

/// <summary>
/// Manager which wraps logo animation.
/// </summary>
IAnimationManager LogoAnimationManager { get; }

/// <summary>
/// Manager which wraps intro definition.
/// </summary>
IStoryboardManager IntroManager { get; }

/// <summary>
/// Manager which wraps intro animation.
/// </summary>
IAnimationManager IntroAnimationManager { get; }
}

/// <summary>
Expand All @@ -69,6 +79,8 @@ public class NegumData : IData
public ISelectionManager SelectionManager { get; internal set; }
public IFightManager FightManager { get; internal set; }
public IStoryboardManager LogoManager { get; internal set; }
public IAnimationManager LogoAnimationManager { get; internal set; }
public IStoryboardManager IntroManager { get; internal set; }
public IAnimationManager IntroAnimationManager { get; internal set; }
}
}
21 changes: 13 additions & 8 deletions Negum.Core/Readers/IAnimationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,33 @@ public class AnimationReader : ConfigurationReader, IAnimationReader
protected override void InitializeConfigurationSection(IConfiguration configuration,
IConfigurationSection section)
{
if (!section.Name.ToLower().StartsWith(SectionName.ToLower()))
{
return;
}

var animationSection = new AnimationSection();

this.ParseAnimationSectionData(section.Name, animationSection);
this.ParseCollisions(section, animationSection);

configuration.AddSection(animationSection);
}

protected virtual void ParseAnimationSectionData(string sectionName, AnimationSection animationSection)
{
// Technically anything else should be understand as an error.
animationSection.Name = SectionName;

var numberString = sectionName.Split(" ");
var number = int.Parse(numberString[^1]);
var number = int.Parse(numberString[^1]); // We want to make sure that it is a valid number
animationSection.ActionNumber = number;
}

protected virtual void ParseCollisions(IConfigurationSection section, AnimationSection animationSection)
{
AnimationSectionEntry animationEntry = null;

foreach (var entry in section)
{
if (entry.Value.StartsWith("Clsn") && entry.Value.Contains(":"))
Expand All @@ -57,7 +62,7 @@ protected virtual void ParseCollisions(IConfigurationSection section, AnimationS
{
animationSection.Entries.Add(animationEntry);
}

animationEntry = new AnimationSectionEntry();
this.ParseHeaderData(animationEntry, entry.Value);
}
Expand All @@ -71,11 +76,11 @@ protected virtual void ParseCollisions(IConfigurationSection section, AnimationS
{
animationEntry = new AnimationSectionEntry();
}

this.ParseAnimationData(animationEntry, entry.Value);
}
}

if (animationEntry != null)
{
animationSection.Entries.Add(animationEntry);
Expand All @@ -88,7 +93,7 @@ protected virtual void ParseHeaderData(AnimationSectionEntry animationEntry, str
animationEntry.TypeId = int.Parse(value.Substring(4, 1));
}

protected virtual void ParseBoxData(AnimationSectionEntry animationEntry, string value) =>
protected virtual void ParseBoxData(AnimationSectionEntry animationEntry, string value) =>
animationEntry.AddBox(value);

protected virtual void ParseAnimationData(AnimationSectionEntry animationEntry, string value)
Expand Down
3 changes: 1 addition & 2 deletions Negum.Core/Readers/IConfigurationWithSubsectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ protected virtual bool IsSubsection(IConfigurationSection lastSection, IConfigur
return false;
}

// COMMENT: -3 indicates "def" postfix
var lastSectionNamePrefix = lastSection.Name[..^3];
var lastSectionNamePrefix = lastSection.Name.ToLower().EndsWith("def") ? lastSection.Name[..^3] : lastSection.Name;

return section.Name.StartsWith(lastSectionNamePrefix);
}
Expand Down
15 changes: 14 additions & 1 deletion Tests/Negum.Core.Tests/Managers/StoryboardManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace Negum.Core.Tests.Managers
public class StoryboardManagerTests : TestBase
{
[Theory]
[InlineData("https://raw.githubusercontent.com/TheNegumProject/DragonBallMugenEdition2009/main/data/Backup/intro.def")]
[InlineData(
"https://raw.githubusercontent.com/TheNegumProject/DragonBallMugenEdition2009/main/data/Backup/intro.def")]
public async Task Should_Count_Number_Of_Scenes_And_Print_Details(string path)
{
this.InitializeContainer();
Expand All @@ -30,6 +31,18 @@ public async Task Should_Count_Number_Of_Scenes_And_Print_Details(string path)
var timeDetails = time.GetTimeSpan().Ticks;

Assert.True(timeDetails == 35);

var introConfig = await this.ParseAnimation(path);
var introManager = (IAnimationManager) NegumContainer.Resolve<IAnimationManager>().UseConfiguration(introConfig);
var actions = introManager.Animations;

Assert.True(actions.Count() == 3);
Assert.True(actions.ElementAt(0).ActionNumber == 0);
Assert.True(actions.ElementAt(0).Parts.Count() == 1);
Assert.True(actions.ElementAt(1).ActionNumber == 1);
Assert.True(actions.ElementAt(1).Parts.Count() == 1);
Assert.True(actions.ElementAt(2).Parts.Count() == 1);
Assert.True(actions.ElementAt(2).Parts.ElementAt(0).Frames.Count() == 2301);
}
}
}

0 comments on commit d25c57c

Please sign in to comment.