コード例 #1
0
ファイル: BoardComponent.cs プロジェクト: Bakkes/Karo
        private void DrawPieceAt(CellWrapper cell, int x, int y)
        {
            // Nothing to draw if this cell is empty.
            if (cell.IsEmpty()) { return; }

            // Define the model of the piece we have to use (max/min).
            Model pieceModel = cell.IsMaxPiece() ? _maxModel : _minModel;
            Vector3 color = cell.IsMaxPiece() ? Color.Red.ToVector3() : Color.Green.ToVector3();
            ICamera camera = (ICamera)Game.Services.GetService(typeof(ICamera));
            Matrix[] transforms = new Matrix[_tileModel.Bones.Count];
            _tileModel.CopyAbsoluteBoneTransformsTo(transforms);
            bool marked = false;
            if (KaroGameManager.CurrentMove != null)
            {
                marked = KaroGameManager.CurrentMove.GetFromCell() ==
                    cell.GetRelativePosition();
            }

            // Flip the piece if neccesary
            Matrix world = Matrix.CreateRotationX(MathHelper.ToRadians(-270));
            if (cell.IsFlipped())
            {
                world *= Matrix.CreateRotationX(MathHelper.ToRadians(180));
            }

            BoundingBox pieceBox = Utilities.CreateBoundingBox(pieceModel, world);
            BoundingBox tileBox = Utilities.CreateBoundingBox(pieceModel, world);
            float extraHeight = pieceBox.Max.Y / 2 + tileBox.Max.Y;

            // Draw the piece on the cell.
            foreach (ModelMesh mesh in pieceModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.LightingEnabled = true;
                    effect.DirectionalLight0.Enabled = true;
                    effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0.2f, 0f);
                    effect.DirectionalLight0.Direction = new Vector3(1f, -1f, 1f);
                    effect.DirectionalLight0.SpecularColor = new Vector3(0f, 1f, 0f);
                    effect.DirectionalLight1.Enabled = true;
                    effect.DirectionalLight1.DiffuseColor = new Vector3(0.7f, 0.7f, 0.7f);
                    effect.DirectionalLight1.Direction = new Vector3(1f, -1f, 1f);
                    effect.DirectionalLight1.SpecularColor = new Vector3(0f, 1f, 0f);
                    effect.World = world * Matrix.CreateTranslation(new Vector3(x * (SIZE + GAP), extraHeight, y * (SIZE + GAP)));
                    effect.View = camera.View;
                    effect.Projection = camera.Projection;
                    if (marked)
                    {
                        effect.DiffuseColor = new Vector3(1f, 1f, 0f);
                    }
                    else
                    {
                        effect.DiffuseColor = color;
                    }
                }
                mesh.Draw();
            }
        }