From 0db69eab5937094d154c02659ae047f733f65f64 Mon Sep 17 00:00:00 2001 From: Yann Dupont 01 Date: Sat, 2 Apr 2022 12:25:09 -0400 Subject: [PATCH] Blood sucking start --- Assets/Prefabs/Vampire.prefab | 29 +++++++++- Assets/Scenes/YannTest.unity | 4 +- Assets/Scripts/BloodSucker.cs | 90 ++++++++++++++++++++++++++++++ Assets/Scripts/BloodSucker.cs.meta | 11 ++++ 4 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 Assets/Scripts/BloodSucker.cs create mode 100644 Assets/Scripts/BloodSucker.cs.meta diff --git a/Assets/Prefabs/Vampire.prefab b/Assets/Prefabs/Vampire.prefab index e244e45..70b4266 100644 --- a/Assets/Prefabs/Vampire.prefab +++ b/Assets/Prefabs/Vampire.prefab @@ -12,6 +12,7 @@ GameObject: - component: {fileID: 1214567908930553592} - component: {fileID: 3126145803593047825} - component: {fileID: 1967503440015794769} + - component: {fileID: 7967951869135974023} - component: {fileID: 1214567908930553595} - component: {fileID: 1214567908930553477} - component: {fileID: 945832017} @@ -86,6 +87,20 @@ MonoBehaviour: m_EditorClassIdentifier: minionPrefabs: [] aimArrow: {fileID: 5124059627794595469} +--- !u!114 &7967951869135974023 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1214567908930553593} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4bf4411b21939dd4780eef6579306f3d, type: 3} + m_Name: + m_EditorClassIdentifier: + suckDuration: 1 + suckRange: 3 --- !u!212 &1214567908930553595 SpriteRenderer: m_ObjectHideFlags: 0 @@ -207,7 +222,19 @@ MonoBehaviour: m_ActionId: eefb12f1-0980-4e79-a169-5b8d9c5f8909 m_ActionName: Player/Jump[/XInputControllerWindows/buttonSouth,/Keyboard/space] - m_PersistentCalls: - m_Calls: [] + m_Calls: + - m_Target: {fileID: 7967951869135974023} + m_TargetAssemblyTypeName: BloodSucker, Assembly-CSharp + m_MethodName: ToggleSuck + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 m_ActionId: bd3662a8-0f8c-46c3-a035-be5ce123a673 m_ActionName: Player/Suck[/XInputControllerWindows/buttonWest,/Keyboard/e] - m_PersistentCalls: diff --git a/Assets/Scenes/YannTest.unity b/Assets/Scenes/YannTest.unity index dcb3214..9b0efe4 100644 --- a/Assets/Scenes/YannTest.unity +++ b/Assets/Scenes/YannTest.unity @@ -132,7 +132,7 @@ PrefabInstance: m_Modifications: - target: {fileID: 3328484984159178892, guid: f7f5d2b1228d13f4d9015073aced3e81, type: 3} propertyPath: m_IsActive - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 8365024801073869717, guid: f7f5d2b1228d13f4d9015073aced3e81, type: 3} propertyPath: m_LocalPosition.x @@ -220,7 +220,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8365024802335227869, guid: f7f5d2b1228d13f4d9015073aced3e81, type: 3} propertyPath: orthographic size - value: 7 + value: 10 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: f7f5d2b1228d13f4d9015073aced3e81, type: 3} diff --git a/Assets/Scripts/BloodSucker.cs b/Assets/Scripts/BloodSucker.cs new file mode 100644 index 0000000..a2735fa --- /dev/null +++ b/Assets/Scripts/BloodSucker.cs @@ -0,0 +1,90 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + +public class BloodSucker : MonoBehaviour { + + [SerializeField] float suckDuration = 1f; + [SerializeField] float suckRange = 1f; + + Entity currentTarget; + VampireEntity vampireEntity; + bool isSucking; + float currentSuckTimer; + + void Awake() { + vampireEntity = GetComponent(); + } + + void FixedUpdate() { + if (currentTarget == null) { + SearchSuckTarget(); + } + + if (currentTarget != null) { + if (Vector3.Distance(currentTarget.transform.position, transform.position) > suckRange) { + SetTarget(null); + } else { + if (isSucking) { + PerformSuck(Time.fixedDeltaTime); + // } else { + // HighlightTarget(); + } + } + } + } + + public void ToggleSuck(InputAction.CallbackContext context) { + if (context.performed) { + if (currentTarget != null) { + isSucking = true; + currentSuckTimer = suckDuration; + } + } else if (context.canceled) { + isSucking = false; + } + } + + void SearchSuckTarget() { + foreach (Collider2D coll in Physics2D.OverlapCircleAll(transform.position, suckRange)) { + Entity entity = coll.GetComponent(); + if (entity != null && entity.gameObject != gameObject) { + // TODO : check if target is dead + SetTarget(entity); + } + } + } + + void SetTarget(Entity newTarget) { + if (currentTarget != null) { + UnHighlightTarget(); + } + + currentTarget = newTarget; + + if (currentTarget != null) { + // print("new target : " + currentTarget.name); + HighlightTarget(); + } + } + + void PerformSuck(float deltaTime) { + currentSuckTimer -= deltaTime; + if (currentSuckTimer < 0f) { + // print("One token sucked"); + // TODO check if no token left + SetTarget(null); + isSucking = false; + } + } + + void HighlightTarget() { + + } + + void UnHighlightTarget() { + + } + +} diff --git a/Assets/Scripts/BloodSucker.cs.meta b/Assets/Scripts/BloodSucker.cs.meta new file mode 100644 index 0000000..40e76ce --- /dev/null +++ b/Assets/Scripts/BloodSucker.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4bf4411b21939dd4780eef6579306f3d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: