From 827588bb1858fe3984741c97bd3c633073799c08 Mon Sep 17 00:00:00 2001 From: MindfulMinun Date: Fri, 25 Feb 2022 13:12:42 -0600 Subject: [PATCH] Health system + Water Spirit AI! --- Assets/Art Final/squid_spritesheet.png | Bin 0 -> 453 bytes Assets/Art Final/squid_spritesheet.png.meta | 192 ++++++++++++++++++++ Assets/Health.cs | 29 +-- Assets/Prefabs/Water Spirit.prefab | 36 +++- Assets/Scene 1 Assets/PlayerMovement.cs | 36 ++-- Assets/Scenes/Aquaphobia.unity | 10 +- Assets/WaterSpirit.cs | 11 +- 7 files changed, 281 insertions(+), 33 deletions(-) create mode 100644 Assets/Art Final/squid_spritesheet.png create mode 100644 Assets/Art Final/squid_spritesheet.png.meta diff --git a/Assets/Art Final/squid_spritesheet.png b/Assets/Art Final/squid_spritesheet.png new file mode 100644 index 0000000000000000000000000000000000000000..18a691fb0866da5b1ca3efc31a860d7e72de9a7c GIT binary patch literal 453 zcmV;$0XqJPP)000^Y1^@s6|D2(?00001b5ch_0Itp) z=>Px$eMv+?R9Hu2WEcfRhX5ndDn}JEj0P47iC{FaMgxn45FkM*Q7KDLN&o+)dl!jP zOM=~WS4ycr2nGPif{S-AFz72AFwio)^A3zUSbl8!JuFL}=(*(1o%HqVA?sS1`{hmsvcHA+Ewo|?5FdQk%_R|dNs z1XC7r;YbV5pclY{*$$$b4j>P}0*#uUK@O;)UPrHSHi+ z8-i*-Q1y;Xj|LW6sSAn`^gx2~sTo*9$qs@AJSbpbV@=c^a~f(9La^yTM2~_ZCFl`B v?Fa!mj#_pMcwkW!(sb558dxL*H~=e-|0=KZ-0c7W002ovPDHLkV1fVu>$0w? literal 0 HcmV?d00001 diff --git a/Assets/Art Final/squid_spritesheet.png.meta b/Assets/Art Final/squid_spritesheet.png.meta new file mode 100644 index 0000000..75dc116 --- /dev/null +++ b/Assets/Art Final/squid_spritesheet.png.meta @@ -0,0 +1,192 @@ +fileFormatVersion: 2 +guid: 1a0db7ae73c9e43ac856b25d6ad47dbc +TextureImporter: + internalIDToNameTable: + - first: + 213: -5029764186598712489 + second: squid_spritesheet_0 + - first: + 213: -3022340133225536526 + second: squid_spritesheet_1 + - first: + 213: -2944302567237882039 + second: squid_spritesheet_2 + externalObjects: {} + serializedVersion: 11 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + vTOnly: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: 0 + aniso: 1 + mipBias: 0 + wrapU: 1 + wrapV: 1 + wrapW: 1 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 2 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 16 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 8 + textureShape: 1 + singleChannelComponent: 0 + flipbookRows: 1 + flipbookColumns: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + ignorePngGamma: 0 + applyGammaDecoding: 0 + platformSettings: + - serializedVersion: 3 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 3 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 3 + buildTarget: WebGL + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: + - serializedVersion: 2 + name: squid_spritesheet_0 + rect: + serializedVersion: 2 + x: 0 + y: 0 + width: 25 + height: 26 + alignment: 0 + pivot: {x: 0, y: 0} + border: {x: 0, y: 0, z: 0, w: 0} + outline: [] + physicsShape: [] + tessellationDetail: 0 + bones: [] + spriteID: 75b5dc10f10b23ab0800000000000000 + internalID: -5029764186598712489 + vertices: [] + indices: + edges: [] + weights: [] + - serializedVersion: 2 + name: squid_spritesheet_1 + rect: + serializedVersion: 2 + x: 25 + y: 0 + width: 25 + height: 26 + alignment: 0 + pivot: {x: 0, y: 0} + border: {x: 0, y: 0, z: 0, w: 0} + outline: [] + physicsShape: [] + tessellationDetail: 0 + bones: [] + spriteID: 2f36fa079ad7e06d0800000000000000 + internalID: -3022340133225536526 + vertices: [] + indices: + edges: [] + weights: [] + - serializedVersion: 2 + name: squid_spritesheet_2 + rect: + serializedVersion: 2 + x: 50 + y: 0 + width: 25 + height: 26 + alignment: 0 + pivot: {x: 0, y: 0} + border: {x: 0, y: 0, z: 0, w: 0} + outline: [] + physicsShape: [] + tessellationDetail: 0 + bones: [] + spriteID: 94fe3882a6cb327d0800000000000000 + internalID: -2944302567237882039 + vertices: [] + indices: + edges: [] + weights: [] + outline: [] + physicsShape: [] + bones: [] + spriteID: 5e97eb03825dee720800000000000000 + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + spritePackingTag: + pSDRemoveMatte: 0 + pSDShowRemoveMatteOption: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Health.cs b/Assets/Health.cs index c64cbef..4abf499 100644 --- a/Assets/Health.cs +++ b/Assets/Health.cs @@ -1,40 +1,41 @@ using UnityEngine; using UnityEngine.Events; +using System.Collections; // @MindfulMinun public class Health : MonoBehaviour { public int startingHealth = 3; + public float damageTimeout = 1f; public int currentHealth; public bool isInvincible; public bool isDead; - public Animator healthBarAnimator; - public UnityEvent OnDead; - public UnityEvent OnHit; + public UnityEvent OnDead = new UnityEvent(); + public UnityEvent OnHit = new UnityEvent(); void Start() { - OnDead = new UnityEvent(); - OnHit = new UnityEvent(); - if (healthBarAnimator == null) { - Debug.Log("HealthBarAnimator not found! Please attach the Health bar's animator to the Health script so the health bar can be animated."); - } currentHealth = startingHealth; } void Update() { if (Input.GetKeyDown(KeyCode.E)) { takeHit(); - OnHit.Invoke(); - } - - if (healthBarAnimator != null) { - healthBarAnimator.SetInteger("Health", currentHealth); } } - void takeHit() { + public void takeHit() { if (isInvincible) return; currentHealth = Mathf.Clamp(currentHealth - 1, 0, startingHealth); isDead = currentHealth <= 0; + OnHit.Invoke(); if (isDead) OnDead.Invoke(); + StartCoroutine(damageTimer()); + } + + // Waits for a second before allowing the player to take damage again + private IEnumerator damageTimer() { + bool wasInvincible = isInvincible; + isInvincible = true; + yield return new WaitForSeconds(damageTimeout); + isInvincible = wasInvincible; } } diff --git a/Assets/Prefabs/Water Spirit.prefab b/Assets/Prefabs/Water Spirit.prefab index 3118f06..32efde0 100644 --- a/Assets/Prefabs/Water Spirit.prefab +++ b/Assets/Prefabs/Water Spirit.prefab @@ -9,6 +9,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 5789802511877378932} + - component: {fileID: 1864841646642678475} m_Layer: 0 m_Name: Hitbox m_TagString: Untagged @@ -24,12 +25,38 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4636133840327072359} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: -0.19406313, y: -0.4422784, z: -91.9304} + m_LocalPosition: {x: -0.191, y: -0.063, z: 0} m_LocalScale: {x: 0.16666667, y: 0.16666667, z: 1} m_Children: [] m_Father: {fileID: 7026014451242677585} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!61 &1864841646642678475 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4636133840327072359} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0, y: 0} + oldSize: {x: 0, y: 0} + newSize: {x: 0, y: 0} + adaptiveTilingThreshold: 0 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 2.79, y: 6.9} + m_EdgeRadius: 0 --- !u!1 &7026014451242677587 GameObject: m_ObjectHideFlags: 0 @@ -57,8 +84,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 7026014451242677587} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 2.01, y: 2.52, z: 0} - m_LocalScale: {x: 6, y: 6, z: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 5789802511877378932} m_Father: {fileID: 0} @@ -104,7 +131,7 @@ SpriteRenderer: m_SortingLayerID: 1027733709 m_SortingLayer: 2 m_SortingOrder: 0 - m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0} + m_Sprite: {fileID: -5029764186598712489, guid: 1a0db7ae73c9e43ac856b25d6ad47dbc, type: 3} m_Color: {r: 1, g: 1, b: 1, a: 1} m_FlipX: 0 m_FlipY: 0 @@ -127,6 +154,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: edd699794297e4f719289f355101655e, type: 3} m_Name: m_EditorClassIdentifier: + boxCollider: {fileID: 0} swimSpeed: 1 --- !u!50 &7026014451242677591 Rigidbody2D: diff --git a/Assets/Scene 1 Assets/PlayerMovement.cs b/Assets/Scene 1 Assets/PlayerMovement.cs index 9b1556c..839e406 100644 --- a/Assets/Scene 1 Assets/PlayerMovement.cs +++ b/Assets/Scene 1 Assets/PlayerMovement.cs @@ -7,13 +7,18 @@ public class PlayerMovement : MonoBehaviour { public Movement controller; public Animator animator; public Health health; + public Animator healthBarAnimator; public float runSpeed = 40f; float horizontalMove = 0f; bool jump = false; bool crouch = false; - void Start() { + void Awake() { + if (health == null) { + Debug.Log("Health not found! Please attach the Health script to the player so the player can take damage."); + } + Debug.Log(health); if (health != null) { health.OnHit.AddListener(OnHit); health.OnDead.AddListener(OnDead); @@ -23,6 +28,10 @@ public class PlayerMovement : MonoBehaviour { // Update is called once per frame void Update() { horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; + + if (healthBarAnimator != null) { + healthBarAnimator.SetInteger("Health", health.currentHealth); + } if (Input.GetButtonDown("Jump")) { jump = true; @@ -34,34 +43,34 @@ public class PlayerMovement : MonoBehaviour { crouch = false; } - if(Movement.pause) { + if (Movement.pause) { animator.SetFloat("Horizontal", 0); - } - if (animator != null && !Movement.pause) { + } else if (animator != null) { animator.SetFloat("Horizontal", Input.GetAxis("Horizontal")); animator.SetBool("Airborne", !controller.m_Grounded); } } void FixedUpdate() { + if (Movement.pause) return; controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump); jump = false; } //if colliding with platform, move with platform void OnCollisionEnter2D (Collision2D other) { - if (other.gameObject.tag == "Platform") { - transform.parent = other.transform; - } + if (other.gameObject.tag == "Platform") { + transform.parent = other.transform; + } // if(other.gameObject.tag == "Detection") { // Debug.Log("detect"); // other.gameObject.GetComponent().Detected(transform.position - other.gameObject.transform.position); // } - } - void OnCollisionExit2D (Collision2D other) { - if (other.gameObject.tag == "Platform") { - transform.parent = null; - } - } + } + void OnCollisionExit2D (Collision2D other) { + if (other.gameObject.tag == "Platform") { + transform.parent = null; + } + } public void ChangePos(double x, double y) { @@ -78,5 +87,6 @@ public class PlayerMovement : MonoBehaviour { void OnDead() { Debug.Log("Fuck, I'm dead!"); + Movement.pause = true; } } diff --git a/Assets/Scenes/Aquaphobia.unity b/Assets/Scenes/Aquaphobia.unity index 8c0ada9..dd251dd 100644 --- a/Assets/Scenes/Aquaphobia.unity +++ b/Assets/Scenes/Aquaphobia.unity @@ -484,10 +484,18 @@ PrefabInstance: propertyPath: m_GravityScale value: 1 objectReference: {fileID: 0} + - target: {fileID: 5439994587591701612, guid: faf72c53445dba543b9e5a89dc76b3e1, type: 3} + propertyPath: health + value: + objectReference: {fileID: 1113210904} - target: {fileID: 5439994587591701612, guid: faf72c53445dba543b9e5a89dc76b3e1, type: 3} propertyPath: runSpeed value: 20 objectReference: {fileID: 0} + - target: {fileID: 5439994587591701612, guid: faf72c53445dba543b9e5a89dc76b3e1, type: 3} + propertyPath: healthBarAnimator + value: + objectReference: {fileID: 128127589} - target: {fileID: 5439994587591701614, guid: faf72c53445dba543b9e5a89dc76b3e1, type: 3} propertyPath: m_TagString value: Player @@ -512,10 +520,10 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: startingHealth: 3 + damageTimeout: 1 currentHealth: 0 isInvincible: 0 isDead: 0 - healthBarAnimator: {fileID: 128127589} OnDead: m_PersistentCalls: m_Calls: [] diff --git a/Assets/WaterSpirit.cs b/Assets/WaterSpirit.cs index a17ac96..93fba2c 100644 --- a/Assets/WaterSpirit.cs +++ b/Assets/WaterSpirit.cs @@ -4,6 +4,7 @@ using UnityEngine; public class WaterSpirit : MonoBehaviour { private Transform player; + public BoxCollider2D boxCollider; public float swimSpeed = .25f; private Rigidbody2D rb; private Vector2 movement = Vector2.zero; @@ -11,6 +12,7 @@ public class WaterSpirit : MonoBehaviour { void Start() { rb = this.GetComponent(); + boxCollider = this.GetComponent(); sizeOffset = Random.Range(2f, 10f); player = GameObject.FindWithTag("Player").transform; if (player == null) { @@ -23,14 +25,21 @@ public class WaterSpirit : MonoBehaviour { Vector3 direction = player.position - transform.position; movement = direction.normalized; // Scale spirit size so they look like the swim like squid lol - float size = Mathf.Sin(Time.time * sizeOffset) + 4; + float size = Mathf.Sin(Time.time * sizeOffset) * .25f + 1; transform.localScale = new Vector3(size, size, 1); } void FixedUpdate() { + if (Movement.pause) return; moveMe(movement); } + private void OnTriggerEnter2D(Collider2D collision) { + if (collision.tag == "Player") { + collision.GetComponent().takeHit(); + } + } + void moveMe(Vector2 direction) { rb.MovePosition(rb.position + direction * swimSpeed * Time.fixedDeltaTime); }