Click here to Skip to main content
15,897,371 members
Articles / Web Development / HTML

MVC Bricks for ASP.net

Rate me:
Please Sign up or sign in to vote.
4.96/5 (120 votes)
4 May 2011CPOL11 min read 220.9K   6.4K   155  
Learn how to create a simple game using ASP.net MVC, jQuery, State Machine and CSS3 gradients
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVCBricks.Core
{
    public class GameManager : MVCBricks.Core.IView
    {
        private static GameManager instance = null;
        private static BricksPresenter presenter = null;
        private static BoardViewModel currentBoard = null;

        private GameManager()
        {
            currentBoard = new BoardViewModel();
            currentBoard.Bricks = new BrickViewModel[] { };

            presenter = new BricksPresenter(this);
            presenter.InitializeBoard();
            presenter.Tick();
        }

        public static GameManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new GameManager();
                }
                return instance;
            }
        }

        public BricksPresenter Presenter
        {
            get { return presenter; }
        }

        public BoardViewModel CurrentBoard
        {
            get { return currentBoard; }
        }

        public void InitializeBoard()
        {
            presenter.InitializeBoard();
            currentBoard.IsGameOver = false;
        }

        public void DisplayBoard(string arrayString, IBrick[,] brickArray, int width, int height)
        {
            currentBoard.Bricks = GetBricksArray(height, width, brickArray);
        }

        public void DisplayScore(int score, int hiScore, int lines, int level, MVCBricks.Core.Shapes.IShape next)
        {
            currentBoard.Score = score;
            currentBoard.HiScore = hiScore;
            currentBoard.Lines = lines;
            currentBoard.Level = level;
            currentBoard.Next = GetBricksArray(next.ShapeArray.GetUpperBound(1) + 1, next.ShapeArray.GetUpperBound(0) + 1, next.ShapeArray);
        }

        private BrickViewModel[] GetBricksArray(int rowCount, int colCount, IBrick[,] array)
        {
            var bricksList = new List<BrickViewModel>();

            for (var row = 0; row < rowCount; row++)
            {
                for (var col = 0; col < colCount; col++)
                {
                    var b = array[col, row];
                    if (b != null)
                    {
                        bricksList.Add(new BrickViewModel()
                        {
                            Row = row,
                            Col = col,
                            Color = b.Color.ToString().Replace("Color [", "").Replace("]", "")
                        });
                    }
                    else
                    {
                        bricksList.Add(new BrickViewModel()
                        {
                            Row = row,
                            Col = col,
                            Color = "rgba(0, 0, 0, 1.0)"
                        });
                    }
                }
            }
            return bricksList.ToArray();
        }

        public void GameOver()
        {
            currentBoard.IsGameOver = true;
        }

        public void HighlightCompletedRow(int row)
        {
            //throw new NotImplementedException();
        }
    }

    public class BrickViewModel
    {
        public int Row { get; set; }
        public int Col { get; set; }
        public string Color { get; set; }
    }

    public class BoardViewModel
    {
        public BoardViewModel()
        {
            IsGameOver = false;
        }

        public BrickViewModel[] Bricks { get; set; }
        public int Score { get; set; }
        public int HiScore { get; set; }
        public int Lines { get; set; }
        public int Level { get; set; }
        public BrickViewModel[] Next { get; set; }
        public bool IsGameOver { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions