Skip to content

Commit

Permalink
Flickering lights in the modular dungeon scene
Browse files Browse the repository at this point in the history
  • Loading branch information
Selinux24 committed May 22, 2024
1 parent 36a56b5 commit b669b4f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 12 deletions.
47 changes: 47 additions & 0 deletions Samples/03 TerrainSamples/SceneModularDungeon/LightController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Engine;
using SharpDX;
using System;

namespace TerrainSamples.SceneModularDungeon
{
/// <summary>
/// Light basic controller
/// </summary>
class LightController
{
private readonly Random rnd = Helper.NewGenerator();
private readonly float initialTime = Helper.RandomGenerator.NextFloat(0, 100);

/// <summary>
/// Spot light
/// </summary>
public ISceneLightPoint Light { get; set; }
/// <summary>
/// Light position function
/// </summary>
public Func<Vector3> PositionFnc { get; set; }

/// <summary>
/// Updates the light position
/// </summary>
/// <param name="gameTime">Game time</param>
public void Update(IGameTime gameTime)
{
float r = 0.01f;
float h = 0.005f;
float v = 5f;

float totalSeconds = gameTime.TotalSeconds + initialTime;

float cos = MathF.Cos(v * totalSeconds);
float sin = MathF.Sin(v * totalSeconds);

float x = r * cos * rnd.NextFloat(0.15f, 9f);
float z = r * sin * rnd.NextFloat(0.85f, 9f);

float y = h * sin * rnd.NextFloat(0.35f, 9f);

Light.Position = PositionFnc() + new Vector3(x, y, z);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ModularDungeonScene : WalkableScene
private readonly Vector3 cameraInitialInterest = new(1001, 1000, 1000);

private SceneLightPoint torch = null;
private readonly List<LightController> lightControllers = [];

private ModularScenery scenery = null;

Expand Down Expand Up @@ -224,7 +225,7 @@ public override void Update(IGameTime gameTime)

NavigationGraph?.UpdateObstacles((state) =>
{
if(state == GraphUpdateStates.Updated)
if (state == GraphUpdateStates.Updated)
{
debugDrawers.DrawGraph();
debugDrawers.DrawObstacles(obstacles);
Expand All @@ -245,6 +246,8 @@ public override void Update(IGameTime gameTime)
{
UpdateStateMap();
}

lightControllers.ForEach(l => l.Update(gameTime));
}
private void UpdateStatePlayer(IGameTime gameTime)
{
Expand Down Expand Up @@ -717,14 +720,6 @@ private void UpdatePlayerInput()
Camera.Goto(Camera.Position);
}

if (torch.Enabled)
{
torch.Position =
Camera.Position +
(Camera.Direction * 0.5f) +
(Camera.Left * 0.2f);
}

if (Game.Input.KeyJustReleased(Keys.L))
{
torch.Enabled = !torch.Enabled;
Expand Down Expand Up @@ -1251,8 +1246,7 @@ private void ChangeToLevel(string name)
Camera.SetPosition(cameraInitialPosition);
Camera.SetInterest(cameraInitialInterest);

Lights.ClearPointLights();
Lights.ClearSpotLights();
ClearLevelLights();

soundEffectsManager.Stop();

Expand Down Expand Up @@ -1308,7 +1302,7 @@ private void ChangeToLevelCompleted()
Camera.SetPosition(pos);
Camera.SetInterest(pos + dir);

Lights.Add(torch);
InitializeLevelLights();

soundEffectsManager.Start(0.5f);

Expand Down Expand Up @@ -1466,6 +1460,47 @@ private void StartEntitiesObstacles()
}
}

private void ClearLevelLights()
{
Lights.ClearPointLights();
Lights.ClearSpotLights();
lightControllers.Clear();
}
private void InitializeLevelLights()
{
var controllers = Lights.PointLights
.Where(l => l.Name.Contains("Dn_Torch") || l.Name.Contains("Dn_Temple"))
.Select(l =>
{
Vector3 orig = l.Position;

return new LightController()
{
Light = l,
PositionFnc = () =>
{
return orig;
},
};
});

lightControllers.AddRange(controllers);

Lights.Add(torch);

lightControllers.Add(new()
{
Light = torch,
PositionFnc = () =>
{
return
Camera.Position +
(Camera.Direction * 0.5f) +
(Camera.Left * 0.2f);
},
});
}

private void ClearNPCs()
{
ratActive = false;
Expand Down

0 comments on commit b669b4f

Please sign in to comment.