Skip to content

Commit

Permalink
Everything is squared away nicely, just need to test before release.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirdoombox committed Apr 5, 2020
1 parent 3aa16e1 commit 1418c6c
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 57 deletions.
5 changes: 3 additions & 2 deletions BannerLib.Core/CoreSubModule.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.MountAndBlade;

namespace BannerLib.Core
{
public class CoreSubModule : MBSubModuleBase
{
protected override void OnSubModuleLoad()
protected override void OnBeforeInitialModuleScreenSetAsRoot()
{
InformationManager.DisplayMessage(new InformationMessage("BannerLib Loaded."));
InformationManager.DisplayMessage(new InformationMessage("Loaded BannerLib", Colors.Green));
}
}
}
5 changes: 0 additions & 5 deletions BannerLib.Gameplay/GameplaySubModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using System;
using System.Linq;
using BannerLib.Gameplay.Perks;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.MountAndBlade;
#pragma warning disable 1591

Expand Down
3 changes: 3 additions & 0 deletions BannerLib.Gameplay/ModelSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace BannerLib.Gameplay
{
/// <summary>
///
/// </summary>
public static class ModelSystem
{
/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions BannerLib.Gameplay/Perks/PerkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace BannerLib.Gameplay.Perks
{
/// <summary>
/// The base type that all perks should derive from.
/// </summary>
public class PerkBase
{
/// <summary>
Expand Down Expand Up @@ -53,7 +56,7 @@ public class PerkBase
public int PerkSkillRequirement { get; protected set; }

/// <summary>
/// Converts a <see cref="Perk"/> object to a <see cref="PerkObject"/> implicitly so it can be used in all the same places.
/// Converts a <see cref="PerkBase"/> object to a <see cref="PerkObject"/> implicitly so it can be used in all the same places.
/// </summary>
/// <param name="perk">Perk to convert.</param>
/// <returns>Internally stored PerkObject.</returns>
Expand All @@ -75,7 +78,10 @@ internal void InitWithPerkObject()
Skill = m_perkObject.Skill;
PerkSkillRequirement = (int)Math.Round(m_perkObject.RequiredSkillValue);
}


/// <summary>
/// This constructor is used purely to set up the object with defaults.
/// </summary>
protected internal PerkBase()
{
PrimaryBonus = default;
Expand Down
64 changes: 60 additions & 4 deletions BannerLib.Gameplay/Perks/PerkCreator.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;

namespace BannerLib.Gameplay.Perks
{
/// <summary>
/// The class used to register perks with the system.
/// </summary>
public class PerkCreator
{
private readonly string m_modName;
private readonly Game m_game;

/// <summary>
/// Initialize a new instance of the <see cref="PerkCreator"/> class for your mod.
/// </summary>
/// <param name="game">The game that you plan to add perks to.</param>
/// <param name="modName">The name of your mod, this should be constant, any changes between sessions will
/// cause issues.</param>
public PerkCreator(Game game, string modName)
{
m_game = game;
m_modName = modName;
}

/// <summary>
/// Register a perk with bannerlord.
/// </summary>
/// <param name="id">The ID of the perk, this should be unique and consistent, inconsistency between sessions
/// will cause problems.</param>
/// <typeparam name="T">The PerkBase derived class to create a perk from.</typeparam>
/// <returns>A new instance of your PerkBase derived class that is fully initialised.</returns>
public T Register<T>(string id) where T : PerkBase, new()
{
var registeredPerk = RegisterInternal(new T(), id);
InitializePerk(registeredPerk);
return registeredPerk;
}

/// <summary>
/// Register two perks with bannerlord that are alternatives to one another, so in the character screen you
/// would have to make a choice between the two.
/// </summary>
/// <param name="idFirst">The ID of the first perk, this should be unique and consistent, inconsistency
/// between sessions will cause problems.</param>
/// <param name="idSecond">The ID of the second perk, this should be unique and consistent, inconsistency
/// between sessions will cause problems.</param>
/// <typeparam name="TFirst">The first PerkBase derived class to create a perk from.</typeparam>
/// <typeparam name="TSecond">The second PerkBase derived class to create a perk from.</typeparam>
/// <returns>New instances of your PerkBase derived classes that are ready to use.</returns>
public (TFirst, TSecond) RegisterAlternatives<TFirst, TSecond>(string idFirst, string idSecond)
where TFirst : PerkBase, new() where TSecond : PerkBase, new()
{
Expand All @@ -32,7 +56,15 @@ public PerkCreator(Game game, string modName)
InitializePerk(registeredFirst, registeredSecond);
return (registeredFirst, registeredSecond);
}


/// <summary>
/// Registers a new perk as an alternative for one that already exists in the base game.
/// </summary>
/// <param name="id">The ID of the perk, this should be unique and consistent,
/// inconsistency between sessions will cause problems.</param>
/// <param name="existingPerk">A PerkObject that already exists - See <see cref="DefaultPerks"/> for a full list.</param>
/// <typeparam name="TNew">The PerkBase derived class to create a perk from.</typeparam>
/// <returns>A new instance of your PerkBase derived class that are ready to use, and a PerkBase for the existing perk.</returns>
public (TNew, PerkBase) RegisterAlternativeForExistingPerk<TNew>(string id, PerkObject existingPerk)
where TNew : PerkBase, new()
{
Expand All @@ -45,17 +77,41 @@ public PerkCreator(Game game, string modName)
return (registeredNew, existing);
}

/// <summary>
/// Functions just like <see cref="Register{T}(string)"/>
/// however the name of your PerkBase derived type is used as the ID.
/// </summary>
/// <typeparam name="T">The PerkBase derived class to create a perk from.</typeparam>
/// <returns>A new instance of your PerkBase derived class that is fully initialised.</returns>
public T Register<T>() where T : PerkBase, new() =>
Register<T>(typeof(T).Name);

/// <summary>
/// Functions just like <see cref="RegisterAlternatives{TFirst,TSecond}(string,string)"/>
/// however the names of your PerkBase derived types are used as the ID.
/// </summary>
/// <typeparam name="TFirst">The first PerkBase derived class to create a perk from.</typeparam>
/// <typeparam name="TSecond">The Second PerkBase derived class to create a perk from.</typeparam>
/// <returns>New instances of your PerkBase derived classes that are ready to use.</returns>
public (TFirst, TSecond) RegisterAlternatives<TFirst, TSecond>()
where TFirst : PerkBase, new() where TSecond : PerkBase, new() =>
RegisterAlternatives<TFirst, TSecond>(typeof(TFirst).Name, typeof(TSecond).Name);

/// <summary>
/// Functions just like <see cref="RegisterAlternativeForExistingPerk{TNew}(string,TaleWorlds.CampaignSystem.PerkObject)"/>
/// however the names of your PerkBase derived types are used as the ID.
/// </summary>
/// <param name="existingPerk">A PerkObject that already exists - See <see cref="DefaultPerks"/>
/// for a full list.</param>
/// <typeparam name="TNew">The PerkBase derived class to create a perk from.</typeparam>
/// <returns>A new instance of your PerkBase derived class that are ready to use, and a PerkBase for the existing perk.</returns>
public (TNew, PerkBase) RegisterAlternativeForExistingPerk<TNew>(PerkObject existingPerk)
where TNew : PerkBase, new() =>
RegisterAlternativeForExistingPerk<TNew>(typeof(TNew).Name, existingPerk);

/// <summary>
/// Call this once you're done registering all your perks.
/// </summary>
public void UpdatePerks()
{
foreach (var hero in Hero.All)
Expand Down
74 changes: 37 additions & 37 deletions BannerLib.Gameplay/Perks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@ Allows easily adding perks to a game with their existence and state being saved

```csharp
public class ExampleSubModule : MBSubModuleBase
{
// Run this once the game has finished initialization.
public override void OnGameInitializationFinished(Game game)
{
// Run this once the game has finished initialization.
public override void OnGameInitializationFinished(Game game)
{
// Only run this if we're in the campaign.
if (!(game.GameType is Campaign)) return ;
// Initialize a PerkCreator for your mod.
var perks = new PerkCreator(game, "TestMod");
// Register two perks which are treated as alternatives.
perks.RegisterAlternatives<TestingPerk, TestingAlternate>();
// Call this once you're done adding perks.
perks.UpdatePerks();
}
// Only run this if we're in the campaign.
if (!(game.GameType is Campaign)) return ;
// Initialize a PerkCreator for your mod.
var perks = new PerkCreator(game, "TestMod");
// Register two perks which are treated as alternatives.
perks.RegisterAlternatives<TestingPerk, TestingAlternate>();
// Call this once you're done adding perks.
perks.UpdatePerks();
}
}


// Creating a perk type is as easy as this, derive from perkbase and in the constructor
// Set all the properties you need.
// You can freely add extra functionality to your perk for your own use.
public class TestingPerk : PerkBase
// Creating a perk type is as easy as this, derive from perkbase and in the constructor
// Set all the properties you need.
// You can freely add extra functionality to your perk for your own use.
public class TestingPerk : PerkBase
{
public TestingPerk()
{
public TestingPerk()
{
Name = "Testing Perk One";
PrimaryBonus = 10;
Description = $"Deal {PrimaryBonus} Extra Damage.";
PrimaryRole = SkillEffect.PerkRole.PartyMember;
EffectType = SkillEffect.EffectIncrementType.Add;
Skill = DefaultSkills.Bow;
PerkSkillRequirement = 0;
}
Name = "Testing Perk One";
PrimaryBonus = 10;
Description = $"Deal {PrimaryBonus} Extra Damage.";
PrimaryRole = SkillEffect.PerkRole.PartyMember;
EffectType = SkillEffect.EffectIncrementType.Add;
Skill = DefaultSkills.Bow;
PerkSkillRequirement = 0;
}
}

public class TestingAlternate : PerkBase
public class TestingAlternate : PerkBase
{
public TestingAlternate()
{
public TestingAlternate()
{
Name = "Testing Perk Two";
PrimaryBonus = 100;
Description = $"Deal {PrimaryBonus} Less Damage.";
PrimaryRole = SkillEffect.PerkRole.PartyMember;
EffectType = SkillEffect.EffectIncrementType.Add;
Skill = DefaultSkills.Bow;
PerkSkillRequirement = 0;
}
Name = "Testing Perk Two";
PrimaryBonus = 100;
Description = $"Deal {PrimaryBonus} Less Damage.";
PrimaryRole = SkillEffect.PerkRole.PartyMember;
EffectType = SkillEffect.EffectIncrementType.Add;
Skill = DefaultSkills.Bow;
PerkSkillRequirement = 0;
}
}
```

47 changes: 40 additions & 7 deletions BannerLib.Misc/InquiryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using TaleWorlds.Core;

namespace BannerLib
namespace BannerLib.Misc
{
/// <summary>
///
/// </summary>
public class InquiryBuilder
{
private readonly string m_title;
Expand All @@ -18,26 +21,48 @@ private InquiryBuilder(string title)
{
m_title = title;
}


/// <summary>
/// Create a new InquiryBuilder.
/// </summary>
/// <param name="title">The title that will be displayed at the top of the inquiry.</param>
/// <returns>This <see cref="InquiryBuilder"/> for chaining method calls.</returns>
public static InquiryBuilder Create(string title)
{
return new InquiryBuilder(title);
}


/// <summary>
/// Adds description text to an inquiry.
/// </summary>
/// <param name="descriptionText">The description text that will be shown.</param>
/// <returns>This <see cref="InquiryBuilder"/> for chaining method calls.</returns>
public InquiryBuilder WithDescription(string descriptionText)
{
m_descriptionText = descriptionText;
return this;
}


/// <summary>
/// While every Inquiry has an Affirmative, this allows you to set what happens when it is clicked and what the text on the button is.
/// </summary>
/// <param name="onAffirmative">Callback called when the player clicks the affirmative button.</param>
/// <param name="affirmativeText">The text displayed on the affirmative button.</param>
/// <returns>This <see cref="InquiryBuilder"/> for chaining method calls.</returns>
public InquiryBuilder WithAffirmative(Action onAffirmative, string affirmativeText = "")
{
if (!string.IsNullOrWhiteSpace(affirmativeText)) m_affirmativeText = affirmativeText;
m_affirmativeAction = onAffirmative ??
throw new ArgumentException("Action should not be null.", nameof(onAffirmative));
return this;
}


/// <summary>
/// Adds a negative button to the inquiry.
/// </summary>
/// <param name="onNegative">Callback called when the player clicks the negative button.</param>
/// <param name="negativeText">The text displayed on the negative button.</param>
/// <returns>This <see cref="InquiryBuilder"/> for chaining method calls.</returns>
public InquiryBuilder WithNegative(Action onNegative, string negativeText = "")
{
if (!string.IsNullOrWhiteSpace(negativeText)) m_negativeText = negativeText;
Expand All @@ -46,13 +71,21 @@ public InquiryBuilder WithNegative(Action onNegative, string negativeText = "")
m_withNegative = true;
return this;
}


/// <summary>
/// Builds the <see cref="InquiryData"/> up for use in <see cref="InformationManager.ShowInquiry"/>
/// </summary>
/// <returns>The built <see cref="InquiryData"/></returns>
public InquiryData Build()
{
return new InquiryData(m_title, m_descriptionText, c_WITH_AFFIRMATIVE, m_withNegative, m_affirmativeText,
m_negativeText, m_affirmativeAction, m_negativeAction);
}


/// <summary>
/// Builds the <see cref="InquiryData"/> and immediately displays it in-game.
/// </summary>
/// <param name="pauseGameActiveState">Should the game be paused whilst your inquiry is being displayed?</param>
public void BuildAndPublish(bool pauseGameActiveState)
{
InformationManager.ShowInquiry(Build(), pauseGameActiveState);
Expand Down
2 changes: 2 additions & 0 deletions BannerLib.Misc/MiscSubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BannerLib.Misc
{
#pragma warning disable 1591
public class MiscSubModule : MBSubModuleBase
#pragma warning restore 1591
{

}
Expand Down

0 comments on commit 1418c6c

Please sign in to comment.