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(); } }
/// <summary> /// Draws a tile with the specified relative position. /// </summary> private void DrawCellAt(CellWrapper cell, int x, int y, bool marked = false) { ICamera camera = (ICamera)Game.Services.GetService(typeof(ICamera)); Matrix world = Matrix.CreateRotationX(MathHelper.ToRadians(-90)); bool clickableTile = IsMarkedCell(new Vector2((float)x, (float)y)); foreach (ModelMesh mesh in _tileModel.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), 0, y * (SIZE + GAP))); effect.View = camera.View; effect.Projection = camera.Projection; if (marked) { effect.Alpha = 0.2f; } else { effect.Alpha = 1f; } if (_lastMoveHighlight[0] != null && _lastMoveHighlight[0] == cell.GetRelativePosition()) { effect.DiffuseColor = new Vector3(1f, 1f, 0f); } else if (_lastMoveHighlight[1] != null && _lastMoveHighlight[1] == cell.GetRelativePosition()) { effect.DiffuseColor = new Vector3(1f, 1f, 0f); } else if (clickableTile || marked) { effect.DiffuseColor = new Vector3(1f, 1f, 1f); } else { effect.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f); } } mesh.Draw(); } if (!cell.IsEmpty()) { DrawPieceAt(cell, x, y); } }