using System;
using System.Drawing;
using System.Collections.Generic;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Bornander.Games.BoardGame;
using Bornander.Games.Direct3D;
using Bornander.Games.BoardGame.Direct3D;
namespace Bornander.Games.Checkers.Direct3D
{
class CheckersModelRepository : IBoardGameModelRepository
{
#region Private members
private Model blackSquare;
private Model redSquare;
private Model redPiece;
private Model redKingPiece;
private Model blackPiece;
private Model blackKingPiece;
#endregion
public CheckersModelRepository()
{
}
#region IBoardGameModelRepository Members
public void Initialize(Microsoft.DirectX.Direct3D.Device device)
{
// Create a box to be used as a board square
// The .Clone call is used to get a Mesh that has vertices that
// contain both position, normal and color which I need to render them using flat shading.
Mesh blockMesh = Mesh.Box(device, 1.0f, 0.5f, 1.0f).Clone(MeshFlags.Managed, VertexFormats.PositionNormal | VertexFormats.Specular, device);
// Create some red and black material and their highlighted counterparts.
Material darkRedMaterial = new Material();
darkRedMaterial.Ambient = Color.DarkRed;
darkRedMaterial.Diffuse = Color.DarkRed;
Material redMaterial = new Material();
redMaterial.Ambient = Color.Red;
redMaterial.Diffuse = Color.Red;
Material highlightedRedMaterial = new Material();
highlightedRedMaterial.Ambient = Color.LightSalmon;
highlightedRedMaterial.Diffuse = Color.LightSalmon;
Material squareBlackMaterial = new Material();
Color squareBlack = Color.FromArgb(0xFF, 0x30, 0x30, 0x30);
squareBlackMaterial.Ambient = squareBlack;
squareBlackMaterial.Diffuse = squareBlack;
Material blackMaterial = new Material();
Color almostBlack = Color.FromArgb(0xFF, 0x10, 0x10, 0x10);
blackMaterial.Ambient = almostBlack;
blackMaterial.Diffuse = almostBlack;
Material highlightedBlackMaterial = new Material();
highlightedBlackMaterial.Ambient = Color.DarkGray;
highlightedBlackMaterial.Diffuse = Color.DarkGray;
blackSquare = new Model(blockMesh, new Material[] { squareBlackMaterial, highlightedBlackMaterial });
redSquare = new Model(blockMesh, new Material[] { darkRedMaterial, highlightedRedMaterial });
blackSquare.PositionOffset = new Vector3(0.0f, -0.25f, 0.0f);
redSquare.PositionOffset = new Vector3(0.0f, -0.25f, 0.0f);
// Create meshes for the pieces.
Mesh pieceMesh = Mesh.Cylinder(device, 0.4f, 0.4f, 0.2f, 32, 1).Clone(MeshFlags.Managed, VertexFormats.PositionNormal | VertexFormats.Specular, device);
Mesh kingPieceMesh = Mesh.Cylinder(device, 0.4f, 0.2f, 0.6f, 32, 1).Clone(MeshFlags.Managed, VertexFormats.PositionNormal | VertexFormats.Specular, device);
redPiece = new Model(pieceMesh, new Material[] { redMaterial, redMaterial });
blackPiece = new Model(pieceMesh, new Material[] { blackMaterial, blackMaterial });
redKingPiece = new Model(kingPieceMesh, new Material[] { redMaterial, redMaterial });
blackKingPiece = new Model(kingPieceMesh, new Material[] { blackMaterial, blackMaterial });
redPiece.PositionOffset = new Vector3(0.0f, 0.1f, 0.0f);
redKingPiece.PositionOffset = new Vector3(0.0f, 0.3f, 0.0f);
blackPiece.PositionOffset = new Vector3(0.0f, 0.1f, 0.0f);
blackKingPiece.PositionOffset = new Vector3(0.0f, 0.3f, 0.0f);
// The Mesh.Cylinder creates a cylider that extends along the Z axis
// but I want it to extend along the Y axis, this is easily fixed by
// rotating it 90 degrees aroung the X axis.
// First create the rotation...
Quaternion rotation = Quaternion.RotationAxis(new Vector3(1.0f, 0.0f, 0.0f), (float)Math.PI / 2.0f);
/// ... then apply it to all piece models
redPiece.Orientation = rotation;
blackPiece.Orientation = rotation;
redKingPiece.Orientation = rotation;
blackKingPiece.Orientation = rotation;
}
public Model GetBoardSquareModel(Square square)
{
if ((square.Column + square.Row) % 2 == 0)
return blackSquare;
else
return redSquare;
}
public Model GetBoardPieceModel(int pieceId)
{
if (pieceId == CheckersLogic.PieceStateBlack)
return blackPiece;
if (pieceId == CheckersLogic.PieceStateBlackKing)
return blackKingPiece;
if (pieceId == CheckersLogic.PieceStateRed)
return redPiece;
if (pieceId == CheckersLogic.PieceStateRedKing)
return redKingPiece;
return null;
}
#endregion
}
}