Skip to content

Commit 35e7013

Browse files
committed
Added new utility scripts: GameManager, DataManager, CameraController, HealthSystem, and TooltipManager
1 parent 5992199 commit 35e7013

File tree

8 files changed

+518
-0
lines changed

8 files changed

+518
-0
lines changed

.gitignore

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Unity generated
2+
[Ll]ibrary/
3+
[Tt]emp/
4+
[Oo]bj/
5+
[Bb]uild/
6+
[Bb]uilds/
7+
[Ll]ogs/
8+
[Uu]ser[Ss]ettings/
9+
10+
# Unity3D generated meta files
11+
*.pidb.meta
12+
*.pdb.meta
13+
*.mdb.meta
14+
15+
# Unity3D generated file on crash reports
16+
sysinfo.txt
17+
18+
# Builds
19+
*.apk
20+
*.aab
21+
*.unitypackage
22+
*.app
23+
24+
# Crashlytics generated file
25+
crashlytics-build.properties
26+
27+
# Visual Studio
28+
.vs/
29+
*.csproj
30+
*.unityproj
31+
*.sln
32+
*.suo
33+
*.tmp
34+
*.user
35+
*.userprefs
36+
*.pidb
37+
*.booproj
38+
*.svd
39+
*.pdb
40+
*.mdb
41+
*.opendb
42+
*.VC.db
43+
44+
# OS generated
45+
.DS_Store
46+
.DS_Store?
47+
._*
48+
.Spotlight-V100
49+
.Trashes
50+
ehthumbs.db
51+
Thumbs.db

CameraController.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using UnityEngine;
2+
3+
public class CameraController : MonoBehaviour
4+
{
5+
[Header("Target Settings")]
6+
public Transform target;
7+
public Vector3 offset = new Vector3(0, 5, -10);
8+
9+
[Header("Smooth Settings")]
10+
public float smoothSpeed = 5f;
11+
public float rotationSpeed = 2f;
12+
13+
[Header("Boundaries")]
14+
public float minY = 2f;
15+
public float maxY = 10f;
16+
public float minX = -10f;
17+
public float maxX = 10f;
18+
19+
private void LateUpdate()
20+
{
21+
if (target == null) return;
22+
23+
// Calculate desired position
24+
Vector3 desiredPosition = target.position + offset;
25+
26+
// Clamp position within boundaries
27+
desiredPosition.y = Mathf.Clamp(desiredPosition.y, minY, maxY);
28+
desiredPosition.x = Mathf.Clamp(desiredPosition.x, minX, maxX);
29+
30+
// Smoothly move camera
31+
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
32+
transform.position = smoothedPosition;
33+
34+
// Look at target
35+
Vector3 targetPosition = new Vector3(target.position.x, target.position.y, target.position.z);
36+
Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
37+
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
38+
}
39+
}

DataManager.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using UnityEngine;
2+
using System;
3+
4+
public class DataManager : MonoBehaviour
5+
{
6+
public static DataManager Instance { get; private set; }
7+
8+
private void Awake()
9+
{
10+
if (Instance == null)
11+
{
12+
Instance = this;
13+
DontDestroyOnLoad(gameObject);
14+
}
15+
else
16+
{
17+
Destroy(gameObject);
18+
}
19+
}
20+
21+
public void SaveData(string key, object value)
22+
{
23+
if (value is int intValue)
24+
PlayerPrefs.SetInt(key, intValue);
25+
else if (value is float floatValue)
26+
PlayerPrefs.SetFloat(key, floatValue);
27+
else if (value is string stringValue)
28+
PlayerPrefs.SetString(key, stringValue);
29+
else
30+
Debug.LogError($"Unsupported data type for saving: {value.GetType()}");
31+
32+
PlayerPrefs.Save();
33+
}
34+
35+
public T LoadData<T>(string key, T defaultValue)
36+
{
37+
if (!PlayerPrefs.HasKey(key))
38+
return defaultValue;
39+
40+
if (typeof(T) == typeof(int))
41+
return (T)Convert.ChangeType(PlayerPrefs.GetInt(key), typeof(T));
42+
else if (typeof(T) == typeof(float))
43+
return (T)Convert.ChangeType(PlayerPrefs.GetFloat(key), typeof(T));
44+
else if (typeof(T) == typeof(string))
45+
return (T)Convert.ChangeType(PlayerPrefs.GetString(key), typeof(T));
46+
else
47+
{
48+
Debug.LogError($"Unsupported data type for loading: {typeof(T)}");
49+
return defaultValue;
50+
}
51+
}
52+
53+
public void DeleteData(string key)
54+
{
55+
if (PlayerPrefs.HasKey(key))
56+
{
57+
PlayerPrefs.DeleteKey(key);
58+
PlayerPrefs.Save();
59+
}
60+
}
61+
62+
public void DeleteAllData()
63+
{
64+
PlayerPrefs.DeleteAll();
65+
PlayerPrefs.Save();
66+
}
67+
}

GameManager.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class GameManager : MonoBehaviour
5+
{
6+
public static GameManager Instance { get; private set; }
7+
8+
public bool isGamePaused { get; private set; }
9+
public bool isGameOver { get; private set; }
10+
11+
private void Awake()
12+
{
13+
if (Instance == null)
14+
{
15+
Instance = this;
16+
DontDestroyOnLoad(gameObject);
17+
}
18+
else
19+
{
20+
Destroy(gameObject);
21+
}
22+
}
23+
24+
public void PauseGame()
25+
{
26+
isGamePaused = true;
27+
Time.timeScale = 0f;
28+
}
29+
30+
public void ResumeGame()
31+
{
32+
isGamePaused = false;
33+
Time.timeScale = 1f;
34+
}
35+
36+
public void GameOver()
37+
{
38+
isGameOver = true;
39+
// Add your game over logic here
40+
}
41+
42+
public void RestartGame()
43+
{
44+
isGameOver = false;
45+
// Add your restart logic here
46+
}
47+
}

HealthSystem.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using UnityEngine;
2+
using UnityEngine.Events;
3+
4+
public class HealthSystem : MonoBehaviour
5+
{
6+
[Header("Health Settings")]
7+
public float maxHealth = 100f;
8+
public float currentHealth;
9+
public bool isInvulnerable = false;
10+
11+
[Header("Events")]
12+
public UnityEvent onDeath;
13+
public UnityEvent<float> onHealthChanged;
14+
public UnityEvent<float> onDamageTaken;
15+
public UnityEvent<float> onHealed;
16+
17+
private void Start()
18+
{
19+
currentHealth = maxHealth;
20+
onHealthChanged?.Invoke(currentHealth);
21+
}
22+
23+
public void TakeDamage(float damage)
24+
{
25+
if (isInvulnerable) return;
26+
27+
currentHealth = Mathf.Max(0, currentHealth - damage);
28+
onDamageTaken?.Invoke(damage);
29+
onHealthChanged?.Invoke(currentHealth);
30+
31+
if (currentHealth <= 0)
32+
{
33+
Die();
34+
}
35+
}
36+
37+
public void Heal(float amount)
38+
{
39+
currentHealth = Mathf.Min(maxHealth, currentHealth + amount);
40+
onHealed?.Invoke(amount);
41+
onHealthChanged?.Invoke(currentHealth);
42+
}
43+
44+
public void SetInvulnerable(bool invulnerable)
45+
{
46+
isInvulnerable = invulnerable;
47+
}
48+
49+
private void Die()
50+
{
51+
onDeath?.Invoke();
52+
// Add additional death logic here
53+
}
54+
55+
public float GetHealthPercentage()
56+
{
57+
return currentHealth / maxHealth;
58+
}
59+
60+
public bool IsAlive()
61+
{
62+
return currentHealth > 0;
63+
}
64+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Unity Tool Scripts
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)