84 lines
3.7 KiB
C#
Raw Normal View History

2025-11-20 17:06:04 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2025-11-21 20:16:36 +08:00
using DG.Tweening;
2025-11-20 17:06:04 +08:00
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;
2025-11-21 02:12:50 +08:00
[SerializeField] private RectTransform dealCardPlace;
2025-11-21 20:16:36 +08:00
[SerializeField] private float moveCardGoDeckDuration = 0.1f;
private Vector3 cardDeckWorldPosition;
2025-11-20 17:06:04 +08:00
// ========== Private ======================
2025-11-21 02:12:50 +08:00
private List<GameObject> characterList = new();
2025-11-20 17:06:04 +08:00
private readonly List<Vector3> characterPositionList = new();
2025-11-21 20:16:36 +08:00
public void SetupCharacterManager(List<GameObject> characters)
2025-11-20 17:06:04 +08:00
{
if (mainCamera == null) mainCamera = Camera.main;
2025-11-21 02:12:50 +08:00
RectTransformUtility.ScreenPointToWorldPointInRectangle(
rect: dealCardPlace,
screenPoint: dealCardPlace.position,
cam: mainCamera,
2025-11-21 20:16:36 +08:00
out cardDeckWorldPosition);
2025-11-21 02:12:50 +08:00
characterList = characters;
MathWorldPositionPoint();
2025-11-20 17:06:04 +08:00
}
2025-11-21 20:16:36 +08:00
public (List<Sequence>, List<Card>) RenderCharacterSpriteReturnCardTween()
2025-11-20 17:06:04 +08:00
{
2025-11-21 20:16:36 +08:00
List<Sequence> characterOwnedCardTweenList = new();
List<Card> cardDeck = new();
2025-11-20 17:06:04 +08:00
for (var i = 0; i < characterList.Count; i++)
{
2025-11-21 20:16:36 +08:00
GameObject characterPrefab = Instantiate<GameObject>(characterList[i].gameObject,
position: characterPositionList[i],
2025-11-20 17:06:04 +08:00
rotation: Quaternion.identity,
parent: characterContainer
);
2025-11-21 20:16:36 +08:00
int characterID = 100000 + (i + 1) * 10;
2025-11-21 02:12:50 +08:00
characterPrefab.name = $"Character_{characterID}";
Character characterComponent = characterPrefab.GetComponent<Character>();
2025-11-21 20:16:36 +08:00
var (aCharacterOwnedCardTween, aCharacterOwnedCardList) = characterComponent.SetupCharacter(deckPosition: cardDeckWorldPosition,
id: characterID, doRotation: Quaternion.identity, doScale: Vector3.one / 10f, animationDuration: moveCardGoDeckDuration);
characterOwnedCardTweenList.AddRange(aCharacterOwnedCardTween);
cardDeck.AddRange(aCharacterOwnedCardList);
2025-11-20 17:06:04 +08:00
}
2025-11-21 20:16:36 +08:00
return (characterOwnedCardTweenList, cardDeck);
2025-11-20 17:06:04 +08:00
}
2025-11-21 02:12:50 +08:00
public void MathWorldPositionPoint()
2025-11-20 17:06:04 +08:00
{
// 计算屏幕边界
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;
}
}
}