Advertisement
Guest User

Untitled

a guest
Oct 18th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using StarsGame.Universe_Objects.Planets;
  8.  
  9. namespace StarsGame
  10. {
  11.     public class PlanetBlockGeometry
  12.     {
  13.         private static readonly int MAX_TEXTURE_ATLAS_X = 25;
  14.         private static readonly int TEXTURE_WIDTH = 32;
  15.         private static readonly float TEX_COORD_PERCENT = 0.04f;
  16.  
  17.         #region geometric vars
  18.  
  19.         private Coordinate position;
  20.         public Coordinate Position
  21.         {
  22.           get { return position; }
  23.         }
  24.  
  25.         private Vector3 center;
  26.  
  27.         List<Quad> quads;
  28.         public List<Quad> Quads
  29.         {
  30.             get { return quads; }
  31.         }
  32.  
  33.         private Blocks.Block block;
  34.         public Blocks.Block Block
  35.         {
  36.             get { return block; }
  37.         }
  38.  
  39.         private BoundingSphere boundingSphere;
  40.         public BoundingSphere BoundingSphere
  41.         {
  42.             get { return boundingSphere; }
  43.         }
  44.  
  45.  
  46.         #region specific face quads
  47.        
  48.         public Quad GetSpecificFace(PlanetGeometry.FACING_DIRECTION faceName)
  49.         {
  50.             foreach (Quad q in quads)
  51.             {
  52.                 if (q.FaceName == faceName)
  53.                     return q;
  54.             }
  55.             throw new Exception("This quad does not contain a face of that name");
  56.         }
  57.  
  58.         #endregion
  59.  
  60.         #endregion
  61.  
  62.         public PlanetBlockGeometry(Coordinate position, List<Quad> quads, Blocks.Block type, Vector3 center)
  63.         {
  64.             this.position = position;
  65.             this.quads = quads;
  66.             this.block = type;
  67.             this.center = center;
  68.             if (quads != null)
  69.                 foreach (Quad c in this.quads)
  70.                 {
  71.                     SetTexCoords(c);
  72.                     c.CreateColorCorners(Block.baseColor);
  73.                     c.CorrectTriangles(GetCenter());
  74.                 }
  75.             boundingSphere = new BoundingSphere(GetCenter(), GetSize());
  76.         }
  77.  
  78.         private void SetTexCoords(Quad quad)
  79.         {
  80.             int xPos = block.ID % MAX_TEXTURE_ATLAS_X;
  81.             int yPos = block.ID / MAX_TEXTURE_ATLAS_X;
  82.             quad.Corners[3].TextureCoordinate = new Vector2(xPos * TEX_COORD_PERCENT, yPos * TEX_COORD_PERCENT);
  83.             quad.Corners[0].TextureCoordinate = new Vector2((xPos + 1) * TEX_COORD_PERCENT, yPos * TEX_COORD_PERCENT);
  84.             quad.Corners[2].TextureCoordinate = new Vector2(xPos * TEX_COORD_PERCENT, (yPos + 1) * TEX_COORD_PERCENT);
  85.             quad.Corners[1].TextureCoordinate = new Vector2((xPos + 1) * TEX_COORD_PERCENT, (yPos + 1) * TEX_COORD_PERCENT);
  86.         }
  87.  
  88.         public void AddQuad(Quad quad)
  89.         {
  90.             SetTexCoords(quad);
  91.             quads.Add(quad);
  92.         }
  93.  
  94.         public void RemoveQuad(Quad quad)
  95.         {
  96.             quads.Remove(quad);
  97.         }
  98.  
  99.         public Vector3 GetCenter()
  100.         {
  101.             return center;
  102.         }
  103.  
  104.         private float GetSize()
  105.         {
  106.             if (quads.Count > 0)
  107.                 return (Vector3.Distance(quads[0].Corners[0].Position, quads[0].Corners[2].Position));
  108.             else
  109.                 return 1;
  110.         }
  111.  
  112.         public Vector3[] GetAllCornerPositions()
  113.         {
  114.             List<Vector3> cornerPositions = new List<Vector3>(quads.Count * 4);
  115.             for (int q = 0; q < quads.Count; q++)
  116.             {
  117.                 cornerPositions.AddRange(quads[q].GetCornerPositions());
  118.             }
  119.             return cornerPositions.ToArray();
  120.         }
  121.  
  122.         public VertexPositionNormalTexture[] GetAllVertices()
  123.         {
  124.             List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
  125.             foreach (Quad q in quads)
  126.             {
  127.                 vertices.AddRange(q.Corners);
  128.             }
  129.             return vertices.ToArray();
  130.         }
  131.  
  132.         public int[] GetAllIndices()
  133.         {
  134.             List<int> indices = new List<int>();
  135.             for (int q = 0; q < quads.Count; q++)
  136.             {
  137.                 foreach (Triangle tri in quads[q].Triangles)
  138.                 {
  139.                     indices.Add(tri.Indices[0] + (q * 4));
  140.                     indices.Add(tri.Indices[1] + (q * 4));
  141.                     indices.Add(tri.Indices[2] + (q * 4));
  142.                 }
  143.             }
  144.             return indices.ToArray();
  145.         }
  146.  
  147.         public void TakeDamage(int damage)
  148.         {
  149.             block.health -= damage;
  150.         }
  151.  
  152.         public bool IsAlive()
  153.         {
  154.             return block.Alive();
  155.         }
  156.     }
  157. }
  158.  
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement