113 lines
4.7 KiB
C#
113 lines
4.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using DG.Tweening;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class TurnCharacterManager : MonoBehaviour
|
|
{
|
|
// ========== SerializeField ==============
|
|
[SerializeField] private float screenOffsetLeft = 8f;
|
|
[SerializeField] private float screenOffsetRight = 6f;
|
|
|
|
|
|
// ========== Private ======================
|
|
private readonly List<Vector3> player_position_list = new();
|
|
private readonly List<Vector3> enemy_position_list = new();
|
|
|
|
// private Vector3 cardDeckWorldPosition;
|
|
private const int POSITION_NUMBER = 3;
|
|
private bool is_setup;
|
|
private Camera main_camera;
|
|
private TurnManagerSingleton manager_singleton;
|
|
|
|
private void Awake()
|
|
{
|
|
CharacterManagerEvent.OnCharacterManagerSetup += setup_character_manager;
|
|
}
|
|
|
|
private void setup_character_manager()
|
|
{
|
|
if (is_setup) return;
|
|
manager_singleton = TurnManagerSingleton.Instance;
|
|
if (manager_singleton.GetNeedDebugLog()) TurnManagerSingleton.print_msg("CharacterManager", "初始化", "初始化成功");
|
|
main_camera = Camera.main;
|
|
is_setup = true;
|
|
}
|
|
|
|
public List<Card> render_character_sprite_return_card_list(List<GameObject> player_list, Transform player_container,
|
|
Transform card_deck_container, List<GameObject> enemy_list, Transform enemy_container)
|
|
{
|
|
math_sprite_world_position(player_container);
|
|
List<Card> card_deck = new();
|
|
render_friend_gameobject();
|
|
render_enemy_gameobject();
|
|
return card_deck;
|
|
|
|
|
|
void render_friend_gameobject()
|
|
{
|
|
for (var i = 0; i < POSITION_NUMBER; i++)
|
|
{
|
|
GameObject character_prefab = Instantiate<GameObject>(player_list[i].gameObject,
|
|
position: player_position_list[i],
|
|
rotation: Quaternion.identity,
|
|
parent: player_container
|
|
);
|
|
int characterID = 100000 + (i + 1) * 10;
|
|
character_prefab.name = $"Character_{characterID}";
|
|
Character character_component = character_prefab.GetComponent<Character>();
|
|
var a_character_card = character_component.setup_character(characterID, card_deck_container, main_camera, true);
|
|
card_deck.AddRange(a_character_card);
|
|
}
|
|
}
|
|
|
|
void render_enemy_gameobject()
|
|
{
|
|
for (var i = 0; i < enemy_list.Count; i++)
|
|
{
|
|
// rotation 旋转180度
|
|
GameObject enemyPrefab = Instantiate<GameObject>(enemy_list[i].gameObject,
|
|
position: enemy_position_list[i],
|
|
rotation: Quaternion.Euler(0, 180, 0),
|
|
parent: enemy_container
|
|
);
|
|
int enemyID = 700000 + (i + 1) * 10;
|
|
enemyPrefab.name = $"Enemy_{enemyID}";
|
|
Character characterComponent = enemyPrefab.GetComponent<Character>();
|
|
characterComponent.setup_character(enemyID, enemy_container, main_camera, false);
|
|
Transform canvas = enemyPrefab.transform.GetChild(1);
|
|
canvas.Rotate(0, 180, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void math_sprite_world_position(Transform player_container)
|
|
{
|
|
// 计算屏幕边界
|
|
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_player = screen_offset_left + i * sprite_space + sprite_offset;
|
|
float position_x_enemy = screen_half + screen_offset_right + i * sprite_space + sprite_offset;
|
|
player_position_list.Add(switch_screen_position_to_world_position(position_x_player));
|
|
enemy_position_list.Add(switch_screen_position_to_world_position(position_x_enemy));
|
|
}
|
|
|
|
return;
|
|
|
|
Vector3 switch_screen_position_to_world_position(float screen_position_x)
|
|
{
|
|
Vector3 world_position = main_camera!.ScreenToWorldPoint(new Vector3(screen_position_x, Screen.height / 2f, 0));
|
|
world_position.y += player_container.transform.position.y;
|
|
world_position.z = 0;
|
|
return world_position;
|
|
}
|
|
}
|
|
} |