Skip to content

Commit

Permalink
Code optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Jun 30, 2024
1 parent 81d1e20 commit 0b62b7a
Showing 1 changed file with 45 additions and 71 deletions.
116 changes: 45 additions & 71 deletions Spacetris/BackgroundEffects/Starfield.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ public class Starfield : Transformable, IBackgroundEffects
private readonly Image _mediumStarImage;
private readonly Image _largeStarImage;

private readonly uint _maxSmallStars;
private readonly uint _maxMediumStars;
private readonly uint _maxLargeStars;
private readonly int _maxSmallStars;
private readonly int _maxMediumStars;
private readonly int _maxLargeStars;

private readonly List<Point2> _smallStars = [];
private readonly List<Point2> _mediumStars = [];
private readonly List<Point2> _largeStars = [];
private readonly List<Point2> _smallStars = new();
private readonly List<Point2> _mediumStars = new();
private readonly List<Point2> _largeStars = new();

private readonly Random _randomX;
private readonly Random _randomY;

private readonly Image _image;
private Texture _texture;
private Sprite _sprite;
private readonly Texture _texture;
private readonly Sprite _sprite;

private readonly uint _width;
private readonly uint _height;
Expand All @@ -34,9 +34,8 @@ public Starfield(uint width, uint height)
{
_width = width;
_height = height;

_image = new Image(width, height, Color.Black);
_texture = new Texture(_image);

_texture = new Texture(width, height);
_sprite = new Sprite(_texture);

const uint smallSize = 1;
Expand All @@ -48,28 +47,25 @@ public Starfield(uint width, uint height)
_largeStarImage = new Image(largeSize, largeSize, Color.White);

_randomX = new Random(new Time().AsMilliseconds());
var randomY = new Random(new Time().AsMilliseconds() + 100);
_randomY = new Random(new Time().AsMilliseconds() + 100);

const uint reduceStars = 8;
const uint classDifference = 3;

_maxSmallStars = width / (reduceStars * 10) * (height / reduceStars);
_maxMediumStars = width / (reduceStars * 10 * classDifference) * (height / (reduceStars * classDifference));
_maxLargeStars = width / (reduceStars * 10 * classDifference * classDifference) * (height / (reduceStars * classDifference * classDifference));
_maxSmallStars = (int)(width / (reduceStars * 10) * (height / reduceStars));
_maxMediumStars = (int)(width / (reduceStars * 10 * classDifference) * (height / (reduceStars * classDifference)));
_maxLargeStars = (int)(width / (reduceStars * 10 * classDifference * classDifference) * (height / (reduceStars * classDifference * classDifference)));

while (_smallStars.Count <= _maxSmallStars)
{
_smallStars.Add(new Point2(_randomX.Next() % width + 1, randomY.Next() % height + 1));
}

while (_mediumStars.Count <= _maxMediumStars)
{
_mediumStars.Add(new Point2(_randomX.Next() % width + 1, randomY.Next() % height + 1));
}
InitializeStars(_smallStars, _maxSmallStars);
InitializeStars(_mediumStars, _maxMediumStars);
InitializeStars(_largeStars, _maxLargeStars);
}

while (_largeStars.Count <= _maxLargeStars)
private void InitializeStars(List<Point2> stars, int maxStars)
{
while (stars.Count < maxStars)
{
_largeStars.Add(new Point2(_randomX.Next() % width + 1, randomY.Next() % height + 1));
stars.Add(new Point2(_randomX.Next((int)_width), _randomY.Next((int)_height)));
}
}

Expand All @@ -86,59 +82,39 @@ public void Update(float deltaTime)
_totalStarsMoveTimer = 0;

// Move the stars down and remove them if they cross the bottom line
for (var i = 0; i < _smallStars.Count; i++)
{
if (_smallStars[i].Y > _height)
{
_smallStars.RemoveAt(i);
continue;
}

_smallStars[i] += new Point2(0, 1);
}

for (var i = 0; i < _mediumStars.Count; i++)
{
if (_mediumStars[i].Y > _height)
{
_mediumStars.RemoveAt(i);
continue;
}

_mediumStars[i] += new Point2(0, 2);
}
UpdateStars(_smallStars, 1);
UpdateStars(_mediumStars, 2);
UpdateStars(_largeStars, 3);

for (var i = 0; i < _largeStars.Count; i++)
// Adding more stars if their numbers falls bellow the maximum number
ReplenishStars(_smallStars, _maxSmallStars);
ReplenishStars(_mediumStars, _maxMediumStars);
ReplenishStars(_largeStars, _maxLargeStars);
}

private void UpdateStars(List<Point2> stars, int speed)
{
for (int i = stars.Count - 1; i >= 0; i--)
{
if (_largeStars[i].Y > _height)
stars[i] += new Point2(0, speed);
if (stars[i].Y > _height)
{
_largeStars.RemoveAt(i);
continue;
stars.RemoveAt(i);
}

_largeStars[i] += new Point2(0, 3);
}

// Adding more stars if their numbers falls bellow the maximum number
while (_smallStars.Count <= _maxSmallStars)
{
_smallStars.Add(new Point2(_randomX.Next() % _width + 1, 0));
}

while (_mediumStars.Count <= _maxMediumStars)
{
_mediumStars.Add(new Point2(_randomX.Next() % _width + 1, 0));
}

while (_largeStars.Count <= _maxLargeStars)
}

private void ReplenishStars(List<Point2> stars, int maxStars)
{
while (stars.Count < maxStars)
{
_largeStars.Add(new Point2(_randomX.Next() % _width + 1, 0));
stars.Add(new Point2(_randomX.Next((int)_width), 0));
}
}

public void Draw(RenderTarget target, RenderStates states)
{
_texture = new Texture(_image);
_texture.Update(new Image(_width, _height, Color.Black));

foreach (var star in _smallStars)
{
Expand All @@ -155,8 +131,6 @@ public void Draw(RenderTarget target, RenderStates states)
_texture.Update(_largeStarImage, (uint)star.X, (uint)star.Y);
}

_sprite = new Sprite(_texture);

target.Draw(_sprite);
}
}

0 comments on commit 0b62b7a

Please sign in to comment.