move time fixed

This commit is contained in:
dddushesss 2021-12-20 22:10:12 +03:00
parent 10363a03f4
commit 0eba5f8bee
3 changed files with 44 additions and 29 deletions

View File

@ -73465,17 +73465,10 @@ AnimationClip:
m_HasGenericRootTransform: 0 m_HasGenericRootTransform: 0
m_HasMotionFloatCurves: 0 m_HasMotionFloatCurves: 0
m_Events: m_Events:
- time: 0.33333334 - time: 0.53333336
functionName: Step functionName: Step
data: data:
objectReferenceParameter: {fileID: 0} objectReferenceParameter: {fileID: 0}
floatParameter: 0 floatParameter: 0
intParameter: 0 intParameter: 0
messageOptions: 0 messageOptions: 0
- time: 0.53333336
functionName:
data:
objectReferenceParameter: {fileID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0

View File

@ -400,49 +400,49 @@ AnimatorController:
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
- m_Name: BackToIdle - m_Name: BackToIdle
m_Type: 9 m_Type: 9
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
- m_Name: Build - m_Name: Build
m_Type: 9 m_Type: 9
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
- m_Name: TreeAttack - m_Name: TreeAttack
m_Type: 9 m_Type: 9
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
- m_Name: Move - m_Name: Move
m_Type: 9 m_Type: 9
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
- m_Name: SuperJump - m_Name: SuperJump
m_Type: 9 m_Type: 9
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
- m_Name: Frozen - m_Name: Frozen
m_Type: 9 m_Type: 9
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
- m_Name: isMoving - m_Name: isMoving
m_Type: 4 m_Type: 4
m_DefaultFloat: 0 m_DefaultFloat: 0
m_DefaultInt: 0 m_DefaultInt: 0
m_DefaultBool: 0 m_DefaultBool: 0
m_Controller: {fileID: 0} m_Controller: {fileID: 9100000}
m_AnimatorLayers: m_AnimatorLayers:
- serializedVersion: 5 - serializedVersion: 5
m_Name: Base Layer m_Name: Base Layer

View File

@ -7,12 +7,17 @@ using Object = UnityEngine.Object;
namespace Chars namespace Chars
{ {
struct AnimLength
{
public float Move;
}
public class Player : IUnit public class Player : IUnit
{ {
private HexCoordinates _curentPosition; private HexCoordinates spawnPos;
private bool _isAlive; private bool _isAlive;
private GameObject _instance; private GameObject _instance;
private GameObject prefab; private GameObject prefab;
private AnimLength _animLength;
private HexCell _cell; private HexCell _cell;
private HexGrid _hexGrid; private HexGrid _hexGrid;
private Texture _texture; private Texture _texture;
@ -20,13 +25,15 @@ namespace Chars
private Animator _animator; private Animator _animator;
private PlayerView _playerView; private PlayerView _playerView;
private bool _isMoving; private bool _isMoving;
private static readonly int Moving = Animator.StringToHash("isMoving");
private static readonly int Move1 = Animator.StringToHash("Move");
private float _tick = 0.8f;
public bool IsMoving => _isMoving; public bool IsMoving => _isMoving;
public GameObject Playerinstance => _instance; public GameObject PlayerInstance => _instance;
public Player(PlayerData playerData, HexGrid hexGrid) public Player(PlayerData playerData, HexGrid hexGrid)
{ {
_curentPosition = playerData.spawnPos; spawnPos = playerData.spawnPos;
prefab = playerData.playerPrefab; prefab = playerData.playerPrefab;
_isAlive = false; _isAlive = false;
_hexGrid = hexGrid; _hexGrid = hexGrid;
@ -37,44 +44,59 @@ namespace Chars
public void Move(HexDirection direction) public void Move(HexDirection direction)
{ {
_tick = Time.time;
if (_cell.GetNeighbor(direction)) if (_cell.GetNeighbor(direction))
{ {
_isMoving = true; _isMoving = true;
_cell = _cell.GetNeighbor(direction); _cell = _cell.GetNeighbor(direction);
_curentPosition = _cell.coordinates;
_instance.transform.LookAt(_cell.transform); _instance.transform.LookAt(_cell.transform);
_animator.SetTrigger("Move"); _animator.SetTrigger(Move1);
_animator.SetBool("isMoving", _isMoving); _animator.SetBool(Moving, _isMoving);
_playerView.OnStep += () => _playerView.OnStep += () =>
{ {
_isMoving = false; _isMoving = false;
_cell.PaintHex(_texture); _cell.PaintHex(_texture);
_animator.SetBool("isMoving", _isMoving); _tick = Time.time - _tick;
_animator.SetBool(Moving, _isMoving);
}; };
_instance.transform.DOMove(_cell.transform.position, _animator.GetCurrentAnimatorClipInfo(0).LongLength);
_instance.transform.DOMove(_cell.transform.position, _animLength.Move);
} }
} }
private void SetAnimLength()
{
AnimationClip[] clips = _animator.runtimeAnimatorController.animationClips;
foreach(var clip in clips)
{
_animLength.Move = clip.name switch
{
"Jump" => clip.length,
_ => _animLength.Move
};
}
}
public void Spawn() public void Spawn()
{ {
if (!_isAlive) if (!_isAlive)
{ {
_cell = _hexGrid.GetCellFromCoord(_curentPosition); _cell = _hexGrid.GetCellFromCoord(spawnPos);
_cell.PaintHex(_texture); _cell.PaintHex(_texture);
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
_cell.GetNeighbor((HexDirection)i).PaintHex(_texture); _cell.GetNeighbor((HexDirection)i).PaintHex(_texture);
} }
_instance = Object.Instantiate(prefab, _cell.transform.parent); _instance = Object.Instantiate(prefab, _cell.transform.parent);
_instance.transform.localPosition = _cell.transform.localPosition; _instance.transform.localPosition = _cell.transform.localPosition;
OnPlayerSpawned?.Invoke(_instance); OnPlayerSpawned?.Invoke(_instance);
_isAlive = true; _isAlive = true;
_animator = _instance.GetComponent<Animator>(); _animator = _instance.GetComponent<Animator>();
_playerView = _instance.GetComponent<PlayerView>(); _playerView = _instance.GetComponent<PlayerView>();
SetAnimLength();
} }
} }