Reworked ExperienceBar.cs
-Alters expBarSprite's fill amount according to % ( exp gained / required ) -Scales according to viewport -Used Mathf.Lerp() to smooth bar growth over time.deltatime * lerpSpeed -Added clicks integer to keep track of total clicks Needs: -Delevel if ExpLess() called until expObtained < previousExpRequired -Create function to query for previous and future level requirements returned and stored within structs
This commit is contained in:
		
							parent
							
								
									ad57d40c70
								
							
						
					
					
						commit
						12775c8ef7
					
				
										
											Binary file not shown.
										
									
								
							@ -4,86 +4,88 @@ using UnityEngine.UI;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ExperienceBar : MonoBehaviour {
 | 
					public class ExperienceBar : MonoBehaviour {
 | 
				
			||||||
 | 
					 | 
				
			||||||
  public float expObtained = 0;
 | 
					 | 
				
			||||||
  public int level = 0;
 | 
					 | 
				
			||||||
  public float Increment = 10;
 | 
					 | 
				
			||||||
  public GameObject levelText;
 | 
					 | 
				
			||||||
  public float barWidth;
 | 
					 | 
				
			||||||
  public float barHeight;
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
  GameObject expBar;
 | 
					 | 
				
			||||||
  // Eventually.. public RectTransform gainedExp
 | 
					  // Eventually.. public RectTransform gainedExp
 | 
				
			||||||
  //Array used as expRequired[leveldesired]
 | 
					  //Array used as expRequired[leveldesired]
 | 
				
			||||||
  public float expRequired = 100;
 | 
					
 | 
				
			||||||
  public float clicksNeeded = 10;
 | 
					  [SerializeField]
 | 
				
			||||||
  float barMovement;
 | 
					  private int level = 1;
 | 
				
			||||||
  float barPosition;
 | 
					  [SerializeField]
 | 
				
			||||||
 | 
					  private float expObtained = 0;
 | 
				
			||||||
 | 
					  [SerializeField]
 | 
				
			||||||
 | 
					  private float expRequired = 100;
 | 
				
			||||||
 | 
					  [SerializeField]
 | 
				
			||||||
 | 
					  private float fillAmount;
 | 
				
			||||||
 | 
					  [SerializeField]
 | 
				
			||||||
 | 
					  private GameObject levelText;
 | 
				
			||||||
 | 
					  [SerializeField]
 | 
				
			||||||
 | 
					  private Image expBarSprite;
 | 
				
			||||||
 | 
					  [SerializeField]
 | 
				
			||||||
 | 
					  private float lerpSpeed;
 | 
				
			||||||
 | 
					  [SerializeField]
 | 
				
			||||||
 | 
					  private int clicks;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					  private float clicksNeeded = 10;
 | 
				
			||||||
 | 
					  private float previousExpRequired;
 | 
				
			||||||
 | 
					  private float barMovement;
 | 
				
			||||||
 | 
					  private float barPosition;
 | 
				
			||||||
 | 
					  private float Increment = 10;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  // Use this for initialization
 | 
					  // Use this for initialization
 | 
				
			||||||
  void Start () {
 | 
					  void Start () {
 | 
				
			||||||
    expBar = this.gameObject;
 | 
					    fillAmount = 0;
 | 
				
			||||||
    barPosition = 0;
 | 
					 | 
				
			||||||
    this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector3(barPosition, barHeight);
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Update is called once per frame
 | 
					  // Update is called once per frame
 | 
				
			||||||
  void Update () {
 | 
					  void Update () {
 | 
				
			||||||
    //Check if Exp bar is full
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(this.gameObject.GetComponent<RectTransform>().sizeDelta.x == barWidth || expBar.GetComponent<RectTransform>().sizeDelta.x > barWidth)
 | 
					    if (fillAmount != expBarSprite.fillAmount)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					    expBarSprite.fillAmount = Mathf.Lerp(expBarSprite.fillAmount, fillAmount, Time.deltaTime * lerpSpeed);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    //Check if Exp bar is full
 | 
				
			||||||
 | 
					    if (expBarSprite.fillAmount >= 1.0f)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      LevelUp();
 | 
					      LevelUp();
 | 
				
			||||||
      clicksNeeded = (expRequired - expObtained) / Increment;
 | 
					      clicksNeeded = (expRequired - expObtained) / Increment;
 | 
				
			||||||
      barPosition = 0;
 | 
					      fillAmount = 0;
 | 
				
			||||||
      this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector3(barPosition, barHeight);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public void ExpMore()
 | 
					  public void ExpMore()
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 | 
					    ++clicks;
 | 
				
			||||||
    expObtained = expObtained + Increment;
 | 
					    expObtained = expObtained + Increment;
 | 
				
			||||||
    //Since increment is 10 && Level 1 = 100
 | 
					    fillAmount = (expObtained / expRequired);
 | 
				
			||||||
    //10 clicks per level DIVDED BY clicks required for exp gain
 | 
					    Debug.Log("fillAmount = " + fillAmount);
 | 
				
			||||||
    //int clicks = 0;
 | 
					 | 
				
			||||||
    //clicks++;
 | 
					 | 
				
			||||||
    barMovement = (barWidth / clicksNeeded) / 10.0f ; 
 | 
					 | 
				
			||||||
    barPosition = barMovement + barPosition;
 | 
					 | 
				
			||||||
    Debug.Log("barPosition = " + barPosition);
 | 
					 | 
				
			||||||
    //use time.deltatime to smooth this out to make it look better?
 | 
					 | 
				
			||||||
    this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector3(barPosition * 10, barHeight); 
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public void LevelUp()
 | 
					  public void LevelUp()
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    level++;
 | 
					    ++level;
 | 
				
			||||||
    levelText.GetComponent<Text>().text = level.ToString();
 | 
					    levelText.GetComponent<Text>().text = level.ToString();
 | 
				
			||||||
    expRequired = Mathf.Pow(expRequired, 1.05f) + expRequired ;
 | 
					    previousExpRequired = expRequired;
 | 
				
			||||||
 | 
					    expObtained = 0;
 | 
				
			||||||
 | 
					    expRequired = Mathf.Pow(expRequired, 1.05f);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public void ResetExp()
 | 
					  public void ResetExp()
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    barPosition = 0;
 | 
					 | 
				
			||||||
    this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector3(barPosition, barHeight);
 | 
					 | 
				
			||||||
    expObtained = 0;
 | 
					 | 
				
			||||||
    level = 1;
 | 
					    level = 1;
 | 
				
			||||||
    levelText.GetComponent<Text>().text = level.ToString();
 | 
					    levelText.GetComponent<Text>().text = level.ToString();
 | 
				
			||||||
 | 
					    fillAmount = 0;
 | 
				
			||||||
 | 
					    expObtained = 0;
 | 
				
			||||||
    expRequired = 100;
 | 
					    expRequired = 100;
 | 
				
			||||||
    clicksNeeded = 10;
 | 
					    clicksNeeded = 10;
 | 
				
			||||||
 | 
					    clicks = 0;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public void ExpLess()
 | 
					  public void ExpLess()
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 | 
					    --clicks;
 | 
				
			||||||
    expObtained = expObtained - Increment;
 | 
					    expObtained = expObtained - Increment;
 | 
				
			||||||
    //Since increment is 10 && Level 1 = 100
 | 
					    fillAmount = (expObtained / expRequired);
 | 
				
			||||||
    //10 clicks per level DIVDED BY clicks required for exp gain
 | 
					    Debug.Log("fillAmount = " + fillAmount);
 | 
				
			||||||
    //int clicks = 0;
 | 
					 | 
				
			||||||
    //clicks++;
 | 
					 | 
				
			||||||
    barMovement = (barWidth / clicksNeeded) / 10.0f;
 | 
					 | 
				
			||||||
    barPosition = barPosition - barMovement;
 | 
					 | 
				
			||||||
    Debug.Log("barPosition = " + barPosition);
 | 
					 | 
				
			||||||
    //use time.deltatime to smooth this out to make it look better?
 | 
					 | 
				
			||||||
    this.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector3(barPosition * 10, barHeight);
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user