Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TacticsMove : MonoBehaviour {
- public LayerMask tileLayer;
- private bool moving = false;
- private bool jumping = false;
- private bool jumpingUp = false;
- private bool movingEdge = false;
- private bool fallingDown = false;
- public float moveSpeed = 2;
- public float jumpVelocity = 4.5f;
- private Vector3 velocity = new Vector3();
- private Vector3 heading = new Vector3();
- private Vector3 jumpTarget;
- private Unit unit;
- private Queue<Tile> path;
- private List<Tile> walkableTiles;
- void Start(){
- unit = GetComponent<Unit>();
- }
- void Update(){
- if(moving){
- Walk();
- } else if(this.gameObject == TurnManager.GetActiveUnit()) {
- CheckMouse();
- }
- }
- private void CheckMouse(){
- if(Input.GetMouseButtonUp(0)){
- Debug.Log("Check Mouse");
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if(Physics.Raycast(ray, out hit, Mathf.Infinity, tileLayer)){
- if(hit.collider.tag == "Tile") {
- Tile t = hit.collider.GetComponent<Tile>();
- if(t.moveable){
- MoveTo(t);
- }
- }
- }
- }
- if(Input.GetButtonDown("Cancel")){
- MovingCleanUp();
- }
- }
- public void PrepareMovement(){
- unit.GetCurrentTile().current = true;
- int range = (int)(unit.GetCurrentAP() / unit.apToMove.Value);
- walkableTiles = unit.GetCurrentTile().TilesInRange(unit.jumpHeight.Value, false, range);
- foreach(Tile tile in walkableTiles){
- tile.moveable = true;
- }
- }
- public void MoveTo(Tile target){
- Debug.Log("MoveTo");
- SetStateToMoving(true);
- unit.GetCurrentTile().current = false;
- path = CreatePath(target);
- int apCost = (int)(path.Count * unit.apToMove.Value);
- unit.ReduceAP(apCost);
- }
- private void Walk(){
- if (path.Count>0){
- Tile t = path.Peek();
- Vector3 target = t.transform.position;
- // target.y += (halfHeight + t.GetComponent<Collider>().bounds.extents.y)* transform.localScale.y;
- target.y += t.GetComponent<Collider>().bounds.extents.y;
- if(Vector3.Distance(transform.position, target) >= 0.05f) {
- // jumping = transform.position.y != target.y; //correto
- SetStateToJumping(transform.position.y != target.y);
- if(jumping){
- Jump(target);
- } else{
- CalculateHeading(target);
- SetHorizontalVelocity();
- }
- transform.forward = heading;
- transform.position += velocity * Time.deltaTime;
- } else{
- //Tile center reached
- transform.position = target;
- path.Dequeue();
- }
- } else {
- MovingCleanUp();
- }
- }
- private Queue<Tile> CreatePath(Tile target){
- Queue<Tile> path = new Queue<Tile>();
- Tile closest = unit.GetCurrentTile();
- while(closest != target){
- foreach(Tile t in closest.FindNeighbors(unit.jumpHeight.Value, false)){
- if(Vector3.Distance(t.transform.position, target.transform.position) < Vector3.Distance(closest.transform.position, target.transform.position)){
- closest = t;
- }
- }
- path.Enqueue(closest);
- }
- return path;
- }
- private void SetStateToMoving(bool b){
- GetComponent<Animator>().SetBool("moving",b);
- moving = b;
- }
- private void SetStateToJumping(bool b){
- jumping = b;
- GetComponent<Animator>().SetBool("jumping",b);
- }
- private void SetStateToJumpingUp(bool b){
- jumpingUp = b;
- GetComponent<Animator>().SetBool("jumpingUp",b);
- }
- private void SetStateToFallingDown(bool b){
- fallingDown = b;
- GetComponent<Animator>().SetBool("fallingDown",b);
- }
- public void MovingCleanUp(){
- if(walkableTiles != null){
- foreach(Tile t in walkableTiles){
- t.moveable = false;
- }
- }
- unit.GetCurrentTile().current = false;
- SetStateToMoving(false);
- }
- public void CalculateHeading(Vector3 target){
- heading = target-transform.position;
- heading.Normalize();
- transform.forward = heading;
- }
- private void SetHorizontalVelocity(){
- velocity = heading * moveSpeed;
- }
- private void Jump(Vector3 target){
- if(fallingDown){
- FallDownward(target);
- } else if(jumpingUp){
- JumpUpward(target);
- } else if(movingEdge){
- MoveToEdge();
- } else {
- PrepareJump(target);
- }
- }
- private void PrepareJump(Vector3 target){
- float targetY = target.y;
- target.y = transform.position.y;
- CalculateHeading(target);
- if(transform.position.y > targetY){
- SetStateToFallingDown(false);
- SetStateToJumpingUp(false);
- movingEdge = true;
- jumpTarget = transform.position + (target - transform.position) / 2.0f;
- } else {
- SetStateToFallingDown(false);
- SetStateToJumpingUp(false);
- movingEdge = false;
- velocity = heading * moveSpeed / 3.0f;
- float difference = targetY - transform.position.y;
- velocity.y = jumpVelocity * (0.5f + difference / 2.0f);
- }
- }
- private void FallDownward(Vector3 target){
- velocity += Physics.gravity * Time.deltaTime;
- if(transform.position.y <= target.y){
- SetStateToFallingDown(false);
- Vector3 p = transform.position;
- p.y = target.y;
- transform.position = p;
- velocity = new Vector3();
- }
- }
- private void JumpUpward(Vector3 target){
- velocity += Physics.gravity * Time.deltaTime;
- if (transform.position.y > target.y){
- SetStateToJumpingUp(false);
- fallingDown = true;
- }
- }
- private void MoveToEdge(){
- if(Vector3.Distance(transform.position, jumpTarget) >= 0.05f ){
- SetHorizontalVelocity();
- } else {
- movingEdge = false;
- fallingDown = true;
- velocity /= 4.0f;
- velocity.y = 1.5f;
- }
- }
- public bool IsWalking(){
- return moving;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement