Skip to content
Snippets Groups Projects
Commit 55c146e1 authored by Gard Aleksander Furre's avatar Gard Aleksander Furre
Browse files

Added simple xp bar

parent bf87a58a
No related branches found
No related tags found
2 merge requests!29Updating master,!27Added simple xp bar
......@@ -56,6 +56,13 @@ namespace BigSock {
public float MaxHP => Stats.MaxHP;
public float baseMaxHP = 10;
/*
Xp....
*/
public float dropXP;
public float xp;
public float maxXp;
public int level;
/*
......@@ -194,7 +201,11 @@ namespace BigSock {
Alive = false;
// Inform the attacker killed us.
if(attack.Actor != null) attack.Actor.TargetKilled(this, attack);
if(attack.Actor != null) {
attack.Actor.TargetKilled(this, attack);
if(attack.Actor is PlayerController player)
player.GainXp(dropXP);
}
AfterDeath();
......@@ -239,7 +250,6 @@ namespace BigSock {
print($"[Character.AfterDeath()] start. | {HP}, {Alive}");
Destroy(gameObject);
}
}
......@@ -328,6 +338,12 @@ namespace BigSock {
public void TargetKilled(Character target, AttackStats attack) {
OnKill?.Invoke(this, target, attack);
}
public void GiveXp(float xp) {
this.xp += xp;
OnGainedXP?.Invoke(this, xp);
}
/*
Triggers when character uses an attack.
......@@ -381,9 +397,9 @@ namespace BigSock {
/*
Triggers when character gains xp.
Params: actor, amount.
Params: sorce, amount.
*/
public event Action<Character, int> OnGainedXP;
public event Action<Character, float> OnGainedXP;
/*
Triggers when character levels up.
......
......@@ -17,6 +17,9 @@ namespace BigSock {
{
public HPBar hpBar;
public XpBar xpBar;
public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter;
......@@ -43,6 +46,10 @@ namespace BigSock {
protected override void Start()
{
base.Start();
xpBar.SetMaxXp(maxXp);
xpBar.SetXp(xp);
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
hpBar.SetMaxHealth(Convert.ToInt32(MaxHP));
......@@ -163,5 +170,21 @@ namespace BigSock {
base.AfterDamage(attack);
hpBar.SetHealth(Convert.ToInt32(HP));
}
public void GainXp(float xp){
GiveXp(xp);
CheckXp();
xpBar.SetXp(this.xp);
}
private void CheckXp(){
if(xp > maxXp){
level += 1;
xp -= maxXp;
maxXp = (level + 1) * 100;
xpBar.SetMaxXp(maxXp);
}
}
}
}
......@@ -9,6 +9,7 @@ namespace BigSock {
public partial class EnemyController : Character {
public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter;
protected List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace BigSock.UI{
public class XpBar : MonoBehaviour
{
public Slider slider;
public TextMeshPro xpText;
protected void Start(){
slider = GetComponent<Slider>();
// xpText = GetComponent<TextMeshPro>();
}
public void SetXp(float xp){
slider.value = xp;
xpText.SetText($"{xp}/{slider.maxValue}");
}
public void SetMaxXp(float xp){
slider.maxValue = xp;
}
}
}
fileFormatVersion: 2
guid: a2ed0e7debd7bc7468e3a430508a7e71
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment