Guest User

Untitled

a guest
Feb 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.    
  3.     import flash.display.MovieClip;
  4.  
  5.     public class LevelSystem extends MovieClip {
  6.        
  7.         const MAX_LEVEL = 50;
  8.         const XP_INCREMENT = 500;
  9.        
  10.         public var int iXP; // Total amount of gathered XP
  11.         public var int iLevel; // Current level
  12.         public var int iXPGatheredForNextLevel; // Amount of XP gathered for the next level
  13.         public var int iXPRequiredForNextLevel; // Amount of XP required for the next level
  14.        
  15.         public function LevelSystem {
  16.             CalculateLevelProgress();
  17.         }
  18.        
  19.         public function GiveXP(int amount) {
  20.             XP += amount;
  21.  
  22.             CalculateLevelProgress();
  23.  
  24.             while (XPGatheredForNextLevel >= XPRequiredForNextLevel && Level < MAX_LEVEL) {
  25.                 Level++;
  26.            
  27.                 // Recalculate level progress after leveling up
  28.                 CalculateLevelProgress();
  29.             }
  30.         }
  31.        
  32.         public function CalculateLevelProgress() {
  33.             local int xpToCurrentLevel; // Total amount of XP gathered with current and previous levels
  34.            
  35.             xpToCurrentLevel = 0.5 * Level * (Level-1) * XP_INCREMENT;
  36.             XPGatheredForNextLevel = XP - xpToCurrentLevel;
  37.             XPRequiredForNextLevel = Level * XP_INCREMENT;
  38.         }
  39.     }
  40. }
Add Comment
Please, Sign In to add comment