Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using StarsGame.Universe_Objects.Planets;
- namespace StarsGame
- {
- public class PlanetBlockGeometry
- {
- private static readonly int MAX_TEXTURE_ATLAS_X = 25;
- private static readonly int TEXTURE_WIDTH = 32;
- private static readonly float TEX_COORD_PERCENT = 0.04f;
- #region geometric vars
- private Coordinate position;
- public Coordinate Position
- {
- get { return position; }
- }
- private Vector3 center;
- List<Quad> quads;
- public List<Quad> Quads
- {
- get { return quads; }
- }
- private Blocks.Block block;
- public Blocks.Block Block
- {
- get { return block; }
- }
- private BoundingSphere boundingSphere;
- public BoundingSphere BoundingSphere
- {
- get { return boundingSphere; }
- }
- #region specific face quads
- public Quad GetSpecificFace(PlanetGeometry.FACING_DIRECTION faceName)
- {
- foreach (Quad q in quads)
- {
- if (q.FaceName == faceName)
- return q;
- }
- throw new Exception("This quad does not contain a face of that name");
- }
- #endregion
- #endregion
- public PlanetBlockGeometry(Coordinate position, List<Quad> quads, Blocks.Block type, Vector3 center)
- {
- this.position = position;
- this.quads = quads;
- this.block = type;
- this.center = center;
- if (quads != null)
- foreach (Quad c in this.quads)
- {
- SetTexCoords(c);
- c.CreateColorCorners(Block.baseColor);
- c.CorrectTriangles(GetCenter());
- }
- boundingSphere = new BoundingSphere(GetCenter(), GetSize());
- }
- private void SetTexCoords(Quad quad)
- {
- int xPos = block.ID % MAX_TEXTURE_ATLAS_X;
- int yPos = block.ID / MAX_TEXTURE_ATLAS_X;
- quad.Corners[3].TextureCoordinate = new Vector2(xPos * TEX_COORD_PERCENT, yPos * TEX_COORD_PERCENT);
- quad.Corners[0].TextureCoordinate = new Vector2((xPos + 1) * TEX_COORD_PERCENT, yPos * TEX_COORD_PERCENT);
- quad.Corners[2].TextureCoordinate = new Vector2(xPos * TEX_COORD_PERCENT, (yPos + 1) * TEX_COORD_PERCENT);
- quad.Corners[1].TextureCoordinate = new Vector2((xPos + 1) * TEX_COORD_PERCENT, (yPos + 1) * TEX_COORD_PERCENT);
- }
- public void AddQuad(Quad quad)
- {
- SetTexCoords(quad);
- quads.Add(quad);
- }
- public void RemoveQuad(Quad quad)
- {
- quads.Remove(quad);
- }
- public Vector3 GetCenter()
- {
- return center;
- }
- private float GetSize()
- {
- if (quads.Count > 0)
- return (Vector3.Distance(quads[0].Corners[0].Position, quads[0].Corners[2].Position));
- else
- return 1;
- }
- public Vector3[] GetAllCornerPositions()
- {
- List<Vector3> cornerPositions = new List<Vector3>(quads.Count * 4);
- for (int q = 0; q < quads.Count; q++)
- {
- cornerPositions.AddRange(quads[q].GetCornerPositions());
- }
- return cornerPositions.ToArray();
- }
- public VertexPositionNormalTexture[] GetAllVertices()
- {
- List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
- foreach (Quad q in quads)
- {
- vertices.AddRange(q.Corners);
- }
- return vertices.ToArray();
- }
- public int[] GetAllIndices()
- {
- List<int> indices = new List<int>();
- for (int q = 0; q < quads.Count; q++)
- {
- foreach (Triangle tri in quads[q].Triangles)
- {
- indices.Add(tri.Indices[0] + (q * 4));
- indices.Add(tri.Indices[1] + (q * 4));
- indices.Add(tri.Indices[2] + (q * 4));
- }
- }
- return indices.ToArray();
- }
- public void TakeDamage(int damage)
- {
- block.health -= damage;
- }
- public bool IsAlive()
- {
- return block.Alive();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement