From 7afb27396ba75b6f5ab8cd5dd91f5452a80f3b84 Mon Sep 17 00:00:00 2001 From: Hubert Stachowiak <76792092+CreativeNameHUH@users.noreply.github.com> Date: Sat, 13 Aug 2022 23:09:35 +0200 Subject: [PATCH] Remove compiler warnings Added missing namespaces to the classes. Added missing access modifiers. Removed null check for colliders on each update, list of available collision function is used instead. Removed unused variables, methods, and comments. Small codes optimization. --- .../Scripts/BaseScripts/BaseCharacter.cs | 230 +++++----------- .../Assets/Scripts/BaseScripts/BaseWorld.cs | 16 +- GeckoAndCricket/Assets/Scripts/BulletEvent.cs | 25 +- .../Assets/Scripts/EnemiesScripts/BatAI.cs | 135 ++++----- .../Assets/Scripts/EnemiesScripts/FrogAI.cs | 139 +++++----- .../Scripts/EnemiesScripts/MillipedeAI.cs | 26 +- .../Assets/Scripts/EnemiesScripts/SnakeAI.cs | 33 ++- .../Scripts/EnemiesScripts/SundewTurret.cs | 17 +- .../Assets/Scripts/EnemiesScripts/TurtleAI.cs | 24 +- .../Assets/Scripts/PlayerScripts/Player.cs | 259 ++++++++---------- .../Assets/Scripts/PlayerScripts/Shooting.cs | 55 ++-- GeckoAndCricket/Assets/Scripts/Rope.cs | 5 +- GeckoAndCricket/Assets/Scripts/RopeSegment.cs | 5 +- .../GeckoAndCricket.sln.DotSettings | 17 +- 14 files changed, 460 insertions(+), 526 deletions(-) diff --git a/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseCharacter.cs b/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseCharacter.cs index 39542ae..975f9a9 100644 --- a/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseCharacter.cs +++ b/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseCharacter.cs @@ -1,5 +1,6 @@ #nullable enable using System; +using System.Collections.Generic; using Newtonsoft.Json; using UnityEngine; using Directory = System.IO.Directory; @@ -20,8 +21,7 @@ public class BaseCharacter : MonoBehaviour public float jumpForce = 12f; public float stompForce = 2f; public float pushForce = 7f; - public int maxJumps = 1; - + // Character private movement fields: private float _tempMovementSpeed; @@ -34,13 +34,13 @@ public class BaseCharacter : MonoBehaviour public float ceilingCheckSize = 0.2f; // Character protected fields: - protected Rigidbody2D Rigidbody; - protected HingeJoint2D HingeJoint; + protected Rigidbody2D characterRigidbody; + protected HingeJoint2D characterHingeJoint; // Character protected collision fields // TODO: Not sure about using ints instead of enums - protected int FloorType; - protected int WallType; + protected int floorType; + protected int wallType; [Header("Character flags:")] public bool isFacingRight = true; @@ -48,23 +48,26 @@ public class BaseCharacter : MonoBehaviour public bool isFlippedHorizontally; // Character protected flags: - protected bool IsGrounded = true; - protected bool IsTouchingWall; - protected bool IsTouchingCeiling; - protected bool IsAttachedToRope; + protected bool isGrounded = true; + protected bool isTouchingWall; + protected bool isTouchingCeiling; + protected bool isAttachedToRope; // Character private flags: private bool _wasTouchingDifferentFloor; private bool _wasTouchingDifferentWall; + + // Available collision functions: + private readonly List _collisionFunctions = new List(); [Header("Debug BaseCharacter:")] public Vector3 resetPosition; #pragma warning restore 8618 #region Getters and Setters - public bool IsGroundedFlag { get => IsGrounded; set => IsGrounded = value; } - public bool IsTouchingWallFlag { get => IsTouchingWall; set => IsTouchingWall = value; } - public bool IsTouchingCeilingFlag { get => IsTouchingCeiling; set => IsTouchingCeiling = value; } + public bool IsGroundedFlag { get => isGrounded; set => isGrounded = value; } + public bool IsTouchingWallFlag { get => isTouchingWall; set => isTouchingWall = value; } + public bool IsTouchingCeilingFlag { get => isTouchingCeiling; set => isTouchingCeiling = value; } #endregion @@ -77,7 +80,6 @@ public bool FlipHorizontally() { Vector3 flip = body.localScale; flip.x *= -1; - //isFacingRight = !isFacingRight; body.localScale = flip; return isFlippedHorizontally = !isFlippedHorizontally; @@ -129,78 +131,12 @@ private void SetRotation(float xAngle, float yAngle, float zAngle) { body.localEulerAngles = new Vector3(xAngle, yAngle, zAngle); } - - /// - /// Rotates character sprite in x axis. - /// - /// angle has to be in range from -360 to 360 - /// Returns new x angle. - public float RotateX(float angle) - { - if (angle < -360f || angle > 360f) - throw new ArgumentOutOfRangeException(); - - return Rotate(angle, 0f, 0f).x; - } - - /// - /// Rotates character sprite in y axis. - /// - /// angle has to be in range from -360 to 360 - /// Returns new y angle. - public float RotateY(float angle) - { - if (angle < -360f || angle > 360f) - throw new ArgumentOutOfRangeException(); - - return Rotate(0f, angle, 0f).y; - } - - /// - /// Rotates character sprite in z axis. - /// - /// angle has to be in range from -360 to 360 - /// Returns new z angle. - public float RotateZ(float angle) - { - if (angle < -360f || angle > 360f) - throw new ArgumentOutOfRangeException(); - - return Rotate(0f, 0f, angle).z; - } - - private Vector3 Rotate(float xAngle, float yAngle, float zAngle) - { - Vector3 currentRotation = body.localEulerAngles; - - if (currentRotation.x > 359f || currentRotation.x < -359f) - { - currentRotation.x = 0f; - } - - if (currentRotation.y > 359f || currentRotation.y < -359f) - { - currentRotation.y = 0f; - } - - if (currentRotation.z > 359f || currentRotation.z < -359f) - { - currentRotation.z = 0f; - } - - Vector3 newRotation = new Vector3(currentRotation.x + xAngle, currentRotation.y + yAngle, - currentRotation.z + zAngle); - - body.localEulerAngles = newRotation; - - return newRotation; - } #endregion #region Collisions - private bool CheckWallCollision() + private void CheckWallCollision() { - foreach (int? type in BaseWorld.WallType.GetSurfaceTypes()) + foreach (int? type in BaseWorld.wallType.GetSurfaceTypes()) { // TODO: We shouldn't check that everytime. if (type == null) @@ -209,16 +145,17 @@ private bool CheckWallCollision() if (!Physics2D.OverlapCircle(wallCollider.position, wallCheckSize, (int) type)) continue; - WallType = (int) type; - return true; + wallType = (int) type; + isTouchingWall = true; + return; } - return false; + isTouchingWall = false; } - private bool CheckFloorCollision() + private void CheckFloorCollision() { - foreach (int? type in BaseWorld.FloorType.GetSurfaceTypes()) + foreach (int? type in BaseWorld.floorType.GetSurfaceTypes()) { // TODO: We shouldn't check that everytime. if (type == null) @@ -226,77 +163,75 @@ private bool CheckFloorCollision() if (!Physics2D.OverlapCircle(groundCollider.position, groundCheckSize, (int) type)) continue; - FloorType = (int) type; - return true; + floorType = (int) type; + isGrounded = true; + return; } - return false; + isGrounded = false; + } + + private void CheckCeilingCollision() + { + isTouchingCeiling = Physics2D.OverlapCircle(ceilingCollider.position, ceilingCheckSize, BaseWorld.world.ceilingLayer); } public int InteractWithWallType() { - if (WallType == BaseWorld.WallType.Lava) + if (wallType == BaseWorld.wallType.Lava) { - Rigidbody.transform.localPosition = resetPosition; + characterRigidbody.transform.localPosition = resetPosition; _wasTouchingDifferentWall = true; - //Debug.Log("Lava"); } // ReSharper disable once CompareOfFloatsByEqualityOperator - else if (WallType == BaseWorld.WallType.Honey && movementSpeed != BaseWorld.World.honeySpeed) + else if (wallType == BaseWorld.wallType.Honey && movementSpeed != BaseWorld.world.honeySpeed) { - Rigidbody.gravityScale = 0f; - movementSpeed = BaseWorld.World.honeySpeed; + characterRigidbody.gravityScale = 0f; + movementSpeed = BaseWorld.world.honeySpeed; _wasTouchingDifferentWall = true; - //Debug.Log("Honey"); } // ReSharper disable once CompareOfFloatsByEqualityOperator - else if (WallType == BaseWorld.WallType.Ice && Rigidbody.gravityScale != 0f) + else if (wallType == BaseWorld.wallType.Ice && characterRigidbody.gravityScale != 0f) { - Rigidbody.gravityScale = BaseWorld.World.GetGravityScale(); + characterRigidbody.gravityScale = BaseWorld.world.GetGravityScale(); _wasTouchingDifferentWall = true; - //Debug.Log("Ice"); } - else if (WallType == BaseWorld.WallType.Normal) + else if (wallType == BaseWorld.wallType.Normal) { _wasTouchingDifferentWall = false; movementSpeed = _tempMovementSpeed; - Rigidbody.gravityScale = 0f; - //Debug.Log("Normal"); + characterRigidbody.gravityScale = 0f; } - return WallType; + return wallType; } public int InteractWithFloorType() { - if (FloorType == BaseWorld.FloorType.Lava) + if (floorType == BaseWorld.floorType.Lava) { - Rigidbody.transform.localPosition = resetPosition; - //Debug.Log("Lava"); + characterRigidbody.transform.localPosition = resetPosition; } // ReSharper disable once CompareOfFloatsByEqualityOperator - else if (FloorType == BaseWorld.FloorType.Honey && movementSpeed != BaseWorld.World.honeySpeed) + else if (floorType == BaseWorld.floorType.Honey && movementSpeed != BaseWorld.world.honeySpeed) { _wasTouchingDifferentFloor = true; - movementSpeed = BaseWorld.World.honeySpeed; - //Debug.Log("Honey"); + movementSpeed = BaseWorld.world.honeySpeed; } // ReSharper disable once CompareOfFloatsByEqualityOperator - else if (FloorType == BaseWorld.FloorType.Ice && movementSpeed != BaseWorld.World.iceSpeed) + else if (floorType == BaseWorld.floorType.Ice && movementSpeed != BaseWorld.world.iceSpeed) { _wasTouchingDifferentFloor = true; - movementSpeed = BaseWorld.World.iceSpeed; - //Debug.Log("Ice"); + movementSpeed = BaseWorld.world.iceSpeed; } - else if ((_wasTouchingDifferentFloor || _wasTouchingDifferentWall) && FloorType == BaseWorld.FloorType.Normal) + else if ((_wasTouchingDifferentFloor || _wasTouchingDifferentWall) && floorType == BaseWorld.floorType.Normal) { _wasTouchingDifferentFloor = false; _wasTouchingDifferentWall = false; movementSpeed = _tempMovementSpeed; - //Debug.Log("Normal"); } - return FloorType; + return floorType; } /// @@ -304,33 +239,12 @@ public int InteractWithFloorType() /// public void CheckCollision() { - // TODO: This shouldn't check for null every time the function is called. - if (groundCollider != null) - IsGrounded = CheckFloorCollision(); - if (wallCollider != null) - IsTouchingWall = CheckWallCollision(); - if (ceilingCollider != null) - IsTouchingCeiling = Physics2D.OverlapCircle(ceilingCollider.position, ceilingCheckSize, BaseWorld.World.ceilingLayer); + foreach (Action? action in _collisionFunctions) + action(); } #endregion #region Movement - /// - /// Moves character - /// - /// Argument takes a reference to Vector2 object. - public void Move(ref Vector2 direction) - { - if (!IsAttachedToRope) - { - Rigidbody.velocity = direction; - } - if ((direction.x > 0 && !isFacingRight) || (direction.x < 0 && isFacingRight)) - isFacingRight = !FlipHorizontally(); - if ((IsTouchingCeiling && !IsGrounded && !isFlippedVertically)||(!IsTouchingCeiling && isFlippedVertically)) - FlipVertically(); - } - /// /// Moves character. /// @@ -338,10 +252,10 @@ public void Move(ref Vector2 direction) /// Argument takes a reference to float y value. public void Move(ref float x, ref float y) { - Rigidbody.velocity = new Vector2(x, y); + characterRigidbody.velocity = new Vector2(x, y); if ((x > 0 && !isFacingRight) || (x < 0 && isFacingRight)) isFacingRight = !FlipHorizontally(); - if ((IsTouchingCeiling && !IsGrounded && !isFlippedVertically)||(!IsTouchingCeiling && isFlippedVertically)) + if ((isTouchingCeiling && !isGrounded && !isFlippedVertically)||(!isTouchingCeiling && isFlippedVertically)) FlipVertically(); } @@ -351,26 +265,26 @@ public void Move(ref float x, ref float y) /// Argument takes a reference to float x value. /// Argument takes a reference to float y value. /// Argument takes a reference to float direction value - public void Move(ref float x, ref float y, float direction) + public void Move(ref float x, ref float y, ref float direction) { - Rigidbody.AddForce(x * Vector2.right); - Rigidbody.velocity = new Vector2(Rigidbody.velocity.x, y); + characterRigidbody.AddForce(x * Vector2.right); + characterRigidbody.velocity = new Vector2(characterRigidbody.velocity.x, y); if ((direction > 0.01f && !isFacingRight && isFlippedHorizontally) || (direction < 0f && isFacingRight && !isFlippedHorizontally)) isFacingRight = !FlipHorizontally(); - if ((IsTouchingCeiling && !IsGrounded && !isFlippedVertically)||(!IsTouchingCeiling && isFlippedVertically)) + if ((isTouchingCeiling && !isGrounded && !isFlippedVertically)||(!isTouchingCeiling && isFlippedVertically)) FlipVertically(); } public void Jump() { - Vector2 jump = new Vector2(Rigidbody.velocity.x, jumpForce); - Rigidbody.velocity = jump; + Vector2 jump = new Vector2(characterRigidbody.velocity.x, jumpForce); + characterRigidbody.velocity = jump; } public void Stomp() { Vector2 stomp = new Vector2(0f, stompForce); - Rigidbody.velocity -= stomp; + characterRigidbody.velocity -= stomp; } #endregion @@ -418,6 +332,7 @@ public void Deserialize() return; } + // ReSharper disable once UnusedVariable var template = new { health, movementSpeed, jumpForce @@ -431,25 +346,25 @@ public void Deserialize() #if DEBUG public unsafe bool* GetIsGroundedPointer() { - fixed (bool* ptr = &IsGrounded) + fixed (bool* ptr = &isGrounded) return ptr; } public unsafe bool* GetIsTouchingWallPointer() { - fixed (bool* ptr = &IsTouchingWall) + fixed (bool* ptr = &isTouchingWall) return ptr; } public unsafe bool* GetIsTouchingCeilingPointer() { - fixed (bool* ptr = &IsTouchingCeiling) + fixed (bool* ptr = &isTouchingCeiling) return ptr; } public unsafe bool* GetIsAttachedToRopePointer() { - fixed (bool* ptr = &IsAttachedToRope) + fixed (bool* ptr = &isAttachedToRope) return ptr; } @@ -465,9 +380,16 @@ public void Deserialize() protected void Awake() { - Rigidbody = GetComponent(); - HingeJoint = GetComponent(); + characterRigidbody = GetComponent(); + characterHingeJoint = GetComponent(); _tempMovementSpeed = movementSpeed; + + if (groundCollider) + _collisionFunctions.Add(CheckFloorCollision); + if (wallCollider) + _collisionFunctions.Add(CheckWallCollision); + if (ceilingCollider) + _collisionFunctions.Add(CheckCeilingCollision); } protected void FixedUpdate() diff --git a/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseWorld.cs b/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseWorld.cs index b6197e6..821a087 100644 --- a/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseWorld.cs +++ b/GeckoAndCricket/Assets/Scripts/BaseScripts/BaseWorld.cs @@ -19,10 +19,10 @@ public class BaseWorld : MonoBehaviour public float gravityMultiplier = 1f; // Public: - public static BaseWorld World; - public static BaseSurfaceTypes FloorType; - public static BaseSurfaceTypes WallType; - public static GameObject Player; + public static BaseWorld world; + public static BaseSurfaceTypes floorType; + public static BaseSurfaceTypes wallType; + public static GameObject player; // Private: private float _gravityScale; @@ -34,13 +34,13 @@ public ref float GetGravityScale() private void Awake() { - if (World == null) + if (world == null) { - World = this; + world = this; } - FloorType ??= new FloorTypes(floorLayers); - WallType ??= new WallTypes(wallLayers); + floorType ??= new FloorTypes(floorLayers); + wallType ??= new WallTypes(wallLayers); } } diff --git a/GeckoAndCricket/Assets/Scripts/BulletEvent.cs b/GeckoAndCricket/Assets/Scripts/BulletEvent.cs index 2652da5..8f1474b 100644 --- a/GeckoAndCricket/Assets/Scripts/BulletEvent.cs +++ b/GeckoAndCricket/Assets/Scripts/BulletEvent.cs @@ -1,5 +1,3 @@ -using System.Collections; -using System.Collections.Generic; using EnemiesScripts; using UnityEngine; @@ -8,19 +6,18 @@ public class BulletEvent : MonoBehaviour private void OnTriggerEnter2D(Collider2D collider) { Destroy(gameObject); - if (collider.tag == "EnemyWeakPoint") + if (collider.tag != "EnemyWeakPoint") return; + + GameObject enemy = collider.transform.parent.gameObject; + if (enemy.GetComponent() == null) { - GameObject enemy = collider.transform.parent.gameObject; - if (enemy.GetComponent() == null) - { - Debug.Log("Pierwszy if"); - Destroy(enemy); - } - else if(!enemy.GetComponent().isImmune) - { - Debug.Log("Drugi if"); - Destroy(enemy); - } + //Debug.Log("Pierwszy if"); + Destroy(enemy); + } + else if(!enemy.GetComponent().isImmune) + { + //Debug.Log("Drugi if"); + Destroy(enemy); } } } diff --git a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/BatAI.cs b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/BatAI.cs index e76c85d..67ada1f 100644 --- a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/BatAI.cs +++ b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/BatAI.cs @@ -5,89 +5,102 @@ namespace EnemiesScripts { public class BatAI : MonoBehaviour { - [SerializeField] GameObject path; - private List BatPathPositions = new List(); - private int currentPosition; - public bool detectPlayer = false; - PolygonCollider2D batVisionCollider; - public bool isImmune = true; - public static float checkTimer = 3f; - public float currentCheckTimer = checkTimer; + [SerializeField] private GameObject path; + + // public variables: + public const float CheckTimer = 3f; + public float currentCheckTimer = CheckTimer; public float speedTime = 10f; + + // public flags: + public bool detectPlayer; + public bool isImmune = true; + + // private variables: + private int _currentPosition; + + private readonly List _batPathPositions = new List(); + private PolygonCollider2D _batVisionCollider; - - // Start is called before the first frame update - void Start() - { - batVisionCollider = this.gameObject.transform.GetChild(0).GetComponent(); - foreach (Transform child in path.transform) { - BatPathPositions.Add(child.position); - } - currentPosition = 0; - transform.position = BatPathPositions[0]; - - } - - // Update is called once per frame - void Update() + #region BatAI + private void CheckTerrain() { - Attack(); - CheckTerrain(); - } - void Attack() { - if (detectPlayer) + if (isImmune) { - batVisionCollider.enabled = false; - isImmune = false; - if (currentPosition == 1) + if (currentCheckTimer <= 0) { - speedTime = 20f; + _batVisionCollider.enabled = !_batVisionCollider.enabled; + currentCheckTimer = CheckTimer; } else { - speedTime = 10f; - } - if (currentPosition <= 1 && transform.position != BatPathPositions[currentPosition + 1]) { - transform.position = Vector3.MoveTowards(transform.position, BatPathPositions[currentPosition + 1], speedTime * Time.deltaTime); - if (transform.position == BatPathPositions[currentPosition + 1]) { - currentPosition += 1; - } - } - else { - if (transform.position != BatPathPositions[0]) - { - transform.position = Vector3.MoveTowards(transform.position, BatPathPositions[0], speedTime * Time.deltaTime); - } - else { - currentPosition = 0; - detectPlayer = false; - isImmune = true; - } + currentCheckTimer -= Time.deltaTime; } } + else + { + currentCheckTimer = CheckTimer; + } } - void CheckTerrain() { - if (isImmune) + + private void Attack() + { + if (!detectPlayer) return; + + _batVisionCollider.enabled = false; + isImmune = false; + + speedTime = _currentPosition == 1 ? 20f : 10f; + + if (_currentPosition <= 1 && transform.position != _batPathPositions[_currentPosition + 1]) { - if (currentCheckTimer <= 0) + transform.position = Vector3.MoveTowards(transform.position, _batPathPositions[_currentPosition + 1], speedTime * Time.deltaTime); + if (transform.position == _batPathPositions[_currentPosition + 1]) { - batVisionCollider.enabled = !batVisionCollider.enabled; - currentCheckTimer = checkTimer; + _currentPosition += 1; } - else + } + else + { + if (transform.position != _batPathPositions[0]) { - currentCheckTimer -= Time.deltaTime; + transform.position = Vector3.MoveTowards(transform.position, _batPathPositions[0], speedTime * Time.deltaTime); + } + else + { + _currentPosition = 0; + detectPlayer = false; + isImmune = true; } - } - else { - currentCheckTimer = checkTimer; } } + #endregion + + #region Unity private void OnTriggerEnter2D(Collider2D collision) { - if (collision.tag == "Player") { + if (collision.CompareTag("Player")) + { detectPlayer = true; } } + + private void Start() + { + _batVisionCollider = gameObject.transform.GetChild(0).GetComponent(); + foreach (Transform child in path.transform) + _batPathPositions.Add(child.position); + + _currentPosition = 0; + transform.position = _batPathPositions[0]; + + } + + private void Update() + { + Attack(); + CheckTerrain(); + } + #endregion } } diff --git a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/FrogAI.cs b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/FrogAI.cs index ca20f7f..36098fb 100644 --- a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/FrogAI.cs +++ b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/FrogAI.cs @@ -1,90 +1,101 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; using BaseScripts; -using BaseScripts.SurfaceTypes; using PlayerScripts; +using UnityEngine; -public class FrogAI : BaseCharacter +namespace EnemiesScripts { - private bool mustPatrol; - public bool mustTurn; - public bool grapplingPlayer; - // Start is called before the first frame update - void Start() - { - mustPatrol = true; - grapplingPlayer = false; - } - // Update is called once per frame - void Update() + public class FrogAI : BaseCharacter { - if (IsTouchingWall && IsGrounded || FloorType == BaseWorld.FloorType.Lava) + // public flags: + public bool mustTurn; + public bool grapplingPlayer; + + // private flags: + private bool _mustPatrol; + + #region FrogAI + private void Patrol() { - Flip(); + float x = movementSpeed; + float y = Mathf.Abs(jumpForce/2); + + if (isGrounded) + Move(ref x, ref y); } - if (mustPatrol) + + private void Attack() { - Patrol(); - } - else { - Attack(); + float x = movementSpeed*3; + float y = Mathf.Abs(jumpForce / 1.5f); + + if (isGrounded && !grapplingPlayer) + Move(ref x, ref y); } - } - void Patrol() { - float x = movementSpeed; - float y = Mathf.Abs(jumpForce/2); - if (IsGrounded) + public void Move(float x,float y) { - Move(x,y); + base.Move(ref x, ref y); } - } - void Attack() { - float x = movementSpeed*3; - float y = Mathf.Abs(jumpForce / 1.5f); - if (IsGrounded && !grapplingPlayer) + private void Flip() { - Move(x,y); + _mustPatrol = false; + movementSpeed *= -1; + _mustPatrol = true; } - } - public void Move(float x,float y){ - base.Move(ref x, ref y); - } - void Flip() - { - mustPatrol = false; - movementSpeed *= -1; - mustPatrol = true; - } - private void OnTriggerEnter2D(Collider2D collision) - { - if (collision.CompareTag("Player")) { - mustPatrol = false; + #endregion + + #region Unity + private void OnTriggerEnter2D(Collider2D collision) + { + if (collision.CompareTag("Player")) + _mustPatrol = false; } - } - private void OnTriggerExit2D(Collider2D collision) - { - if (collision.CompareTag("Player")) + + private void OnTriggerExit2D(Collider2D collision) { - mustPatrol = true; + if (!collision.CompareTag("Player")) return; + + _mustPatrol = true; grapplingPlayer = false; } - } - private void OnCollisionEnter2D(Collision2D collision) - { - if (collision.gameObject.CompareTag("Player")) { + + private void OnCollisionEnter2D(Collision2D collision) + { + if (!collision.gameObject.CompareTag("Player")) return; + grapplingPlayer = true; gameObject.GetComponent().isTrigger = true; Transform grapple = collision.gameObject.transform.Find("GrapplePosition").transform; - this.transform.parent = grapple; - this.transform.position = grapple.position; - Rigidbody.isKinematic = true; - Rigidbody.velocity = new Vector2(0f, 0f); + + Transform frogTransform = transform; + + frogTransform.parent = grapple; + frogTransform.position = grapple.position; + characterRigidbody.isKinematic = true; + characterRigidbody.velocity = new Vector2(0f, 0f); collision.gameObject.GetComponent().isGrappled = true; + } + + private void Start() + { + _mustPatrol = true; + grapplingPlayer = false; + } - - + private void Update() + { + if (isTouchingWall && isGrounded || floorType == BaseWorld.floorType.Lava) + { + Flip(); + } + if (_mustPatrol) + { + Patrol(); + } + else { + Attack(); + } } + #endregion } } diff --git a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/MillipedeAI.cs b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/MillipedeAI.cs index a7d0f7c..bf0ef49 100644 --- a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/MillipedeAI.cs +++ b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/MillipedeAI.cs @@ -16,20 +16,20 @@ public class MillipedeAI : SnakeAI // Private flags; private bool _isGoingDown; - #region AI + #region MillipedeAI private new void Patrol() { float xSpeed, ySpeed; if (seePlayer) { - ySpeed = IsTouchingWall ? verticalMovementSpeed * 2.0f : 0.0f; - xSpeed = (IsGrounded || IsTouchingCeiling) && !IsTouchingWall ? movementSpeed * 2.0f : 0.0f; + ySpeed = isTouchingWall ? verticalMovementSpeed * 2.0f : 0.0f; + xSpeed = (isGrounded || isTouchingCeiling) && !isTouchingWall ? movementSpeed * 2.0f : 0.0f; } else { - ySpeed = IsTouchingWall ? verticalMovementSpeed : 0.0f; - xSpeed = (IsGrounded || IsTouchingCeiling) && !IsTouchingWall ? movementSpeed : 0.0f; + ySpeed = isTouchingWall ? verticalMovementSpeed : 0.0f; + xSpeed = (isGrounded || isTouchingCeiling) && !isTouchingWall ? movementSpeed : 0.0f; } Move(ref xSpeed, ref ySpeed); @@ -66,13 +66,13 @@ public class MillipedeAI : SnakeAI if (!mustPatrol || (!IsGroundedFlag && !IsTouchingCeilingFlag) - || (FloorType == BaseWorld.FloorType.Lava && mustTurn) - || verticalMovementSpeed < 0.0f && !IsGrounded) + || (floorType == BaseWorld.floorType.Lava && mustTurn) + || verticalMovementSpeed < 0.0f && !isGrounded) return; - if ((!IsTouchingWall && Rigidbody.velocity.y != 0.0f && !_isGoingDown) - || (IsTouchingCeiling && IsTouchingWall) - || (IsGrounded && IsTouchingWall && verticalMovementSpeed < 0.0f)) + if ((!isTouchingWall && characterRigidbody.velocity.y != 0.0f && !_isGoingDown) + || (isTouchingCeiling && isTouchingWall) + || (isGrounded && isTouchingWall && verticalMovementSpeed < 0.0f)) { verticalMovementSpeed = -verticalMovementSpeed; _isGoingDown = !_isGoingDown; @@ -81,10 +81,10 @@ public class MillipedeAI : SnakeAI if (Math.Abs(transform.position.x) > Math.Abs(_startPosition.x + patrolDistance.x) - || FloorType == BaseWorld.FloorType.Lava + || floorType == BaseWorld.floorType.Lava || (!Physics2D.OverlapCircle(groundCollider.position, groundCheckSize, groundLayer) && IsGroundedFlag) || Math.Abs(transform.position.y) > Math.Abs(_startPosition.y + patrolDistance.y) - || verticalMovementSpeed < 0.0f && !IsGrounded) + || verticalMovementSpeed < 0.0f && !isGrounded) { mustTurn = true; } @@ -94,7 +94,7 @@ public class MillipedeAI : SnakeAI } - Rigidbody.gravityScale = IsGroundedFlag ? BaseWorld.World.GetGravityScale() : 0.0f; + characterRigidbody.gravityScale = IsGroundedFlag ? BaseWorld.world.GetGravityScale() : 0.0f; } #endregion } diff --git a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SnakeAI.cs b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SnakeAI.cs index c05d53b..24508b3 100644 --- a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SnakeAI.cs +++ b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SnakeAI.cs @@ -1,5 +1,6 @@ using BaseScripts; using UnityEngine; +using UnityEngine.Serialization; namespace EnemiesScripts { @@ -12,15 +13,18 @@ public class SnakeAI : BaseCharacter public LayerMask groundLayer; // Private variables: - [SerializeField] - private GameObject _distancePoint; + [FormerlySerializedAs("_distancePoint")] [SerializeField] + private GameObject distancePoint; //Roboczy timer na czas braku animacji do ataku private const float Timer = 2f; private const float RecoveryTimer = 1f; private float _currentTimer = Timer; + +#pragma warning disable CS0414 private float _currentRecoveryTimer = RecoveryTimer; +#pragma warning restore CS0414 // Public flags: public bool seePlayer; @@ -30,7 +34,7 @@ public class SnakeAI : BaseCharacter protected bool mustPatrol; protected bool mustAttack; - #region AI + #region SnakeAI protected void Patrol() { float x; @@ -39,7 +43,7 @@ protected void Patrol() else x = movementSpeed; - float y = Rigidbody.velocity.y; + float y = characterRigidbody.velocity.y; Move(ref x, ref y); } protected void Flip() @@ -69,23 +73,22 @@ protected void Attack() #region Collision private void OnTriggerStay2D(Collider2D collision) { - if (collision.gameObject.tag == "Player") { - float dist = Vector3.Distance(_distancePoint.transform.position, collision.gameObject.transform.position); - if (dist < 2.3f) - { - mustPatrol = false; - mustAttack = true; - } - } + if (!collision.gameObject.CompareTag("Player")) return; + float dist = Vector3.Distance(distancePoint.transform.position, collision.gameObject.transform.position); + + if (!(dist < 2.3f)) return; + + mustPatrol = false; + mustAttack = true; } private void OnTriggerEnter2D(Collider2D collision) { - if (collision.gameObject.tag == "Player") + if (collision.gameObject.CompareTag("Player")) seePlayer = true; } private void OnTriggerExit2D(Collider2D collision) { - if (collision.gameObject.tag == "Player") + if (collision.gameObject.CompareTag("Player")) seePlayer = false; } #endregion @@ -100,7 +103,7 @@ protected void Start() protected void Update() { - if (mustTurn || IsTouchingWall) + if (mustTurn || isTouchingWall) { Flip(); } diff --git a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SundewTurret.cs b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SundewTurret.cs index d42dd83..fc72d03 100644 --- a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SundewTurret.cs +++ b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/SundewTurret.cs @@ -1,32 +1,33 @@ using UnityEngine; +using UnityEngine.Serialization; namespace EnemiesScripts { public class SundewTurret : MonoBehaviour { // Private variables: - [SerializeField] private GameObject _bulletPrefab; - [SerializeField] private Transform _mouthPosition; + [FormerlySerializedAs("_bulletPrefab")] [SerializeField] private GameObject bulletPrefab; + [FormerlySerializedAs("_mouthPosition")] [SerializeField] private Transform mouthPosition; private const float AttackTimer = 1f; - private float currentAttackTime = AttackTimer; + private float _currentAttackTime = AttackTimer; private void Update() { - if (currentAttackTime <= 0f) + if (_currentAttackTime <= 0f) { - currentAttackTime = AttackTimer; + _currentAttackTime = AttackTimer; Shooting(); } else { - currentAttackTime -= Time.deltaTime; + _currentAttackTime -= Time.deltaTime; } } private void Shooting() { - GameObject enemyBulletClone=Instantiate(_bulletPrefab, _mouthPosition); + GameObject enemyBulletClone=Instantiate(bulletPrefab, mouthPosition); Rigidbody2D rb = enemyBulletClone.GetComponent(); - rb.velocity = _mouthPosition.right * 10f; + rb.velocity = mouthPosition.right * 10f; } } } diff --git a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/TurtleAI.cs b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/TurtleAI.cs index ab6783f..b1361d8 100644 --- a/GeckoAndCricket/Assets/Scripts/EnemiesScripts/TurtleAI.cs +++ b/GeckoAndCricket/Assets/Scripts/EnemiesScripts/TurtleAI.cs @@ -5,9 +5,6 @@ namespace EnemiesScripts { public class TurtleAI : BaseCharacter { - // ReSharper disable ConvertToConstant.Global - // ReSharper disable FieldCanBeMadeReadOnly.Global - // ReSharper disable MemberCanBePrivate.Global [Header("Turtle Properties:")] public float playerLaunchForce = 3f; public int jumpsToFlip = 2; @@ -15,10 +12,7 @@ public class TurtleAI : BaseCharacter [Header("Turtle Colliders:")] public Collider2D bottomCollider; public Collider2D topCollider; - // ReSharper restore ConvertToConstant.Global - // ReSharper restore FieldCanBeMadeReadOnly.Global - // ReSharper restore MemberCanBePrivate.Global - + // Turtle private variables: private Vector3 _localPosition; @@ -45,7 +39,7 @@ private void Flip() _canMove = false; FlipVertically(); - Rigidbody.constraints = RigidbodyConstraints2D.FreezePosition; + characterRigidbody.constraints = RigidbodyConstraints2D.FreezePosition; bottomCollider.enabled = false; topCollider.enabled = false; @@ -62,24 +56,24 @@ private void LaunchPlayer() if (_isTurningUpsideDown) return; - BaseWorld.Player.GetComponent().AddForce(new Vector2(0f, playerLaunchForce), ForceMode2D.Impulse); + BaseWorld.player.GetComponent().AddForce(new Vector2(0f, playerLaunchForce), ForceMode2D.Impulse); } private void InteractWithColliders() { - if (topCollider.IsTouching(BaseWorld.Player.GetComponent()) && !_isTouchingPlayer) + if (topCollider.IsTouching(BaseWorld.player.GetComponent()) && !_isTouchingPlayer) { _isTouchingPlayer = true; - BaseWorld.Player.GetComponent().IsGroundedFlag = true; + BaseWorld.player.GetComponent().IsGroundedFlag = true; Flip(); } - else if (bottomCollider.IsTouching(BaseWorld.Player.GetComponent())) + else if (bottomCollider.IsTouching(BaseWorld.player.GetComponent())) { Debug.Log("LaunchPlayer"); LaunchPlayer(); } - else if (!bottomCollider.IsTouching(BaseWorld.Player.GetComponent()) && - !topCollider.IsTouching(BaseWorld.Player.GetComponent()) && _isTouchingPlayer) + else if (!bottomCollider.IsTouching(BaseWorld.player.GetComponent()) && + !topCollider.IsTouching(BaseWorld.player.GetComponent()) && _isTouchingPlayer) _isTouchingPlayer = false; } @@ -104,7 +98,7 @@ private void Start() if (Physics2D.OverlapPoint(new Vector2(_localPosition.x, _localPosition.y + 1.0f), LayerMask.NameToLayer("Player"))) return; - Rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation; + characterRigidbody.constraints = RigidbodyConstraints2D.FreezeRotation; bottomCollider.enabled = true; topCollider.enabled = true; _isTurningUpsideDown = false; diff --git a/GeckoAndCricket/Assets/Scripts/PlayerScripts/Player.cs b/GeckoAndCricket/Assets/Scripts/PlayerScripts/Player.cs index 023463a..f71c06e 100644 --- a/GeckoAndCricket/Assets/Scripts/PlayerScripts/Player.cs +++ b/GeckoAndCricket/Assets/Scripts/PlayerScripts/Player.cs @@ -1,14 +1,15 @@ using System.Collections; using BaseScripts; +using EnemiesScripts; using UnityEngine; using UnityEngine.UI; + #if DEBUG using DebugUtility; #endif namespace PlayerScripts { - //[RequireComponent(typeof(BaseWorld))] public class Player : BaseCharacter { [Header("Player movement variables:")] @@ -27,6 +28,19 @@ public class Player : BaseCharacter public Transform attachedTo; + [Header("Roll variables:")] + public bool isRolling; + + [Header("Grapple variables:")] + public bool isGrappled; + + public Canvas grappleCanvas; + public Slider grapple; + + // private grapple variables: + private const float GrappleBarTimer=0.01f; + private float _currentGrappleBarTimer=GrappleBarTimer; + [Header("Player stamina variables:")] public float maxStamina = 3f; // Jump @@ -38,14 +52,6 @@ public class Player : BaseCharacter // Degradation: public float staminaDegradationValue = 0.5f; public int staminaDegradationTime = 1; - // Roll - public bool isRolling; - // Grapple - public bool isGrappled; - private static float grappleBarTimer=0.01f; - private float currentGrappleBarTimer=grappleBarTimer; - public Canvas grappleCanvas; - public Slider grapple; [Header("Player input:")] public KeyCode climbKey = KeyCode.X; @@ -59,38 +65,40 @@ public class Player : BaseCharacter private bool _wasTouchingWall; private bool _wasClimbKeyPressed; + +#pragma warning disable CS0649 + // These aren't initialized private GameObject _disregard; + // ReSharper disable InconsistentNaming private GameObject disregard; + // ReSharper restore InconsistentNaming +#pragma warning restore CS0649 #region Getters - public bool GetIsTouchingCeiling => IsTouchingCeiling; - public bool GetIsTouchingWall => IsTouchingWall; - public bool GetIsGrounded => IsGrounded; - #endregion #region Movement private void Move() { - Vector2 velocity = Rigidbody.velocity; + Vector2 velocity = characterRigidbody.velocity; float xInput = Input.GetAxis("Horizontal"); float x = xInput * movementSpeed; // 5f float y = velocity.y; - if (IsTouchingCeiling && !IsGrounded && !Input.GetKeyUp(climbKey) && _stamina > 0f) + if (isTouchingCeiling && !isGrounded && !Input.GetKeyUp(climbKey) && _stamina > 0f) { - Rigidbody.gravityScale = 0f; + characterRigidbody.gravityScale = 0f; _wasTouchingCeiling = true; } else if (_wasTouchingCeiling) { _wasClimbKeyPressed = false; _wasTouchingCeiling = false; - Rigidbody.gravityScale = BaseWorld.World.GetGravityScale(); + characterRigidbody.gravityScale = BaseWorld.world.GetGravityScale(); } - if (IsTouchingWall && !_isWallJumping && _wasClimbKeyPressed) + if (isTouchingWall && !_isWallJumping && _wasClimbKeyPressed) { if (_stamina > 0f) { @@ -104,16 +112,16 @@ private void Move() } else { - Rigidbody.gravityScale = BaseWorld.World.GetGravityScale(); + characterRigidbody.gravityScale = BaseWorld.world.GetGravityScale(); SetRotationZ(-90f); - y = Mathf.Clamp(Rigidbody.velocity.y, -wallSlidingSpeed, float.MaxValue); + y = Mathf.Clamp(characterRigidbody.velocity.y, -wallSlidingSpeed, float.MaxValue); _wasClimbKeyPressed = false; } _wasTouchingWall = true; } else if (_wasTouchingWall && !_isWallJumping) { - Rigidbody.gravityScale = BaseWorld.World.GetGravityScale(); + characterRigidbody.gravityScale = BaseWorld.world.GetGravityScale(); SetRotationZ(0f); _wasTouchingWall = false; _wasClimbKeyPressed = false; @@ -129,6 +137,7 @@ private void Move() float velocityForce; if (Mathf.Abs(x) < 0.01f) velocityForce = stoppingForce; + // ReSharper disable once CompareOfFloatsByEqualityOperator else if (Mathf.Abs(velocity.x) > 0f && Mathf.Sign(x) != Mathf.Sign(velocity.x)) velocityForce = turnForce; else @@ -137,7 +146,7 @@ private void Move() x = Mathf.Pow(Mathf.Abs(xDiff) * xAcceleration, velocityForce) * Mathf.Sign(x); x = Mathf.Lerp(velocity.x, x, 1); - base.Move(ref x, ref y, xInput); + base.Move(ref x, ref y, ref xInput); #if DEBUG if (_debug == null && debugMessageType == DebugType.Movement) @@ -164,9 +173,9 @@ private void WallJump() if (Input.GetAxis("Horizontal") != 0) direction = -Input.GetAxis("Horizontal"); - direction *= Rigidbody.velocity.x; - Rigidbody.gravityScale = BaseWorld.World.GetGravityScale(); - Rigidbody.AddForce(new Vector2(wallJumpForce * direction , jumpForce), ForceMode2D.Impulse); + direction *= characterRigidbody.velocity.x; + characterRigidbody.gravityScale = BaseWorld.world.GetGravityScale(); + characterRigidbody.AddForce(new Vector2(wallJumpForce * direction , jumpForce), ForceMode2D.Impulse); } private void SetWallJumpToFalse() @@ -179,12 +188,12 @@ private void SetWallJumpToFalse() if (!Input.GetKeyDown(KeyCode.Space) || _stamina < staminaToJump) return; _stamina -= staminaJumpCost; - if (IsGrounded || IsAttachedToRope) + if (isGrounded || isAttachedToRope) { base.Jump(); doubleJump = true; } - else if (IsTouchingWall) + else if (isTouchingWall) { WallJump(); doubleJump = true; @@ -196,144 +205,114 @@ private void SetWallJumpToFalse() } } - private void Roll() { - if ((Rigidbody.velocity.x > 0 || Rigidbody.velocity.x < 0) && IsGrounded && Input.GetKeyDown("s")) + private void Roll() + { + if ((characterRigidbody.velocity.x > 0 || characterRigidbody.velocity.x < 0) && isGrounded && Input.GetKeyDown("s")) { isRolling = true; - this.gameObject.GetComponent().enabled = false; + gameObject.GetComponent().enabled = false; } - else if(!isRolling && (this.gameObject.GetComponent().enabled == false)) + else if(!isRolling && gameObject.GetComponent().enabled == false) { - this.gameObject.GetComponent().enabled = true; + gameObject.GetComponent().enabled = true; } } private void Stomp(bool isTouchingStuff) { - if (Input.GetKeyDown("s") && !isTouchingStuff) - { + if (!Input.GetKeyDown("s") && isTouchingStuff) Stomp(); - } } private void AttachToRope(Rigidbody2D ropeSeg) { Debug.Log(ropeSeg); - ropeSeg.gameObject.GetComponent().isPlayerAttached = true; - HingeJoint.connectedBody = ropeSeg; - HingeJoint.enabled = true; - IsAttachedToRope = true; - attachedTo = ropeSeg.gameObject.transform.parent; + GameObject ropeGameObject; + (ropeGameObject = ropeSeg.gameObject).GetComponent().isPlayerAttached = true; + characterHingeJoint.connectedBody = ropeSeg; + characterHingeJoint.enabled = true; + isAttachedToRope = true; + attachedTo = ropeGameObject.transform.parent; } private void DetachFromRope() { - HingeJoint.connectedBody.GetComponent().isPlayerAttached = false; - HingeJoint.enabled = false; - IsAttachedToRope = false; - HingeJoint.connectedBody = null; + characterHingeJoint.connectedBody.GetComponent().isPlayerAttached = false; + characterHingeJoint.enabled = false; + isAttachedToRope = false; + characterHingeJoint.connectedBody = null; } private void SlideOnRope(int direction) { - RopeSegment actualRopeSegment = HingeJoint.connectedBody.gameObject.GetComponent(); + RopeSegment actualRopeSegment = characterHingeJoint.connectedBody.gameObject.GetComponent(); GameObject newRopeSegment = null; if (direction > 0) { - if (actualRopeSegment.above != null) + if (actualRopeSegment.above != null && actualRopeSegment.above.gameObject.GetComponent() != null) { - if (actualRopeSegment.above.gameObject.GetComponent() != null) - { - newRopeSegment = actualRopeSegment.above; - } + newRopeSegment = actualRopeSegment.above; } } - else + else if (actualRopeSegment.below != null) { - if (actualRopeSegment.below != null) - { - newRopeSegment = actualRopeSegment.below; - } - } - if (newRopeSegment != null) - { - transform.position = newRopeSegment.transform.position; - actualRopeSegment.isPlayerAttached = false; - newRopeSegment.GetComponent().isPlayerAttached = true; - HingeJoint.connectedBody = newRopeSegment.GetComponent(); + newRopeSegment = actualRopeSegment.below; } + + if (newRopeSegment == null) return; + + transform.position = newRopeSegment.transform.position; + actualRopeSegment.isPlayerAttached = false; + newRopeSegment.GetComponent().isPlayerAttached = true; + characterHingeJoint.connectedBody = newRopeSegment.GetComponent(); } private void OnTriggerEnter2D(Collider2D collision) { - if (!IsAttachedToRope && collision.gameObject.CompareTag("Rope")) + if (isAttachedToRope || !collision.gameObject.CompareTag("Rope") || + attachedTo == collision.gameObject.transform.parent || + (_disregard != null && collision.gameObject.transform.parent.gameObject == _disregard) || + isAttachedToRope || !collision.gameObject.CompareTag("Rope") || !(detachTimer <= 0f) || + attachedTo == collision.gameObject.transform.parent) return; + + if (disregard == null || collision.gameObject.transform.parent.gameObject != disregard) { - if (attachedTo != collision.gameObject.transform.parent) - { - if (_disregard == null || collision.gameObject.transform.parent.gameObject != _disregard) - { - if (!IsAttachedToRope && collision.gameObject.tag == "Rope" && detachTimer <= 0f) - { - if (attachedTo != collision.gameObject.transform.parent) - { - if (disregard == null || collision.gameObject.transform.parent.gameObject != disregard) - { - AttachToRope(collision.gameObject.GetComponent()); - } - } - } - } - } + AttachToRope(collision.gameObject.GetComponent()); } } private void OnTriggerExit2D(Collider2D collision) { if (isRolling && collision.gameObject.CompareTag("Roll")) - { isRolling = false; - } } private void Swing() { - if (Input.GetKey("a")) - { - if (IsAttachedToRope) - { - Rigidbody.AddRelativeForce(new Vector2(-1, 0) * pushForce); + if (Input.GetKey("a") && isAttachedToRope) + characterRigidbody.AddRelativeForce(new Vector2(-1, 0) * pushForce); - } - } - if (Input.GetKey("d")) - { - if (IsAttachedToRope) - { - Rigidbody.AddRelativeForce(new Vector2(1, 0) * pushForce); - } - } - if (Input.GetKeyDown("w") && IsAttachedToRope) - { + if (Input.GetKey("d") && isAttachedToRope) + characterRigidbody.AddRelativeForce(new Vector2(1, 0) * pushForce); + + if (Input.GetKeyDown("w") && isAttachedToRope) SlideOnRope(1); - } - if (Input.GetKeyDown("s") && IsAttachedToRope) - { + + if (Input.GetKeyDown("s") && isAttachedToRope) SlideOnRope(-1); - } - if (Input.GetKeyDown("space") && IsAttachedToRope) - { - detachTimer = 1f; - DetachFromRope(); - } + + if (!Input.GetKeyDown("space") || !isAttachedToRope) return; + + detachTimer = 1f; + DetachFromRope(); } - private void DetachRopeTimer() { - if (!IsAttachedToRope && detachTimer>0f) - { + private void DetachRopeTimer() + { + if (!isAttachedToRope && detachTimer>0f) detachTimer -= Time.deltaTime; - } - else if(detachTimer<=0f &&attachedTo!=null){ + else if(detachTimer <= 0f && attachedTo != null) attachedTo = null; - } + } #endregion @@ -346,20 +325,10 @@ private IEnumerator Stamina(bool isUsingStamina) { _isCoroutineRunning = true; - /*float staminaUsage = isUsingStamina ? staminaDegradationValue : staminaRegenerationValue; - - do - { - yield return new WaitForSeconds(staminaRegenerationTime); - _stamina += staminaUsage; - Debug.Log("Stamina = " + _stamina); - } while (_stamina < maxStamina || _stamina <= 0);*/ - - while (_stamina < maxStamina && IsGrounded) + while (_stamina < maxStamina && isGrounded) { yield return new WaitForSeconds(staminaRegenerationTime); _stamina += staminaRegenerationValue; - //Debug.Log("Stamina + " + _stamina); } while (_stamina > 0 && isUsingStamina && @@ -368,7 +337,6 @@ private IEnumerator Stamina(bool isUsingStamina) { yield return new WaitForSeconds(staminaDegradationTime); _stamina -= staminaDegradationValue; - //Debug.Log("Stamina - " + _stamina); } _isCoroutineRunning = false; } @@ -454,17 +422,23 @@ public unsafe float* XValue #endregion #region Actions - void grappleEscape() { + + private void GrappleEscape() + { if (grappleCanvas.enabled == false) { grappleCanvas.enabled = true; } if (Input.GetKeyDown(KeyCode.Z)) { grapple.value += 2f; - if (grapple.value == grapple.maxValue) { - Transform enemy = this.gameObject.transform.Find("GrapplePosition").transform.Find("Frog"); + + // ReSharper disable CompareOfFloatsByEqualityOperator + if (grapple.value == grapple.maxValue) + { + // TODO: I think this should be partially moved to the frog script GetComponent is expensive and then we can remove Move method in the frog script. - Hubert + Transform enemy = gameObject.transform.Find("GrapplePosition").transform.Find("Frog"); enemy.gameObject.GetComponent().isKinematic = false; enemy.gameObject.GetComponent().movementSpeed *=-1; - enemy.gameObject.GetComponent().Move(enemy.gameObject.GetComponent().movementSpeed*1.5f, enemy.gameObject.GetComponent().velocity.y); + enemy.gameObject.GetComponent().Move(enemy.gameObject.GetComponent().movementSpeed * 1.5f, enemy.gameObject.GetComponent().velocity.y); enemy.gameObject.GetComponent().grapplingPlayer = false; enemy.gameObject.GetComponent().isTrigger = false; enemy.parent=null; @@ -473,13 +447,15 @@ void grappleEscape() { grappleCanvas.enabled = false; } } - if (grapple.value > 0f) { - currentGrappleBarTimer -= Time.deltaTime; - if (currentGrappleBarTimer <= 0f) { - currentGrappleBarTimer = grappleBarTimer; - grapple.value -= 0.2f; - } - } + + if (!(grapple.value > 0f)) return; + + _currentGrappleBarTimer -= Time.deltaTime; + + if (!(_currentGrappleBarTimer <= 0f)) return; + + _currentGrappleBarTimer = GrappleBarTimer; + grapple.value -= 0.2f; } #endregion @@ -488,7 +464,7 @@ void grappleEscape() { private new void Awake() { base.Awake(); - BaseWorld.Player = gameObject; + BaseWorld.player = gameObject; isGrappled = false; } private void Start() @@ -499,7 +475,7 @@ private void Start() #endif // Ustawia grawitację świata na taką jaką ma gracz. - BaseWorld.World.GetGravityScale() = Rigidbody.gravityScale; + BaseWorld.world.GetGravityScale() = characterRigidbody.gravityScale; _stamina = maxStamina; grapple.maxValue = 10f; grapple.value = 0f; @@ -514,9 +490,10 @@ private void Update() InteractWithFloorType(); //Debug.Log(jumpForce); - bool isTouchingStuff = !IsGrounded && (IsTouchingWall || IsTouchingCeiling || IsAttachedToRope); - if (!_isCoroutineRunning && ((_stamina < maxStamina && IsGrounded) || isTouchingStuff)) + bool isTouchingStuff = !isGrounded && (isTouchingWall || isTouchingCeiling || isAttachedToRope); + if (!_isCoroutineRunning && ((_stamina < maxStamina && isGrounded) || isTouchingStuff)) StartCoroutine(Stamina(isTouchingStuff)); + if (!isGrappled) { Jump(); @@ -527,7 +504,7 @@ private void Update() } else { - grappleEscape(); + GrappleEscape(); } #if DEBUG @@ -546,9 +523,7 @@ private void Update() { base.FixedUpdate(); if (!isGrappled) - { Move(); - } } #endregion } diff --git a/GeckoAndCricket/Assets/Scripts/PlayerScripts/Shooting.cs b/GeckoAndCricket/Assets/Scripts/PlayerScripts/Shooting.cs index d52cddd..a2b743a 100644 --- a/GeckoAndCricket/Assets/Scripts/PlayerScripts/Shooting.cs +++ b/GeckoAndCricket/Assets/Scripts/PlayerScripts/Shooting.cs @@ -1,40 +1,45 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; -public class Shooting : MonoBehaviour +namespace PlayerScripts { - [SerializeField] private Camera mainCamera; - private Transform aimT; - public Transform bulletPoint; - public GameObject bullet; - public float bulletSpeed = 50; - // Start is called before the first frame update - void Awake() + public class Shooting : MonoBehaviour { - aimT = transform.Find("Aim"); - } + [SerializeField] private Camera mainCamera; + public Transform bulletPoint; + public GameObject bullet; + + private Transform _aimT; - // Update is called once per frame - void Update() - { - AimingAndShooting(); - } - void AimingAndShooting() { - Vector3 mousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition); - mousePosition.z = 0f; + public float bulletSpeed = 50; + + // Start is called before the first frame update + private void Awake() + { + _aimT = transform.Find("Aim"); + } - Vector3 aimDirection = (mousePosition - transform.position).normalized; - float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg; - aimT.eulerAngles = new Vector3(0, 0, angle); + // Update is called once per frame + private void Update() + { + AimingAndShooting(); + } + + private void AimingAndShooting() + { + Vector3 mousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition); + mousePosition.z = 0f; + + Vector3 aimDirection = (mousePosition - transform.position).normalized; + float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg; + _aimT.eulerAngles = new Vector3(0, 0, angle); + + if (!Input.GetMouseButtonDown(0)) return; - if (Input.GetMouseButtonDown(0)) { GameObject bulletClone = Instantiate(bullet); bulletClone.transform.position = bulletPoint.transform.position; bulletClone.transform.rotation = Quaternion.Euler(0, 0, angle); bulletClone.GetComponent().velocity = bulletPoint.right * bulletSpeed; } - } } diff --git a/GeckoAndCricket/Assets/Scripts/Rope.cs b/GeckoAndCricket/Assets/Scripts/Rope.cs index d0f0541..ab125ef 100644 --- a/GeckoAndCricket/Assets/Scripts/Rope.cs +++ b/GeckoAndCricket/Assets/Scripts/Rope.cs @@ -1,5 +1,3 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; public class Rope : MonoBehaviour @@ -16,7 +14,8 @@ private void Start() private void GenerateRope() { Rigidbody2D prevRb = hook; - for (int i = 0; i < numLinks; i++) { + for (int i = 0; i < numLinks; i++) + { GameObject newSeg = Instantiate(prefSeg); newSeg.transform.parent = transform; newSeg.transform.position = transform.position; diff --git a/GeckoAndCricket/Assets/Scripts/RopeSegment.cs b/GeckoAndCricket/Assets/Scripts/RopeSegment.cs index 6b40b17..4c1ae38 100644 --- a/GeckoAndCricket/Assets/Scripts/RopeSegment.cs +++ b/GeckoAndCricket/Assets/Scripts/RopeSegment.cs @@ -1,5 +1,3 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; public class RopeSegment : MonoBehaviour @@ -17,7 +15,8 @@ private void Start() float spriteBottom = above.GetComponent().bounds.size.y; GetComponent().connectedAnchor = new Vector2(0, spriteBottom * -1); } - else { + else + { GetComponent().connectedAnchor = new Vector2(0, 0); } } diff --git a/GeckoAndCricket/GeckoAndCricket.sln.DotSettings b/GeckoAndCricket/GeckoAndCricket.sln.DotSettings index bc0af17..c3f53bb 100644 --- a/GeckoAndCricket/GeckoAndCricket.sln.DotSettings +++ b/GeckoAndCricket/GeckoAndCricket.sln.DotSettings @@ -1,3 +1,18 @@  TOLERANCE - True \ No newline at end of file + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True \ No newline at end of file