Radar.cs
public text v1 · immutableusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using FlatRedBall;
using FlatRedBall.Content.AnimationChain;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Input;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Content;
using FlatRedBall.Instructions;
using System.Threading;
using System.Reflection;
using FlatRedBall.Gui;
using FlatRedBall.Graphics;
using FlatRedBall.Graphics.Lighting;
using FlatRedBall.Content.Particle;
using FlatRedBall.Graphics.Particle;
//using System.Drawing;
using FlatRedBall.ManagedSpriteGroups;
using FlatRedBall.Math;
using FlatRedBall.Content.Polygon;
using FlatRedBallXna1.Screens;
using FlatRedBall.Graphics.Model;
using FlatRedBallXna1;
using XNATerrainEditor;
namespace GCGE
{
public class Radar: IDisposable
{
public Players Owner;
public Sprite background;
public List<Sprite> withinRange;
public List<Sprite> activeUnits;
public Texture2D tBuilding, tShip;
public float maxDistance, minDistance;
//public Vector3 sectorStart;
private double timeToUpdate;
private float radarScale;
public float RadarScale
{
get { return radarScale; }
set
{
switch (this.Owner.viewport)
{
case FlatRedBall.Camera.SplitScreenViewport.FullScreen:
radarScale = value;
break;
case FlatRedBall.Camera.SplitScreenViewport.TopHalf:
case FlatRedBall.Camera.SplitScreenViewport.BottomHalf:
case FlatRedBall.Camera.SplitScreenViewport.LeftHalf:
case FlatRedBall.Camera.SplitScreenViewport.RightHalf:
radarScale = value / 1.5f;
break;
case FlatRedBall.Camera.SplitScreenViewport.TopLeft:
case FlatRedBall.Camera.SplitScreenViewport.TopRight:
case FlatRedBall.Camera.SplitScreenViewport.BottomLeft:
case FlatRedBall.Camera.SplitScreenViewport.BottomRight:
radarScale = value / 1.75f;
break;
}
}
}
public Radar(Players own, float MAX)
{
this.Owner = own;
this.maxDistance = MAX;
this.minDistance = 100;
this.RadarScale = 9;
//this.sectorStart = new Vector3(0, 0, 0);
this.timeToUpdate = 31; //prime number
withinRange = new List<Sprite>();
activeUnits = new List<Sprite>();
background = SpriteManager.AddSprite(@"Content\Textures\radar.png", "Global", own.HUD.Layer);
/*for (int i = 0; i < 25; i++)
{
Sprite s = new Sprite();
s = SpriteManager.AddSprite(@"Content\Textures\blip", "Global", own.HUD.Layer);
s.ScaleX = 1f;
s.ScaleY = 1f;
s.Position = background.Position;
s.ColorOperation = ColorOperation.Add;
s.Red = Color.Red.ToVector3().X;
s.Green = Color.Red.ToVector3().Y;
s.Blue = Color.Red.ToVector3().Z;
withinRange.Add(s);
}*/
}
public void FixRadarPosScale(float HUDScale, float RADARScale, float MAX)
{
this.maxDistance = MAX;
background.ScaleX = 2.5f *1 / HUDScale;
background.ScaleY = 2.5f *1 / HUDScale;
background.Z = Owner.HUD.Z - 60;
background.X = Owner.HUD.RelativeXEdgeAt(background.Z) * .65f;// -background.ScaleX * 2;
background.Y = -Owner.HUD.RelativeYEdgeAt(background.Z) * .60f;// +background.ScaleY * 2;
//clear the old and initiate a new one
/*foreach (Sprite c in withinRange)
{
c.RemoveSelfFromListsBelongingTo();
SpriteManager.RemoveSprite(c);
}
withinRange = new List<Sprite>();*/
/*for (int i = 0; i < 25; i++)
{
Sprite s = new Sprite();
s = SpriteManager.AddSprite(@"Content\Textures\blip", "Global", this.Owner.HUD.Layer);
s.ScaleX = background.ScaleX / 10f;
s.ScaleY = s.ScaleX;
s.Position = background.Position;
s.ColorOperation = ColorOperation.Add;
s.Red = this.Owner.playerColor.ToVector3().X;
s.Green = this.Owner.playerColor.ToVector3().Y;
s.Blue = this.Owner.playerColor.ToVector3().Z;
withinRange.Add(s);
}*/
this.RadarScale = 2.5f * RADARScale;//9;
}
public void AddToRadar(BaseEntity ship, bool active)
{
Sprite s;
if (active)
{
s = SpriteManager.AddSprite(@"Content\Textures\blipActive.png", "Global", this.Owner.HUD.Layer);
if (GlobalTextures.tActive == null)
GlobalTextures.tActive = s.Texture;
}
else
{
switch (ship.EntityType)
{
case entitytype.Buildings:
s = SpriteManager.AddSprite(@"Content\Textures\blipBuilding.png", "Global", this.Owner.HUD.Layer);
if (GlobalTextures.tBuilding == null)
GlobalTextures.tBuilding = s.Texture;
if (tBuilding == null)
tBuilding = s.Texture;
break;
case entitytype.Ships1:
s = SpriteManager.AddSprite(@"Content\Textures\blip.png", "Global", this.Owner.HUD.Layer);
if (GlobalTextures.tShip == null)
GlobalTextures.tShip = s.Texture;
if (tShip == null)
tShip = s.Texture;
break;
default:
s = SpriteManager.AddSprite(@"Content\Textures\blip.png", "Global", this.Owner.HUD.Layer);
break;
}
}
//SpriteManager.ConvertToManuallyUpdated(s);
s.ScaleX = background.ScaleX / 10f;
s.ScaleY = s.ScaleX;
s.Position = background.Position;
s.ColorOperation = ColorOperation.Add;
s.Red = ship.Owner.playerColor.ToVector3().X;
s.Green = ship.Owner.playerColor.ToVector3().Y;
s.Blue = ship.Owner.playerColor.ToVector3().Z;
s.Name = ship.Name;
s.Visible = false;
if (active)
{
activeUnits.Add(s);
} else
withinRange.Add(s);
}
public void RemoveFromRadar(ControlledEntities ship)
{
foreach (Sprite s in withinRange)
{
if (s.Name == ship.Name)
{
withinRange.Remove(s);
SpriteManager.RemoveSprite(s);
}
}
}
/*public void Update(object data)
{
this.Update((ControlledEntities)data);
}*/
public void Update(ControlledEntities entity)//, ref Levels1 curLevel)//, ref GameTime gameTime)
{
this.background.RotationZ = (float)Math.PI * 2 - entity.RotationY;
Vector3 diffC = Vector3.Subtract(this.Owner.viewCamera.Position, entity.model.Position);
diffC.Normalize();
double shipAngC = Math.Atan2(diffC.Z, diffC.X);
if (shipAngC >= Math.PI * 2) shipAngC -= Math.PI * 2;
double shipDistC = Vector3.Distance(this.Owner.viewCamera.Position, entity.model.Position);
//entity.primaryTarget.target = null;
double closestDistance = 999999;
//this.timeToUpdate -= TimeManager.SecondDifference * 1000; //(float)gameTime.ElapsedGameTime.TotalMilliseconds;
//if (this.timeToUpdate < 0)
{
this.Owner.inviewThisFrame.Clear();
//this.timeToUpdate = 31;
int i = 0;
int t = 0;
int a = 0;
for (int x = entity.sector.X - entity.sector.RangeX; x < entity.sector.X + entity.sector.RangeX; x++)
{
for (int z = entity.sector.Z - entity.sector.RangeZ; z < entity.sector.Z + entity.sector.RangeZ; z++)
{
for (int y = 0; y < entity.sector.Y + entity.sector.RangeY; y++)
{
if (x > -1 && x < this.Owner.curLevel.sectorMap.SIZE.X
&& y > -1 && y < this.Owner.curLevel.sectorMap.SIZE.Y
&& z > -1 && z < this.Owner.curLevel.sectorMap.SIZE.Z)
{
//check against each entity in list
foreach (Buildings s in this.Owner.curLevel.sectorMap.sector
[x, y, z].buildings)
{
Vector3 diff = Vector3.Subtract(entity.model.Position, s.model.Position);
double dist2 = (Math.Sqrt((diff.X * diff.X) + (diff.Z * diff.Z))); //maximum range
double dist = Vector3.Distance(entity.model.Position, s.model.Position) - s.boundingSphere.Radius;
diff.Normalize();
if (this.Owner.viewCamera.BoundingFrustum.Contains(s.boundingSphere) != ContainmentType.Disjoint)
{
//Vector3 diffS = Vector3.Subtract(this.Owner.viewCamera.Position, s.model.Position);
//diffS.Normalize();
double fAngS = Math.Atan2(diff.Z, diff.X); //(diffS.Z, diffS.X);
if (fAngS >= Math.PI * 2) fAngS -= Math.PI * 2;
//targeting
if (s.Owner.Name != this.Owner.Name)
{
//if (shipDistC < Vector3.Distance(this.Owner.viewCamera.Position, s.model.Position) && (Math.Abs(shipAngC - fAngS) <= MathHelper.ToRadians(5) || Math.Abs(shipAngC - fAngS) >= MathHelper.ToRadians(355)))
if (Math.Abs(shipAngC - fAngS) <= MathHelper.ToRadians(5) || Math.Abs(shipAngC - fAngS) >= MathHelper.ToRadians(355))
{
Vector3 targetPos = Vector3.Normalize(s.model.Position - entity.model.Position);
if (Math.Abs(this.Owner.active.model.RotationMatrix.Backward.Y - targetPos.Y) <= .22f) //.11 aprox. 10d
{
if (this.Owner.active.primaryTarget.target == null)
{
entity.primaryTarget.target = s;
entity.primaryTarget.distance = dist;
}
else
{
if (dist < this.Owner.active.primaryTarget.distance)
{
this.Owner.active.primaryTarget.target = s;
this.Owner.active.primaryTarget.distance = dist;
}
}
if (this.Owner.active.findtarget)
{
//if (t == this.Owner.active.selectedTargetidx)
if (this.Owner.active.selectedTarget.target == null)
{
this.Owner.active.selectedTarget.target = s;
if (this.Owner.active.selectedTarget.recticle == null)
{
this.Owner.active.selectedTarget.AddToTargetRecticle(ref this.Owner.viewCamera, Color.CornflowerBlue, 2.5f, true);
}
this.Owner.active.selectedTarget.recticle.Visible = true;
this.Owner.active.selectedTarget.distance = dist;
}
else
{
//needs to be farther than selected, but be
//the closest other than that
if (s != this.Owner.active.selectedTarget.target &&
dist < closestDistance)
{
//this should be selected target
this.Owner.active.selectedTarget.target = s;
if (this.Owner.active.selectedTarget.recticle == null)
{
this.Owner.active.selectedTarget.AddToTargetRecticle(ref this.Owner.viewCamera, Color.CornflowerBlue, 2.5f, true);
}
this.Owner.active.selectedTarget.recticle.Visible = true;
this.Owner.active.selectedTarget.distance = dist;
closestDistance = dist;
}
}
}
}
}
}
//display model?
//if (Vector3.Distance(this.Owner.viewCamera.Position, s.model.Position) < this.Owner.viewCamera.FarClipPlane / 2)
if (dist < this.maxDistance * .9)
{
this.Owner.inviewThisFrame.Add(s.Name);
if (!this.Owner.inviewModels.Contains(s.Name))
{
ModelManager.AddToLayer(s.model, this.Owner.viewCamera.Layer);
this.Owner.inviewModels.Add(s.Name);
}
}
//add targetRecticle
if (t >= this.Owner.targetRecticles.Count)
{
this.Owner.AddToTargetRecticle(s);
}
if (t < this.Owner.targetRecticles.Count)
{
//fix it
this.Owner.targetRecticles[t].ScaleX =
this.Owner.targetRecticles[t].ScaleY = s.boundingSphere.Radius * 2f;
this.Owner.targetRecticles[t].Position = s.model.Position;
this.Owner.targetRecticles[t].Name = s.Name;
this.Owner.targetRecticles[t].Red = s.Owner.playerColor.ToVector3().X;
this.Owner.targetRecticles[t].Green = s.Owner.playerColor.ToVector3().Y;
this.Owner.targetRecticles[t].Blue = s.Owner.playerColor.ToVector3().Z;
this.Owner.targetRecticles[t].Visible = true;
t++;
}
}
if (s.Owner.playerType == PlayerType.AI
|| s.Owner.active.Name != s.Name)
{
float fAng = (MathHelper.ToRadians(360) - (float)Math.Atan2(diff.Z, diff.X));
fAng += MathHelper.ToRadians(360) - this.Owner.active.RotationY;//shield.RotationY;
if (fAng >= Math.PI * 2) fAng -= (float)Math.PI * 2;
if (i >= withinRange.Count)
{
AddToRadar(s, false);
}
if (i < withinRange.Count)
{
dist2 /= this.maxDistance;
if (dist2 < 1.0f)
{
withinRange[i].X = background.X + (float)dist2 * this.RadarScale * (float)Math.Cos(fAng);
withinRange[i].Y = background.Y + (float)dist2 * this.RadarScale * (float)Math.Sin(fAng);
withinRange[i].Z = background.Z;
/*}
else
{
withinRange[i].X = background.X + 1 * this.RadarScale * (float)Math.Cos(fAng);
withinRange[i].Y = background.Y + 1 * this.RadarScale * (float)Math.Sin(fAng);
withinRange[i].Z = background.Z;
}*/
if (withinRange[i].Texture != GlobalTextures.tBuilding)
withinRange[i].Texture = GlobalTextures.tBuilding;
withinRange[i].Red = s.Owner.playerColor.ToVector3().X * .5f;
withinRange[i].Green = s.Owner.playerColor.ToVector3().Y * .5f;
withinRange[i].Blue = s.Owner.playerColor.ToVector3().Z * .5f;
withinRange[i].ScaleX = background.ScaleX * .05f * (1 - diff.Y * .5f);//-(diff.Y / 5f);
withinRange[i].ScaleY = withinRange[i].ScaleX;
withinRange[i].Visible = true;
i++;
}
}
}
}
foreach (ControlledEntities s in this.Owner.curLevel.sectorMap.sector
[x, y, z].entities)
{
//display model
bool isVisible = false;
if (this.Owner.viewCamera.BoundingFrustum.Contains(s.boundingSphere) != ContainmentType.Disjoint)
{
isVisible = true;
this.Owner.inviewThisFrame.Add(s.Name);
if (!this.Owner.inviewModels.Contains(s.Name))
{
ModelManager.AddToLayer(s.model, this.Owner.viewCamera.Layer);
this.Owner.inviewModels.Add(s.Name);
}
}
double dist = Vector3.Distance(entity.model.Position, s.model.Position) - s.boundingSphere.Radius;
if (dist < this.maxDistance)
{
Vector3 diff = Vector3.Subtract(entity.model.Position, s.model.Position);
double dist2 = (Math.Sqrt((diff.X * diff.X) + (diff.Z * diff.Z))); //maximum range
diff.Normalize();
//Vector3 diffS = Vector3.Subtract(this.Owner.viewCamera.Position, s.model.Position);
//diffS.Normalize();
double fAngS = Math.Atan2(diff.Z, diff.X); //(diffS.Z, diffS.X);
if (fAngS >= Math.PI * 2) fAngS -= Math.PI * 2;
//if (this.Owner.viewCamera.BoundingFrustum.Contains(s.boundingSphere) != ContainmentType.Disjoint)
if(isVisible)
{
//targeting
if (s.Owner.Name != this.Owner.Name)
{
//if (shipDistC < Vector3.Distance(this.Owner.viewCamera.Position, s.model.Position) && (Math.Abs(shipAngC - fAngS) <= MathHelper.ToRadians(5) || Math.Abs(shipAngC - fAngS) >= MathHelper.ToRadians(355)))
if (Math.Abs(shipAngC - fAngS) <= MathHelper.ToRadians(5) || Math.Abs(shipAngC - fAngS) >= MathHelper.ToRadians(355))
{
Vector3 targetPos = Vector3.Normalize(s.model.Position - entity.model.Position);
if (Math.Abs(this.Owner.active.model.RotationMatrix.Backward.Y - targetPos.Y) <= .22f)
{
if (this.Owner.active.primaryTarget.target == null)
{
entity.primaryTarget.target = s;
entity.primaryTarget.distance = dist;
}
else
{
if (dist < this.Owner.active.primaryTarget.distance)
{
this.Owner.active.primaryTarget.target = s;
this.Owner.active.primaryTarget.distance = dist;
}
}
//selected targeting
if (this.Owner.active.findtarget)
{
//if (t == this.Owner.active.selectedTargetidx)
if (this.Owner.active.selectedTarget.target == null)
{
this.Owner.active.selectedTarget.target = s;
if (this.Owner.active.selectedTarget.recticle == null)
{
this.Owner.active.selectedTarget.AddToTargetRecticle(ref this.Owner.viewCamera, Color.CornflowerBlue, 2.5f, true);
}
this.Owner.active.selectedTarget.recticle.Visible = true;
this.Owner.active.selectedTarget.distance = dist;
}
else
{
//needs to be farther than selected, but be
//the closest other than that
if (s != this.Owner.active.selectedTarget.target &&
dist < closestDistance)
{
//this should be selected target
this.Owner.active.selectedTarget.target = s;
if (this.Owner.active.selectedTarget.recticle == null)
{
this.Owner.active.selectedTarget.AddToTargetRecticle(ref this.Owner.viewCamera, Color.CornflowerBlue, 2.5f, true);
}
this.Owner.active.selectedTarget.recticle.Visible = true;
this.Owner.active.selectedTarget.distance = dist;
closestDistance = dist;
}
}
}
}
}
}
//add targetRecticle
if (s.Name != this.Owner.active.Name)
{
if (t >= this.Owner.targetRecticles.Count)
{
this.Owner.AddToTargetRecticle(s);
}
if (t < this.Owner.targetRecticles.Count)
{
//fix it
this.Owner.targetRecticles[t].ScaleX =
this.Owner.targetRecticles[t].ScaleY = s.boundingSphere.Radius * 2f;
this.Owner.targetRecticles[t].Position = s.model.Position;
this.Owner.targetRecticles[t].Name = s.Name;
this.Owner.targetRecticles[t].Red = s.Owner.playerColor.ToVector3().X;
this.Owner.targetRecticles[t].Green = s.Owner.playerColor.ToVector3().Y;
this.Owner.targetRecticles[t].Blue = s.Owner.playerColor.ToVector3().Z;
this.Owner.targetRecticles[t].Visible = true;
t++;
}
}
}
if (s.Owner.playerType == PlayerType.AI
|| s.Owner.active.Name != s.Name)
{
float fAng = (MathHelper.ToRadians(360) - (float)Math.Atan2(diff.Z, diff.X));
fAng += MathHelper.ToRadians(360) - this.Owner.active.RotationY;//shield.RotationY;
if (fAng >= Math.PI * 2) fAng -= (float)Math.PI * 2;
if (i >= withinRange.Count)
{
AddToRadar(s, false);
}
if (i < withinRange.Count)
{
dist2 /= this.maxDistance;
if (dist2 < 1.0f)
{
withinRange[i].X = background.X + (float)dist2 * this.RadarScale * (float)Math.Cos(fAng);
withinRange[i].Y = background.Y + (float)dist2 * this.RadarScale * (float)Math.Sin(fAng);
withinRange[i].Z = background.Z;
/*}
else
{
withinRange[i].X = background.X + 1 * this.RadarScale * (float)Math.Cos(fAng);
withinRange[i].Y = background.Y + 1 * this.RadarScale * (float)Math.Sin(fAng);
withinRange[i].Z = background.Z;
}*/
if (withinRange[i].Texture != GlobalTextures.tShip)
withinRange[i].Texture = GlobalTextures.tShip;
withinRange[i].Red = s.Owner.playerColor.ToVector3().X;
withinRange[i].Green = s.Owner.playerColor.ToVector3().Y;
withinRange[i].Blue = s.Owner.playerColor.ToVector3().Z;
withinRange[i].ScaleX = background.ScaleX * .05f * (1 - diff.Y * .5f); //- (diff.Y / 5f);
withinRange[i].ScaleY = withinRange[i].ScaleX;
withinRange[i].Visible = true;
i++;
}
}
}
}
}
}
}
}
}
foreach (Players p in PlayerManagement.player)
{
if (p.playerType != PlayerType.AI)
{
//double dist = Vector3.Distance(entity.model.Position, p.active.model.Position) - p.active.boundingSphere.Radius;
Vector3 diff = Vector3.Subtract(entity.model.Position, p.active.model.Position);
double dist2 = (Math.Sqrt((diff.X * diff.X) + (diff.Z * diff.Z))); //maximum range
diff.Normalize();
float fAng = (MathHelper.ToRadians(360) - (float)Math.Atan2(diff.Z, diff.X));
fAng += MathHelper.ToRadians(360) - this.Owner.active.RotationY;//shield.RotationY;
if (fAng >= Math.PI * 2) fAng -= (float)Math.PI * 2;
if (a >= activeUnits.Count)
{
AddToRadar(p.active, true);
}
if (a < activeUnits.Count)
{
dist2 /= this.maxDistance;
if (dist2 < 1.0f)
{
activeUnits[a].X = background.X + (float)dist2 * this.RadarScale * (float)Math.Cos(fAng);
activeUnits[a].Y = background.Y + (float)dist2 * this.RadarScale * (float)Math.Sin(fAng);
activeUnits[a].Z = background.Z;
}
else
{
activeUnits[a].X = background.X + 1 * this.RadarScale * (float)Math.Cos(fAng);
activeUnits[a].Y = background.Y + 1 * this.RadarScale * (float)Math.Sin(fAng);
activeUnits[a].Z = background.Z;
}
activeUnits[a].Red = p.playerColor.ToVector3().X;
activeUnits[a].Green = p.playerColor.ToVector3().Y;
activeUnits[a].Blue = p.playerColor.ToVector3().Z;
activeUnits[a].ScaleX = background.ScaleX * .067f * (1 - diff.Y * .5f);
activeUnits[a].ScaleY = activeUnits[a].ScaleX;
activeUnits[a].RotationZ = p.active.RotationY + background.RotationZ;//(float)Math.PI * 2 - p.active.RotationY;
activeUnits[a].Visible = true;
a++;
}
}
}
for (int v = i; v < withinRange.Count; v++)
{
withinRange[v].Visible = false;
}
for (int v2 = t; v2 < this.Owner.targetRecticles.Count; v2++)
{
this.Owner.targetRecticles[v2].Visible = false;
}
for (int v3 = a; v3 < activeUnits.Count; v3++)
{
activeUnits[v3].Visible = false;
}
//if (this.Owner.active.findtarget && this.Owner.active.selectedTargetidx >= t)
// this.Owner.active.selectedTargetidx = 0; //recycle
}
}
#region IDisposable Members
public void Dispose()
{
throw new NotImplementedException();
}
#endregion
}
}