diff --git a/Assets/Images/Player/Animation/Player.controller b/Assets/Images/Player/Animation/Player.controller index 72c9796..ce79651 100644 --- a/Assets/Images/Player/Animation/Player.controller +++ b/Assets/Images/Player/Animation/Player.controller @@ -382,7 +382,7 @@ m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_Name: Player_Move + m_Name: Player Locomotion m_Speed: 1 m_CycleOffset: 0 m_Transitions: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index a8ea4fb..3d09674 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -2055,7 +2055,14 @@ flashActive: 0 flashLength: 1 flashCounter: 1 - speed: 500 + health: 100 + mana: 100 + speed: 5 + isAttacking: 0 + attackBaseValue: 20 + attackRange: 1 + attackTimer: 1.5 + maxAttackTimer: 1.5 normalAttack: 0 strongAttack: 0 shield: 0 diff --git a/Assets/Scripts/Player/Interfaces.meta b/Assets/Scripts/Player/Interfaces.meta new file mode 100644 index 0000000..037d77f --- /dev/null +++ b/Assets/Scripts/Player/Interfaces.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 40908dc94d328ca4ca956dfa22b07120 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/Interfaces/IHealthSystem.cs b/Assets/Scripts/Player/Interfaces/IHealthSystem.cs new file mode 100644 index 0000000..1ba754d --- /dev/null +++ b/Assets/Scripts/Player/Interfaces/IHealthSystem.cs @@ -0,0 +1,12 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public interface IHealthSystem +{ + public void Heal(float ammount); + public void TakeDamage(float ammount); + + public bool IsDead(); + public float GetHealth(); +} diff --git a/Assets/Scripts/Player/Interfaces/IHealthSystem.cs.meta b/Assets/Scripts/Player/Interfaces/IHealthSystem.cs.meta new file mode 100644 index 0000000..0f876a5 --- /dev/null +++ b/Assets/Scripts/Player/Interfaces/IHealthSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4089eb98b2e161944b9f6fe8dd6bd993 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 3756dfc..e573497 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -1,11 +1,12 @@ using System.Collections; using System.Collections.Generic; +using System.Xml.Serialization; using UnityEngine; -public class PlayerController : MonoBehaviour +public class PlayerController : MonoBehaviour, IHealthSystem { private Rigidbody2D rb; - [SerializeField]public Animator anim; + [SerializeField] private Animator anim; public bool flashActive; [SerializeField] @@ -17,16 +18,19 @@ //private Transform attackBoxTransform; //private BoxCollider2D attackBoxCollider; - [SerializeField] - private float speed = 0f; + [Header("Attribute Settings: ")] + [SerializeField] private float health = 100f; + [SerializeField] private float mana = 100f; + [SerializeField] private float speed = 0f; - private float attackAnimTime = 0.25f; - private float attackAnimCounter = 0.25f; + [Header("Combat Settings: ")] + [SerializeField] private bool isAttacking = false; + [SerializeField] private float attackBaseValue = 20f; + [SerializeField] private float attackRange = 1f; + [SerializeField] private float attackTimer = 0f; + [SerializeField] private float maxAttackTimer = 1.5f; - private float attackTime = 0.5f; - private float attackCounter = 0.5f; - - private float strongAttackHoldTime = 3f; + private float horizontalInput, verticalInput; public bool normalAttack; public bool strongAttack; @@ -36,139 +40,99 @@ public int strongPlayerAttack = 15; public int defensePlayer = 5; - void Start() + private void Awake() { rb = GetComponent(); anim = GetComponent(); playerSprite = GetComponent(); } + void Start() + { + + } + private void Update() { - if (flashActive) + PlayerMovement(); + PlayerDefend(); + PlayerAttack(); + PlayerAnimController(); + } + + //Player Movement + private void PlayerMovement() + { + if (!isAttacking) { - if (flashCounter > flashLength * .99f) - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 0f); - } - else if (flashCounter > flashLength * .82f) - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 1f); - } - else if (flashCounter > flashLength * .66f) - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 0f); - } - else if (flashCounter > flashLength * .49) - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 1f); - } - else if (flashCounter > flashLength * .33) - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 0f); - } - else if (flashCounter > flashLength * .16f) - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 1f); - } - else if (flashCounter > 0f) - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 0f); - } - else - { - playerSprite.color = new Color(playerSprite.color.r, playerSprite.color.g, playerSprite.color.b, 1f); - flashActive = false; - } - flashCounter -= Time.deltaTime; + //Get Keyboard Input values + horizontalInput = Input.GetAxisRaw("Horizontal"); + verticalInput = Input.GetAxisRaw("Vertical"); + + //Set velocity based on the input + rb.velocity = new Vector2(horizontalInput, verticalInput).normalized * speed; } } - void FixedUpdate() + + //Player Attack + private void PlayerAttack() { - float horizontalInput = Input.GetAxisRaw("Horizontal"); - float verticalInput = Input.GetAxisRaw("Vertical"); - - rb.velocity = new Vector2(horizontalInput, verticalInput).normalized * speed * Time.fixedDeltaTime; - - anim.SetFloat("moveX", rb.velocity.x); - anim.SetFloat("moveY", rb.velocity.y); - - if(Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1) + if (Input.GetMouseButtonDown(0)) { - anim.SetFloat("LastMoveX", Input.GetAxisRaw("Horizontal")); - anim.SetFloat("LastMoveY", Input.GetAxisRaw("Vertical")); - } + rb.velocity = Vector3.zero; //Sets the character velocity to 0 + isAttacking = true; + attackTimer = 0f; // Reset attack timer - if (normalAttack) - { - rb.velocity = Vector2.zero; - attackAnimCounter -= Time.fixedDeltaTime; - if(attackAnimCounter <= 0) + Vector2 origin = transform.position; + Vector2 direction = new Vector2(horizontalInput, verticalInput).normalized; + + if (direction == Vector2.zero) + direction = new Vector2(anim.GetFloat("LastMoveX"), anim.GetFloat("LastMoveY")); + + RaycastHit2D hit = Physics2D.Raycast(origin, direction, attackRange); + + if (hit.collider != null) { - anim.SetBool("isAttacking", false); - normalAttack = false; - } - } - else if (strongAttack) - { - print("Strong Attack Ready"); - rb.velocity = Vector2.zero; - attackAnimCounter -= Time.fixedDeltaTime; - if (attackAnimCounter <= 0) - { - anim.SetBool("isAttacking", false); - strongAttack = false; - } - } - else if (shield) - { - print("Shield Active"); - rb.velocity = Vector2.zero; - } - - attackCounter -= Time.fixedDeltaTime; - if(attackCounter <= 0 && shield == false) - { - // Attack - if (Input.GetMouseButton(0)) - { - strongAttackHoldTime -= Time.fixedDeltaTime; - - } - if (!Input.GetMouseButton(0) && strongAttackHoldTime < 3f) - { - //Basic Attack - if(strongAttackHoldTime > 0) + if (hit.collider.TryGetComponent(out IHealthSystem healthSystem)) { - attackAnimCounter = attackAnimTime; - attackCounter = attackTime; - anim.SetBool("isAttacking", true); - normalAttack = true; - strongAttackHoldTime = 3f; + healthSystem.TakeDamage(attackBaseValue); } } - //Strong Attack - if (strongAttackHoldTime <= 0) - { - attackAnimCounter = attackAnimTime; - attackCounter = attackTime; - anim.SetBool("isAttacking", true); - strongAttack = true; - strongAttackHoldTime = 3f; - } - } - - //Shield - if (Input.GetKey(KeyCode.Space)) - { - shield = true; - } - else - { - shield = false; + anim.SetBool("IsAttacking", true); } - //print("strongAttackHoldTime: " + strongAttackHoldTime); + if (isAttacking) + { + attackTimer += Time.deltaTime; + + if (attackTimer >= maxAttackTimer) + { + isAttacking = false; + anim.SetBool("IsAttacking", false); + } + } } + + + private void PlayerDefend() => shield = Input.GetKey(KeyCode.Space); + + private void PlayerAnimController() + { + anim.SetFloat("moveX", horizontalInput); + anim.SetFloat("moveY", verticalInput); + + if (isAttacking) + { + //Detect what was the last input + Vector2 vec = new Vector2(horizontalInput, verticalInput); + anim.SetFloat("LastMoveX", vec.x); + anim.SetFloat("LastMoveY", vec.y); + } + } + + public void TakeDamage(float ammount) => health -= ammount; + public void Heal(float ammount) => health += ammount; + public float GetHealth() => health; + public bool IsDead() => health <= 0f ? true : false; }