init
This commit is contained in:
8
Assets/Scripts/Camera.meta
Normal file
8
Assets/Scripts/Camera.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc5afb0092411e14b9b2c83498bfba3c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
29
Assets/Scripts/Camera/CameraController.cs
Normal file
29
Assets/Scripts/Camera/CameraController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Camera/CameraController.cs.meta
Normal file
3
Assets/Scripts/Camera/CameraController.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73886bd5d98b4e13af3b19513287a041
|
||||
timeCreated: 1725857707
|
||||
25
Assets/Scripts/Camera/CameraInteract.cs
Normal file
25
Assets/Scripts/Camera/CameraInteract.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Camera/CameraInteract.cs.meta
Normal file
11
Assets/Scripts/Camera/CameraInteract.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a3494e2751e9f649a71dbde9a2b7b58
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Assets/Scripts/Camera/ScreenAspect.cs
Normal file
44
Assets/Scripts/Camera/ScreenAspect.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Camera/ScreenAspect.cs.meta
Normal file
3
Assets/Scripts/Camera/ScreenAspect.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 83d8d7f8bc114be1b896a809ca71906c
|
||||
timeCreated: 1725861908
|
||||
3
Assets/Scripts/Event.meta
Normal file
3
Assets/Scripts/Event.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2788c901978644dd94e6a4b8f54a40b2
|
||||
timeCreated: 1725858866
|
||||
3
Assets/Scripts/Event/EventArgs.meta
Normal file
3
Assets/Scripts/Event/EventArgs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b9504e434a44b2e8917bc39c318fb94
|
||||
timeCreated: 1725859219
|
||||
14
Assets/Scripts/Event/EventArgs/CameraInterActArgs.cs
Normal file
14
Assets/Scripts/Event/EventArgs/CameraInterActArgs.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d7b7ed755c3432eb1ffd466ce229575
|
||||
timeCreated: 1725859232
|
||||
3
Assets/Scripts/Event/EventHandler.meta
Normal file
3
Assets/Scripts/Event/EventHandler.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4cf9fa5509bd4199a7ee9c43efae6a6e
|
||||
timeCreated: 1725859119
|
||||
@@ -0,0 +1,6 @@
|
||||
using Event.EventArgs;
|
||||
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void CameraInterActHandler(CameraInterActArgs e);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e4c03d33ea9c40bf8e045c468a7bc0d9
|
||||
timeCreated: 1725859168
|
||||
42
Assets/Scripts/Event/EventManager.cs
Normal file
42
Assets/Scripts/Event/EventManager.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Event/EventManager.cs.meta
Normal file
3
Assets/Scripts/Event/EventManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6c7b780f301469883211fac6d1fe774
|
||||
timeCreated: 1725858879
|
||||
3
Assets/Scripts/Items.meta
Normal file
3
Assets/Scripts/Items.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb92b8251c1c4721872ec7d659c828cc
|
||||
timeCreated: 1725859672
|
||||
3
Assets/Scripts/Items/Abstract.meta
Normal file
3
Assets/Scripts/Items/Abstract.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a43ae24583a4a8cb476617435772627
|
||||
timeCreated: 1725859800
|
||||
26
Assets/Scripts/Items/Abstract/ItemBase.cs
Normal file
26
Assets/Scripts/Items/Abstract/ItemBase.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Items/Abstract/ItemBase.cs.meta
Normal file
3
Assets/Scripts/Items/Abstract/ItemBase.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed31ae56e9e5426a869778bbc9fa3c37
|
||||
timeCreated: 1725859814
|
||||
3
Assets/Scripts/Items/Interface.meta
Normal file
3
Assets/Scripts/Items/Interface.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78101257a45b4759ad2cede2ac74b17f
|
||||
timeCreated: 1725859783
|
||||
6
Assets/Scripts/Items/Interface/IItem.cs
Normal file
6
Assets/Scripts/Items/Interface/IItem.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Items.Interface
|
||||
{
|
||||
public interface IItem
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Items/Interface/IItem.cs.meta
Normal file
3
Assets/Scripts/Items/Interface/IItem.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a8d2b3ec8724e45b00cbf6eac467b4d
|
||||
timeCreated: 1725859735
|
||||
17
Assets/Scripts/Items/TestItem.cs
Normal file
17
Assets/Scripts/Items/TestItem.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Items/TestItem.cs.meta
Normal file
3
Assets/Scripts/Items/TestItem.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be562f2cd7af4802854932ea7550ed73
|
||||
timeCreated: 1725860034
|
||||
3
Assets/Scripts/Keyboard.meta
Normal file
3
Assets/Scripts/Keyboard.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5dbc812600ef47ee91714079b303afbc
|
||||
timeCreated: 1725861967
|
||||
17
Assets/Scripts/Keyboard/KeyMapping.cs
Normal file
17
Assets/Scripts/Keyboard/KeyMapping.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Keyboard/KeyMapping.cs.meta
Normal file
3
Assets/Scripts/Keyboard/KeyMapping.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 804e202fc5db48258358354e6bf69509
|
||||
timeCreated: 1725862011
|
||||
135
Assets/Scripts/Keyboard/KeySettingManager.cs
Normal file
135
Assets/Scripts/Keyboard/KeySettingManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Keyboard/KeySettingManager.cs.meta
Normal file
3
Assets/Scripts/Keyboard/KeySettingManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de55af222f74476380e7cd6bae3af057
|
||||
timeCreated: 1725861988
|
||||
3
Assets/Scripts/Player.meta
Normal file
3
Assets/Scripts/Player.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9358351d0e194151b0ef38440ebb8ad3
|
||||
timeCreated: 1725861728
|
||||
24
Assets/Scripts/Player/PlayerMotion.cs
Normal file
24
Assets/Scripts/Player/PlayerMotion.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Player/PlayerMotion.cs.meta
Normal file
3
Assets/Scripts/Player/PlayerMotion.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e27ae197ca343dc80e5b947f6f9fd68
|
||||
timeCreated: 1725861763
|
||||
3
Assets/Scripts/UI.meta
Normal file
3
Assets/Scripts/UI.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0235b12b2c96474e90c2f9c0d8c99257
|
||||
timeCreated: 1725859681
|
||||
Reference in New Issue
Block a user