вторник, 5 ноября 2013 г.

Puzzle game for Unity3D in C#

The idea of this 2D game is as follows: Given a 800*600 image, a custom unity plugin splits into pieces (say, 4) in edit mode and then randomly shuffles pieces around the gameboard. In run-time user has to drag and drop pieces into its original position with mouse. If original image is assembled, the goal of the game is achieved and game setup is restarted (pieces shuffled again).

Play puzzle Unity game onlne

Download puzzle game Unity source code 

Script that handles game logic
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System;

public class DragIt : MonoBehaviour {
 
 public GameObject winImage;
 
 private Ray ray;
 private RaycastHit hit;
 
 private bool dragging=false;
 private GameObject draggingObject = null;
 private GameObject targetObject = null;
 private Color savedColor;
 
 // Update is called once per frame
 void Update () {
  if (Input.GetMouseButton(0)) {
   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   if (Physics.Raycast(ray, out hit)) {
    
    if (!dragging) {
     dragging = true;
     draggingObject = hit.transform.gameObject;
    } 
    if (dragging) {
     draggingObject.transform.position = new Vector3(hit.point.x,hit.point.y,200);
     
     // calculate distance to all pieces, except current object
     var pieces = Helper.IteratePieces().Where (p=>p!=draggingObject).ToList();
     var distances = pieces.Select(p=>Vector3.Distance(p.transform.position,draggingObject.transform.position)).ToList();
     
     var minDistance = distances.Min(); // get minimum distance
     
     // check distance between current position and original position 
     PositionData storage = draggingObject.GetComponent();
     var currentDistance = Vector3.Distance(storage.ShuffledPosition,draggingObject.transform.position);
     GameObject targetObjectNew=null;
     if (currentDistance < minDistance)
      targetObjectNew = draggingObject;
     else {
      var index = distances.IndexOf(minDistance);
      targetObjectNew = pieces[index];
     }
     
     //Debug.Log("Distances: " + Helper.ToString(distances) + ", target " + targetObjectNew.name + " min distance: " + minDistance + ", current distance: " + currentDistance);
          
     
     // target object changed
     if (targetObjectNew != targetObject) {
      //Debug.Log("Distances: " + Helper.ToString(distances) + ", target " + targetObjectNew.name + " min distance: " + minDistance + "current distance: " + currentDistance);
      Debug.Log("Switch to  "  + targetObjectNew.name );
      if (targetObject!=null && targetObject != draggingObject ) {
       targetObject.renderer.material.color = savedColor;
       //Debug.Log("Restored color for "  + targetObject.name + " to " + savedColor.ToString() );
      }
      
      if (targetObjectNew != draggingObject) {
       
       // highlight target object
       savedColor = targetObjectNew.renderer.material.color;
       targetObjectNew.renderer.material.color = Color.green;
      }
      
      targetObject = targetObjectNew;
     } 
     
     
     
    }
    
   }
  } else if (dragging) {
   dragging = false; // dropping
   PositionData storageSource = draggingObject.GetComponent();
   
   
   
   // reset target object position, if it is the same object
   if (targetObject==draggingObject)
    draggingObject.transform.position = storageSource.ShuffledPosition;
   else {
    // restore color
    if (targetObject!=null)
     targetObject.renderer.material.color = savedColor;
    PositionData storageTarget = targetObject.GetComponent();
    
    // swap positions between source and target
    targetObject.transform.position = storageSource.ShuffledPosition;
    draggingObject.transform.position = storageTarget.ShuffledPosition;
    
    // store new positions
    storageSource.ShuffledPosition = draggingObject.transform.position;
    storageTarget.ShuffledPosition = targetObject.transform.position;
    
    // check win condition and notify by color blinking
    if (!CheckWinCondition()) 
    {
     if (draggingObject.transform.position == storageSource.OriginalPosition)
      StartCoroutine(Blink (Color.green, draggingObject));
     else
      StartCoroutine(Blink (Color.red, draggingObject));
    
     if (targetObject.transform.position == storageTarget.OriginalPosition)
      StartCoroutine( Blink (Color.green, targetObject));
     else
      StartCoroutine(Blink (Color.red, targetObject));
    }
    
   }
   Debug.Log("Dropped  "  + targetObject.name );
   targetObject = null;
   draggingObject = null;
  }
  
  
  
 
 }
 
 bool CheckWinCondition() {
  bool win = Helper.IteratePieces().Select(o=>o.GetComponent()).All(d=>d.OriginalPosition==d.ShuffledPosition);
  if (win) {
   StartCoroutine(Win ());
  }
  return win;
 }
 
 /// 
 /// Blink with the specified color for a given game object.
 /// 
 /// 
 /// Color.
 /// 
 /// 
 /// Go.
 /// 
 IEnumerator Blink(Color color, GameObject go) {
  Color savedcolor = go.renderer.material.color;
  for(int i=0;i<=2;i++) {
   yield return new WaitForSeconds(.1f);
   go.renderer.material.color = color;
   yield return new WaitForSeconds(.25f);
   go.renderer.material.color = savedcolor;
  }
 }
 
 IEnumerator Win() {
  Helper.IteratePieces().ToList().ForEach(o=>StartCoroutine(Blink (Color.green, o)));
  yield return new WaitForSeconds(.2f);
  
  if (winImage!=null)
   winImage.SetActive(true);
  else
   Debug.LogError("WinImage not found");
  
  // enjoy a "GAME WON" title for a moment and restart a game
  yield return new WaitForSeconds(5f);
  if (winImage!=null)
   winImage.SetActive(false);
  Helper.ShuffleGameObjects();
 }
}


Unity plugin source code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System;
using System.Linq;

public class CreatePuzzle : ScriptableWizard {

 public Texture2D Picture;
 
 public int N=2;
 
 [MenuItem("GameObject/Create Other/Puzzle")]
    static void CreateWizard()
    {
        ScriptableWizard.DisplayWizard("Create Puzzle",typeof(CreatePuzzle));
    }
 
 void OnWizardCreate()
 {
  int width = Picture.width;
  int height = Picture.height;
  Debug.Log("wh: " + width + "/" + height);
    
  for(int x=1;x<=N;x++)
   for(int y=1;y<=N;y++) {
    int xPiece = Convert.ToInt32(width*x/(float)N - width/2f - width/2f/N);
    int yPiece = Convert.ToInt32(height*y/(float)N - height/2f - height/2f/N);
    string name = "piece_" + x + "_" + y; 
    //Debug.Log(name + ": " + xPiece + "/" + yPiece);
    Rect uv = new Rect( (x-1)/(float)N,(y-1)/(float)N, 1/(float)N, 1/(float)N);
    CreateMesh(name,xPiece,yPiece,uv, width/N, height/N);
  }
  
  Shuffle(); // shuffle pieces to make a puzzle
 }
 
 GameObject CreateMesh(string meshName, int x, int y, Rect uv, int width, int height) {
  
  string assetFolder = "Assets";
  
  
  int anchorX=Convert.ToInt32(width/2);
  int anchorY=Convert.ToInt32(height/2);
   
  //Create Vertices
  Vector3[] Vertices = new Vector3[4];
  
  //Create UVs
  Vector2[] UVs = new Vector2[4];
  
  //Two triangles of quad
  int[] Triangles = new int[6];
  
  //Assign vertices based on pivot
  
  //Bottom-left
  Vertices[0].x = -anchorX;
  Vertices[0].y = -anchorY;
  
  //Bottom-right
  Vertices[1].x = Vertices[0].x+width;
  Vertices[1].y = Vertices[0].y;
   
  //Top-left
  Vertices[2].x = Vertices[0].x;
  Vertices[2].y = Vertices[0].y+height;
  
  //Top-right
  Vertices[3].x = Vertices[0].x+width;
  Vertices[3].y = Vertices[0].y+height;
  
  Vector2[] uvs2 = new Vector2[]
        {
            new Vector2(uv.x, uv.y),      //Bottom-left
            new Vector2(uv.x+uv.width, uv.y),    //Bottom-right
            new Vector2(uv.x, uv.y+uv.height),    //Top-left
            new Vector2(uv.x+uv.width, uv.y+uv.height),  //Top-right 
        };
  
  //Assign triangles
  Triangles[0]=3;
  Triangles[1]=1;
  Triangles[2]=2;
  
  Triangles[3]=2;
  Triangles[4]=1;
  Triangles[5]=0;
  
  //Generate mesh
  Mesh mesh = new Mesh();
  mesh.name = meshName;
  mesh.vertices = Vertices;
  mesh.uv = uvs2;
  mesh.triangles = Triangles;
  mesh.RecalculateNormals();
  
  //Create asset in database
  AssetDatabase.CreateAsset(mesh, AssetDatabase.GenerateUniqueAssetPath(assetFolder + "/" + meshName) + ".asset");
        AssetDatabase.SaveAssets();
  
  //Create plane game object
  GameObject piece = new GameObject(meshName);
  MeshFilter meshFilter = (MeshFilter)piece.AddComponent(typeof(MeshFilter));
  piece.AddComponent(typeof(MeshRenderer));
  
  
  //Assign mesh to mesh filter
  meshFilter.sharedMesh = mesh;
  
  mesh.RecalculateBounds();
  
  //Add a box collider component
  piece.AddComponent(typeof(BoxCollider));
  
  // place piece into designed coordinates
  piece.transform.Translate(new Vector3(x,y,200));
   
  // capture original position
  piece.AddComponent();
  PositionData storage = piece.GetComponent();
  storage.OriginalPosition = piece.transform.position;
  
  var tempMaterial = new Material(piece.renderer.material); // 
  tempMaterial.mainTexture = Picture;
  piece.renderer.material = tempMaterial;


  //plane.renderer.sharedMaterial.mainTexture = Picture;
  piece.transform.parent = GameObject.Find("scene").transform;
  return piece;
 
 }
 
 void Shuffle() {
  Helper.ShuffleGameObjects();
  
 }
}


Helper class
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Linq;

public static class Helper  {

 public static IEnumerable Shuffle(this IEnumerable enumerable)
 {
     var r = new System.Random();
     return enumerable.OrderBy(x=>r.Next()).ToList();
 }
 
 public static IEnumerable IteratePieces() {
  var parent = GameObject.Find("scene").transform;
  for (int i = 0; i < parent.childCount; ++i)
        {
            var piece = parent.GetChild(i).gameObject;
   yield return piece;
        }
 }
 
 public static IEnumerable GetPositions(this IEnumerable enumerable) {
  return enumerable.Select (o=>o.transform.position);
 }
 
 public static string ToString(this IEnumerable enumerable) {
  return string.Join (" ",enumerable.Select (i=>i.ToString()).ToArray());
 }
 
 public static void ShuffleGameObjects() {
  var objects = Helper.IteratePieces().ToList();
  var positions = objects.GetPositions();
  var positionsShuffed = positions.Shuffle().ToList();
  for (int i = 0; i < objects.Count; i++) {
   objects[i].transform.position = positionsShuffed[i];
   PositionData storage = objects[i].GetComponent();
   storage.ShuffledPosition = positionsShuffed[i];
  }
 }
 
 
}

воскресенье, 3 ноября 2013 г.

Console Tetris game


This is a sample implementation of the tetris game in console. The tetris game was implemented both in C++ and C#. C++ version uses a predefined base class that provides access to character console buffer, keyboard control and main generic game cycle. C# version uses the same C++ base class, but this time compiled into managed DLL assembly.

Download C++ console tetris game source code
Download C# console tetris game source code


C++ code sample
const float tetris::SPEED = 0.5; // default seconds per each down move 
const float tetris::HIGH_SPEED = 0.05;

tetris::tetris() : BaseApp(24, 30)
{
 srand (time(NULL));
 pausing = true;
 figure=NULL;
 figureNext=NULL;
 moveTimer =  0;

 DrawScene(); // draw scene
 DrawWalls();


 SpawnNewFigure();

}

void tetris::SpawnNewFigure() 
{
 Figure::POINT pt;
 currentSpeed = SPEED;
 moveDownCounter = 0;
 // take next figure as current one
 if (figure) {
  delete figure;
  figure=NULL;
 }
 if (figureNext) 
  figure = figureNext;

 // initialize current figure for the first time
 // place current figure at initial position on top
 pt.x = (X_SIZE-6)/2;
 pt.y = 2;
 if (!figure) figure = new Figure(pt,this);
 else figure->PlaceTo(pt); 
 figure->Draw();


 // prepare next figure for future cycle
 // create and show next figure on the right
 pt.x = X_SIZE-5;
 figureNext = new Figure(pt,this);
 figureNext->Draw();
 pausing = false;

}

void tetris::KeyPressed(int btnCode)
{
 try {
  if (btnCode == 224) btnCode = getch();
  switch(btnCode) 
  {
   case 119: case 72: currentSpeed=SPEED; break;   // w, up arrow
   case 115: case 80: currentSpeed=HIGH_SPEED; break;  // s, down arrow
   case 97: case 75: figure->MoveLeft(); break;   // a, left arrow
   case 100: case 77: figure->MoveRight(); break;   // d, right arrow
   case 32: figure->Rotate(); break;      // space
   case 'p': pausing=!pausing; break;      // pause
  }
 } catch(...) {
  pausing = true;
  SpawnNewFigure();
 }

}

void tetris::UpdateF(float deltaTime)
{
 try {
  if (figure && !pausing) 
  {
   moveTimer += deltaTime;
   if (moveTimer>=currentSpeed) 
   {
    moveTimer = 0;
    bool ok = figure->MoveDown();
    if (ok) 
    {
     moveDownCounter++;
     ok = figure->CanMoveDown();
    }
    if (!ok) 
    {
     pausing = true;
     if (moveDownCounter==0) // if we could not make even 1 move down - GAME OVER: restart a game
     {
      DrawScene(); // draw scene
      DrawWalls();
     }
     SpawnNewFigure(); // if reached a floor, respawn a new figure from top
    }
   }
  }
 } catch(...) {
  pausing = true;
  SpawnNewFigure();
 }
}

void tetris::DrawWalls()
{
 for (int x = 0; x < X_SIZE; x++)
 {
  SetChar(x, 0, W);
  SetChar(x, Y_SIZE-1, W);
 }

 for (int y = 0; y < Y_SIZE; y++)
 {
  SetChar(0, y, W);
  SetChar(X_SIZE-1, y, W);
 }

 for (int x = X_SIZE-7; x < X_SIZE; x++)
 {
  SetChar(x, 5, W);
 }

 for (int y = 0; y < 5; y++)
 {
  SetChar(X_SIZE-7, y, W);
 }

}

void tetris::DrawScene() 
{
 for (int x = 0; x < X_SIZE; x++)
 {
  for (int y = 0; y < Y_SIZE; y++)
  {
   SetChar(x,y, E);
  }
 }
}

C# source code sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

using FigureLayout = System.Collections.Generic.List;

namespace tetrisCSharp
{
    
    class Tetris : BaseApp
    {
        public const char W = '#'; // wall character
        public const char F = '*'; // figure character
        public const char E = '.'; // empty character

        float moveTimer;
        bool pausing;
        int moveDownCounter;

        const float SPEED = 0.5f; // default speed: seconds per move 
        const float HIGH_SPEED = 0.05f; // high speed

        float currentSpeed = SPEED;   // current speed

        Figure figure;
        Figure figureNext;

        void DrawWalls()
        {
            Enumerable.Range(0, X_SIZE).ForEach(x => { SetChar(x, 0, W); SetChar(x, Y_SIZE - 1, W); }); // horizontal walls
            Enumerable.Range(0, Y_SIZE).ForEach(y => { SetChar(0, y, W); SetChar(X_SIZE - 1, y, W); }); // vertical walls

            // box, showing next figure
            Enumerable.Range(X_SIZE - 7, 7).ForEach(x => SetChar(x, 5, W));
            Enumerable.Range(0, 5).ForEach(y => SetChar(X_SIZE - 7, y, W));
        }
        
        void DrawScene()
        {
            // place empty char in all cells
            Enumerable.Range(0, X_SIZE).CrossJoinAndExecute(Enumerable.Range(0, Y_SIZE), (x, y) => SetChar(x, y, E));
        }

        void SpawnNewFigure()
        {
            POINT pt;
            currentSpeed = SPEED;
            moveDownCounter = 0;
            // take next figure as current one
            if (figure != null)
            {
                figure = null;
            }
            if (figureNext != null)
                figure = figureNext;

            // initialize current figure for the first time
            // place current figure at initial position on top
            pt.x = (X_SIZE - 6) / 2;
            pt.y = 2;
            if (figure == null) figure = new Figure(pt, this);
            else figure.PlaceTo(pt);
            figure.Draw();


            // prepare next figure for future cycle
            // create and show next figure on the right
            pt.x = X_SIZE - 5;
            figureNext = new Figure(pt, this);
            figureNext.Draw();
            pausing = false;
        }

        internal Tetris()
            : base(24, 30)
        {
            pausing = true;
            moveTimer = 0;

            DrawScene(); // draw scene
            DrawWalls();


            SpawnNewFigure();
        }

       
        public override void KeyPressed(int btnCode)
        {
            try
            {
                switch (btnCode)
                {
                    case 119:
                    case 72: currentSpeed = SPEED; break;   // w, up arrow
                    case 115:
                    case 80: currentSpeed = HIGH_SPEED; break;  // s, down arrow
                    case 97:
                    case 75: figure.MoveLeft(); break;       // a, left arrow
                    case 100:
                    case 77: figure.MoveRight(); break;       // d, right arrow
                    case 32: figure.Rotate(); break;    // space
                    case 'p': pausing = !pausing; break;   // pause
                }
            }
            catch
            {
                pausing = true;
                SpawnNewFigure();
            }
        }

        public override void UpdateF(float deltaTime)
        {
            try
            {
                if (figure!=null && !pausing)
                {
                    moveTimer += deltaTime;
                    if (moveTimer >= currentSpeed)
                    {
                        moveTimer = 0;
                        bool ok = figure.MoveDown();
                        if (ok)
                        {
                            moveDownCounter++;
                            ok = figure.CanMoveDown();
                        }
                        if (!ok)
                        {
                            pausing = true;
                            if (moveDownCounter == 0) // if we could not make even 1 move down - GAME OVER: restart a game
                            {
                                DrawScene(); // draw scene
                                DrawWalls();
                            }
                            SpawnNewFigure(); // if reached a floor, respawn a new figure from top
                        }
                    }
                }
            }
            catch
            {
                pausing = true;
                SpawnNewFigure();
            }
        }
    }
}