Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TacticsMove : MonoBehaviour {
  6.  
  7.     public LayerMask tileLayer;
  8.  
  9.     private bool moving = false;
  10.     private bool jumping = false;
  11.     private bool jumpingUp = false;
  12.     private bool movingEdge = false;
  13.     private bool fallingDown = false;
  14.  
  15.     public float moveSpeed = 2;
  16.     public float jumpVelocity = 4.5f;
  17.  
  18.     private Vector3 velocity = new Vector3();
  19.     private Vector3 heading = new Vector3();
  20.     private Vector3 jumpTarget;
  21.  
  22.     private Unit unit;
  23.  
  24.     private Queue<Tile> path;
  25.     private List<Tile> walkableTiles;
  26.  
  27.  
  28.     void Start(){
  29.         unit = GetComponent<Unit>();
  30.     }
  31.  
  32.     void Update(){
  33.         if(moving){
  34.             Walk();
  35.         } else if(this.gameObject == TurnManager.GetActiveUnit()) {
  36.             CheckMouse();
  37.         }
  38.     }
  39.  
  40.     private void CheckMouse(){
  41.         if(Input.GetMouseButtonUp(0)){
  42.             Debug.Log("Check Mouse");
  43.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  44.             RaycastHit hit;
  45.             if(Physics.Raycast(ray, out hit, Mathf.Infinity, tileLayer)){
  46.                 if(hit.collider.tag == "Tile") {
  47.                     Tile t = hit.collider.GetComponent<Tile>();
  48.                     if(t.moveable){
  49.                         MoveTo(t);
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         if(Input.GetButtonDown("Cancel")){
  55.             MovingCleanUp();
  56.         }
  57.     }
  58.  
  59.     public void PrepareMovement(){
  60.         unit.GetCurrentTile().current = true;
  61.         int range = (int)(unit.GetCurrentAP() / unit.apToMove.Value);
  62.         walkableTiles = unit.GetCurrentTile().TilesInRange(unit.jumpHeight.Value, false, range);
  63.         foreach(Tile tile in walkableTiles){
  64.             tile.moveable = true;
  65.         }
  66.     }
  67.  
  68.     public void MoveTo(Tile target){
  69.         Debug.Log("MoveTo");
  70.         SetStateToMoving(true);
  71.         unit.GetCurrentTile().current = false;
  72.         path = CreatePath(target);
  73.         int apCost = (int)(path.Count * unit.apToMove.Value);
  74.         unit.ReduceAP(apCost);
  75.     }
  76.  
  77.     private void Walk(){
  78.         if (path.Count>0){
  79.             Tile t = path.Peek();
  80.             Vector3 target = t.transform.position;
  81.             // target.y += (halfHeight  + t.GetComponent<Collider>().bounds.extents.y)* transform.localScale.y;
  82.             target.y += t.GetComponent<Collider>().bounds.extents.y;
  83.  
  84.             if(Vector3.Distance(transform.position, target) >= 0.05f) {
  85.                 // jumping = transform.position.y != target.y; //correto
  86.                 SetStateToJumping(transform.position.y != target.y);
  87.                 if(jumping){
  88.                     Jump(target);
  89.                 } else{
  90.                     CalculateHeading(target);
  91.                     SetHorizontalVelocity();
  92.                 }
  93.  
  94.                 transform.forward = heading;
  95.                 transform.position += velocity * Time.deltaTime;
  96.  
  97.             } else{
  98.                 //Tile center reached
  99.                 transform.position = target;
  100.                 path.Dequeue();
  101.             }
  102.  
  103.         } else {
  104.             MovingCleanUp();
  105.         }
  106.     }
  107.  
  108.     private Queue<Tile> CreatePath(Tile target){
  109.         Queue<Tile> path = new Queue<Tile>();
  110.         Tile closest = unit.GetCurrentTile();
  111.         while(closest != target){
  112.             foreach(Tile t in closest.FindNeighbors(unit.jumpHeight.Value, false)){
  113.                 if(Vector3.Distance(t.transform.position, target.transform.position) < Vector3.Distance(closest.transform.position, target.transform.position)){
  114.                     closest = t;
  115.                 }
  116.             }
  117.             path.Enqueue(closest);
  118.         }
  119.         return path;
  120.     }
  121.  
  122.     private void SetStateToMoving(bool b){
  123.         GetComponent<Animator>().SetBool("moving",b);
  124.         moving = b;
  125.     }
  126.  
  127.     private void SetStateToJumping(bool b){
  128.         jumping = b;
  129.         GetComponent<Animator>().SetBool("jumping",b);
  130.     }
  131.  
  132.     private void SetStateToJumpingUp(bool b){
  133.         jumpingUp = b;
  134.         GetComponent<Animator>().SetBool("jumpingUp",b);
  135.     }
  136.  
  137.     private void SetStateToFallingDown(bool b){
  138.         fallingDown = b;
  139.         GetComponent<Animator>().SetBool("fallingDown",b);
  140.     }
  141.  
  142.     public void MovingCleanUp(){
  143.         if(walkableTiles != null){
  144.             foreach(Tile t in walkableTiles){
  145.                 t.moveable = false;
  146.             }
  147.         }
  148.         unit.GetCurrentTile().current = false;
  149.         SetStateToMoving(false);
  150.     }
  151.  
  152.     public void CalculateHeading(Vector3 target){
  153.         heading = target-transform.position;
  154.         heading.Normalize();
  155.         transform.forward = heading;
  156.     }
  157.  
  158.     private void SetHorizontalVelocity(){
  159.         velocity = heading * moveSpeed;
  160.     }
  161.  
  162.     private void Jump(Vector3 target){
  163.  
  164.         if(fallingDown){
  165.             FallDownward(target);
  166.         } else if(jumpingUp){
  167.             JumpUpward(target);
  168.         } else if(movingEdge){
  169.             MoveToEdge();
  170.         } else {
  171.             PrepareJump(target);
  172.         }
  173.     }
  174.  
  175.     private void PrepareJump(Vector3 target){
  176.         float targetY = target.y;
  177.         target.y = transform.position.y;
  178.         CalculateHeading(target);
  179.         if(transform.position.y > targetY){
  180.             SetStateToFallingDown(false);
  181.             SetStateToJumpingUp(false);
  182.             movingEdge = true;
  183.             jumpTarget = transform.position + (target - transform.position) / 2.0f;
  184.  
  185.         } else {
  186.             SetStateToFallingDown(false);
  187.             SetStateToJumpingUp(false);
  188.             movingEdge = false;
  189.  
  190.             velocity = heading * moveSpeed / 3.0f;
  191.             float difference = targetY - transform.position.y;
  192.             velocity.y = jumpVelocity * (0.5f + difference / 2.0f);
  193.         }
  194.     }
  195.  
  196.     private void FallDownward(Vector3 target){
  197.         velocity += Physics.gravity * Time.deltaTime;
  198.         if(transform.position.y <= target.y){
  199.             SetStateToFallingDown(false);
  200.             Vector3 p = transform.position;
  201.             p.y = target.y;
  202.             transform.position = p;
  203.             velocity = new Vector3();
  204.         }
  205.     }
  206.  
  207.     private void JumpUpward(Vector3 target){
  208.         velocity += Physics.gravity * Time.deltaTime;
  209.         if (transform.position.y > target.y){
  210.             SetStateToJumpingUp(false);
  211.             fallingDown = true;
  212.         }
  213.     }
  214.  
  215.     private void MoveToEdge(){
  216.         if(Vector3.Distance(transform.position, jumpTarget) >= 0.05f ){
  217.             SetHorizontalVelocity();
  218.         } else {
  219.             movingEdge = false;
  220.             fallingDown = true;
  221.             velocity /= 4.0f;
  222.             velocity.y = 1.5f;
  223.         }
  224.     }
  225.  
  226.     public bool IsWalking(){
  227.         return moving;
  228.     }
  229.  
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement