From fec9ee55a5c248a46afc6a751da9954981047ddc Mon Sep 17 00:00:00 2001
From: Ny Bruker <robinhs@stud.ntnu.no>
Date: Sat, 24 Sep 2022 19:28:40 +0200
Subject: [PATCH] Made attacks damage the enemy. - OnColide for projectiles. -
 Layers to prevent collision between player and bullets. - Commented out some
 unused print statements. - Added hp handling to EnemyController.

---
 MrBigsock/Assets/Code/EmptyCollider.cs        |  4 +-
 .../Assets/Code/attack/AttackMovement.cs      | 22 +++++++
 MrBigsock/Assets/Code/orc/EnemyController.cs  | 64 ++++++++++++++++---
 MrBigsock/Assets/Prefabs/BigSock.prefab       |  2 +-
 MrBigsock/Assets/Prefabs/attack.prefab        |  2 +-
 .../BurstAotSettings_StandaloneWindows.json   | 17 +++++
 .../BurstAotSettings_WebGL.json               | 14 ++++
 .../CommonBurstAotSettings.json               |  6 ++
 .../ProjectSettings/TimelineSettings.asset    | 16 +++++
 9 files changed, 133 insertions(+), 14 deletions(-)
 create mode 100644 MrBigsock/ProjectSettings/BurstAotSettings_StandaloneWindows.json
 create mode 100644 MrBigsock/ProjectSettings/BurstAotSettings_WebGL.json
 create mode 100644 MrBigsock/ProjectSettings/CommonBurstAotSettings.json
 create mode 100644 MrBigsock/ProjectSettings/TimelineSettings.asset

diff --git a/MrBigsock/Assets/Code/EmptyCollider.cs b/MrBigsock/Assets/Code/EmptyCollider.cs
index c30a1746..0198f138 100644
--- a/MrBigsock/Assets/Code/EmptyCollider.cs
+++ b/MrBigsock/Assets/Code/EmptyCollider.cs
@@ -12,7 +12,7 @@ namespace BigSock {
 	
 			private void OnTriggerEnter2D(Collider2D other)
 			{
-					Debug.Log("enter");
+					//Debug.Log("enter");
 					OnColliderEnter2D_Action?.Invoke(other);
 			}
 
@@ -23,7 +23,7 @@ namespace BigSock {
 
 			private void OnTriggerExit2D(Collider2D other)
 			{
-					Debug.Log("exit");
+					//Debug.Log("exit");
 					OnColliderExit2D_Action?.Invoke(other);
 			}
 	}
diff --git a/MrBigsock/Assets/Code/attack/AttackMovement.cs b/MrBigsock/Assets/Code/attack/AttackMovement.cs
index c6daed42..5855e881 100644
--- a/MrBigsock/Assets/Code/attack/AttackMovement.cs
+++ b/MrBigsock/Assets/Code/attack/AttackMovement.cs
@@ -12,6 +12,12 @@ namespace BigSock {
 		bool moved = false;
 		Vector3 direction;
 
+		/*
+			Damage of the character.
+		*/
+		public double Damage => baseDamage;
+		public double baseDamage = 1;
+
 		// Start is called before the first frame update
 		void Start()
 		{
@@ -35,5 +41,21 @@ namespace BigSock {
 			transform.Translate(direction * speed * Time.deltaTime);
 				
 		}
+		
+		
+		void OnCollisionEnter2D(Collision2D collision)
+		{
+			print($"[AttackMovement.OnCollisionEnter2D()] {collision.transform.position}");
+			var target = collision.gameObject.GetComponent<EnemyController>();
+
+			if(target != null){
+				if(target.TakeDamage(Damage)){
+					
+				}
+			} 
+		}
+
+
+
 	}
 }
\ No newline at end of file
diff --git a/MrBigsock/Assets/Code/orc/EnemyController.cs b/MrBigsock/Assets/Code/orc/EnemyController.cs
index ec6f25fe..195e3d5e 100644
--- a/MrBigsock/Assets/Code/orc/EnemyController.cs
+++ b/MrBigsock/Assets/Code/orc/EnemyController.cs
@@ -2,6 +2,7 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
+using System;
 
 
 namespace BigSock {
@@ -43,6 +44,49 @@ namespace BigSock {
 			attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D;
 		}
 
+
+
+		public bool Alive { get; private set; } = true;
+		public DateTime NextTimeCanTakeDamage { get; private set; } = DateTime.Now;
+		public TimeSpan IFrameDuration { get; private set; } = new TimeSpan(0, 0, 0, 0, 333);
+		/*
+				Adds damage to the player if they don't have IFrames.
+		*/
+		public bool TakeDamage(double amount) {
+			// Check if player has IFrames
+			if(NextTimeCanTakeDamage > DateTime.Now)
+				return false;
+			// Start new IFrames
+			NextTimeCanTakeDamage = DateTime.Now + IFrameDuration;
+
+			print($"[EnemyController.TakeDamage()] start. | {HP} - {amount}");
+
+			// Add damage
+			HP -= amount;
+
+			TryKill();
+
+			return true;
+		}
+
+		/*
+				Try to kill the player.
+		*/
+		public bool TryKill() {
+			if(Alive && HP <= 0) {
+				print($"[EnemyController.TryKill()] start. | {HP}, {Alive}");
+				Alive = false;
+
+				Destroy(this);
+
+				return true;
+			}
+			return false;
+		}
+
+
+
+
 		private void RotateAnimation(Vector2 direction)
 		{
 			if (direction.x > 0.01f){
@@ -109,21 +153,21 @@ namespace BigSock {
 	public partial class EnemyController { //attack
 
 		private void Attack_OnColliderEnter2D(Collider2D other){
-				if (other.gameObject.tag == "Player")
-						isInMelee = true;
+			if (other.gameObject.tag == "Player")
+				isInMelee = true;
 		}
 
 		
 
 		private void Attack_OnColliderStay2D(Collider2D other){
-				if (other.gameObject.tag == "Player"){
-						var player = other.gameObject.GetComponent<PlayerController>();
-						if(player.TakeDamage(Damage)){
-								//knockback ? 
-								//animer nå ?
-								
-					}
-				} 
+			if (other.gameObject.tag == "Player"){
+				var player = other.gameObject.GetComponent<PlayerController>();
+				if(player.TakeDamage(Damage)){
+						//knockback ? 
+						//animer nå ?
+						
+				}
+			} 
 		}
 		private void Attack_OnColliderExit2D(Collider2D other){
 				if (other.gameObject.tag == "Player")
diff --git a/MrBigsock/Assets/Prefabs/BigSock.prefab b/MrBigsock/Assets/Prefabs/BigSock.prefab
index e001802e..ce2c672a 100644
--- a/MrBigsock/Assets/Prefabs/BigSock.prefab
+++ b/MrBigsock/Assets/Prefabs/BigSock.prefab
@@ -15,7 +15,7 @@ GameObject:
   - component: {fileID: 8339702841083125274}
   - component: {fileID: 8616172023331984945}
   - component: {fileID: 8280117626939289948}
-  m_Layer: 0
+  m_Layer: 9
   m_Name: BigSock
   m_TagString: Player
   m_Icon: {fileID: 0}
diff --git a/MrBigsock/Assets/Prefabs/attack.prefab b/MrBigsock/Assets/Prefabs/attack.prefab
index 1a9aeeaa..a6ad0cd9 100644
--- a/MrBigsock/Assets/Prefabs/attack.prefab
+++ b/MrBigsock/Assets/Prefabs/attack.prefab
@@ -14,7 +14,7 @@ GameObject:
   - component: {fileID: 7173793660907891949}
   - component: {fileID: 7173793660907891948}
   - component: {fileID: 5355219341519538053}
-  m_Layer: 0
+  m_Layer: 10
   m_Name: attack
   m_TagString: Untagged
   m_Icon: {fileID: 0}
diff --git a/MrBigsock/ProjectSettings/BurstAotSettings_StandaloneWindows.json b/MrBigsock/ProjectSettings/BurstAotSettings_StandaloneWindows.json
new file mode 100644
index 00000000..e02ae332
--- /dev/null
+++ b/MrBigsock/ProjectSettings/BurstAotSettings_StandaloneWindows.json
@@ -0,0 +1,17 @@
+{
+  "MonoBehaviour": {
+    "Version": 4,
+    "EnableBurstCompilation": true,
+    "EnableOptimisations": true,
+    "EnableSafetyChecks": false,
+    "EnableDebugInAllBuilds": false,
+    "UsePlatformSDKLinker": false,
+    "CpuMinTargetX32": 0,
+    "CpuMaxTargetX32": 0,
+    "CpuMinTargetX64": 0,
+    "CpuMaxTargetX64": 0,
+    "CpuTargetsX32": 6,
+    "CpuTargetsX64": 72,
+    "OptimizeFor": 0
+  }
+}
diff --git a/MrBigsock/ProjectSettings/BurstAotSettings_WebGL.json b/MrBigsock/ProjectSettings/BurstAotSettings_WebGL.json
new file mode 100644
index 00000000..771d37b7
--- /dev/null
+++ b/MrBigsock/ProjectSettings/BurstAotSettings_WebGL.json
@@ -0,0 +1,14 @@
+{
+  "MonoBehaviour": {
+    "Version": 4,
+    "EnableBurstCompilation": true,
+    "EnableOptimisations": true,
+    "EnableSafetyChecks": false,
+    "EnableDebugInAllBuilds": false,
+    "CpuMinTargetX32": 0,
+    "CpuMaxTargetX32": 0,
+    "CpuMinTargetX64": 0,
+    "CpuMaxTargetX64": 0,
+    "OptimizeFor": 0
+  }
+}
diff --git a/MrBigsock/ProjectSettings/CommonBurstAotSettings.json b/MrBigsock/ProjectSettings/CommonBurstAotSettings.json
new file mode 100644
index 00000000..0293dafc
--- /dev/null
+++ b/MrBigsock/ProjectSettings/CommonBurstAotSettings.json
@@ -0,0 +1,6 @@
+{
+  "MonoBehaviour": {
+    "Version": 4,
+    "DisabledWarnings": ""
+  }
+}
diff --git a/MrBigsock/ProjectSettings/TimelineSettings.asset b/MrBigsock/ProjectSettings/TimelineSettings.asset
new file mode 100644
index 00000000..cfaebd7a
--- /dev/null
+++ b/MrBigsock/ProjectSettings/TimelineSettings.asset
@@ -0,0 +1,16 @@
+%YAML 1.1
+%TAG !u! tag:unity3d.com,2011:
+--- !u!114 &1
+MonoBehaviour:
+  m_ObjectHideFlags: 61
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 0}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: a287be6c49135cd4f9b2b8666c39d999, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  assetDefaultFramerate: 60
+  m_DefaultFrameRate: 60
-- 
GitLab