⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.cs

📁 C#开发的运行于windows mobile PDA上的游戏
💻 CS
📖 第 1 页 / 共 2 页
字号:
////////////////////////////////////////////////
// 
// Project: Lines.NET
// Version: 1.1
// Author:  Vladimir L.
// 
// homepage: http://www.boomsoft.org
// e-mail:   support@boomsoft.org
// 
// Copyright (c) 2003-2004, Boomsoft.org
// 

using System;
using System.Drawing;
using System.Threading;
using Lines.Utils;

namespace Lines.Core
{
	/// <summary>
	/// Represents a top level wrapper for the Lines.NET game logic.
	/// </summary>
	/// <remarks>
	/// <p>This class encloses the top-level game logic of Lines.NET. The major functionality of the game 
	/// might be accessed through an instance of this class. It might be useful to access a 
	/// <see cref="Lines.Core.Board"/> methods and attributes via <see cref="Game.Board"/> property
	/// though.</p>
	/// <p>This class provides a set of events that are fired during the game so that one may assign
	/// his/her own event handler to them. There is a possibility to fire some of those events from 
	/// outside of the scope of this class. It's done in order to allow <see cref="Lines.Core.Board"/>
	/// and <see cref="Lines.Core.Ball"/> classes to propagate bottom-up events. This methods must not
	/// be abused.</p>
	/// </remarks>
	public class Game
	{
		/// <summary>
		/// Describes the ball event handler function interface.
		/// </summary>
		public delegate void BallEventHandler(Game game, Ball ball);
		/// <summary>
		/// Describes the balls event handler function interface.
		/// </summary>
		public delegate void BallsEventHandler(Game game, Ball[] balls);
		/// <summary>
		/// Describes the ball move event handler function interface.
		/// </summary>
		public delegate void BallMoveEventHandler(Game game, Ball ball, Point oldPos, Point newPos);

		#region Events
		/// <summary>
		/// Occurs when a new ball is added to the board.
		/// </summary>
		public event BallEventHandler BallAdd;
		/// <summary>
		/// Occurs when a ball is removed from the board.
		/// </summary>
		public event BallEventHandler BallDelete;
		/// <summary>
		/// Occurs when a ball has finished its movement over the board.
		/// </summary>
		public event BallEventHandler BallMoved;
		/// <summary>
		/// Occurs when a ball is moved from one field of the board to another (one-step movement).
		/// </summary>
		public event BallMoveEventHandler BallMove;
		/// <summary>
		/// Occurs when a set of balls is removed from the board.
		/// </summary>
		public event BallsEventHandler BallsDelete;
		#endregion

		#region Attributes
		/// <summary>
		/// Holds the reference to an instance of <see cref="Lines.Core.Board"/> class.
		/// </summary>
		private Board board;
		/// <summary>
		/// Defines wether the board is currently locked.
		/// </summary>
		private bool isLocked = false;
		/// <summary>
		/// Defines wether the current game is over.
		/// </summary>
		/// <remarks>
		/// This is an ad hoc for implementing a game over event. Unfortunately the problems in 
		/// threading with using classes of System.Windows.Forms namespace under .NET Compact Framework
		/// made me employ this strange approach.
		/// </remarks>
		private bool isGameOver = false;
		
		/// <summary>
		/// Defines the amount of new balls that will appear on each step of the game.
		/// </summary>
		private int ballsPerStep;
		/// <summary>
		/// Defines the minimum amount of balls the same color in line to be a reason to
		/// remove them from the board.
		/// </summary>
		private int ballsInLine;
		/// <summary>
		/// Defines the destroy ball pause in milliseconds.
		/// </summary>
		private int destroyBallPause;

		/// <summary>
		/// Defines whether the balls should be destroyed one by one or all at once.
		/// </summary>
		private bool oneByOneDestruction = true;

		/// <summary>
		/// Holds the counter of steps that player have done.
		/// </summary>
		private int stepCount = 0;
		/// <summary>
		/// Holds the player current score.
		/// </summary>
		private int score = 0;

		/// <summary>
		/// The reference to a hi score list class.
		/// </summary>
		private HiScoreList hiScore;
		/// <summary>
		/// Keeps the file name of hi score list.
		/// </summary>
		private string recordsFilename;

		/// <summary>
		/// The list of balls that will appear at the next step.
		/// </summary>
		private Ball[] virtualBalls;

		#endregion

		#region Properties

		/// <summary>
		/// Gets the reference to the game board.
		/// </summary>
		public Board Board
		{
			get {return board;}
		}

		/// <summary>
		/// Gets or sets an amount of new balls that will appear on each step of the game.
		/// </summary>
		/// <remarks>
		/// When assigned the value must be greater then <c>0</c>.
		/// </remarks>
		public int BallsPerStep
		{
			get {return ballsPerStep;}
			set {ballsPerStep = (value > 0) ? value : ballsPerStep;}
		}

		/// <summary>
		/// Gets or sets a minimum amount of balls the same color in line to be a reason to
		/// remove them from the board.
		/// </summary>
		/// <remarks>
		/// When assigned the value must be greater then <c>2</c>.
		/// </remarks>
		public int BallsInLine
		{
			get {return ballsInLine;}
			set {ballsInLine = (value > 2) ? value : ballsInLine;}
		}

		/// <summary>
		/// Gets or sets a value of destroy ball pause in milliseconds.
		/// </summary>
		public int DestroyBallPause
		{
			get {return destroyBallPause;}
			set {destroyBallPause = value;}
		}

		/// <summary>
		/// Gets or sets a value of <see cref="oneByOneDestruction"/> flag.
		/// </summary>
		/// <remarks>
		/// Defines whether the balls should be destroyed one by one or all at once.
		/// </remarks>
		public bool OneByOneDestruction
		{
			get {return oneByOneDestruction;}
			set {oneByOneDestruction = value;}
		}

		/// <summary>
		/// Returns current step.
		/// </summary>
		public int StepCount
		{
			get {return stepCount;}
		}

		/// <summary>
		/// Returns a value of current player score.
		/// </summary>
		public int Score
		{
			get {return score;}
		}

		/// <summary>
		/// Gets an array of balls that will apper the next step.
		/// </summary>
		public Ball[] VirtualBalls
		{
			get {return virtualBalls;}
		}

		/// <summary>
		/// Gets or sets a hi score list file name.
		/// </summary>
		/// <remarks>
		/// While setting a new file name the hi score records are read from the file if present.
		/// </remarks>
		public string RecordsFilename
		{
			get {return recordsFilename;}
			set
			{
				recordsFilename = value;
				hiScore.Load(recordsFilename);
			}
		}

		/// <summary>
		/// Gets a value of <see cref="isLocked"/> flag.
		/// </summary>
		/// <remarks>
		/// Defines wether the board is currently locked.
		/// </remarks>
		public bool IsLocked
		{
			get {return isLocked;}
		}

		/// <summary>
		/// Checks whether the current score is a record and return <c>true</c> if it is so, <c>false</c>
		/// otherwise.
		/// </summary>
		public bool IsRecord
		{
			get {return Records.IsRecord(score, stepCount);}
		}

		/// <summary>
		/// Lousy implementation of game over event. A solution for threading problem in .NET 
		/// Compact Framework.
		/// <seealso cref="isGameOver"/>
		/// </summary>
		public bool IsGameOverEvent
		{
			get 
			{
				// Solution for thread problem :(
				if (isGameOver)
				{
					isGameOver = false;
					return true;
				}
				return false;
			}
		}

		/// <summary>
		/// Gets the reference to a hi score list class.
		/// </summary>
		public HiScoreList Records
		{
			get {return hiScore;}
		}
		#endregion

		/// <summary>
		/// Creates an instance of Lines.NET game class.
		/// </summary>
		/// <remarks>
		/// <p>Initializes the default values of game properties and loads hi score list from the records 
		/// file name.</p>
		/// <p>Constructor does not perform complete initialization. Use the method <see cref="NewGame"/>
		/// do so and start a new game.</p>
		/// </remarks>
		public Game()
		{
			ballsPerStep = AppSettings.Instance.BallsPerStep;
			ballsInLine = AppSettings.Instance.MinBallsInLine;
			destroyBallPause = AppSettings.Instance.PauseDestroyBall;
			
			hiScore = new HiScoreList(AppSettings.Instance.HiScoreListSize);
			recordsFilename = PathHelper.GetAppFile(AppSettings.Instance.HiScoreListFile);

			// Load records from file
			hiScore.Load(recordsFilename);
		}

		/// <summary>
		/// Starts a new game.
		/// </summary>
		/// <remarks>
		/// Creates a new instance of empty board, reset all counters and unlockes the board 
		/// if it was locked.
		/// </remarks>
		public void NewGame()
		{
			// Create new game
			board = new Board(this);

			// Generate first set of balls

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -