This commit is contained in:
2024-09-09 20:30:24 +08:00
commit f1e9f43ba4
165 changed files with 103426 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,29 @@
using UnityEngine;
namespace Camera
{
public class CameraController : MonoBehaviour
{
public float mouseSensitivity = 100.0f;
public Transform playerBody;
private float _xRotation;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
var mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
var mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
_xRotation -= mouseY;
_xRotation = Mathf.Clamp(_xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 73886bd5d98b4e13af3b19513287a041
timeCreated: 1725857707

View File

@@ -0,0 +1,25 @@
using Event;
using UnityEngine;
namespace Camera
{
public class CameraInterAct : MonoBehaviour
{
[SerializeField] private int maxInterActDistance = 10;
[SerializeField] private GameObject target;
private void Update()
{
Physics.Raycast(transform.position, transform.forward, out var raycastHit, maxInterActDistance);
if (raycastHit.collider)
EventManager.Instance.OnCameraInterAct(raycastHit.collider.gameObject);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position + transform.forward * maxInterActDistance);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0a3494e2751e9f649a71dbde9a2b7b58
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,44 @@
using UnityEngine;
namespace Camera
{
public class ScreenAspect : MonoBehaviour
{
//目标比例默认16:9
public float TargetAspect = 16f / 9f;
private UnityEngine.Camera _mainCamera;
private void Awake()
{
_mainCamera = UnityEngine.Camera.main;
var windowAspect = Screen.width / (float)Screen.height;
var scaleHeight = windowAspect / TargetAspect;
if (scaleHeight < 1f)
{
var rect = _mainCamera.rect;
rect.width = 1f;
rect.height = scaleHeight;
rect.x = 0;
rect.y = (1f - scaleHeight) / 2f;
_mainCamera.rect = rect;
}
else
{
var scaleWidth = 1f / scaleHeight;
var rect = _mainCamera.rect;
rect.width = scaleWidth;
rect.height = 1f;
rect.x = (1f - scaleWidth) / 2f;
rect.y = 0;
_mainCamera.rect = rect;
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 83d8d7f8bc114be1b896a809ca71906c
timeCreated: 1725861908

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2788c901978644dd94e6a4b8f54a40b2
timeCreated: 1725858866

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3b9504e434a44b2e8917bc39c318fb94
timeCreated: 1725859219

View File

@@ -0,0 +1,14 @@
using UnityEngine;
namespace Event.EventArgs
{
public class CameraInterActArgs : System.EventArgs
{
public GameObject Item { get; }
public CameraInterActArgs(GameObject item)
{
Item = item;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7d7b7ed755c3432eb1ffd466ce229575
timeCreated: 1725859232

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4cf9fa5509bd4199a7ee9c43efae6a6e
timeCreated: 1725859119

View File

@@ -0,0 +1,6 @@
using Event.EventArgs;
namespace Event.EventHandler
{
public delegate void CameraInterActHandler(CameraInterActArgs e);
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e4c03d33ea9c40bf8e045c468a7bc0d9
timeCreated: 1725859168

View File

@@ -0,0 +1,42 @@
using Event.EventArgs;
using Event.EventHandler;
using UnityEngine;
namespace Event
{
public class EventManager : MonoBehaviour
{
private static EventManager _instance;
public static EventManager Instance
{
get
{
if (_instance)
return _instance;
_instance = FindObjectOfType<EventManager>() ??
new GameObject("EventManager").AddComponent<EventManager>();
return _instance;
}
}
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
}
else
{
_instance = this;
}
}
public event CameraInterActHandler CameraInterAct;
public void OnCameraInterAct(GameObject item)
{
CameraInterAct?.Invoke(new CameraInterActArgs(item));
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c6c7b780f301469883211fac6d1fe774
timeCreated: 1725858879

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fb92b8251c1c4721872ec7d659c828cc
timeCreated: 1725859672

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8a43ae24583a4a8cb476617435772627
timeCreated: 1725859800

View File

@@ -0,0 +1,26 @@
using Event;
using Event.EventArgs;
using Keyboard;
using UnityEngine;
namespace Items.Abstract
{
public abstract class ItemBase : MonoBehaviour
{
private void Start()
{
EventManager.Instance.CameraInterAct += ReceiveEvent;
}
protected virtual void ReceiveEvent(CameraInterActArgs item)
{
if (item.Item != gameObject) return;
}
protected void ActivateItem()
{
Debug.Log("Item is activated");
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ed31ae56e9e5426a869778bbc9fa3c37
timeCreated: 1725859814

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 78101257a45b4759ad2cede2ac74b17f
timeCreated: 1725859783

View File

@@ -0,0 +1,6 @@
namespace Items.Interface
{
public interface IItem
{
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1a8d2b3ec8724e45b00cbf6eac467b4d
timeCreated: 1725859735

View File

@@ -0,0 +1,17 @@
using Event.EventArgs;
using Items.Abstract;
using Items.Interface;
using Keyboard;
using UnityEngine;
namespace Items
{
public class TestItem : ItemBase , IItem
{
protected override void ReceiveEvent(CameraInterActArgs item)
{
if(Input.GetKeyDown(KeySettingManager.Instance.GetKey("InterAct")))
ActivateItem();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: be562f2cd7af4802854932ea7550ed73
timeCreated: 1725860034

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5dbc812600ef47ee91714079b303afbc
timeCreated: 1725861967

View File

@@ -0,0 +1,17 @@
using UnityEngine;
namespace Keyboard
{
[System.Serializable]
public class KeyMapping
{
public string actionName;
public KeyCode keyCode;
public KeyMapping(string actionName, KeyCode keyCode)
{
this.actionName = actionName;
this.keyCode = keyCode;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 804e202fc5db48258358354e6bf69509
timeCreated: 1725862011

View File

@@ -0,0 +1,135 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using Newtonsoft.Json;
namespace Keyboard
{
public class KeySettingManager : MonoBehaviour
{
public List<KeyMapping> keyMappings = new();
public string filePath;
private static KeySettingManager _instance;
public static KeySettingManager Instance
{
get
{
if (_instance)
return _instance;
_instance = FindObjectOfType<KeySettingManager>() ??
new GameObject("KeySettingManager").AddComponent<KeySettingManager>();
return _instance;
}
}
public Vector2 Direction { get; private set; }
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
}
else
{
_instance = this;
}
DontDestroyOnLoad(gameObject);
filePath = Application.persistentDataPath + "/" + "KeySetting.json";
LoadKeySettings();
}
//加载键位设置
private void LoadKeySettings()
{
if (File.Exists(filePath))
{
var json = File.ReadAllText(filePath);
keyMappings = JsonConvert.DeserializeObject<List<KeyMapping>>(json);
}
else
{
//如果文件不存在,则创建默认键位设置
keyMappings.Add(new KeyMapping("InterAct", KeyCode.E));
keyMappings.Add(new KeyMapping("Left", KeyCode.A));
keyMappings.Add(new KeyMapping("Right", KeyCode.D));
keyMappings.Add(new KeyMapping("Up", KeyCode.W));
keyMappings.Add(new KeyMapping("Down", KeyCode.S));
SaveKeySettings();
}
}
//保存键位设置
private void SaveKeySettings()
{
var json = JsonConvert.SerializeObject(keyMappings, Formatting.Indented);
File.WriteAllText(filePath, json);
}
//获取键位
public KeyCode GetKey(string actionName)
{
return (from mapping in keyMappings where mapping.actionName == actionName select mapping.keyCode)
.FirstOrDefault();
}
//设置键位
public void SetKey(string actionName, KeyCode newKeyCode)
{
foreach (var mapping in keyMappings.Where(mapping => mapping.actionName == actionName))
{
mapping.keyCode = newKeyCode;
break;
}
SaveKeySettings();
}
//更新Direction
private void Update()
{
if (Input.GetKey(GetKey("Left")))
{
Direction = new Vector2(-1, Direction.y);
}
if (Input.GetKey(GetKey("Right")))
{
Direction = new Vector2(1, Direction.y);
}
if (Input.GetKey(GetKey("Up")))
{
Direction = new Vector2(Direction.x, 1);
}
if (Input.GetKey(GetKey("Down")))
{
Direction = new Vector2(Direction.x, -1);
}
if (Input.GetKey(GetKey("Left")) && Input.GetKey(GetKey("Right")))
{
Direction = new Vector2(0, Direction.y);
}
if (!Input.GetKey(GetKey("Left")) && !Input.GetKey(GetKey("Right")))
{
Direction = new Vector2(0, Direction.y);
}
if (Input.GetKey(GetKey("Up")) && Input.GetKey(GetKey("Down")))
{
Direction = new Vector2(Direction.x, 0);
}
if (!Input.GetKey(GetKey("Up")) && !Input.GetKey(GetKey("Down")))
{
Direction = new Vector2(Direction.x, 0);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de55af222f74476380e7cd6bae3af057
timeCreated: 1725861988

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9358351d0e194151b0ef38440ebb8ad3
timeCreated: 1725861728

View File

@@ -0,0 +1,24 @@
using System;
using Keyboard;
using UnityEngine;
namespace Player
{
public class PlayerMotion : MonoBehaviour
{
[SerializeField] private Rigidbody rb;
[SerializeField] private Animator animator;
private Vector3 _velocity;
private void Update()
{
Run();
}
private void Run()
{
_velocity = KeySettingManager.Instance.Direction.x * transform.right + KeySettingManager.Instance.Direction.y * transform.forward;
rb.AddForce(_velocity.normalized * (Time.deltaTime * 10), ForceMode.Impulse);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4e27ae197ca343dc80e5b947f6f9fd68
timeCreated: 1725861763

3
Assets/Scripts/UI.meta Normal file
View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0235b12b2c96474e90c2f9c0d8c99257
timeCreated: 1725859681