Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ExperienceLevelAPI
- {
- #region Enumerators for Switchers
- /// <summary>
- /// Select if you want your character can reach level zero or not, see whats fits better in your lvl formula
- /// </summary>
- public enum AllowLevelZero
- {
- /// <summary>
- /// Operations made will permit your character reach from or some moment come back to level Zero
- /// </summary>
- allowLevelZero,
- /// <summary>
- /// Operations made will never permit your character reach level zero
- /// </summary>
- dontAllowLevelZero
- }
- /// <summary>
- /// Select if you want your character can reach level zero or not, see whats fits better in your lvl formula
- /// </summary>
- public enum AllowXPZero
- {
- /// <summary>
- /// Operations made will permit your character reach from or some moment come back to level Zero
- /// </summary>
- allowXPZero,
- /// <summary>
- /// Operations made will never permit your character reach level zero
- /// </summary>
- dontAllowXPZero
- }
- #endregion
- /// <summary>
- /// Create level and experience manager for new characters
- /// </summary>
- public class Character
- {
- /// <summary>
- /// Select if you want your character can reach level zero or not, see whats fits better in your lvl formula
- /// </summary>
- public AllowLevelZero allowLevelZero;
- /// <summary>
- /// Select if you want your character can reach level zero or not, see whats fits better in your lvl formula
- /// </summary>
- public AllowXPZero allowXPZero;
- /// <summary>
- /// Level update action > Insert the desired formula inside (Make the method using
- /// the Experience_Current and the ConstantValue/ConstantsValues to reach next lvl)
- /// This action will be called inside LevelUpdateEvent() method and will return the long Level_Current
- /// for the operations
- /// </summary>
- public Action OnLevelUpdate = () => { };
- /// <summary>
- /// Experience update action > Insert the desired formula inside (Make the method using
- /// Level_Current and the constants to update your XP)
- /// This action will be called inside ExperienceUpdateEvent() method and will return the Experience_Current
- /// </summary>
- public Action OnExperienceUpate = () => { };
- /// <summary>
- /// Recomended use the same formula you used to update your lvl, just swap the Level_Current value on the formula
- /// by the Temp_ArbitraryLvl
- /// </summary>
- public Action OnCalculateArbitraryLevel = () => { };
- /// <summary>
- /// Recomended use the same formula you used to update your experience, just swap the Level_Current value on the formula
- /// by the Temp_ArbitraryExperience
- /// </summary>
- public Action OnCalculateArbitraryExperience = () => { };
- #region Variables
- //***get and set current level
- private long m_Level;
- /// <summary>
- /// Actual Level of your character to Get and Set must be used on the lvl calc formula and used on the character
- /// </summary>
- public long Level_Current
- {
- get { return m_Level; }
- set
- {
- long v = value;
- if (m_Level != v)
- m_Level = v;
- }
- }
- private long m_LevelArbitrary;
- /// <summary>
- /// Arbitrary Level of your character to Get and Set must be used on the character
- /// </summary>
- public long Level_Arbitrary
- {
- get { return m_LevelArbitrary; }
- set
- {
- long v = value;
- if (m_LevelArbitrary != v)
- m_LevelArbitrary = v;
- }
- }
- //*** get current experience
- private long m_experience;
- /// <summary>
- /// Actual xp of your character to Get and Set
- /// </summary>
- public long Experience_Current
- {
- get { return m_experience; }
- set
- {
- long v = value;
- if (m_experience != v)
- m_experience = v;
- }
- }
- private long m_ArbitraryExperience;
- /// <summary>
- /// Arbitrary xp of your character to Get and Set
- /// </summary>
- public long Experience_Arbitrary
- {
- get { return m_ArbitraryExperience; }
- set
- {
- long v = value;
- if (m_ArbitraryExperience != v)
- m_ArbitraryExperience = v;
- }
- }
- //To handle single constants
- private float m_constant;
- /// <summary>
- /// Single constant handler for your project (Recomended value 0.4f)
- /// </summary>
- public float ConstantValue
- {
- get { return m_constant; }
- set
- {
- float v = value;
- if (m_constant != v)
- m_constant = v;
- }
- }
- private long m_tempPreviousLvl;
- /// <summary>
- /// When lvl updates store the previous lvl
- /// </summary>
- private long Temp_PreviousLevel
- {
- get { return m_tempPreviousLvl; }
- set
- {
- long v = value;
- if (m_tempPreviousLvl != v)
- m_tempPreviousLvl = v;
- }
- }
- private long m_maxExperienceToReachNextLevel;
- /// <summary>
- /// Maximum xp to reach an arbitrary level
- /// </summary>
- public long MaxExperienceToReachDesiredLevel
- {
- get { return m_maxExperienceToReachNextLevel; }
- set
- {
- long v = value;
- if (m_maxExperienceToReachNextLevel != v)
- m_maxExperienceToReachNextLevel = v;
- }
- }
- private long m_experienceRemainingToReachNextLevel;
- /// <summary>
- /// Debug your current lvl see how many xp you need to reach the next
- /// </summary>
- /// <returns>xp needed to your next lvl up</returns>
- public long ExperienceRemainingToReachNextLevelDebug
- {
- get
- {
- return m_experienceRemainingToReachNextLevel;
- }
- set
- {
- long v = value;
- if (m_experienceRemainingToReachNextLevel != v)
- m_experienceRemainingToReachNextLevel = v;
- }
- }
- private long m_experienceRemainingToReachArbitraryLevel;
- /// <summary>
- /// From your current lvl see how many xp you need to reach the an arbitrary lvl
- /// </summary>
- /// <returns>xp needed to you reach that lvl</returns>
- private long ExperienceRemainingToReachArbitraryLevel
- {
- get { return m_experienceRemainingToReachArbitraryLevel; }
- set
- {
- long v = value;
- if (m_experienceRemainingToReachArbitraryLevel != v)
- m_experienceRemainingToReachArbitraryLevel = v;
- }
- }
- private long m_experienceToReachArbitraryLevel;
- /// <summary>
- ///Total XP to an arbitrary lvl
- /// </summary>
- /// <returns>xp needed to you reach that lvl</returns>
- private long ExperienceToReachArbitraryLevel
- {
- get { return m_experienceToReachArbitraryLevel; }
- set
- {
- long v = value;
- if (m_experienceToReachArbitraryLevel != v)
- m_experienceToReachArbitraryLevel = v;
- }
- }
- private float m_percentualProgressForTheNextLvl;
- /// <summary>
- /// Stores the float percentage of the value to reach next lvl
- /// </summary>
- private float PercentualProgressToNextLevel
- {
- get { return m_percentualProgressForTheNextLvl; }
- set
- {
- float v = value;
- if (m_percentualProgressForTheNextLvl != v)
- m_percentualProgressForTheNextLvl = v;
- }
- }
- private long m_levelDesiredToDeltaCall;
- /// <summary>
- /// Stores the lvl reference to calculate the delta between current lvl and the next
- /// </summary>
- public long LevelDisiredToDeltaCall
- {
- get { return m_levelDesiredToDeltaCall; }
- set
- {
- long v = value;
- if (m_levelDesiredToDeltaCall != v)
- m_levelDesiredToDeltaCall = v;
- }
- }
- #endregion
- #region Methods
- ///**************METHODS***************\\\
- /// <summary>
- /// Level Update event (the action with the method for the lvl update runs inside this method)
- /// </summary>
- /// <returns></returns>
- public long LevelUpdateEvent()
- {
- //If the player level is less than 2 method will run an check to see if you can reach lvl/xp 0
- if (Level_Current <= 2)
- {
- //Check if player can reach level zero
- switch (allowLevelZero)
- {
- case AllowLevelZero.allowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches lvl 0
- break;
- case AllowLevelZero.dontAllowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches lvl 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches lvl 0
- break;
- }
- //Check if player can reach xp zero
- switch (allowXPZero)
- {
- case AllowXPZero.allowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches xp 0
- break;
- case AllowXPZero.dontAllowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches xp 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches xp 0
- break;
- }
- }
- OnLevelUpdate(); //Calls the lvl update action with the method inside
- return Level_Current;
- }
- /// <summary>
- /// XP Update event (the action with the method for the lvl update runs inside this method)
- /// </summary>
- /// <returns></returns>
- public long ExperienceUpdateEvent()
- {
- //If the player level is less than 2 method will run an check to see if you can reach lvl/xp 0
- if (Level_Current <= 2)
- {
- //Check if player can reach level zero
- switch (allowLevelZero)
- {
- case AllowLevelZero.allowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches lvl 0
- break;
- case AllowLevelZero.dontAllowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches lvl 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches lvl 0
- break;
- }
- //Check if player can reach xp zero
- switch (allowXPZero)
- {
- case AllowXPZero.allowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches xp 0
- break;
- case AllowXPZero.dontAllowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches xp 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches xp 0
- break;
- }
- }
- OnExperienceUpate(); //Calls the xp update action with the method inside
- return Experience_Current;
- }
- /// <summary>
- /// Arbitrary XP Update event (the action with the method for the lvl update runs inside this method)
- /// </summary>
- /// <returns></returns>
- public long ArbitraryExperienceUpdateEvent()
- {
- //If the player level is less than 2 method will run an check to see if you can reach lvl/xp 0
- if (Level_Current <= 2)
- {
- //Check if player can reach level zero
- switch (allowLevelZero)
- {
- case AllowLevelZero.allowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches lvl 0
- break;
- case AllowLevelZero.dontAllowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches lvl 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches lvl 0
- break;
- }
- //Check if player can reach xp zero
- switch (allowXPZero)
- {
- case AllowXPZero.allowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches xp 0
- break;
- case AllowXPZero.dontAllowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches xp 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches xp 0
- break;
- }
- }
- OnCalculateArbitraryExperience(); //Calls the xp update action with the method inside
- return Experience_Arbitrary;
- }
- /// <summary>
- /// Arbitrary XP Update event (the action with the method for the lvl update runs inside this method)
- /// </summary>
- /// <returns></returns>
- public long ArbitraryLevelUpdateEvent()
- {
- //If the player level is less than 2 method will run an check to see if you can reach lvl/xp 0
- if (Level_Current <= 2)
- {
- //Check if player can reach level zero
- switch (allowLevelZero)
- {
- case AllowLevelZero.allowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches lvl 0
- break;
- case AllowLevelZero.dontAllowLevelZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches lvl 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches lvl 0
- break;
- }
- //Check if player can reach xp zero
- switch (allowXPZero)
- {
- case AllowXPZero.allowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 0; //Allow player reaches xp 0
- break;
- case AllowXPZero.dontAllowXPZero:
- if (Experience_Current <= 0)
- Experience_Current = 1; //Dont Allow player reaches xp 0
- break;
- default:
- if (Experience_Current <= 0)
- Experience_Current = 1; // By default dont allow player reaches xp 0
- break;
- }
- }
- OnCalculateArbitraryLevel(); //Calls the xp update action with the method inside
- return Level_Arbitrary;
- }
- /// <summary>
- /// Add 1 more lvl
- /// </summary>
- /// <param name="lvl">lvl sum value (If you want lvl Up 1 lvl insert 1 here)</param>
- /// <returns></returns>
- public long Level_Add(int lvl)
- {
- m_tempPreviousLvl = Level_Current; //Stores previous lvl before it updates
- Level_Current += lvl; //Sums lvl points to enter on new lvl
- ExperienceUpdateEvent(); //Runs the XP event formula
- return Level_Current; //Return current lvl
- }
- /// <summary>
- /// Subtract 1 lvl
- /// </summary>
- /// <param name="lvl">lvl sub value (If you want make character lost 1 level, insert 1 here)</param>
- /// <returns></returns>
- public long Level_Sub(int lvl)
- {
- m_tempPreviousLvl = Level_Current; //Stores previous lvl before it updates
- Level_Current -= lvl; //Subtract lvl points to enter on new lvl
- ExperienceUpdateEvent(); //Runs the XP event formula
- return Level_Current; //Return current lvl
- }
- /// <summary>
- /// Add Aquired XP value
- /// </summary>
- /// <param name="xp_SumVal">total XP aquired</param>
- /// <returns></returns>
- public long Experience_Add(long xp_SumVal)
- {
- Experience_Current += xp_SumVal; //Sums first and than run LevelUpChecker cause without it LevelUpChecker() will run only beReamininge it return, than if the lvl required is == to current lvl he will not lvl up in correct time, only after
- LevelUpdateEvent(); //Check if character can lvl up
- return Experience_Current;
- }
- /// <summary>
- /// Total XP losted
- /// </summary>
- /// <param name="xp_SumVal">XP lost value</param>
- /// <returns></returns>
- public long Experience_Reduce(long xp_SumVal)
- {
- Experience_Current -= xp_SumVal; //Sub first and than run LevelUpChecker cause without it LevelUpChecker() will run only beReamininge it return, than if the lvl required is == to current lvl he will not lvl up in correct time, only after
- LevelUpdateEvent(); //Check if character can lvl up
- return Experience_Current;
- }
- /// <summary>
- /// Reset Lvl and XP
- /// </summary>
- /// <returns></returns>
- public long Reset()
- {
- Experience_Current = 0;
- LevelUpdateEvent();
- return Experience_Current;
- }
- /// <summary>
- /// Reset experience to the last lvl up
- /// </summary>
- public void ResetXPToLastLvlUp()
- {
- ExperienceUpdateEvent();
- //MAYBE DO AN IF STATEMENT CHECKING IF THE XP VALUE AND LVL IS CORRECT WITH THE LAST LVL UP IF NOT FORCE TO BE
- }
- /// <summary>
- /// Update event to calculate the total xp needed for the next lvl
- /// </summary>
- /// <returns>Max experience neeeded to reach your next lvl</returns>
- public long NextLevelCalculateEvent()
- {
- Level_Arbitrary = Level_Current + 1;
- ArbitraryExperienceUpdateEvent();
- return Experience_Arbitrary - Experience_Current;
- }
- /// <summary>
- /// Get experience value Reamining to an arbitrary level value (The parameter must be greater than current lvl
- /// Or it will return zero
- /// </summary>
- /// <param name="lvlDesireToReach">Insert the desired lvl for the calculation</param>
- /// <returns></returns>
- public long ExperienceNeededToAnArbitraryLevelValueFromYourLvl(long lvlDesireToReach)
- {
- Level_Arbitrary = lvlDesireToReach; //Stores currently lvl desired to reach in the arbitrary lvl to make the calculation
- OnCalculateArbitraryExperience(); //Calculates arbitrary xp debugs the xp necessary in Experience_Arbitrary
- MaxExperienceToReachDesiredLevel = Experience_Arbitrary;//Put the total xp value in this variable to make the account
- ExperienceRemainingToReachArbitraryLevel = MaxExperienceToReachDesiredLevel - Experience_Current; //Sub this total by your current XP
- return ExperienceRemainingToReachArbitraryLevel; //Return the diference between your XP and the desired lvl
- }
- /// <summary>
- /// Get experience value to an arbitrary level value (The parameter must be greater than 0
- /// </summary>
- /// <param name="lvlDesireToReach">Insert the desired lvl for the calculation</param>
- /// <returns>ExperienceToReachArbitraryLevel</returns>
- public long ExperienceNeededToAnArbitraryLevelValue(long lvlDesireToReach)
- {
- //Security logic validator
- if (lvlDesireToReach <= 0)
- return 0;
- Level_Arbitrary = lvlDesireToReach;
- OnCalculateArbitraryExperience();
- ExperienceToReachArbitraryLevel = ArbitraryExperienceUpdateEvent(); //Receive the value
- return ExperienceToReachArbitraryLevel;
- }
- /// <summary>
- /// Get LVL value Reamining to an arbitrary XP value (The parameter must be greater than 0)
- /// </summary>
- /// <param name="xpDesireToReach">Insert the desired lvl for the calculation</param>
- /// <returns>Level_Arbitrary</returns>
- public long LevelNeededToAnArbitraryExperienceValue(long xpDesireToReach)
- {
- //Security logic validator
- if (xpDesireToReach <= 0)
- return 0;
- Experience_Arbitrary = xpDesireToReach;
- OnCalculateArbitraryLevel();
- return Level_Arbitrary;
- }
- /// <summary>
- /// Get percentual experience progress between last and next level-up
- /// </summary>
- /// <returns>PercentualProgressToNextLevel</returns>
- public float PercentualProgress()
- {
- long tempLvl = Level_Current + 1;
- int maxPointsOfCurrentLevel = (int)ExperienceNeededToAnArbitraryLevelValue(Level_Current);
- int maxPointsToReachNextLevel = (int)ExperienceNeededToAnArbitraryLevelValue(tempLvl);
- //Prepare the variables for the calculation of percentage remaining
- float subtractedValueOfTheLastLvl = (float)maxPointsToReachNextLevel - (float)Experience_Current;
- float xpBetweenCurAndNextLevel = (float)maxPointsToReachNextLevel - (float)maxPointsOfCurrentLevel;
- PercentualProgressToNextLevel = (subtractedValueOfTheLastLvl / xpBetweenCurAndNextLevel) * 100f;
- return PercentualProgressToNextLevel;
- }
- /// <summary>
- /// Get experience delta between current experience and an arbitrary level value
- /// </summary>
- /// <param name="lvl">lvl desired to calculate</param>
- /// <returns></returns>
- public long ReturnDelta_BetweenXPAndArbitraryLevel(long lvl)
- {
- Level_Arbitrary = lvl;
- ArbitraryExperienceUpdateEvent();
- long subtractedValueOfTheLastLvl = ExperienceNeededToAnArbitraryLevelValue(Level_Arbitrary) - Experience_Current;
- LevelDisiredToDeltaCall = subtractedValueOfTheLastLvl;
- return LevelDisiredToDeltaCall;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement