misc optimization & refactoring

This commit is contained in:
Regalis
2015-09-29 18:03:38 +03:00
parent cc16bb3ad7
commit 45178e745b
30 changed files with 820 additions and 210 deletions
@@ -15,6 +15,8 @@ namespace Subsurface
const float CheckWallsInterval = 5.0f;
public bool Enabled;
private BackgroundSpritePrefab prefab;
private Vector2 position;
@@ -29,6 +31,12 @@ namespace Subsurface
public Swarm Swarm;
Vector2 drawPosition;
public Vector2 TransformedPosition
{
get { return drawPosition; }
}
public Vector2 Position
{
get { return position; }
@@ -51,6 +59,8 @@ namespace Subsurface
this.position = position;
drawPosition = position + Level.Loaded.Position;
steeringManager = new SteeringManager(this);
velocity = new Vector3(
@@ -141,18 +151,16 @@ namespace Subsurface
if (velocity.X < 0.0f) rotation -= MathHelper.Pi;
}
Vector2 drawPos = position;
if (Level.Loaded != null) drawPos += Level.Loaded.Position;
if (Level.Loaded != null) drawPosition = position + Level.Loaded.Position;
if (depth > 0.0f)
{
Vector2 camOffset = drawPos - GameMain.GameScreen.Cam.WorldViewCenter;
Vector2 camOffset = drawPosition - GameMain.GameScreen.Cam.WorldViewCenter;
drawPos = drawPos - camOffset * (depth / MaxDepth) * 0.05f;
drawPosition = drawPosition - camOffset * (depth / MaxDepth) * 0.05f;
}
prefab.Sprite.Draw(spriteBatch, new Vector2(drawPos.X, -drawPos.Y), Color.Lerp(Color.White, Color.DarkBlue, (depth/MaxDepth)*0.3f),
prefab.Sprite.Draw(spriteBatch, new Vector2(drawPosition.X, -drawPosition.Y), Color.Lerp(Color.White, Color.DarkBlue, (depth/MaxDepth)*0.3f),
rotation, 1.0f - (depth / MaxDepth) * 0.2f, velocity.X > 0.0f ? SpriteEffects.None : SpriteEffects.FlipHorizontally, (depth / MaxDepth));
}
}
@@ -12,6 +12,10 @@ namespace Subsurface
{
const int MaxSprites = 100;
const float checkActiveInterval = 1.0f;
float checkActiveTimer;
private List<BackgroundSpritePrefab> prefabs;
private List<BackgroundSprite> activeSprites;
@@ -31,7 +35,6 @@ namespace Subsurface
public void SpawnSprites(int count)
{
activeSprites.Clear();
if (prefabs.Count == 0) return;
@@ -77,10 +80,26 @@ namespace Subsurface
activeSprites.Clear();
}
public void Update(float deltaTime)
public void Update(Camera cam, float deltaTime)
{
if (checkActiveTimer<0.0f)
{
foreach (BackgroundSprite sprite in activeSprites)
{
sprite.Enabled = (Math.Abs(sprite.TransformedPosition.X - cam.WorldViewCenter.X) < 4000.0f &&
Math.Abs(sprite.TransformedPosition.Y - cam.WorldViewCenter.Y) < 4000.0f);
}
checkActiveTimer = checkActiveInterval;
}
else
{
checkActiveTimer -= deltaTime;
}
foreach (BackgroundSprite sprite in activeSprites)
{
if (!sprite.Enabled) continue;
sprite.Update(deltaTime);
}
}
@@ -89,6 +108,7 @@ namespace Subsurface
{
foreach (BackgroundSprite sprite in activeSprites)
{
if (!sprite.Enabled) continue;
sprite.Draw(spriteBatch);
}
}