151 lines
3.6 KiB
C#
151 lines
3.6 KiB
C#
using System;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public enum PlayerState
|
|
{
|
|
InHome,
|
|
InSafeZone,
|
|
Moving,
|
|
InFinishingPath,
|
|
HasFinished
|
|
}
|
|
|
|
public enum PawnType
|
|
{
|
|
User,
|
|
Bot
|
|
}
|
|
|
|
public class PlayerPawn : MonoBehaviour
|
|
{
|
|
[SerializeField] private PlayerState playerState;
|
|
[SerializeField] private Animator animator;
|
|
[SerializeField] private GameObject playerCountCanvasPrefab;
|
|
[SerializeField] private PlayerCountCanvas playerCountCanvas;
|
|
|
|
public PlayerCountCanvas PlayerCountCanvas => playerCountCanvas;
|
|
|
|
public bool CanSelectPlayer
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
private GameplayManager gameplayManager;
|
|
|
|
public int CurrentTileIndex
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public int PlayerId
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public PlayerType PlayerType
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public bool IsBotPlayer
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public int StepsTaken
|
|
{
|
|
get; private set;
|
|
}
|
|
|
|
public void AssignBotPlayerState(bool state)
|
|
{
|
|
IsBotPlayer = state;
|
|
}
|
|
|
|
public void SetPlayerSelectionState(bool state)
|
|
{
|
|
CanSelectPlayer = state;
|
|
if (CanSelectPlayer)
|
|
{
|
|
// TODO :: Play animation for selecting any of the players
|
|
}
|
|
}
|
|
|
|
public void MoveToTile(Vector3 startingPoint, Action onComplete, int tileIndex)
|
|
{
|
|
StepsTaken++;
|
|
CurrentTileIndex = tileIndex;
|
|
animator?.SetTrigger("Jump");
|
|
|
|
// SFX
|
|
SoundManager soundManager = InterfaceManager.Instance?.GetInterfaceInstance<SoundManager>();
|
|
soundManager?.PlayGameSoundClip(SoundType.Jump);
|
|
|
|
transform.DOMove(startingPoint, 0.6f).onComplete = () =>
|
|
{
|
|
animator?.ResetTrigger("Jump");
|
|
|
|
// Rotate Player
|
|
TilesManager tilesManager = InterfaceManager.Instance.GetInterfaceInstance<TilesManager>();
|
|
gameplayManager = gameplayManager ?? InterfaceManager.Instance.GetInterfaceInstance<GameplayManager>();
|
|
|
|
int nextTileIndex = gameplayManager.GetNextGeneralTileIndex(this);
|
|
Vector3 lookDirection = tilesManager.RetrieveTileBasedOnIndex(nextTileIndex).transform.position - transform.position;
|
|
lookDirection.y = 0;
|
|
transform.DOLookAt(transform.position + lookDirection, 0.2f);
|
|
|
|
onComplete?.Invoke();
|
|
};
|
|
}
|
|
|
|
public void MoveToCustomTilePosition(Vector3 targetPoint)
|
|
{
|
|
StepsTaken++;
|
|
transform.DOMove(targetPoint, 1f);
|
|
}
|
|
|
|
// when he is defeated
|
|
public void MoveBackToHome(Transform transformData)
|
|
{
|
|
Debug.Log($"MoveBackToHome");
|
|
transform.position = transformData.position;
|
|
transform.rotation = transformData.rotation;
|
|
SetPlayerState(PlayerState.InHome);
|
|
StepsTaken = 0;
|
|
}
|
|
|
|
public PlayerState GetPlayerState() => playerState;
|
|
|
|
public void SetPlayerState(PlayerState state)
|
|
{
|
|
playerState = state;
|
|
}
|
|
|
|
private void OnMouseDown()
|
|
{
|
|
if (IsBotPlayer || !CanSelectPlayer) return;
|
|
|
|
SetGameplayManager();
|
|
gameplayManager.OnPawnSelected(this);
|
|
}
|
|
|
|
private void SetGameplayManager()
|
|
{
|
|
gameplayManager = gameplayManager ?? InterfaceManager.Instance.GetInterfaceInstance<GameplayManager>();
|
|
}
|
|
|
|
public void Init(int id, PlayerType playerType)
|
|
{
|
|
PlayerId = id;
|
|
PlayerType = playerType;
|
|
}
|
|
|
|
public void ShowPlayerCountCanvas(bool show)
|
|
{
|
|
if (playerCountCanvasPrefab == null) return;
|
|
|
|
playerCountCanvasPrefab.SetActive(show);
|
|
}
|
|
}
|