Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.94 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class Player : MovingObject
  8. {
  9.     public LayerMask blockingLayer;
  10.     [HideInInspector]
  11.     public Vector3 goalCoordinate;
  12.     private int horizontal, vertical;
  13.  
  14.     public bool isMadeTurn;
  15.     private SpriteRenderer spriteRenderer;
  16.  
  17.     public delegate void DieAction();
  18.     public static event DieAction OnDie;
  19.     public delegate void IncreaseSroreAction(int score);
  20.     public static event IncreaseSroreAction IncreaseScore;
  21.     public delegate void FinishLevelAction();
  22.     public static event FinishLevelAction OnFinishLevel;
  23.  
  24.     void Start ()
  25.     {
  26.         goalCoordinate = transform.position;
  27.         spriteRenderer = GetComponent<SpriteRenderer>();
  28.         horizontal = 0;
  29.         vertical = 0;
  30.        
  31.         base.Start();
  32.     }
  33.  
  34.     // Update is called once per frame
  35.     void FixedUpdate () {
  36.         //Debug.Log((goalCoordinate - transform.position).magnitude);
  37.         if (!IsMoving)
  38.         {
  39.             GameController.MovingObjectsGoals.Remove(transform.position);
  40.             //Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction
  41.             horizontal = (int) Input.GetAxisRaw("Horizontal");
  42.  
  43.             //Get input from the input manager, round it to an integer and store in vertical to set y axis move direction
  44.             vertical = (int) Input.GetAxisRaw("Vertical");
  45.  
  46.             bool isMoved = false;
  47.  
  48.             //Check if we have a non-zero value for horizontal or vertical
  49.             if (horizontal != 0)
  50.             {
  51.                 int h = RoundToInf(horizontal);
  52.  
  53.                 goalCoordinate = new Vector3(h, 0f, 0f) + transform.position;
  54.  
  55.                 boxCollider.enabled = false;
  56.                 var ray = Physics2D.Linecast(transform.position, goalCoordinate, blockingLayer);
  57.                 boxCollider.enabled = true;
  58.  
  59.                 if (h > 0)
  60.                 {
  61.                     spriteRenderer.flipX = false;
  62.                 }
  63.                 else
  64.                 {
  65.                     spriteRenderer.flipX = true;
  66.                 }
  67.  
  68.                 if (ray.transform == null)
  69.                 {
  70.                     StartCoroutine(SmoothMovement(goalCoordinate));
  71.                     isMoved = true;
  72.                 }
  73.                 else if (ray.transform.gameObject.tag == "Stone")
  74.                 {
  75.                     if (ray.transform.position.x < transform.position.x)
  76.                     {
  77.                         ray.transform.gameObject.GetComponent<Stone>().MoveStone(GravityObject.Direction.Left);
  78.                     }
  79.                     else
  80.                     {
  81.                         ray.transform.gameObject.GetComponent<Stone>().MoveStone(GravityObject.Direction.Right);
  82.                     }
  83.                    
  84.                 }
  85.                 else if (ray.transform.gameObject.tag == "Gem")
  86.                 {
  87.                     StartCoroutine(SmoothMovement(goalCoordinate));
  88.                     isMoved = true;
  89.                 }
  90.  
  91.                  //переписать без поиска объекта
  92.             }
  93.             if (vertical != 0 && !isMoved)
  94.             {
  95.                 int v = RoundToInf(vertical);
  96.                 goalCoordinate = new Vector3(0f, v, 0f) + transform.position;
  97.  
  98.                 boxCollider.enabled = false;
  99.                 var ray = Physics2D.Linecast(transform.position, goalCoordinate, blockingLayer);
  100.                 boxCollider.enabled = true;
  101.  
  102.                 if (ray.transform == null)
  103.                 {
  104.                     StartCoroutine(SmoothMovement(goalCoordinate));
  105.                     isMoved = true;
  106.                 }
  107.                 else if (ray.transform.gameObject.tag == "Gem")
  108.                 {
  109.                     StartCoroutine(SmoothMovement(goalCoordinate));
  110.                     isMoved = true;
  111.                 }
  112.                 //FindObjectOfType<AllMovingStaffController>().goals.Add(goalCoordinate);
  113.             }
  114.             if (isMoved)
  115.             {
  116.                 FindObjectOfType<AllMovingStuffController>().goals.Add(goalCoordinate);
  117.                 GameController.MovingObjectsGoals.Add(goalCoordinate);
  118.             }
  119.             isMadeTurn = true;
  120.         }
  121.  
  122.     }
  123.  
  124.     private void OnTriggerEnter2D(Collider2D other)
  125.     {
  126.         if (other.tag == "Gem")
  127.         {
  128.             IncreaseScore(10);
  129.             FindObjectOfType<AllMovingStuffController>().gems.Remove(other.gameObject.GetComponent<Gem>());
  130.             FindObjectOfType<AllMovingStuffController>().goals.Remove(other.gameObject.GetComponent<Gem>().goalCoordinate);
  131.             other.gameObject.GetComponent<Gem>().StopAllCoroutines();
  132.             Debug.Log("Gem with ID = " + other.transform.GetInstanceID() + " deleted");
  133.             Destroy(other.gameObject);
  134.             isMadeTurn = false;
  135.         }
  136.         else if (other.tag == "Dirt")
  137.         {
  138.             Destroy(other.gameObject);
  139.             isMadeTurn = false;
  140.         }
  141.         else if (other.tag == "Exit")
  142.         {
  143.             OnFinishLevel();
  144.         }
  145.  
  146.     }
  147.  
  148.     private int RoundToInf(float number)
  149.     {
  150.         if (number >= 0)
  151.         {
  152.             return Mathf.CeilToInt(number);
  153.         }
  154.         return Mathf.FloorToInt(number);
  155.     }
  156.  
  157.     /*protected override void AttemptMove<T>(int xDir, int yDir)
  158.     {
  159.  
  160.         //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move.
  161.         base.AttemptMove<T>(xDir, yDir);
  162.  
  163.         //Hit allows us to reference the result of the Linecast done in Move.
  164.         RaycastHit2D hit;
  165.  
  166.         //If Move returns true, meaning Player was able to move into an empty space.
  167.         if (Move(xDir, yDir, out hit))
  168.         {
  169.             //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
  170.         }
  171.  
  172.     }
  173.  
  174.     protected override void OnCantMove<T>(T component)
  175.     {
  176.  
  177.     }*/
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement