Advertisement
Guest User

Untitled

a guest
Dec 30th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using Microsoft.Xna.Framework;
  5.  
  6. namespace BackBlast
  7. {
  8.     public class Plane : GameObject
  9.     {
  10.         List<GameObject> Contents = new List<GameObject>();
  11.         List<Matrix> Positions = new List<Matrix>();
  12.         List<Matrix> Angles = new List<Matrix>();
  13.  
  14.         Matrix Rotation;
  15.  
  16.         float AngleX, AngleY;
  17.  
  18.         public Plane(float x, float y) : base(x, y, "")
  19.         {
  20.             Rotation = Matrix.CreateTranslation(0f, 0f, 0f);
  21.             SetLayer(Layer.Objects0);
  22.         }
  23.  
  24.         public void SetAngle(float angleX, float angleY, float angleZ)
  25.         {
  26.             AngleX = Mathx.FixAngle(angleX);
  27.             AngleY = Mathx.FixAngle(angleY);
  28.             Angle = Mathx.FixAngle(angleZ);
  29.             UpdateRotation();
  30.         }
  31.  
  32.         public void SetAngleX(float angleX)
  33.         {
  34.             AngleX = Mathx.FixAngle(angleX);
  35.             UpdateRotation();
  36.         }
  37.  
  38.         public void SetAngleY(float angleY)
  39.         {
  40.             AngleY = Mathx.FixAngle(angleY);
  41.             UpdateRotation();
  42.         }
  43.  
  44.         new public void SetAngle(float angleZ)
  45.         {
  46.             Angle = Mathx.FixAngle(angleZ);
  47.             UpdateRotation();
  48.         }
  49.  
  50.         public void SetAngleZ(float angleZ)
  51.         {
  52.             Angle = Mathx.FixAngle(angleZ);
  53.             UpdateRotation();
  54.         }
  55.  
  56.         public void RotateX(float degrees)
  57.         {
  58.             AngleX = Mathx.FixAngle(AngleX + degrees);
  59.             UpdateRotation();
  60.         }
  61.  
  62.         public void RotateY(float degrees)
  63.         {
  64.             AngleY = Mathx.FixAngle(AngleY + degrees);
  65.             UpdateRotation();
  66.         }
  67.  
  68.         public void RotateZ(float degrees)
  69.         {
  70.             Angle = Mathx.FixAngle(Angle + degrees);
  71.             UpdateRotation();
  72.         }
  73.  
  74.         public void Rotate(float x, float y, float z)
  75.         {
  76.             AngleX = Mathx.FixAngle(AngleX + x);
  77.             AngleY = Mathx.FixAngle(AngleY + y);
  78.             Angle = Mathx.FixAngle(Angle + z);
  79.             UpdateRotation();
  80.         }
  81.  
  82.         private void UpdateRotation()
  83.         {
  84.             Rotation = Matrix.CreateRotationZ(Angle * 0.0174533f) * Matrix.CreateRotationY(AngleY * 0.0174533f) * Matrix.CreateRotationX(AngleX * 0.0174533f);
  85.         }
  86.  
  87.         public void Add(GameObject obj)
  88.         {
  89.             Contents.Add(obj);
  90.             Positions.Add(Matrix.CreateTranslation(obj.X - X, obj.Y - Y, 0f));
  91.             float angle = obj.GetAngle();
  92.             Angles.Add(Matrix.CreateTranslation(Mathx.DCos(angle), Mathx.DSin(angle), 0f));
  93.         }
  94.  
  95.         public void Clear()
  96.         {
  97.             Contents.Clear();
  98.             Positions.Clear();
  99.             Angles.Clear();
  100.         }
  101.  
  102.         public void Remove(GameObject obj)
  103.         {
  104.             int index = Contents.IndexOf(obj);
  105.             Contents.RemoveAt(index);
  106.             Positions.RemoveAt(index);
  107.             Angles.RemoveAt(index);
  108.         }
  109.  
  110.         public void SetPosition(GameObject obj, float x, float y)
  111.         {
  112.             int index = Contents.IndexOf(obj);
  113.             Positions[index] = Matrix.CreateTranslation(obj.X - X, obj.Y - Y, 0f);
  114.         }
  115.  
  116.         public void SetAngle(GameObject obj, float angle)
  117.         {
  118.             int index = Contents.IndexOf(obj);
  119.             Angles[index] = Matrix.CreateTranslation(Mathx.DCos(angle), Mathx.DSin(angle), 0f);
  120.         }
  121.  
  122.         public override bool Update()
  123.         {
  124.             base.Update();
  125.  
  126.             Matrix transformation = Matrix.CreateScale(ScaleX, ScaleY, 1f) * Rotation * Matrix.CreateTranslation(X, Y, 0f);
  127.  
  128.             for (int i = 0; i < Contents.Count; i++)
  129.             {
  130.                 GameObject obj = Contents[i];
  131.                 if (obj.Exists())
  132.                 {
  133.                     Matrix transformedTranslation = Positions[i] * transformation;
  134.                     Vector3 newTranslation = transformedTranslation.Translation;
  135.                     obj.X = newTranslation.X;
  136.                     obj.Y = newTranslation.Y;
  137.                     Matrix transformedRotation = Angles[i] * Rotation;
  138.                     Vector3 newRotation = transformedRotation.Translation;
  139.                     obj.SetAngle(Mathx.DAtan2(newRotation.Y, newRotation.X));
  140.                 }
  141.                 else
  142.                 {
  143.                     Contents.RemoveAt(i);
  144.                     Positions.RemoveAt(i);
  145.                     Angles.RemoveAt(i);
  146.                     i--;
  147.                 }
  148.             }
  149.  
  150.             return false;
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement