123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using KairoEngine.Core;
- namespace KairoEngine.CharacterSystem
- {
- /// <summary>
- /// Action to move a character in a certain direction and speed.
- /// </summary>
- public class CharacterMoveAction : IAction
- {
- CharacterController character; // Reference to the character that will move
- Vector2 direction;
- Vector2 lookDirection;
- bool done = false;
- float speed = 1f;
- // Constructor
- public CharacterMoveAction(CharacterController character, Vector2 direction, Vector2 lookDirection, float speed)
- {
- this.character = character;
- this.direction = direction;
- this.lookDirection = lookDirection;
- this.speed = speed;
-
- }
- public void Start()
- {
- UpdateLookDirection();
- GenericEvents.StartListening(character.unique_name + "-AnimationStepDone", End);
- character.animator.SetFloat("horizontal", (int)lookDirection.x);
- character.animator.SetFloat("vertical", (int)lookDirection.y);
- character.transform.Translate(new Vector3(direction.x * speed * Time.deltaTime, 0, direction.y * speed * Time.deltaTime));
- }
- public void Update()
- {
- UpdateLookDirection();
- character.animator.SetFloat("horizontal", (int)lookDirection.x);
- character.animator.SetFloat("vertical", (int)lookDirection.y);
- character.transform.Translate(new Vector3(direction.x * speed * Time.deltaTime, 0, direction.y * speed * Time.deltaTime));
- }
- public void End()
- {
- if(direction.x > 0.1f || direction.y > 0.1f || direction.x < -0.1f || direction.y < -0.1f) return;
- character.animator.SetFloat("horizontal", 0);
- character.animator.SetFloat("vertical", 0);
- GenericEvents.StopListening(character.unique_name + "-AnimationStepDone", End);
- done = true;
- }
- public bool IsDone()
- {
- return done;
- }
- public void UpdateLookDirection()
- {
- if(speed == 1.25f)
- {
- if(lookDirection.x > 0.1f) lookDirection.x = 1;
- else if(lookDirection.x < -0.1f) lookDirection.x = -1;
- else lookDirection.x = 0;
- if(lookDirection.y > 0.1f) lookDirection.y = 1;
- else if(lookDirection.y < -0.1f) lookDirection.y = -1;
- else lookDirection.y = 0;
- }
- else
- {
- if(lookDirection.x > 0.9f) lookDirection.x = 2;
- else if(lookDirection.x < -0.9f) lookDirection.x = -2;
- else lookDirection.x = 0;
- if(lookDirection.y > 0.9f) lookDirection.y = 2;
- else if(lookDirection.y < -0.9f) lookDirection.y = -2;
- else lookDirection.y = 0;
- }
- }
- }
- }
|