优化流程

This commit is contained in:
mnjnhuang 2025-11-20 17:06:04 +08:00
parent 0b1f906eef
commit 5da1217d89
7 changed files with 146 additions and 18 deletions

View File

@ -8,21 +8,19 @@ public class Character : MonoBehaviour
[SerializeField] private int ID;
[SerializeField] private List<Card> ownedCardList;
private void Awake()
{
TurnManagerEvent.OnSetCardID += SetCardID;
}
private DATACLASS_CHARACTER_RENDER_DATA renderData;
public void SetID(int id) => ID = id;
private void OnDestroy()
{
TurnManagerEvent.OnSetCardID -= SetCardID;
}
private void SetCardID()
public void RecordRenderData(DATACLASS_CHARACTER_RENDER_DATA render_data) => renderData = render_data;
public List<Card> SetCardID()
{
for (int i = 0; i < ownedCardList.Count; i++)
{
ownedCardList[i].SetID(int.Parse($"{ID}{i}"));
}
return ownedCardList;
}
}

View File

@ -0,0 +1,77 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterManager : MonoBehaviour
{
// ========== SerializeField ==============
[SerializeField] private Camera mainCamera;
[SerializeField] private Transform characterContainer;
[SerializeField] private float screenOffsetLeft = 8f;
[SerializeField] private float screenOffsetRight = 6f;
// ========== Private ======================
private List<Character> characterList = new();
private readonly List<Vector3> characterPositionList = new();
private void Start()
{
if (mainCamera == null) mainCamera = Camera.main;
MathWorldPositionPoint(); // 计算每个单位的站位
}
public void SetFightCharacter(List<Character> characters) => characterList = characters;
public List<Card> RenderCharacterSprite()
{
List<Card> fightCharacterOwnedCard = new();
for (var i = 0; i < characterList.Count; i++)
{
DATACLASS_CHARACTER_RENDER_DATA render_data;
render_data.IS_ENEMY = false;
render_data.RENDER_POSITION = characterPositionList[i];
render_data.ROTATION = Quaternion.identity;
GameObject character_prefab = Instantiate<GameObject>(
characterList[i].gameObject,
position: render_data.RENDER_POSITION,
rotation: Quaternion.identity,
parent: characterContainer
);
character_prefab.name = $"Character_{i}";
characterList[i].RecordRenderData(render_data);
var character_card = characterList[i].SetCardID();
fightCharacterOwnedCard.AddRange(character_card);
}
return fightCharacterOwnedCard;
}
private void MathWorldPositionPoint()
{
// 计算屏幕边界
float screen_half = Screen.width / 2f;
float screen_offset_right = screen_half / screenOffsetRight; // 屏幕右边的偏移
float screen_offset_left = screen_half / screenOffsetLeft; // 屏幕左边的偏移
float in_fact_space = screen_half - screen_offset_right - screen_offset_left; // 屏幕内实际可用空间
float sprite_space = in_fact_space / 3f; // 3张牌的实际可用空间
float sprite_offset = (sprite_space * 0 + sprite_space * 1) / 2f; // 每个角色偏移一个单位
for (int i = 0; i < 3; i++)
{
float position_x = screen_offset_left + i * sprite_space + sprite_offset;
characterPositionList.Add(switch_screen_position_to_world_position(position_x));
// float position_x_enemy = screen_half + screen_offset_right + i * sprite_space + _sprite_offset;
// enemyPositionList.Add(switch_screen_position_to_world_position(position_x_enemy));
}
Vector3 switch_screen_position_to_world_position(float screen_position_x)
{
Vector3 world_position = mainCamera!.ScreenToWorldPoint(
new Vector3(screen_position_x, Screen.height / 2f, 0));
world_position.y += characterContainer.transform.position.y;
world_position.z = 0;
return world_position;
}
}
}

View File

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

View File

@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct DATACLASS_CHARACTER_RENDER_DATA
{
public Vector2 RENDER_POSITION;
public bool IS_ENEMY;
public Quaternion ROTATION;
}

View File

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

View File

@ -6,17 +6,19 @@ using UnityEngine;
public class TurnManager : MonoBehaviour
{
// ======== serializeField ========
[SerializeField] private List<Character> playerCharacterList; // 玩家操作的角色列表
[SerializeField] private List<GameObject> playerCharacterGo; // 玩家队伍列表(应该传入5个角色), 暂时只传3个
[SerializeField] private int defaultHandCardNumber = 5; // 默认手牌数量
[SerializeField] private float everyCardDistributeDuration = 0.25f; // 系统发牌时间
[SerializeField] private float everyCardDiscardDuration = 0.25f; // 系统弃牌时间
[SerializeField] private CharacterManager characterManager;
// ======== private ========
private List<Card> cardDeckList = new(); // 牌组
private List<Card> cardPileList = new(); // 弃牌堆
private TurnManagerSingleton turnManagerSingleton;
private List<Card> cardDeckList; // 牌组
private readonly List<Card> cardPileList = new(); // 弃牌堆
private TurnManagerStateEnum turnManagerState; // 状态机
private readonly List<Character> playerCharacterList = new();
private void Start()
{
@ -30,9 +32,9 @@ public class TurnManager : MonoBehaviour
if (turnManagerSingleton.GetNeedDebugLog()) Debug.Log("进入设置状态");
cardDeckList.Clear();
cardPileList.Clear();
foreach (Character character in playerCharacterList)
{
}
// TODO: 创建牌组, 从角色数据中获取牌组列表
foreach (GameObject go in playerCharacterGo) playerCharacterList.Add(go.GetComponent<Character>());
foreach (var playerCharacter in playerCharacterList) playerCharacter.SetID(playerCharacterList.IndexOf(playerCharacter));
characterManager.SetFightCharacter(playerCharacterList); // 从5个角色里选出三个, 暂时只选三个
cardDeckList = characterManager.RenderCharacterSprite();
}
}

View File

@ -1026,6 +1026,7 @@ MonoBehaviour:
defaultHandCardNumber: 5
everyCardDistributeDuration: 0.15
everyCardDiscardDuration: 0.12
characterManager: {fileID: 0}
--- !u!114 &526573211
MonoBehaviour:
m_ObjectHideFlags: 0
@ -1440,6 +1441,7 @@ GameObject:
m_Component:
- component: {fileID: 723158417}
- component: {fileID: 723158418}
- component: {fileID: 723158419}
m_Layer: 0
m_Name: ChararcterManager
m_TagString: Untagged
@ -1469,7 +1471,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 723158416}
m_Enabled: 1
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 30ab4f8291095c347bdf19e4350922ed, type: 3}
m_Name:
@ -1484,6 +1486,22 @@ MonoBehaviour:
enemyContainer: {fileID: 1358330957}
mainCamera: {fileID: 452712464}
showDebugObject: 0
--- !u!114 &723158419
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 723158416}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: da98c08a0661fd2438aa37c5fba5167b, type: 3}
m_Name:
m_EditorClassIdentifier:
mainCamera: {fileID: 0}
characterContainer: {fileID: 0}
screenOffsetLeft: 8
screenOffsetRight: 6
--- !u!1 &893376454
GameObject:
m_ObjectHideFlags: 0