diff --git a/MrBigsock/Assets/Code/Character.cs b/MrBigsock/Assets/Code/Character.cs index 4c2f2f98baebd08132ef90de1bb303309b1ad112..2cb32174c3f8f14b756d6d0716dc14794aebc0f8 100644 --- a/MrBigsock/Assets/Code/Character.cs +++ b/MrBigsock/Assets/Code/Character.cs @@ -11,48 +11,49 @@ namespace BigSock { /* Attack speed of the character. */ - public double AttackSpeed => baseAttackSpeed; - public double baseAttackSpeed = 1; + public float AttackSpeed => baseAttackSpeed; + public float baseAttackSpeed = 1; /* Cooldown between attacks. */ - public double AttackCooldown => 1.0 / AttackSpeed; + public float AttackCooldown => 1.0f / AttackSpeed; /* Movement speed of the character. */ - public double MovementSpeed => baseMovementSpeed; - public double baseMovementSpeed = 1; + public float MovementSpeed => baseMovementSpeed; + public float baseMovementSpeed = 1; /* Damage of the character. */ - public double Damage => baseDamage; - public double baseDamage = 1; + public float Damage => baseDamage; + public float baseDamage = 1; /* Knockback force */ - public float KnockbackForce = 150; + public float KnockbackForce => knockbackForce; + public float knockbackForce = 150; /* Hit points of the character. */ - public double HP { + public float HP { get => baseHP; set => baseHP = value; } - public double baseHP = 10; + public float baseHP = 10; /* Maximum hit points of the character. */ - public double MaxHP => baseMaxHP; - public double baseMaxHP = 10; + public float MaxHP => baseMaxHP; + public float baseMaxHP = 10; @@ -76,10 +77,40 @@ namespace BigSock { public TimeSpan _iFrameDuration = new TimeSpan(0, 0, 0, 0, 333); + + protected Rigidbody2D rb; + + + void Start() { + rb = GetComponent<Rigidbody2D>(); + } + + /* + Add Kcockback. + */ + //public void KnockBack(float force, Vector2 difference) { + // rb.AddForce(difference * force, ForceMode2D.Impulse); + //} + public void KnockBack(IAttackStats attack) { + Vector2 difference = ((Vector2) transform.position - attack.Source).normalized; + //KnockBack(attack.Knockback, difference); + rb.AddForce(difference * attack.Knockback, ForceMode2D.Impulse); + + } + + /* + Adds damage to the player if they don't have IFrames. + */ + public bool TakeDamage(float amount) { + return TakeDamage(new AttackStats{ + Damage = amount, + }); + } + /* Adds damage to the player if they don't have IFrames. */ - public bool TakeDamage(double amount) { + public bool TakeDamage(IAttackStats attack) { // Check if player has IFrames if(NextTimeCanTakeDamage > DateTime.Now) return false; @@ -89,8 +120,8 @@ namespace BigSock { // Add damage - HP -= amount; - OnDamage(amount); + HP -= attack.Damage; + OnDamage(attack); TryKill(); @@ -116,9 +147,9 @@ namespace BigSock { /* Method for what to do when the character takes damage. */ - protected void OnDamage(double amount) { - print($"[Character.TakeDamage()] start. | {HP} - {amount}"); - + protected void OnDamage(IAttackStats attack) { + print($"[Character.TakeDamage()] {HP} - {attack.Damage}"); + KnockBack(attack); } /* diff --git a/MrBigsock/Assets/Code/Core.meta b/MrBigsock/Assets/Code/Core.meta new file mode 100644 index 0000000000000000000000000000000000000000..ed02a0aaad53dc71d5fadf2e859502a736117c66 --- /dev/null +++ b/MrBigsock/Assets/Code/Core.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 764c609c76c014a48866aca15bcd65d9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MrBigsock/Assets/Code/Core/AttackStats.cs b/MrBigsock/Assets/Code/Core/AttackStats.cs new file mode 100644 index 0000000000000000000000000000000000000000..3c46627a71368455c784a50f9a24ed0caf448213 --- /dev/null +++ b/MrBigsock/Assets/Code/Core/AttackStats.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + + +namespace BigSock { + + /* + Represents the stats of an attack. + */ + public class AttackStats : IAttackStats { + /* + The damage of the attack. + */ + public float Damage { get; set; } + + /* + The knockback of the attack. + */ + public float Knockback { get; set; } + + /* + The range of the attack. + */ + public float Range { get; set; } + + /* + The attack speed of the attack. + */ + public float AttackSpeed { get; set; } + + /* + The source of the attack. + */ + public Vector2 Source { get; set; } + + } +} \ No newline at end of file diff --git a/MrBigsock/Assets/Code/Core/AttackStats.cs.meta b/MrBigsock/Assets/Code/Core/AttackStats.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..250859c8e1f4f5b13fbbe419d132ece706198607 --- /dev/null +++ b/MrBigsock/Assets/Code/Core/AttackStats.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cd438d3dc3b447c438972219e0808814 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MrBigsock/Assets/Code/Core/IAttackStats.cs b/MrBigsock/Assets/Code/Core/IAttackStats.cs new file mode 100644 index 0000000000000000000000000000000000000000..a097916ab08bd53f6477036cabc074f97803110c --- /dev/null +++ b/MrBigsock/Assets/Code/Core/IAttackStats.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + + +namespace BigSock { + + /* + Interface represents the stats of an attack. + */ + public interface IAttackStats { + /* + The damage of the attack. + */ + float Damage { get; } + + /* + The knockback of the attack. + */ + float Knockback { get; } + + /* + The range of the attack. + */ + float Range { get; } + + /* + The attack speed of the attack. + */ + float AttackSpeed { get; } + + /* + The source of the attack. + */ + Vector2 Source { get; } + + } +} \ No newline at end of file diff --git a/MrBigsock/Assets/Code/Core/IAttackStats.cs.meta b/MrBigsock/Assets/Code/Core/IAttackStats.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..390420c3dedddeebeabf25fd5314d38000a5f2fa --- /dev/null +++ b/MrBigsock/Assets/Code/Core/IAttackStats.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c8f491559b2c8404bbdf588d7a00b7d9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MrBigsock/Assets/Code/PlayerController.cs b/MrBigsock/Assets/Code/PlayerController.cs index 1fc9aad0913c9142bba4333a780f48928fe73689..865206ad9a5746f46a28a6b767346959803bb449 100644 --- a/MrBigsock/Assets/Code/PlayerController.cs +++ b/MrBigsock/Assets/Code/PlayerController.cs @@ -17,7 +17,7 @@ namespace BigSock { Vector2 movementInput; SpriteRenderer spriteRenderer; - Rigidbody2D rb; + //Rigidbody2D rb; Animator animator; List<RaycastHit2D> castCollisions = new List<RaycastHit2D>(); @@ -41,13 +41,6 @@ namespace BigSock { } - /* - Add Kcockback. - */ - public void KnockBack(float force, Vector2 difference){ - rb.AddForce(difference * force); - - } private void FixedUpdate() { if(canMove) { diff --git a/MrBigsock/Assets/Code/Slime/SlimeController.cs b/MrBigsock/Assets/Code/Slime/SlimeController.cs index 24a65bbad9f5fc1cf2becfe33b50ce74fd17047c..a7d612764b42147510de8700dc9ccd8b0895a0af 100644 --- a/MrBigsock/Assets/Code/Slime/SlimeController.cs +++ b/MrBigsock/Assets/Code/Slime/SlimeController.cs @@ -24,7 +24,7 @@ namespace BigSock { private bool isInMelee = false; - Rigidbody2D rb; + //Rigidbody2D rb; // private Collider2D_Proxy secondCollider; @@ -177,16 +177,26 @@ namespace BigSock { */ public partial class SlimeController { - private void Attack_OnColliderEnter2D(Collider2D other){ + private void Attack_OnColliderEnter2D(Collider2D other) { if (other.gameObject.tag == "Player") isInMelee = true; } - private void Attack_OnColliderStay2D(Collider2D other){ + private void Attack_OnColliderStay2D(Collider2D other) { var player = other.gameObject.GetComponent<PlayerController>(); - if (player != null){ - if(player.TakeDamage(Damage)) { + if (player != null) { + // Create attack object. + var attack = new AttackStats{ + Damage = Damage, + Knockback = KnockbackForce, + Range = 0, + AttackSpeed = AttackSpeed, + Source = transform.position, + }; + + // Get the player to take the damage. + if(player.TakeDamage(attack)) { //knockback ? //animer nå ? diff --git a/MrBigsock/Assets/Code/attack/AttackMovement.cs b/MrBigsock/Assets/Code/attack/AttackMovement.cs index 5855e8818f52c2aa69b9e8fe08c792aba34167f3..f8cc663fb3e7ef4f8d49bf1d9cf361e12d9baf4d 100644 --- a/MrBigsock/Assets/Code/attack/AttackMovement.cs +++ b/MrBigsock/Assets/Code/attack/AttackMovement.cs @@ -15,8 +15,12 @@ namespace BigSock { /* Damage of the character. */ - public double Damage => baseDamage; - public double baseDamage = 1; + public float Damage => baseDamage; + public float baseDamage = 1; + + public float KnockbackForce => knockbackForce; + public float knockbackForce = 50; + // Start is called before the first frame update void Start() @@ -48,8 +52,17 @@ namespace BigSock { print($"[AttackMovement.OnCollisionEnter2D()] {collision.transform.position}"); var target = collision.gameObject.GetComponent<EnemyController>(); - if(target != null){ - if(target.TakeDamage(Damage)){ + if(target != null) { + + var attack = new AttackStats{ + Damage = Damage, + Knockback = KnockbackForce, + //Range = 0, + //AttackSpeed = AttackSpeed, + Source = transform.position, + }; + + if(target.TakeDamage(attack)) { } } diff --git a/MrBigsock/Assets/Code/orc/EnemyController.cs b/MrBigsock/Assets/Code/orc/EnemyController.cs index 2de0b3984594d1055e9dbb045aff31bd86d7731b..ed68aac6a524df55e0e0f8a0fa9e9d495eeb5490 100644 --- a/MrBigsock/Assets/Code/orc/EnemyController.cs +++ b/MrBigsock/Assets/Code/orc/EnemyController.cs @@ -24,7 +24,7 @@ namespace BigSock { private bool isInMelee = false; - Rigidbody2D rb; + //Rigidbody2D rb; // private Collider2D_Proxy secondCollider; @@ -50,15 +50,13 @@ namespace BigSock { - private void RotateAnimation(Vector2 direction) - { + private void RotateAnimation(Vector2 direction) { if (direction.x > 0.01f){ gameObject.GetComponent<SpriteRenderer>().flipX = false; } else if (direction.x < -0.01f){ gameObject.GetComponent<SpriteRenderer>().flipX = true; } - } private void Update(){ @@ -113,7 +111,10 @@ namespace BigSock { } - public partial class EnemyController { //attack + /* + Attack + */ + public partial class EnemyController { private void Attack_OnColliderEnter2D(Collider2D other){ if (other.gameObject.tag == "Player") @@ -122,17 +123,26 @@ namespace BigSock { - private void Attack_OnColliderStay2D(Collider2D other){ - if (other.gameObject.tag == "Player"){ - var player = other.gameObject.GetComponent<PlayerController>(); - if(player.TakeDamage(Damage)){ - //rb position - player position - Vector2 difference = (other.transform.position - transform.position).normalized; - player.KnockBack(KnockbackForce, difference); - //animer nå ? - - } - } + private void Attack_OnColliderStay2D(Collider2D other) { + var player = other.gameObject.GetComponent<PlayerController>(); + if(player != null) { + // Create attack object. + var attack = new AttackStats{ + Damage = Damage, + Knockback = KnockbackForce, + Range = 0, + AttackSpeed = AttackSpeed, + Source = transform.position, + }; + + // Get the player to take the damage. + if(player.TakeDamage(attack)){ + //rb position - player position + //Vector2 difference = (other.transform.position - transform.position).normalized; + //player.KnockBack(KnockbackForce, difference); + //animer nå ? + } + } } private void Attack_OnColliderExit2D(Collider2D other){ if (other.gameObject.tag == "Player") @@ -141,7 +151,10 @@ namespace BigSock { } - public partial class EnemyController { //move + /* + Move + */ + public partial class EnemyController { private void Move_OnColliderEnter2D(Collider2D other) {