using System; using System.Collections.Generic; using System.Text; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using System.Drawing; namespace SemA { class BlockBase { ///File: BlockBase.cs ///Author: DasMoony ///Description: This class delivers the basic block to build a map. /// Also it can save various informations about the block like color, /// position, etc. ///History: 10-23-2007 Created by DasMoony /// 10-23-2007 Edited by DasMoony -added color-methods /// 10-30-2007 Edited by DasMoony -made UpdateColor() public /// 11-07-2007 Edited by DasMoony -fixed a logical bug /// 11-08-2007 Edited by DasMoony -removed rotate-method /// //Constant which sets the size of the blocks const int BLOCKSIZE = 10; //The needed buffers for the block private VertexBuffer buff_myVBufferA; private VertexBuffer buff_myVBufferB; //The Color of the Block private Color col_myColor; /// /// constructor /// /// The target device public BlockBase(Device device) { //Initializing of the Buffers this.col_myColor =Color.White; this.buff_myVBufferA = null; this.buff_myVBufferB = null; this.buff_myVBufferA = new VertexBuffer( typeof(CustomVertex.PositionColored),8,device,Usage.None, CustomVertex.PositionColored.Format,Pool.Managed); this.buff_myVBufferB = new VertexBuffer( typeof(CustomVertex.PositionColored), 8, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed); Initialize(); } /// /// Initializing of the block /// private void Initialize() { CustomVertex.PositionColored[] oVerts = null; oVerts = (CustomVertex.PositionColored[])this.buff_myVBufferA.Lock(0, LockFlags.None); oVerts[0].X = 0; oVerts[0].Y = 0; oVerts[0].Z = 0; oVerts[0].Color = col_myColor.ToArgb(); oVerts[1].X = BLOCKSIZE; oVerts[1].Y = 0; oVerts[1].Z = BLOCKSIZE; oVerts[1].Color = col_myColor.ToArgb(); oVerts[2].X = BLOCKSIZE; oVerts[2].Y = 0; oVerts[2].Z = 0; oVerts[2].Color = col_myColor.ToArgb(); oVerts[3].X = BLOCKSIZE; oVerts[3].Y = -BLOCKSIZE; oVerts[3].Z = 0; oVerts[3].Color = col_myColor.ToArgb(); oVerts[4].X = 0; oVerts[4].Y = -BLOCKSIZE; oVerts[4].Z = 0; oVerts[4].Color = col_myColor.ToArgb(); oVerts[5].X = 0; oVerts[5].Y = -BLOCKSIZE; oVerts[5].Z = BLOCKSIZE; oVerts[5].Color = col_myColor.ToArgb(); oVerts[6].X = 0; oVerts[6].Y = 0; oVerts[6].Z = BLOCKSIZE; oVerts[6].Color = col_myColor.ToArgb(); oVerts[7].X = BLOCKSIZE; oVerts[7].Y = 0; oVerts[7].Z = BLOCKSIZE; oVerts[7].Color = col_myColor.ToArgb(); this.buff_myVBufferA.Unlock(); oVerts = (CustomVertex.PositionColored[])this.buff_myVBufferB.Lock(0, LockFlags.None); oVerts[0].X = BLOCKSIZE; oVerts[0].Y = -BLOCKSIZE; oVerts[0].Z = BLOCKSIZE; oVerts[0].Color = col_myColor.ToArgb(); oVerts[1].X = 0; oVerts[1].Y = -BLOCKSIZE; oVerts[1].Z = 0; oVerts[1].Color = col_myColor.ToArgb(); oVerts[2].X = 0; oVerts[2].Y = -BLOCKSIZE; oVerts[2].Z = BLOCKSIZE; oVerts[2].Color = col_myColor.ToArgb(); oVerts[3].X = 0; oVerts[3].Y = 0; oVerts[3].Z = BLOCKSIZE; oVerts[3].Color = col_myColor.ToArgb(); oVerts[4].X = BLOCKSIZE; oVerts[4].Y = 0; oVerts[4].Z = BLOCKSIZE; oVerts[4].Color = col_myColor.ToArgb(); oVerts[5].X = BLOCKSIZE; oVerts[5].Y = 0; oVerts[5].Z = 0; oVerts[5].Color = col_myColor.ToArgb(); oVerts[6].X = BLOCKSIZE; oVerts[6].Y = -BLOCKSIZE; oVerts[6].Z = 0; oVerts[6].Color = col_myColor.ToArgb(); oVerts[7].X = 0; oVerts[7].Y = -BLOCKSIZE; oVerts[7].Z = 0; oVerts[7].Color = col_myColor.ToArgb(); this.buff_myVBufferB.Unlock(); } /// /// Gets the first buffer A /// public VertexBuffer VertBufferA { get { return this.buff_myVBufferA; } } /// /// Gets the second buffer B /// public VertexBuffer VertBufferB { get { return this.buff_myVBufferB; } } /// /// Translates the block /// /// delta x (in blocks) /// delta y (in blocks) /// delta z (in blocks) public void Translate(float x, float y, float z) { CustomVertex.PositionColored[] oVerts = null; oVerts = (CustomVertex.PositionColored[])this.buff_myVBufferA.Lock(0, LockFlags.None); for (int i = 0; i < oVerts.Length; i++) { oVerts[i].X += x * BLOCKSIZE; oVerts[i].Y += y * BLOCKSIZE; oVerts[i].Z += z * BLOCKSIZE; } this.buff_myVBufferA.Unlock(); oVerts = (CustomVertex.PositionColored[])this.buff_myVBufferB.Lock(0, LockFlags.None); for (int i = 0; i < oVerts.Length; i++) { oVerts[i].X += x * BLOCKSIZE; oVerts[i].Y += y * BLOCKSIZE; oVerts[i].Z += z * BLOCKSIZE; } this.buff_myVBufferB.Unlock(); } /// /// Gets or sets the color of a Block /// public Color BlockColor { get { return this.col_myColor; } set { this.col_myColor = value; } } /// /// Updates the colors of a block /// public void UpdateColor() { CustomVertex.PositionColored[] oVerts = null; oVerts = (CustomVertex.PositionColored[])this.buff_myVBufferA.Lock(0, LockFlags.None); for (int i = 0; i < oVerts.Length; i++) { oVerts[i].Color = this.col_myColor.ToArgb(); } this.buff_myVBufferA.Unlock(); oVerts = (CustomVertex.PositionColored[])this.buff_myVBufferB.Lock(0, LockFlags.None); for (int i = 0; i < oVerts.Length; i++) { oVerts[i].Color = this.col_myColor.ToArgb(); } this.buff_myVBufferB.Unlock(); } } }