// Use this for initialization void Start() { //Find Objects if (PA_VGUI = GameObject.Find("VGUI").GetComponent <VGUI>()) { //Do nothing } else { Debug.Log("VGUI not found, PLayer Attack"); } if (PA_FindObject = GameObject.Find("Inventory")) { PA_Inventory = PA_FindObject.GetComponent <Inventory>(); } else { Debug.Log("Player Attack could not find Inventory"); } //Animation PA_Animator = gameObject.GetComponent <Animator>(); PA_AttackLeft = false; PA_AttackRight = false; PA_AttackUp = false; PA_AttackDown = false; PA_HasAttackedSword = false; //States PA_CurrentState = PA_States.Idle; PA_NewState = PA_States.Idle; PA_ChangeStates = false; PA_StateTimer = 0.1f; PA_ResetStateTimer = 0.2f; //Attack direction PA_PlayerController = gameObject.GetComponent <PlayerController>(); PA_AttackDirection = PA_PlayerController.P_PlayerDirection; PA_FreezeTimer = 0.1f; PA_Frozen = false; //Inflict Damage PA_SwordAttackDamage = 5; PA_AttackBoxSize = new Vector2(1.13f, 0.85f); PA_InflictDamage = false; PA_InflictDamageTimer = 0.3f; PA_HasInflictedDamage = false; }
// Update is called once per frame void Update() { if (PA_HasAttackedSword) { PA_Animator.SetBool("Attack_Left", PA_AttackLeft); PA_Animator.SetBool("Attack_Right", PA_AttackRight); PA_Animator.SetBool("Attack_Up", PA_AttackUp); PA_Animator.SetBool("Attack_Down", PA_AttackDown); PA_HasAttackedSword = false; PA_InflictDamage = true; } //Timer is needed to make sure the overlap collider is created when the animation is played not before. if (PA_InflictDamage) { if (PA_InflictDamageTimer > 0) { PA_InflictDamageTimer -= Time.deltaTime; } else { PA_MainAttack(PA_Direction); } } //If the player moves in a certain dirction, update the attack direction if (PA_PlayerController.P_PlayerDirection.x != 0.0f || PA_PlayerController.P_PlayerDirection.y != 0.0f) { PA_AttackDirection = PA_PlayerController.P_PlayerDirection; } //Timer for each state, to make sure the attack finishes before the next one starts if (PA_ChangeStates) { if (PA_StateTimer <= 0) { PA_StateTimer = 0.1f; PA_ChangeStates = false; PA_CurrentState = PA_NewState; } else { PA_StateTimer -= Time.deltaTime; } } //Return to Idle state and animation if (PA_ResetState) { if (PA_ResetStateTimer <= 0) { PA_CurrentState = PA_States.Idle; PA_ResetStateTimer = 0.2f; PA_AttackDown = false; PA_AttackLeft = false; PA_AttackRight = false; PA_AttackUp = false; PA_HasAttackedSword = true; PA_ResetState = false; PA_InflictDamage = false; PA_InflictDamageTimer = 0.3f; PA_HasInflictedDamage = false; } else { PA_ResetStateTimer -= Time.deltaTime; } } //Check which direction the player is facing, and based on that create the overlap box to damage enemies if (PA_CurrentState == PA_States.Idle) { //Main Weapon if (Input.GetButtonDown("CTRL - X") || Input.GetKeyDown(KeyCode.Z)) { PA_CurrentState = PA_States.Main; //1 Right //2 Left //3 Up //4 Down switch (PA_WhereToSummonAttack()) { case 1: PA_AttackRight = true; break; case 2: PA_AttackLeft = true; break; case 3: PA_AttackUp = true; break; case 4: PA_AttackDown = true; break; default: PA_AttackDown = true; //The player is spawned facing downwards, so if he attacks before moving at all, the direction will be downwards break; } PA_HasAttackedSword = true; PA_HasInflictedDamage = false; PA_ResetState = true; } //Secondary Weapons //Possible bug here when the player tries to pickup a weapon that he shoots of his current weapon as well if (Input.GetButton("CTRL - Y") || Input.GetKeyDown(KeyCode.X)) { if (PA_Inventory.IN_PlayerWeapon1.IT_ID > 0) { Debug.Log(PA_Inventory.IN_PlayerWeapon1.IT_ID); //1 Right //2 Left //3 Up //4 Down switch (PA_WhereToSummonAttack()) { case 1: PA_InstaPos = new Vector2(gameObject.transform.position.x + 1.0f, gameObject.transform.position.y); PA_InstaForce = new Vector2(1000.0f, 0.0f); PA_InstaRot = new Vector3(0.0f, 0.0f, 0.0f); break; case 2: PA_InstaPos = new Vector2(gameObject.transform.position.x - 1.0f, gameObject.transform.position.y); PA_InstaForce = new Vector2(-1000.0f, 0.0f); PA_InstaRot = new Vector3(0.0f, 180.0f, 0.0f); break; case 3: PA_InstaPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 1.0f); PA_InstaForce = new Vector2(0.0f, 1000.0f); PA_InstaRot = new Vector3(0.0f, 0.0f, 90.0f); break; case 4: PA_InstaPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 1.0f); PA_InstaForce = new Vector2(0.0f, -1000.0f); PA_InstaRot = new Vector3(0.0f, 0.0f, -90.0f); break; default: PA_InstaPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 1.0f); PA_InstaForce = new Vector2(0.0f, -1000.0f); PA_InstaRot = new Vector3(0.0f, 0.0f, -90.0f); break; } switch (PA_Inventory.IN_PlayerWeapon1.IT_ID) { case 1: PA_InstaWeapon = Instantiate(PA_DaggerBleeding, PA_InstaPos, gameObject.transform.rotation); PA_InstaWeapon.transform.eulerAngles = PA_InstaRot; PA_InstaWeapon.GetComponent <Rigidbody2D>().AddForce(PA_InstaForce); break; case 2: break; case 3: break; case 4: break; case 5: break; } } } } }
// Update is called once per frame void Update() { if (PA_Frozen) { if (PA_FreezeTimer > 0.0f) { PA_FreezeTimer -= Time.deltaTime; } else { PA_FreezeTimer = 0.1f; PA_PlayerController.UnFreeze(); PA_Frozen = false; } } if (PA_HasAttackedSword) { PA_Animator.SetBool("Attack_Left", PA_AttackLeft); PA_Animator.SetBool("Attack_Right", PA_AttackRight); PA_Animator.SetBool("Attack_Up", PA_AttackUp); PA_Animator.SetBool("Attack_Down", PA_AttackDown); PA_HasAttackedSword = false; PA_InflictDamage = true; } //Timer is needed to make sure the overlap collider is created when the animation is played not before. if (PA_InflictDamage) { if (PA_InflictDamageTimer > 0) { PA_InflictDamageTimer -= Time.deltaTime; } else { PA_Attack(PA_Direction); } } //If the player moves in a certain dirction, update the attack direction if (PA_PlayerController.P_PlayerDirection.x != 0.0f || PA_PlayerController.P_PlayerDirection.y != 0.0f) { PA_AttackDirection = PA_PlayerController.P_PlayerDirection; } //Timer for each state, to make sure the attack finishes before the next one starts if (PA_ChangeStates) { if (PA_StateTimer <= 0) { PA_StateTimer = 0.1f; PA_ChangeStates = false; PA_CurrentState = PA_NewState; } else { PA_StateTimer -= Time.deltaTime; } } //Return to Idle state and animation if (PA_ResetState) { if (PA_ResetStateTimer <= 0) { PA_CurrentState = PA_States.Idle; PA_ResetStateTimer = 0.2f; PA_AttackDown = false; PA_AttackLeft = false; PA_AttackRight = false; PA_AttackUp = false; PA_HasAttackedSword = true; PA_ResetState = false; PA_InflictDamage = false; PA_InflictDamageTimer = 0.3f; PA_HasInflictedDamage = false; } else { PA_ResetStateTimer -= Time.deltaTime; } } //Check which direction the player is facing, and based on that create the overlap box to damage enemies if (PA_CurrentState == PA_States.Idle) { if (Input.GetButtonDown("CTRL - X")) { PA_CurrentState = PA_States.Main; if (PA_AttackDirection.x > 0) { PA_AttackRight = true; PA_HasAttackedSword = true; if (PA_PlayerController.Freeze()) { PA_Frozen = true; PA_PlayerController.P_rb.AddRelativeForce(PA_AttackDirection * 150.0f); } PA_Direction = 1; PA_HasInflictedDamage = false; } else if (PA_AttackDirection.x < 0) { PA_AttackLeft = true; PA_HasAttackedSword = true; if (PA_PlayerController.Freeze()) { PA_Frozen = true; PA_PlayerController.P_rb.AddRelativeForce(PA_AttackDirection * 150.0f); } PA_Direction = 2; PA_HasInflictedDamage = false; } else if (PA_AttackDirection.y > 0) { PA_AttackUp = true; PA_HasAttackedSword = true; if (PA_PlayerController.Freeze()) { PA_Frozen = true; PA_PlayerController.P_rb.AddRelativeForce(PA_AttackDirection * 150.0f); } PA_Direction = 3; PA_HasInflictedDamage = false; } else if (PA_AttackDirection.y < 0) { PA_AttackDown = true; PA_HasAttackedSword = true; if (PA_PlayerController.Freeze()) { PA_Frozen = true; PA_PlayerController.P_rb.AddRelativeForce(PA_AttackDirection * 150.0f); } PA_Direction = 4; PA_HasInflictedDamage = false; } else //The player is spawned facing downwards, so if he attack before moving at all, the direction will be downwards { PA_AttackDown = true; PA_HasAttackedSword = true; if (PA_PlayerController.Freeze()) { PA_Frozen = true; PA_PlayerController.P_rb.AddRelativeForce(PA_AttackDirection * 150.0f); } PA_Direction = 4; PA_HasInflictedDamage = false; } PA_ResetState = true; } } }