186 lines
4.9 KiB
C#
186 lines
4.9 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 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;
|
|
}
|
|
|
|
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>();
|
|
|
|
Vector3 lookDirection = Vector3.zero;
|
|
|
|
Debug.Log($"nextIndex :: playerState: {playerState}");
|
|
if (playerState == PlayerState.InFinishingPath)
|
|
{
|
|
int nextIndex = CurrentTileIndex + 1;
|
|
int maxIndex = tilesManager.GetFinishingTileDataLength(PlayerType) - 1;
|
|
|
|
if (nextIndex <= maxIndex)
|
|
{
|
|
lookDirection = tilesManager.RetrieveFinishingTileBasedOnIndex(PlayerType, nextIndex).transform.position - transform.position;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (gameplayManager.IsGoingInsideFinishingPath(this, out int possibleSteps))
|
|
{
|
|
lookDirection = tilesManager.RetrieveFinishingTileBasedOnIndex(PlayerType, 0).transform.position - transform.position;
|
|
}
|
|
else
|
|
{
|
|
int nextIndex = gameplayManager.TilesManager.GetNextGeneralTileIndex(CurrentTileIndex);
|
|
Debug.Log($"nextIndex :: {nextIndex}, currentIndex: {CurrentTileIndex}");
|
|
lookDirection = tilesManager.RetrieveTileBasedOnIndex(nextIndex).transform.position - transform.position;
|
|
}
|
|
}
|
|
|
|
lookDirection = lookDirection.normalized;
|
|
lookDirection.y = 0;
|
|
|
|
if (lookDirection != Vector3.zero)
|
|
{
|
|
// transform.LookAt(transform.position + lookDirection);
|
|
transform.DOLookAt(transform.position + lookDirection, 0.2f);
|
|
}
|
|
|
|
onComplete?.Invoke();
|
|
};
|
|
}
|
|
|
|
public void MoveToCustomTilePosition(Vector3 targetPoint)
|
|
{
|
|
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(BasePlacementData basePlacementData, PlayerType playerType)
|
|
{
|
|
PlayerId = basePlacementData.playerBaseId;
|
|
PlayerType = playerType;
|
|
|
|
MoveBackToHome(basePlacementData.placementTransform);
|
|
}
|
|
|
|
public void ShowPlayerCountCanvas(bool show)
|
|
{
|
|
playerCountCanvas.gameObject.SetActive(show);
|
|
}
|
|
|
|
public void ResetData()
|
|
{
|
|
SetPlayerState(PlayerState.InHome);
|
|
StepsTaken = 0;
|
|
PlayerId = 0;
|
|
if (playerCountCanvas.gameObject.activeInHierarchy)
|
|
ShowPlayerCountCanvas(false);
|
|
|
|
}
|
|
}
|