PlayerAttack.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5. public class PlayerAttack : NetworkBehaviour{
  6. public StatManager statManager;
  7. private float timeBetweenAttacks;
  8. public float startTimeBtwAttacks;
  9. //public Transform attackPosition;
  10. public LayerMask enemyMask;
  11. public float attackRange;
  12. public Vector3 castOffset;
  13. public int damage;
  14. public playerNetwork pnet;
  15. public GameObject projectile;
  16. public GameObject leftAnim,rightAnim,upAnim,downAnim;
  17. void Awake(){
  18. pnet.playerAttack = this;
  19. }
  20. void Start(){
  21. if(!isLocalPlayer){
  22. Destroy(this);
  23. }else{
  24. statManager.OnStatsChanged += onStatChange;
  25. }
  26. }
  27. public void onStatChange(){
  28. damage = statManager.GetEffectiveValue("strength");
  29. }
  30. // void Update(){
  31. // //Get player look dir
  32. // if(timeBetweenAttacks <=0){
  33. // if(Input.GetMouseButtonDown(0)){
  34. // StartCoroutine(couroutineAttack());
  35. // timeBetweenAttacks = startTimeBtwAttacks;
  36. // }
  37. // }else{
  38. // timeBetweenAttacks -= Time.deltaTime;
  39. // }
  40. // }
  41. IEnumerator couroutineAttack(bool isMagical){
  42. yield return new WaitForSecondsRealtime(0.6f);
  43. Attack(isMagical);
  44. }
  45. public void Attack(bool isMagical){
  46. if(pnet.health<=0){return;}
  47. Vector3 direction = GetPlayerLookDir();
  48. RaycastHit2D hit = Physics2D.Linecast(transform.position + castOffset, castOffset +transform.position + (direction * attackRange), enemyMask);
  49. if(hit.collider == null){return;}
  50. if(hit.collider.transform.GetComponent<enemyScript>() != null){
  51. int damageamount = damage + (pnet.lvl*5);
  52. if(isMagical){
  53. hit.collider.transform.GetComponent<enemyScript>().TakeMagicalDamage(damageamount, netId);
  54. }else{
  55. hit.collider.transform.GetComponent<enemyScript>().TakeDamage(damageamount, netId);
  56. }
  57. Debug.Log("Attacked enemy " + damageamount);
  58. }else{
  59. Debug.Log("Not an enemy : " + hit.collider.transform.name);
  60. }
  61. }
  62. public float magicalProjectileSpawnOffset = 1;
  63. public void MagicalAttack(){
  64. if(pnet.health<=0){return;}
  65. Vector3 direction = GetPlayerLookDir();
  66. //if(isServer){
  67. pnet.MagicalAttack(direction, magicalProjectileSpawnOffset, damage);
  68. /*}else{
  69. Debug.Log(direction);
  70. CmdMagicalAttack(direction);
  71. }*/
  72. }
  73. /* [Command]
  74. void CmdMagicalAttack(Vector3 direction){
  75. magicalAttack(direction);
  76. }*/
  77. Vector3 GetPlayerLookDir(){
  78. Vector3 direction = Vector3.right;
  79. if(leftAnim.activeSelf){
  80. direction = Vector3.left;
  81. }else if(rightAnim.activeSelf){
  82. direction = Vector3.right;
  83. }else if(upAnim.activeSelf){
  84. direction = Vector3.up;
  85. }else if(downAnim.activeSelf){
  86. direction = Vector3.down;
  87. }
  88. return direction;
  89. }
  90. void OnDrawGizmos(){
  91. Gizmos.color = Color.red;
  92. Gizmos.DrawLine(transform.position+castOffset, castOffset+ transform.position + (GetPlayerLookDir() * attackRange));
  93. }
  94. // void Update() {
  95. // if(timeBetweenAttacks <= 0){
  96. // if(Input.GetMouseButtonDown(0)){
  97. // Collider2D[] enemyInRange = Physics2D.OverlapCircleAll(attackPosition.position , attackRange , enemyMask);
  98. // for(int i = 0; i< enemyInRange.Length; i++ ){
  99. // enemyInRange[i].GetComponent<enemyScript>().TakeDamage(damage);
  100. // }
  101. // }
  102. // timeBetweenAttacks = startTimeBtwAttacks;
  103. // }
  104. // else{
  105. // timeBetweenAttacks -= Time.deltaTime;
  106. // }
  107. // }
  108. // void OnDrawGizmosSelected() {
  109. // Gizmos.color = Color.red;
  110. // Gizmos.DrawSphere(attackPosition.position, attackRange);
  111. // }
  112. }