add unlock system for tools
This commit is contained in:
@@ -22,7 +22,6 @@ public class GameManager : MonoBehaviour
|
||||
// Are we currently in a scene transition?
|
||||
private bool isTransitioningScenes = false;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// If there is an instance, and it's not me, delete myself.
|
||||
|
||||
@@ -13,6 +13,7 @@ public class PlayerController : MonoBehaviour
|
||||
public float walkSpeed;
|
||||
public float sprintMultiplier = 1.5f;
|
||||
public float gravity = 10;
|
||||
public float rotationSpeed = 15;
|
||||
|
||||
Interactable nearestInteractable;
|
||||
public CameraController cameraController;
|
||||
@@ -23,14 +24,9 @@ public class PlayerController : MonoBehaviour
|
||||
private bool isSprinting = false;
|
||||
private bool paused = false;
|
||||
|
||||
// Mathematical plane used to catch the raycast from camera to get direction for
|
||||
// looking at the mouse
|
||||
private Plane groundPlane;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
groundPlane = new Plane(Vector3.up, -DougBody.transform.position.y);
|
||||
characterController = GetComponent<CharacterController>();
|
||||
cameraController = GetComponent<CameraController>();
|
||||
SwitchTools(GameManager.Instance.PlayerManager.CurrentToolIndex);
|
||||
@@ -40,7 +36,6 @@ void Start()
|
||||
void Update()
|
||||
{
|
||||
moveDir = Vector3.zero;
|
||||
groundPlane.distance = -DougBody.transform.position.y;
|
||||
|
||||
// TODO: Move input detection somewhere else
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
@@ -223,14 +218,67 @@ void ToolUseDetector()
|
||||
/// </summary>
|
||||
void RotatePlayerTowardMouse()
|
||||
{
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
/*Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
Plane plane = new Plane(cameraController.MainCamera.transform.forward,DougBody.transform.position);
|
||||
|
||||
if (groundPlane.Raycast(ray, out var distance))
|
||||
if (plane.Raycast(ray, out var distance))
|
||||
{
|
||||
Vector3 hitPoint = ray.GetPoint(distance);
|
||||
Vector3 direction = hitPoint - DougBody.transform.position;
|
||||
direction.y = 0f;
|
||||
|
||||
DougBody.transform.LookAt(new Vector3(hitPoint.x, DougBody.transform.position.y, hitPoint.z));
|
||||
///DougBody.transform.LookAt(new Vector3(hitPoint.x, DougBody.transform.position.y, hitPoint.z));
|
||||
if (direction.sqrMagnitude > 0.001f)
|
||||
{
|
||||
Quaternion targetRotation =
|
||||
Quaternion.LookRotation(direction);
|
||||
|
||||
DougBody.transform.rotation = Quaternion.Slerp(
|
||||
DougBody.transform.rotation,
|
||||
targetRotation,
|
||||
rotationSpeed * Time.deltaTime
|
||||
);
|
||||
}
|
||||
}*/
|
||||
|
||||
Camera cam = cameraController.MainCamera;
|
||||
|
||||
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
|
||||
|
||||
Vector3 targetPoint;
|
||||
|
||||
// 1. Try real world hit first (most stable)
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
|
||||
{
|
||||
targetPoint = hit.point;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 2. Fallback to plane at player height
|
||||
Plane plane = new Plane(Vector3.up, DougBody.transform.position);
|
||||
|
||||
if (!plane.Raycast(ray, out float enter))
|
||||
return;
|
||||
|
||||
targetPoint = ray.GetPoint(enter);
|
||||
}
|
||||
|
||||
// 3. Flatten direction so we only rotate on Y axis
|
||||
Vector3 direction = targetPoint - DougBody.transform.position;
|
||||
direction.y = 0f;
|
||||
|
||||
if (direction.sqrMagnitude < 0.001f)
|
||||
return;
|
||||
|
||||
// 4. Convert to rotation
|
||||
Quaternion targetRotation = Quaternion.LookRotation(direction.normalized);
|
||||
|
||||
// 5. Smooth rotation
|
||||
DougBody.transform.rotation = Quaternion.Slerp(
|
||||
DougBody.transform.rotation,
|
||||
targetRotation,
|
||||
rotationSpeed * Time.deltaTime
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -305,8 +353,12 @@ public Vector3 GetVelocity()
|
||||
/// Switches the tools in hand by taking an index
|
||||
///
|
||||
/// <param name="toolIndex">Index of the tool to switch to</param>
|
||||
/// <param name="defaultToUp">
|
||||
/// If true, try the next tool if the tool at toolIndex is not unlocked yet. If false,
|
||||
/// try the previous tool if the tool at toolIndex is not unlocked.
|
||||
/// </param>
|
||||
/// </summary>
|
||||
private void SwitchTools(int toolIndex)
|
||||
private void SwitchTools(int toolIndex, bool defaultToUp=true)
|
||||
{
|
||||
if (toolIndex < 0 || toolIndex >= tools.Length)
|
||||
{
|
||||
@@ -319,9 +371,20 @@ private void SwitchTools(int toolIndex)
|
||||
tool.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
// load new tool
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = toolIndex;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
|
||||
if (GameManager.Instance.PlayerManager.IsToolUnlocked(tools[toolIndex].ToolType))
|
||||
{
|
||||
// load new tool
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = toolIndex;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// try next index
|
||||
if (defaultToUp)
|
||||
SwitchTools(toolIndex + 1, defaultToUp);
|
||||
else
|
||||
SwitchTools(toolIndex - 1, defaultToUp);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -332,11 +395,11 @@ private void SwitchTools(int toolIndex)
|
||||
private void SwitchTools(bool up)
|
||||
{
|
||||
// calculate index of next tool
|
||||
int newIndex = up ? GameManager.Instance.PlayerManager.CurrentToolIndex - 1 : GameManager.Instance.PlayerManager.CurrentToolIndex + 1;
|
||||
int newIndex = up ? GameManager.Instance.PlayerManager.CurrentToolIndex + 1 : GameManager.Instance.PlayerManager.CurrentToolIndex - 1;
|
||||
|
||||
if (newIndex < 0)
|
||||
{
|
||||
newIndex = tools.Length + newIndex;
|
||||
newIndex = tools.Length - 1;
|
||||
}
|
||||
else if (newIndex >= tools.Length)
|
||||
{
|
||||
@@ -346,9 +409,18 @@ private void SwitchTools(bool up)
|
||||
// disable current tool
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(false);
|
||||
|
||||
// load new tool
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = newIndex;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
|
||||
if (GameManager.Instance.PlayerManager.IsToolUnlocked(tools[newIndex].ToolType))
|
||||
{
|
||||
// load new tool
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = newIndex;
|
||||
tools[GameManager.Instance.PlayerManager.CurrentToolIndex].gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// try next tool
|
||||
if (up) SwitchTools(newIndex + 1);
|
||||
else SwitchTools(newIndex - 1, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
@@ -5,6 +7,27 @@
|
||||
public class PlayerManager : MonoBehaviour
|
||||
{
|
||||
public int CurrentToolIndex;
|
||||
private SerializableDictionary<ToolEnum, bool> unlockedToolDict = null;
|
||||
|
||||
public bool IsToolUnlocked(ToolEnum tool)
|
||||
{
|
||||
if (unlockedToolDict.TryGetValue(tool, out bool unlocked)) return unlocked;
|
||||
|
||||
// If no tool dict saved, init the default one (only None is unlocked)
|
||||
unlockedToolDict = new SerializableDictionary<ToolEnum, bool>();
|
||||
|
||||
foreach (ToolEnum toolType in Enum.GetValues(typeof(ToolEnum)))
|
||||
{
|
||||
if (toolType == ToolEnum.None)
|
||||
unlockedToolDict.Add(toolType, true);
|
||||
else
|
||||
unlockedToolDict.Add(toolType, false);
|
||||
}
|
||||
|
||||
// Try again
|
||||
if (unlockedToolDict.TryGetValue(tool, out bool unlockedRetry)) return unlockedRetry;
|
||||
return false;
|
||||
}
|
||||
|
||||
#region Player Data Save/Load
|
||||
public void SavePlayerData(ref PlayerSaveData data)
|
||||
@@ -12,6 +35,7 @@ public void SavePlayerData(ref PlayerSaveData data)
|
||||
data.activeToolIndex = GameManager.Instance.PlayerManager.CurrentToolIndex;
|
||||
data.dougPosition = GameManager.Instance.PlayerController.transform.position;
|
||||
data.currentScene = SceneManager.GetActiveScene().name;
|
||||
data.unlockedToolDict = unlockedToolDict;
|
||||
}
|
||||
|
||||
public void LoadPlayerData(PlayerSaveData data)
|
||||
@@ -25,6 +49,24 @@ public void LoadPlayerData(PlayerSaveData data)
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = data.activeToolIndex;
|
||||
GameManager.Instance.PlayerController.transform.position = data.dougPosition;
|
||||
}
|
||||
|
||||
if (data.unlockedToolDict != null)
|
||||
{
|
||||
unlockedToolDict = data.unlockedToolDict;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If no tool dict saved, init the default one (only None is unlocked)
|
||||
unlockedToolDict = new SerializableDictionary<ToolEnum, bool>();
|
||||
|
||||
foreach (ToolEnum toolType in Enum.GetValues(typeof(ToolEnum)))
|
||||
{
|
||||
if (toolType == ToolEnum.None)
|
||||
unlockedToolDict.Add(toolType, true);
|
||||
else
|
||||
unlockedToolDict.Add(toolType, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -35,4 +77,5 @@ public struct PlayerSaveData
|
||||
public int activeToolIndex;
|
||||
public Vector3 dougPosition;
|
||||
public string currentScene;
|
||||
public SerializableDictionary<ToolEnum, bool> unlockedToolDict;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user