fixed moving and animations

This commit is contained in:
dddushesss 2021-12-20 21:26:55 +03:00
parent ac7e2a0b52
commit be4ababa0b
6 changed files with 63 additions and 6 deletions

18
Assets/PlayerView.cs Normal file
View File

@ -0,0 +1,18 @@
using System;
using UnityEngine;
public class PlayerView : MonoBehaviour
{
private Animator _animator;
public Action OnStep;
void Start()
{
_animator.GetComponent<Animator>();
}
public void Step()
{
OnStep?.Invoke();
}
}

11
Assets/PlayerView.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 04619977b07331e43a21b8d1bc33b6e9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -73472,3 +73472,10 @@ AnimationClip:
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

@ -2934,6 +2934,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 7150739129758644021} - component: {fileID: 7150739129758644021}
- component: {fileID: -4568693640035559416} - component: {fileID: -4568693640035559416}
- component: {fileID: 8989822822968797088}
m_Layer: 0 m_Layer: 0
m_Name: Asvald m_Name: Asvald
m_TagString: Untagged m_TagString: Untagged
@ -2993,6 +2994,18 @@ Animator:
m_HasTransformHierarchy: 1 m_HasTransformHierarchy: 1
m_AllowConstantClipSamplingOptimization: 1 m_AllowConstantClipSamplingOptimization: 1
m_KeepAnimatorControllerStateOnDisable: 0 m_KeepAnimatorControllerStateOnDisable: 0
--- !u!114 &8989822822968797088
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7527582019267571087}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 04619977b07331e43a21b8d1bc33b6e9, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &7550162364350684268 --- !u!1 &7550162364350684268
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -19,7 +19,10 @@ namespace Chars
public Action<GameObject> OnPlayerSpawned; public Action<GameObject> OnPlayerSpawned;
private Animator _animator; private Animator _animator;
private float _tick; private float _tick;
private PlayerView _playerView;
private bool _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)
@ -30,6 +33,7 @@ namespace Chars
_hexGrid = hexGrid; _hexGrid = hexGrid;
_texture = playerData.hexTexture; _texture = playerData.hexTexture;
_tick = playerData.Tick; _tick = playerData.Tick;
_isMoving = false;
} }
@ -37,22 +41,25 @@ namespace Chars
{ {
if (_cell.GetNeighbor(direction)) if (_cell.GetNeighbor(direction))
{ {
_isMoving = true;
_cell = _cell.GetNeighbor(direction); _cell = _cell.GetNeighbor(direction);
_curentPosition = _cell.coordinates; _curentPosition = _cell.coordinates;
_instance.transform.LookAt(_cell.transform); _instance.transform.LookAt(_cell.transform);
_animator.SetTrigger("Move"); _animator.SetTrigger("Move");
_animator.SetBool("isMoving", true); _animator.SetBool("isMoving", _isMoving);
_instance.transform.DOMove(_cell.transform.position, _tick).OnComplete(() => _playerView.OnStep += () =>
{ {
_animator.SetBool("isMoving", false); _isMoving = false;
_animator.SetTrigger("Jump");
_cell.PaintHex(_texture); _cell.PaintHex(_texture);
}); _animator.SetBool("isMoving", _isMoving);
};
_instance.transform.DOMove(_cell.transform.position, _tick);
} }
} }
public void Spawn() public void Spawn()
{ {
if (!_isAlive) if (!_isAlive)
@ -68,6 +75,7 @@ namespace Chars
OnPlayerSpawned?.Invoke(_instance); OnPlayerSpawned?.Invoke(_instance);
_isAlive = true; _isAlive = true;
_animator = _instance.GetComponent<Animator>(); _animator = _instance.GetComponent<Animator>();
_playerView = _instance.GetComponent<PlayerView>();
} }
} }

View File

@ -28,7 +28,7 @@ namespace Chars
public void Execute() public void Execute()
{ {
if (Time.time - _curTime >= _tick && _moveJoystick.Direction != Vector2.zero) if (!_player.IsMoving && _moveJoystick.Direction != Vector2.zero)
{ {
_curTime = Time.time; _curTime = Time.time;
_player.Move(VectorToDirection(_moveJoystick.Direction.normalized)); _player.Move(VectorToDirection(_moveJoystick.Direction.normalized));