// Transforms one or more given pieces to a certain type, on a certain Square or Intersection public void TransformPiece(PieceType type, GameModel.Intersection toIntr, GameModel.Square toSqr, params GameModel.Piece[] piece) { // Transforming one piece if(piece.Length == 1 && toSqr != null) { int index = m_pieces.IndexOf(GetPieceState(piece[0])); m_pieces[index].GetComponent<PieceScript>().Type = type; m_pieces[index].GetComponent<PieceScript>().Square = toSqr; m_pieces[index].GetComponent<PieceScript>().Piece = new GameModel.Piece(type, piece[0].Color); m_pieces[index].GetComponent<PieceScript>().InitSprites(); m_pieces[index].GetComponent<PieceScript>().CheckSprite(); } // Transforming Globules else if(piece.Length > 1 && toIntr != null) { int index = m_pieces.IndexOf(GetPieceState(piece[0])); foreach (GameModel.Piece item in piece) { if(item != piece[0]) m_pieces.Remove(GetPieceState(item)); } m_pieces[index].GetComponent<PieceScript>().Type = type; m_pieces[index].transform.position = type != PieceType.Tetraglobe ? ToUnityCoord(toSqr) : ToUnityCoord(toIntr); m_pieces[index].GetComponent<PieceScript>().Piece = new GameModel.Piece(type, piece[0].Color); m_pieces[index].GetComponent<PieceScript>().InitSprites(); m_pieces[index].GetComponent<PieceScript>().CheckSprite(); } }
public void SwapPiece(GameModel.Piece piece1, GameModel.Piece piece2, GameModel.Square toSqr, GameModel.Intersection toIntr) { MovePiece(piece1, toSqr); MovePiece(piece2, toIntr); GetPieceState(piece1).GetComponent<PieceScript>().CheckSprite(); GetPieceState(piece2).GetComponent<PieceScript>().CheckSprite(); }
public static GameModel.Square ToModelSqr(Vector3 pos) { GameModel.Square square = new GameModel.Square(); square.X = (int)(pos.y / 0.6); square.Y = (int)((pos.x + 4.2) / 0.6); return square; }
public GameObject GetPieceState(GameModel.Square square) { foreach(GameObject item in m_pieces) { if(item.GetComponent<PieceScript>().Square == square) return item; } return null; }
// Removes a piece on a given square public void RemovePiece(GameModel.Square sqr) { GameObject removedPiece = GetPieceState(sqr); if (!m_pieces.Contains(removedPiece)) return; removedPiece.SetActive(false); m_pieces.Remove(removedPiece); Destroy(removedPiece); }
// For the translation of ingame coordinates to Unity and vice-versa : // We inverse X and Y coordinates because Unity's coordinates system doesn't work the same way // In Square(5, 6) we would be 5 squares UP and 6 squares RIGHT // In Vector3(5,6) we could be 5 "meter" RIGHT and 6 "meters" UP // So we need to invert that and then make the math to have distances relevant to the actual "physical" squares ! public static Vector3 ToUnityCoord(GameModel.Square sqr) { Vector3 position = new Vector3(); position.x = (float)(sqr.Y*0.6); // the distance between two squares center is of 0.6 "meters" // since Unity starts 0,0 on TOP-LEFT and the GameModel starts 0,0 on BOTTOM-LEFT // we need to lower our starting position (7*0.6) times to be on BOTTOM-LEFT, hence the -4.2 position.y = (float)(-4.2 + sqr.X*0.6); return position; }
public PieceScript(PieceType type, Color color, GameModel.Square square) { m_type = type; m_color = color; gameObject.transform.position = Board.ToUnityCoord(square); if (!InitSprites()) { Debug.LogError("Error initializating sprites for " + m_type); return; } gameObject.GetComponent <SpriteRenderer>().sprite = (square.X + square.Y) % 2 == 0 ? m_sprite1 : m_sprite2; gameObject.SetActive(true); }
/* ENDOF COORDINATES TRANSLATORS */ // Adds a new GameObject containing a SpriteRenderer, BoxCollider2D, PieceScript, fully initiated; on the board public void PlacePiece(GameModel.Piece piece, GameModel.Square square) { GameObject newPiece = new GameObject(piece.Type.ToString()); newPiece.AddComponent<SpriteRenderer>(); newPiece.AddComponent<BoxCollider2D>(); newPiece.AddComponent<PieceScript>(); newPiece.GetComponent<BoxCollider2D>().isTrigger = true; newPiece.GetComponent<PieceScript>().Collider = newPiece.GetComponent<BoxCollider2D>(); newPiece.GetComponent<PieceScript>().Type = piece.Type; newPiece.GetComponent<PieceScript>().Color = piece.Color; newPiece.GetComponent<PieceScript>().Square = square; newPiece.GetComponent<PieceScript>().Piece = piece; newPiece.GetComponent<PieceScript>().InitSprites(); newPiece.GetComponent<PieceScript>().CheckSprite(); m_pieces.Add(newPiece); Instantiate(newPiece, newPiece.transform.position, Quaternion.identity); }
// Create a Globule on a given square public void CreateGlobule(GameModel.Piece piece, GameModel.Square toSqr) { PlacePiece(piece, toSqr); }
public void MovePiece(GameModel.Piece piece, GameModel.Square toSqr) { GetPieceState(piece).transform.position = ToUnityCoord(toSqr); }