Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Player : MovingObject
- {
- public LayerMask blockingLayer;
- [HideInInspector]
- public Vector3 goalCoordinate;
- private int horizontal, vertical;
- public bool isMadeTurn;
- private SpriteRenderer spriteRenderer;
- public delegate void DieAction();
- public static event DieAction OnDie;
- public delegate void IncreaseSroreAction(int score);
- public static event IncreaseSroreAction IncreaseScore;
- public delegate void FinishLevelAction();
- public static event FinishLevelAction OnFinishLevel;
- void Start ()
- {
- goalCoordinate = transform.position;
- spriteRenderer = GetComponent<SpriteRenderer>();
- horizontal = 0;
- vertical = 0;
- base.Start();
- }
- // Update is called once per frame
- void FixedUpdate () {
- //Debug.Log((goalCoordinate - transform.position).magnitude);
- if (!IsMoving)
- {
- GameController.MovingObjectsGoals.Remove(transform.position);
- //Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction
- horizontal = (int) Input.GetAxisRaw("Horizontal");
- //Get input from the input manager, round it to an integer and store in vertical to set y axis move direction
- vertical = (int) Input.GetAxisRaw("Vertical");
- bool isMoved = false;
- //Check if we have a non-zero value for horizontal or vertical
- if (horizontal != 0)
- {
- int h = RoundToInf(horizontal);
- goalCoordinate = new Vector3(h, 0f, 0f) + transform.position;
- boxCollider.enabled = false;
- var ray = Physics2D.Linecast(transform.position, goalCoordinate, blockingLayer);
- boxCollider.enabled = true;
- if (h > 0)
- {
- spriteRenderer.flipX = false;
- }
- else
- {
- spriteRenderer.flipX = true;
- }
- if (ray.transform == null)
- {
- StartCoroutine(SmoothMovement(goalCoordinate));
- isMoved = true;
- }
- else if (ray.transform.gameObject.tag == "Stone")
- {
- if (ray.transform.position.x < transform.position.x)
- {
- ray.transform.gameObject.GetComponent<Stone>().MoveStone(GravityObject.Direction.Left);
- }
- else
- {
- ray.transform.gameObject.GetComponent<Stone>().MoveStone(GravityObject.Direction.Right);
- }
- }
- else if (ray.transform.gameObject.tag == "Gem")
- {
- StartCoroutine(SmoothMovement(goalCoordinate));
- isMoved = true;
- }
- //переписать без поиска объекта
- }
- if (vertical != 0 && !isMoved)
- {
- int v = RoundToInf(vertical);
- goalCoordinate = new Vector3(0f, v, 0f) + transform.position;
- boxCollider.enabled = false;
- var ray = Physics2D.Linecast(transform.position, goalCoordinate, blockingLayer);
- boxCollider.enabled = true;
- if (ray.transform == null)
- {
- StartCoroutine(SmoothMovement(goalCoordinate));
- isMoved = true;
- }
- else if (ray.transform.gameObject.tag == "Gem")
- {
- StartCoroutine(SmoothMovement(goalCoordinate));
- isMoved = true;
- }
- //FindObjectOfType<AllMovingStaffController>().goals.Add(goalCoordinate);
- }
- if (isMoved)
- {
- FindObjectOfType<AllMovingStuffController>().goals.Add(goalCoordinate);
- GameController.MovingObjectsGoals.Add(goalCoordinate);
- }
- isMadeTurn = true;
- }
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.tag == "Gem")
- {
- IncreaseScore(10);
- FindObjectOfType<AllMovingStuffController>().gems.Remove(other.gameObject.GetComponent<Gem>());
- FindObjectOfType<AllMovingStuffController>().goals.Remove(other.gameObject.GetComponent<Gem>().goalCoordinate);
- other.gameObject.GetComponent<Gem>().StopAllCoroutines();
- Debug.Log("Gem with ID = " + other.transform.GetInstanceID() + " deleted");
- Destroy(other.gameObject);
- isMadeTurn = false;
- }
- else if (other.tag == "Dirt")
- {
- Destroy(other.gameObject);
- isMadeTurn = false;
- }
- else if (other.tag == "Exit")
- {
- OnFinishLevel();
- }
- }
- private int RoundToInf(float number)
- {
- if (number >= 0)
- {
- return Mathf.CeilToInt(number);
- }
- return Mathf.FloorToInt(number);
- }
- /*protected override void AttemptMove<T>(int xDir, int yDir)
- {
- //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move.
- base.AttemptMove<T>(xDir, yDir);
- //Hit allows us to reference the result of the Linecast done in Move.
- RaycastHit2D hit;
- //If Move returns true, meaning Player was able to move into an empty space.
- if (Move(xDir, yDir, out hit))
- {
- //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
- }
- }
- protected override void OnCantMove<T>(T component)
- {
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement