Переглянути джерело

StaminBar 100% done and fixed save of skybox / color

Nathan Gusatto 1 місяць тому
батько
коміт
decbe50772

+ 26 - 1
Assets/ModularFirstPersonController/FirstPersonController/FirstPersonController.cs

@@ -18,6 +18,11 @@ public class FirstPersonController : MonoBehaviour
 {
     private Rigidbody rb;
 
+    #region NathanCustom
+    private Stamina staminaScript;
+    private float staminaMultiplier = 2f;
+    #endregion
+
     #region Camera Movement Variables
 
     public Camera playerCamera;
@@ -166,6 +171,9 @@ public class FirstPersonController : MonoBehaviour
             crosshairObject.gameObject.SetActive(false);
         }
 
+        //Nathan Custom stamina
+        staminaScript = gameObject.GetComponent<Stamina>();
+
         #region Sprint Bar
 
         sprintBarCG = GetComponentInChildren<CanvasGroup>();
@@ -202,10 +210,11 @@ public class FirstPersonController : MonoBehaviour
 
     private void Update()
     {
+
         #region Camera
 
         // Control camera movement
-        if(cameraCanMove)
+        if (cameraCanMove)
         {
             yaw = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mouseSensitivity;
 
@@ -415,6 +424,22 @@ public class FirstPersonController : MonoBehaviour
 
                 rb.AddForce(velocityChange, ForceMode.VelocityChange);
             }
+
+            //Nathan Custom Zone
+            else if (staminaScript.GetIsSprinting())
+            {
+                targetVelocity = transform.TransformDirection(targetVelocity) * walkSpeed * staminaMultiplier;
+
+                // Apply a force that attempts to reach our target velocity
+                // Pasted from below
+                Vector3 velocity = rb.linearVelocity;
+                Vector3 velocityChange = (targetVelocity - velocity);
+                velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
+                velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
+                velocityChange.y = 0;
+
+                rb.AddForce(velocityChange, ForceMode.VelocityChange);
+            }
             // All movement calculations while walking
             else
             {

Різницю між файлами не показано, бо вона завелика
+ 289 - 36
Assets/Scenes/SampleScene.unity


+ 3 - 3
Assets/Scripts/Daylight Cycle/Skybox.mat

@@ -24,16 +24,16 @@ Material:
     serializedVersion: 3
     m_TexEnvs:
     - _Texture1:
-        m_Texture: {fileID: 2800000, guid: 97c83ef8555216848ba4f98a7496a917, type: 3}
+        m_Texture: {fileID: 2800000, guid: 846cce7d90ad21b4b80fcb937193081e, type: 3}
         m_Scale: {x: 1, y: 1}
         m_Offset: {x: 0, y: 0}
     - _Texture2:
-        m_Texture: {fileID: 2800000, guid: 846cce7d90ad21b4b80fcb937193081e, type: 3}
+        m_Texture: {fileID: 2800000, guid: 323f1ea1bce78fe4c90b2286fa1997e2, type: 3}
         m_Scale: {x: 1, y: 1}
         m_Offset: {x: 0, y: 0}
     m_Ints: []
     m_Floats:
-    - _Blend: 0.38507944
+    - _Blend: 0.5475479
     - _Exposure1: 1
     - _Exposure2: 1
     - _ImageType: 0

+ 18 - 12
Assets/Scripts/GameManager.cs

@@ -6,9 +6,6 @@ public class GameManager : MonoBehaviour
 
     private SaveManager saveManager;
     private TimeManager timeManager;
-    private Material skybox;
-    private Color globalLightColor;
-    private Quaternion globalLightRotation;
 
     private GameObject player;
     // Start is called once before the first execution of Update after the MonoBehaviour is created
@@ -21,9 +18,6 @@ public class GameManager : MonoBehaviour
         //Get components
         saveManager = GetComponent<SaveManager>();
         timeManager = GetComponent<TimeManager>();
-        skybox = RenderSettings.skybox;
-        globalLightColor = timeManager.globalLight.color;
-        globalLightRotation = timeManager.globalLight.transform.rotation;
 
         data = saveManager.LoadGame();
         if (data != null)
@@ -53,9 +47,16 @@ public class GameManager : MonoBehaviour
         #region DaylightCycle
         data.minutes = timeManager.Minutes;
         data.hours = timeManager.Hours;
-        data.skybox = skybox;
-        data.globalLightColor = globalLightColor;
-        data.globalLightRotation = globalLightRotation;
+        data.skybox = RenderSettings.skybox;
+        data.globalLightColor = timeManager.globalLight.color;
+        data.globalLightRotation = timeManager.globalLight.transform.rotation;
+
+        #endregion
+
+        #region Stamina
+        data.currentStamina = player.GetComponent<Stamina>().GetCurrentStamina();
+        data.maxStamina = player.GetComponent<Stamina>().maxStamina;
+        data.canSprint = player.GetComponent<Stamina>().GetCanSprint();
 
         #endregion
     }
@@ -67,10 +68,15 @@ public class GameManager : MonoBehaviour
         #region DaylightCycle
         timeManager.Minutes = data.minutes;
         timeManager.Hours = data.hours;
-        skybox = data.skybox;
-        globalLightColor = data.globalLightColor;
+        RenderSettings.skybox = data.skybox;
+        timeManager.globalLight.color = data.globalLightColor;
         RenderSettings.fogColor = data.globalLightColor;
-        globalLightRotation = data.globalLightRotation;
+        timeManager.globalLight.transform.rotation = data.globalLightRotation;
+
+        #endregion
+
+        #region Stamina
+        player.GetComponent<Stamina>().Reinit(data.currentStamina, data.maxStamina, data.canSprint);
 
         #endregion
 

+ 5 - 0
Assets/Scripts/Save/GameData.cs

@@ -12,4 +12,9 @@ public class GameData
     public Color globalLightColor;
     public Quaternion globalLightRotation;
     public Material skybox;
+
+    //Stamina
+    public float maxStamina;
+    public float currentStamina;
+    public bool canSprint;
 }

+ 8 - 0
Assets/Scripts/Stamina.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: ff82b10d9f770a4498bb2945d914465e
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 71 - 0
Assets/Scripts/Stamina/Stamina.cs

@@ -0,0 +1,71 @@
+using System.ComponentModel;
+using UnityEngine;
+using UnityEngine.UI;
+
+public class Stamina : MonoBehaviour
+{
+    public float maxStamina;
+    private float currentStamina;
+    bool canSprint = true;
+    bool isSprinting = false;
+
+    public Slider staminaBar;
+    public Gradient staminaBarGradient;
+    private Image fillImage;
+    // Start is called once before the first execution of Update after the MonoBehaviour is created
+    void Start()
+    {
+        currentStamina = maxStamina;
+        fillImage = staminaBar.fillRect.GetComponent<Image>();
+    }
+
+    // Update is called once per frame
+    void Update()
+    {
+        if (Input.GetKey(KeyCode.LeftShift) && canSprint) //spend stamina
+        {
+            isSprinting = true;
+            currentStamina = Mathf.Max(0, currentStamina - Time.deltaTime);
+        }
+        else //regenerate stamina
+        {
+            isSprinting = false;
+            currentStamina = Mathf.Min(maxStamina, currentStamina + Time.deltaTime / 2f);
+        }
+
+        if (currentStamina == 0) //trigger the cd
+        {
+            canSprint = false;
+        }
+
+        if (!canSprint && currentStamina == maxStamina) //stamina regenerated
+        {
+            canSprint = true;
+        }
+
+        staminaBar.value = currentStamina/maxStamina;
+        fillImage.color = staminaBarGradient.Evaluate(staminaBar.value);
+    }
+
+    public float GetCurrentStamina()
+    {
+        return currentStamina;
+    }
+
+    public bool GetCanSprint()
+    {
+        return canSprint;
+    }
+
+    public bool GetIsSprinting()
+    {
+        return isSprinting;
+    }
+
+    public void Reinit(float currentStamina, float maxStamina, bool canSprint)
+    {
+        this.currentStamina = currentStamina;
+        this.maxStamina = maxStamina;
+        this.canSprint = canSprint;
+    }
+}

+ 2 - 0
Assets/Scripts/Stamina/Stamina.cs.meta

@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: d30a6eaab03b2a04580f077cccd18b3b

+ 1 - 1
Assets/TerrainAssets/DefaultTerrain.asset

@@ -14,7 +14,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   autoUpdate: 1
   uniformScale: 2.5
-  useFlatShading: 0
+  useFlatShading: 1
   useFalloff: 0
   meshHeightMultiplier: 30
   meshHeightCurve:

Деякі файли не було показано, через те що забагато файлів було змінено