86 lines
3.6 KiB
C#
Raw Normal View History

2025-11-20 17:06:04 +08:00
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;
2025-11-21 02:12:50 +08:00
[SerializeField] private RectTransform dealCardPlace;
private Vector3 dealCardPlaceWorldPosition;
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 02:12:50 +08:00
public void SetupBaseMessage(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,
out dealCardPlaceWorldPosition);
characterList = characters;
MathWorldPositionPoint();
2025-11-20 17:06:04 +08:00
}
public List<Card> RenderCharacterSprite()
{
List<Card> fightCharacterOwnedCard = new();
for (var i = 0; i < characterList.Count; i++)
{
2025-11-21 02:12:50 +08:00
Character.CharacterRenderData renderData;
renderData.IS_ENEMY = false;
renderData.RENDER_POSITION = characterPositionList[i];
renderData.ROTATION = Quaternion.identity;
GameObject characterPrefab = Instantiate<GameObject>(
2025-11-20 17:06:04 +08:00
characterList[i].gameObject,
2025-11-21 02:12:50 +08:00
position: renderData.RENDER_POSITION,
2025-11-20 17:06:04 +08:00
rotation: Quaternion.identity,
parent: characterContainer
);
2025-11-21 02:12:50 +08:00
int characterID = (i + 1) * 100;
characterPrefab.name = $"Character_{characterID}";
Character characterComponent = characterPrefab.GetComponent<Character>();
characterComponent.SetupBaseMessage(characterID);
characterComponent.RecordRenderData(renderData);
// var character_card = characterList[i].SetCardID();
// fightCharacterOwnedCard.AddRange(character_card);
2025-11-20 17:06:04 +08:00
}
return fightCharacterOwnedCard;
}
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;
}
}
}