Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
change loop caller
Browse files Browse the repository at this point in the history
 - use avolonia
  • Loading branch information
Phoenixfirewingz committed Jun 26, 2023
1 parent 1abaeeb commit 44113eb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion assets/levels/level_1.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"BaseBallSpeed": 0.1,
"BaseBallSpeed": 3,
"Bricks": [
{
"Colour": "DiscGreen",
Expand Down
2 changes: 1 addition & 1 deletion assets/levels/level_2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"BaseBallSpeed": 0.125,
"BaseBallSpeed": 5,
"Bricks": [
{
"Colour": "DiscGreen",
Expand Down
4 changes: 2 additions & 2 deletions objects/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ public void OnRendering(object sender)
((QuadBatchRenderer)sender).FlushAntiGhost();
}

public void OnUpdate(float DeltaTime)
public void OnUpdate()
{
if (bricks.Count <= 0)
{
DiscWindow.Instance.LevelWon();
return;
}
ball.OnUpdate(this, DeltaTime);
ball.OnUpdate(this);
}
}
}
6 changes: 3 additions & 3 deletions objects/components/Ball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void OnKeyDown(HashSet<Key> keys)
}
}

public void OnUpdate(Level level, float DeltaTime)
public void OnUpdate(Level level)
{
if (Released is false)
{
Expand All @@ -69,7 +69,7 @@ public void OnUpdate(Level level, float DeltaTime)
return;
}

Position += Velocity * DeltaTime;
Position += Velocity;
Position.X = Math.Clamp(Position.X, -171, 171);
Position.Y = Math.Clamp(Position.Y, -171, 171);

Expand Down Expand Up @@ -104,7 +104,7 @@ public void OnUpdate(Level level, float DeltaTime)
{
CoolDown = 5;
brick.Die();
Speed *= 1.0000001f;
Speed *= 1.1f;
Score++;
if (brick.GetBrickType() == BrickType.LIFE)
DiscWindow.Instance.Paddle.AddLife();
Expand Down
8 changes: 4 additions & 4 deletions objects/components/Paddle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public void ResetPaddle()
public void OnKeyDown(HashSet<Key> keys)
{
if (keys.Contains(Key.A) && X < 14)
VelocityX = 0.05f;
VelocityX = 0.75f;
else if (keys.Contains(Key.D) && X > -14)
VelocityX = -0.05f;
VelocityX = -0.75f;
}

public void OnUpdate(float DeltaTime)
public void OnUpdate()
{
X += VelocityX * DeltaTime;
X += VelocityX;
VelocityX = 0;
X = Clamp(X, -15, 15);
VertexUtils.UpdateQuad(X, -150.0f, 0.3f, 0.03f, ref Vertices,0);
Expand Down
23 changes: 14 additions & 9 deletions renderer/Windows/DiscWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public partial class DiscWindow : Window
{
private HashSet<Key> keys = new HashSet<Key>();
private static readonly WaveOutEvent MusicPlayer = new();
private readonly Stopwatch stopwatch = new();
private readonly ScoreBoard ScoreBoard;
private int CurrentLevel = 1;
private bool GameRestart = false;

private readonly DispatcherTimer ticker = new() { Interval = new TimeSpan(0, 0, 0, 0, 1000 / 60) };

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public static DiscWindow Instance { get; set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
Expand All @@ -37,10 +38,11 @@ public DiscWindow()
Instance = this;
Level = new("assets.levels.level_1.json");
ScoreBoard = ScoreBoard.Load();
stopwatch.Start();
MusicPlayer.Init(new Mp3FileReader(AssetUtil.OpenEmbeddedFile("assets.sounds.music.mp3")));
MusicPlayer.Volume = 0.25f;
MusicPlayer.Play();
//MusicPlayer.Play();
ticker.Tick += delegate { tick(); };
ticker.IsEnabled = true;
GC.Collect(2,GCCollectionMode.Aggressive,true,true);
GC.WaitForPendingFinalizers();
}
Expand All @@ -57,7 +59,7 @@ protected override void OnKeyUp(KeyEventArgs e)
base.OnKeyUp(e);
}

public override void Render(DrawingContext context)
protected void tick()
{
if (GameRestart)
{
Expand Down Expand Up @@ -94,10 +96,13 @@ public override void Render(DrawingContext context)
}
GameRestart = true;
}
float DeltaTime = (float)Instance.stopwatch.Elapsed.TotalMilliseconds;
Instance.stopwatch.Restart();
Instance.Level.OnUpdate(DeltaTime);
Instance.Paddle.OnUpdate(DeltaTime);
Level.OnUpdate();
Paddle.OnUpdate();
}

public override void Render(DrawingContext context)
{

GLView.Render(context);
base.Render(context);
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Render);
Expand All @@ -115,10 +120,10 @@ protected void FinishNewUser(int LastScoreData, string UserNameData)

protected override void OnClosing(CancelEventArgs e)
{
ticker.IsEnabled = false;
SoundUtils.CleanUp();
MusicPlayer.Stop();
MusicPlayer.Dispose();
stopwatch.Stop();
Renderer.Dispose();
ScoreBoard.Save(ScoreBoard);
base.OnClosing(e);
Expand Down

0 comments on commit 44113eb

Please sign in to comment.