Skip to content

Commit

Permalink
unload forcefield visuals when offscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
vitellaryjr committed Aug 12, 2024
1 parent 0624d88 commit fcddc1d
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions Code/ForceField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public ForceField(EntityData data, Vector2 offset, EntityID gid) : base(data.Pos
silent = data.Bool("silent");

nodes = data.NodesOffset(offset);
rngOffset = Calc.Random.NextFloat();
lastSigns = new int[nodes.Length];
ends = new List<Sprite>();
lasers = new List<List<Sprite>>();
Expand All @@ -57,9 +58,24 @@ public override void Added(Scene scene)
if (silent)
sound.Stop();

bounds = new Rectangle((int)X, (int)Y, 0, 0);
for (int i = 0; i < nodes.Length; i++)
{
Vector2 node = nodes[i] - Position;
Vector2 nodePos = nodes[i];
if (nodePos.X < bounds.X) {
bounds.Width += (int)(bounds.X - nodePos.X);
bounds.X = (int)nodePos.X;
} else {
bounds.Width = (int)Math.Max(bounds.Width, nodePos.X - bounds.X);
}
if (nodePos.Y < bounds.Y) {
bounds.Height += (int)(bounds.Y - nodePos.Y);
bounds.Y = (int)nodePos.Y;
} else {
bounds.Height = (int)Math.Max(bounds.Height, nodePos.Y - bounds.Y);
}

Vector2 node = nodePos - Position;
Sprite nodesprite = new Sprite(GFX.Game, "objects/" + texture + "/end");
nodesprite.AddLoop("idle", "", 0.1f);
nodesprite.Play("idle");
Expand Down Expand Up @@ -102,19 +118,28 @@ public override void Update()
Player player = Scene.Tracker.GetEntity<Player>();
if (player == null) return;

if (Scene.OnInterval(0.25f, rngOffset) && !InView()) {
inView = false;
} else if (!inView && InView()) {
inView = true;
}

// set alphas for each node
if (visibleDist > 0)
{
float dist = visibleDist * 2f;
for (int i = 0; i <= nodes.Length; i++)
{
Vector2 node = Position;
if (i > 0) node = nodes[i - 1];
dist = Math.Min(Vector2.Distance(player.Position, node), dist);
}
alpha = Calc.ClampedMap(dist, visibleDist, visibleDist * 2f, 1f, 0f);
if (inView) {
if (visibleDist > 0) {
float dist = visibleDist * 2f;
for (int i = 0; i <= nodes.Length; i++) {
Vector2 node = Position;
if (i > 0)
node = nodes[i - 1];
dist = Math.Min(Vector2.Distance(player.Position, node), dist);
}
alpha = Calc.ClampedMap(dist, visibleDist, visibleDist * 2f, 1f, 0f);
} else
alpha = 1f;
} else {
alpha = 0f;
}
else alpha = 1f;

if (flag != "")
{
Expand Down Expand Up @@ -170,13 +195,22 @@ public override void Update()
}
}

private bool InView() {
Camera camera = (Scene as Level).Camera;
return ((bounds.Right > camera.Left - 16f) || (bounds.Left < camera.Right + 16f))
&& ((bounds.Bottom > camera.Top - 16f) || (bounds.Top < camera.Bottom + 16f));
}

public void OnPlayer(Player player)
{
player.Die(Vector2.Zero);
}

public override void Render()
{
if (alpha == 0f)
return;

foreach (Sprite nodesprite in ends)
{
nodesprite.Color = tint * alpha;
Expand Down Expand Up @@ -208,13 +242,17 @@ public override void DebugRender(Camera camera)
}
Draw.Line(start, node, color);
}
// Draw.HollowRect(bounds, Color.Cyan);
}

private string texture;
private Color tint;
private string flag;
private bool invert;
private Vector2[] nodes;
private Rectangle bounds;
private float rngOffset;
private bool inView;
private bool silent;

private float visibleDist;
Expand Down

0 comments on commit fcddc1d

Please sign in to comment.