Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public enum enemyState
  7. {
  8.     idle,
  9.     walk,
  10.     attack,
  11.     stagger
  12. }
  13.  
  14. public class Enemy : MonoBehaviour
  15. {
  16.     [Header("State Machine")]
  17.     public enemyState currentState;
  18.  
  19.     [Header("Health")]
  20.     public FloatValue maxHealth;
  21.     public float health;
  22.  
  23.     [Header("Stats")]
  24.     public string enemyName;
  25.     public int baseAttack;
  26.     public float moveSpeed;
  27.  
  28.     [Header("Player Stats")]
  29.     public int xpGain;
  30.     public int levelUp;
  31.     public GameObject playerLevelUp;
  32.     //public int level = DBManager.level;
  33.  
  34.     [Header("Effects")]
  35.     public GameObject deathEffect;
  36.     private float deathEffectDelay = 1f;
  37.    
  38.     private void Awake()
  39.     {
  40.         health = maxHealth.InitialValue;
  41.     }
  42.  
  43.     void TakeDamage(float damage)
  44.     {
  45.         health -= damage;
  46.         if(health <= 0)
  47.         {
  48.             DeathEffect();
  49.             this.gameObject.SetActive(false);
  50.         }
  51.     }
  52.  
  53.     private void DeathEffect()
  54.     {
  55.         if(deathEffect != null)
  56.         {
  57.             GameObject effect = Instantiate(deathEffect, transform.position, Quaternion.identity);
  58.             Destroy(effect, deathEffectDelay);
  59.         }
  60.     }
  61.  
  62.     public void Knock(Rigidbody2D myrigidbody, float knockTime, float damage)
  63.     {
  64.         xpGain += DBManager.xp + 1;
  65.         if(xpGain >= DBManager.level * DBManager.level * 100)
  66.         {
  67.             playerLevelUp.gameObject.SetActive(true);
  68.             Debug.Log("You have Leveled Up");
  69.             StartCoroutine(LevelUpCO());
  70.         }
  71.         StartCoroutine(XPAdd());
  72.         Debug.Log("XP has Been Added");
  73.        
  74.  
  75.         StartCoroutine(KnockCo(myrigidbody, knockTime));
  76.         TakeDamage(damage);
  77.     }
  78.  
  79.     IEnumerator KnockCo(Rigidbody2D myrigidbody, float knockTime)
  80.     {
  81.         if (myrigidbody != null)
  82.         {
  83.             yield return new WaitForSeconds(knockTime);
  84.             myrigidbody.velocity = Vector2.zero;
  85.             currentState = enemyState.idle;
  86.             myrigidbody.velocity = Vector2.zero;
  87.         }
  88.     }
  89.  
  90.     IEnumerator XPAdd()
  91.     {
  92.         WWWForm form = new WWWForm();
  93.         form.AddField("username", DBManager.username);
  94.         form.AddField("xp", xpGain);
  95.         //form.AddField("level", levelUp);
  96.         WWW www = new WWW(DBManager.GetBaseURL() + "xpPlayer.php", form);
  97.         yield return www;
  98.         Debug.Log(www.text);
  99.     }
  100.  
  101.     private IEnumerator LevelUpCO()
  102.     {
  103.         yield return new WaitForSeconds(3);
  104.         playerLevelUp.gameObject.SetActive(false);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement