SHARE
TWEET

Unity RPG-style Level System

Maurice_dachef May 30th, 2020 590 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class LevelSystem
  7. {
  8.     GameObject player;
  9.     PlayerTamer playerTamer;
  10.     const int MAX_LEVEL = 100;
  11.  
  12.     public LevelSystem(GameObject thePlayer)
  13.     {
  14.         player = thePlayer;
  15.         playerTamer = player.GetComponentInChildren<PlayerTamer>();
  16.     }
  17.  
  18.     public void SetStartingExperience()
  19.     {
  20.         playerTamer.lvl = 1;
  21.         playerTamer.expn = GetExperienceNeeded(playerTamer.lvl);
  22.         return;
  23.     }
  24.  
  25.     public void AddExperience(int amount)
  26.     {
  27.         playerTamer.exp += amount;
  28.         playerTamer.totalExp += amount;
  29.         ExperienceCheck();
  30.     }
  31.  
  32.     private void ExperienceCheck()
  33.     {
  34.         if(playerTamer.exp >= playerTamer.expn)
  35.         {
  36.             LevelUp();
  37.         }
  38.         else
  39.         {
  40.             return;
  41.         }
  42.     }
  43.  
  44.     private void LevelUp()
  45.     {
  46.         if (playerTamer.lvl == MAX_LEVEL)
  47.         {
  48.             return;
  49.         }
  50.         if (playerTamer.lvl != MAX_LEVEL && playerTamer.exp >= playerTamer.expn)
  51.         {
  52.             int ap = 3;
  53.             playerTamer.lvl += 1;
  54.             playerTamer.exp -= playerTamer.expn;
  55.             playerTamer.expn = GetExperienceNeeded(playerTamer.lvl);
  56.             playerTamer.attributePoints += ap;
  57.             Debug.Log("Player gained a level! Player is now Level " + playerTamer.lvl);
  58.             ExperienceCheck();
  59.         }
  60.     }
  61.  
  62.     private int GetExperienceNeeded(int level)
  63.     {
  64.         double baseXP = 500.0;
  65.         double exponent = 2.0;
  66.         level++;
  67.         int newExp = (int)Math.Ceiling(baseXP * (Math.Pow((double)level, exponent)) - (baseXP * (double)level));
  68.         return newExp;
  69.     }
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top