Last active
August 28, 2016 01:44
-
-
Save DarkDesire/37ebe105db58ff51c64f to your computer and use it in GitHub Desktop.
Unity3d пятнашки (barley brake)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class BarleyBrake : MonoBehaviour | |
{ | |
/// <summary> | |
/// Событие, которое произойдёт при победе | |
/// </summary> | |
public Observer OnWin = delegate { }; | |
/// <summary> | |
/// Позиция первого элемента | |
/// </summary> | |
public Vector2 firstElementPosition; | |
/// <summary> | |
/// Элементы пятнашек | |
/// </summary> | |
[SerializeField] | |
private List<BarleyBrakeElement> elements = new List<BarleyBrakeElement>(); | |
[SerializeField] | |
private BarleyBrakeState currentState = BarleyBrakeState.Stationary; | |
/// <summary> | |
/// Добавить элемент | |
/// </summary> | |
/// <param name="element"></param> | |
public void AddElement(BarleyBrakeElement element) | |
{ | |
elements.Add(element); | |
} | |
/// <summary> | |
/// Удалить элемент | |
/// </summary> | |
/// <param name="element"></param> | |
public void RemoveElement(BarleyBrakeElement element) | |
{ | |
elements.Remove(element); | |
} | |
/// <summary> | |
/// Возвращает общее кол-во элементов | |
/// </summary> | |
/// <returns></returns> | |
public int GetNumberOfElements() | |
{ | |
return elements.Count + 1; | |
} | |
/// <summary> | |
/// Возвращает кол-во элементов в ширину( и в высоту ) | |
/// </summary> | |
/// <returns></returns> | |
public int GetNumberOfElementsOnLine() | |
{ | |
return (int)Mathf.Sqrt(elements.Count+1); | |
} | |
/// <summary> | |
/// Вернуть текущее состояние пятнашек | |
/// </summary> | |
/// <returns></returns> | |
public BarleyBrakeState GetCurrentState() | |
{ | |
return currentState; | |
} | |
/// <summary> | |
/// Запустить сдвиг линии | |
/// </summary> | |
/// <param name="directionOfMovement">Направление движения</param> | |
/// <param name="positionOfMoovingElement">Позиция элемента, который начинаем двигать</param> | |
public void MoveLine(Vector2 directionOfMovement, Vector2 positionOfMoovingElement) | |
{ | |
if (directionOfMovement.x != 0) | |
{ | |
elements.FindAll(obj => obj.GetCurrentPosition().y == positionOfMoovingElement.y).ToList().ForEach(obj => obj.StartMove(directionOfMovement)); | |
} | |
else if (directionOfMovement.y != 0) | |
{ | |
elements.FindAll(obj => obj.GetCurrentPosition().x == positionOfMoovingElement.x).ToList().ForEach(obj => obj.StartMove(directionOfMovement)); | |
} | |
ChangeState(BarleyBrakeState.Mooving); | |
} | |
public void TranslateLine(Vector2 directionOfMovement, Vector2 positionOfMoovingElement) | |
{ | |
if (directionOfMovement.x != 0) | |
{ | |
elements.FindAll(obj => obj.GetCurrentPosition().y == positionOfMoovingElement.y).ToList().ForEach(obj => obj.Translation(directionOfMovement)); | |
} | |
else if (directionOfMovement.y != 0) | |
{ | |
elements.FindAll(obj => obj.GetCurrentPosition().x == positionOfMoovingElement.x).ToList().ForEach(obj => obj.Translation(directionOfMovement)); | |
} | |
} | |
private void Start() | |
{ | |
List<Vector2> freePositions = new List<Vector2>(); | |
for (int i = 0; i < elements.Count; i++) | |
{ | |
freePositions.Add(elements[i].GetRightPosition()); | |
} | |
for (int i = 0; i < elements.Count; i++) | |
{ | |
bool hasElement = false; | |
hasElement = freePositions.Remove(elements[i].GetRightPosition()); | |
int positionIndex = Random.Range(0, freePositions.Count-1); | |
elements[i].SetPositionAtOnce(freePositions[positionIndex]); | |
if(hasElement) | |
freePositions.Add(elements[i].GetRightPosition()); | |
freePositions.RemoveAt(positionIndex); | |
} | |
} | |
private void Update() | |
{ | |
if (currentState == BarleyBrakeState.Mooving) | |
if (!elements.Find(obj => obj.GetCurrentState() == BarleyBrakeState.Mooving)) | |
ChangeState(BarleyBrakeState.Stationary); | |
} | |
/// <summary> | |
/// Проверка на победу | |
/// </summary> | |
private void CheckForWin() | |
{ | |
for (int i = 0; i < elements.Count; i++) | |
{ | |
if (elements[i].GetCurrentPosition() != elements[i].GetRightPosition()) | |
return; | |
} | |
OnWin(this, System.EventArgs.Empty); | |
} | |
private void ChangeState(BarleyBrakeState newState) | |
{ | |
if (newState == BarleyBrakeState.Stationary) | |
CheckForWin(); | |
currentState = newState; | |
} | |
} | |
public enum BarleyBrakeState { Mooving, Stationary }; | |
public delegate void Observer(object sender, System.EventArgs e); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class BarleyBrakeCreator : MonoBehaviour | |
{ | |
public Sprite background; | |
public Transform targetCanvas; | |
public List<Sprite> elements = new List<Sprite>(); | |
public float elementWidth = 100, elementHeight = 100; | |
public float firstElementPositionX = 0, firstElementPositionY = 0; | |
public float backgroundWidth = 100, backgroundHeight = 100; | |
public bool elementSizeFoldout = false; | |
public bool elementSizeToggle = false; | |
public bool firstElementPositionFoldout = false; | |
public bool backgroundFoldout = false; | |
public bool backgroundSizeToggle = false; | |
public bool elementsFoldout = false; | |
public bool hasError = false; | |
public ErrorType error; | |
} | |
public enum ErrorType { NoBackground, ZeroSizeBackground, ZeroSizeElement, WrongElementCount, NotAllElementsSetUp, NoTargetCanvas }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment