Game_CodeMM/Assets/00_scripts/Events/TurnScripts/TurnCharacterManager.cs

89 lines
3.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
public class TurnCharacterManager : MonoBehaviour
{
// ========== SerializeField ==============
[SerializeField] private float screenOffsetLeft = 8f;
[SerializeField] private float screenOffsetRight = 6f;
[SerializeField] private RectTransform dealCardPlace;
[SerializeField] private float moveCardGoDeckDuration = 0.1f;
private Vector3 cardDeckWorldPosition;
private bool isSetup;
private Transform characterContainer;
private Camera mainCamera;
// ========== Private ======================
private List<GameObject> characterList = new();
private readonly List<Vector3> characterPositionList = new();
private Transform cardDeckContainer;
public void SetupCharacterManager(List<GameObject> characters, Transform cardParent, Transform characterParent)
{
if (isSetup) return;
if (mainCamera == null) mainCamera = Camera.main;
RectTransformUtility.ScreenPointToWorldPointInRectangle(
rect: dealCardPlace,
screenPoint: dealCardPlace.position,
cam: mainCamera,
out cardDeckWorldPosition);
characterList = characters;
cardDeckContainer = cardParent;
characterContainer = characterParent;
MathWorldPositionPoint();
isSetup = true;
}
public List<Card> RenderCharacterSpriteReturnCardList()
{
List<Card> cardDeck = new();
for (var i = 0; i < characterList.Count; i++)
{
GameObject characterPrefab = Instantiate<GameObject>(characterList[i].gameObject,
position: characterPositionList[i],
rotation: Quaternion.identity,
parent: characterContainer
);
int characterID = 100000 + (i + 1) * 10;
characterPrefab.name = $"Character_{characterID}";
Character characterComponent = characterPrefab.GetComponent<Character>();
List<Card> aCharacterOwnedCardList = characterComponent.SetupCharacter(deckPosition: cardDeckWorldPosition, id: characterID,
cardContainer: cardDeckContainer, camera1: mainCamera);
cardDeck.AddRange(aCharacterOwnedCardList);
}
return cardDeck;
}
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;
}
}
}