This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- format file structure - remove reduntant code code - move float to int
- Loading branch information
Phoenixfirewingz
committed
Jun 27, 2023
1 parent
44113eb
commit dfa8a23
Showing
46 changed files
with
1,045 additions
and
1,202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
packages | ||
bin | ||
obj | ||
Properties | ||
Properties | ||
*.pfx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using Avalonia.Controls; | ||
using Avalonia.Input; | ||
using Avalonia.Media; | ||
using Avalonia.Threading; | ||
using DiscOut.GameObjects.Dynamic; | ||
using DiscOut.GameObjects.World; | ||
using DiscOut.GameObjects.World.Score; | ||
using DiscOut.Util; | ||
using NAudio.Wave; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
namespace DiscOut.Avalonia; | ||
public partial class DiscWindow : Window | ||
{ | ||
private readonly HashSet<Key> keys = new(); | ||
private static readonly WaveOutEvent MusicPlayer = 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. | ||
internal Paddle Paddle { get; } = new(); | ||
internal Level Level { get; private set; } | ||
public DiscWindow() | ||
{ | ||
InitializeComponent(); | ||
Instance = this; | ||
Level = new("assets.levels.level_1.json"); | ||
ScoreBoard = ScoreBoard.Load(); | ||
MusicPlayer.Init(new Mp3FileReader(AssetUtil.OpenEmbeddedFile("assets.sounds.music.mp3"))); | ||
MusicPlayer.Volume = 0.25f; | ||
MusicPlayer.Play(); | ||
ticker.Tick += delegate | ||
{ | ||
if (GameRestart) | ||
{ | ||
if (keys.Contains(Key.Enter)) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND); | ||
GameRestart = false; | ||
DisplayText.Text = ""; | ||
Level = new("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json"); | ||
keys.Clear(); | ||
} | ||
DisplayText.Text = "Press Enter To Restart"; | ||
} | ||
else | ||
{ | ||
Paddle.OnKeyDown(keys); | ||
Level.GetBall().OnKeyDown(keys); | ||
if (!Level.GetBall().IsAlive()) | ||
{ | ||
DisplayText.Text = "Press Space To Start!\n " + ScoreBoard.ToString(); | ||
} | ||
else | ||
DisplayText.Text = ""; | ||
} | ||
if (Paddle.IsDead()) | ||
{ | ||
Paddle.ResetPaddle(); | ||
int Score = Level.GetBall().GetScore(); | ||
if (Score > ScoreBoard.entry[0].Score) | ||
{ | ||
Action<int, string> callback = FinishNewUser; | ||
var popupWindow = new PopUpWindow(Score, callback); | ||
var task = popupWindow.ShowDialog(this); | ||
} | ||
GameRestart = true; | ||
} | ||
Level.OnUpdate(); | ||
Paddle.OnUpdate(); | ||
}; | ||
ticker.IsEnabled = true; | ||
Level.OnUpdate(); | ||
Paddle.OnUpdate(); | ||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); | ||
GC.WaitForPendingFinalizers(); | ||
} | ||
|
||
protected override void OnKeyDown(KeyEventArgs e) | ||
{ | ||
keys.Add(e.Key); | ||
base.OnKeyDown(e); | ||
} | ||
|
||
protected override void OnKeyUp(KeyEventArgs e) | ||
{ | ||
keys.Remove(e.Key); | ||
base.OnKeyUp(e); | ||
} | ||
|
||
public override void Render(DrawingContext context) | ||
{ | ||
GLView.Render(context); | ||
base.Render(context); | ||
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.MaxValue); | ||
} | ||
|
||
protected void FinishNewUser(int LastScoreData, string UserNameData) | ||
{ | ||
ScoreBoard.AddScore(new(UserNameData, LastScoreData)); | ||
ScoreBoard.Save(ScoreBoard); | ||
} | ||
|
||
protected override void OnClosing(CancelEventArgs e) | ||
{ | ||
ticker.IsEnabled = false; | ||
SoundUtils.CleanUp(); | ||
MusicPlayer.Stop(); | ||
MusicPlayer.Dispose(); | ||
Renderer.Dispose(); | ||
ScoreBoard.Save(ScoreBoard); | ||
base.OnClosing(e); | ||
} | ||
|
||
public void LevelWon() | ||
{ | ||
CurrentLevel++; | ||
Level = new Level("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../Windows/Controls/GameOpenGLControl.axaml → DiscOut/Avolonia/GameOpenGLControl.axaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<UserControl xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
x:Class="SquareSmash.renderer.Windows.Controls.GameOpenGLControl"> | ||
x:Class="DiscOut.Avalonia.GameOpenGLControl"> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Avalonia.OpenGL; | ||
using Avalonia.OpenGL.Controls; | ||
using DiscOut.Renderer; | ||
using static Avalonia.OpenGL.GlConsts; | ||
namespace DiscOut.Avalonia; | ||
public partial class GameOpenGLControl : OpenGlControlBase | ||
{ | ||
internal QuadBatchRenderer? Renderer { get; private set; } | ||
public GameOpenGLControl() | ||
=> InitializeComponent(); | ||
|
||
protected override unsafe void OnOpenGlRender(GlInterface gl, int fb) | ||
{ | ||
gl.ClearColor(0, 0, 0, 0); | ||
gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | ||
gl.Viewport(0, 0, (int)DiscWindow.Instance.Width - ((int)DiscWindow.Instance.Width / 16), (int)DiscWindow.Instance.Height - ((int)DiscWindow.Instance.Height / 10)); | ||
DiscWindow.Instance.Paddle.OnRendering(Renderer!); | ||
DiscWindow.Instance.Level.OnRendering(Renderer!); | ||
} | ||
|
||
protected override void OnOpenGlInit(GlInterface gl, int fb) | ||
=> Renderer = new(gl); | ||
|
||
protected override void OnOpenGlDeinit(GlInterface gl, int fb) | ||
=> Renderer!.Dispose(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Avalonia.Controls; | ||
using Avalonia.Interactivity; | ||
using DiscOut.Util; | ||
using System; | ||
namespace DiscOut.Avalonia; | ||
public partial class PopUpWindow : Window | ||
{ | ||
private readonly Action<int, string> Callback; | ||
private readonly int Score; | ||
|
||
public PopUpWindow() | ||
{ | ||
Score = 0; | ||
Callback = (int n, string s) => { }; | ||
InitializeComponent(); | ||
TextInput.Text = ""; | ||
} | ||
|
||
public PopUpWindow(int score, Action<int, string> callback) | ||
{ | ||
Callback = callback; | ||
Score = score; | ||
InitializeComponent(); | ||
TextInput.Text = ""; | ||
} | ||
|
||
private void OnButtonClick(object sender, RoutedEventArgs e) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND); | ||
if (TextInput.Text.Length >= 1 && TextInput.Text.Length <= 10) | ||
{ | ||
Callback.Invoke(Score, TextInput.Text); | ||
Close(); | ||
return; | ||
} | ||
TextInput.Text = "must be between 1 & 10 chars"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using Avalonia.Input; | ||
using DiscOut.Avalonia; | ||
using DiscOut.GameObjects.Static; | ||
using DiscOut.GameObjects.World; | ||
using DiscOut.Renderer; | ||
using DiscOut.Util; | ||
using System; | ||
using System.Collections.Generic; | ||
namespace DiscOut.GameObjects.Dynamic; | ||
internal class Ball | ||
{ | ||
public int Score { get; set; } = 0; | ||
private int LastScore = 0; | ||
private Vertex[] Vertices = new Vertex[6]; | ||
private float PositionX; | ||
private float PositionY = -140; | ||
private float VelocityX; | ||
private float VelocityY; | ||
private readonly byte LaunchSpeed; | ||
private bool Released = false; | ||
private byte CoolDown = 0; | ||
public Ball(byte speed) | ||
{ | ||
LaunchSpeed = speed; | ||
PositionX = DiscWindow.Instance.Paddle.GetPositionX(); | ||
VertexUtils.PreMakeQuad(0, 0, 0, 0, 0, ref Vertices); | ||
} | ||
public void ResetBall() | ||
{ | ||
VelocityX = 0; | ||
VelocityY = 0; | ||
Released = false; | ||
} | ||
public int GetScore() | ||
{ | ||
LastScore = Score; | ||
Score = 0; | ||
return LastScore; | ||
} | ||
public bool IsAlive() => Released; | ||
public void OnKeyDown(HashSet<Key> keys) | ||
{ | ||
if (keys.Contains(Key.Space) && !Released) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND); | ||
DiscWindow.Instance.DisplayText.Text = ""; | ||
VelocityY = LaunchSpeed; | ||
VelocityX = Random.Shared.NextSingle() > 0.5f ? -LaunchSpeed : LaunchSpeed; | ||
Released = true; | ||
} | ||
} | ||
public void OnUpdate(Level level) | ||
{ | ||
if (Released is false) | ||
{ | ||
PositionX = DiscWindow.Instance.Paddle.GetPositionX() * 10; | ||
PositionY = -145; | ||
VertexUtils.UpdateQuad(PositionX, PositionY, 0.03f, 0.03f, ref Vertices); | ||
return; | ||
} | ||
PositionX = Math.Clamp(PositionX + VelocityX, -171, 171); | ||
PositionY = Math.Clamp(PositionY + VelocityY, -171, 171); | ||
if (DiscWindow.Instance.Paddle.DoseFullIntersects(Vertices)) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND); | ||
VelocityY = Math.Abs(VelocityY); | ||
} | ||
else if (PositionY >= 170) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND); | ||
VelocityY = -Math.Abs(VelocityY); | ||
} | ||
else if (PositionX <= -170) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND); | ||
VelocityX = Math.Abs(VelocityX); | ||
} | ||
else if (PositionX >= 170) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND); | ||
VelocityX = -Math.Abs(VelocityX); | ||
} | ||
else | ||
{ | ||
foreach (Brick brick in level.GetBricks()) | ||
{ | ||
if (brick.DoseFullIntersects(Vertices)) | ||
{ | ||
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND); | ||
if (CoolDown == 0) | ||
{ | ||
CoolDown = 5; | ||
brick.Die(); | ||
Score++; | ||
if (brick.GetBrickType() == BrickType.LIFE) | ||
DiscWindow.Instance.Paddle.AddLife(); | ||
VelocityY = -(Math.Abs(VelocityY) * 1.02f); | ||
} | ||
else | ||
VelocityY = -Math.Abs(VelocityY); | ||
|
||
if (Random.Shared.Next(12) > 7) | ||
{ | ||
if (Random.Shared.Next(12) > 7) | ||
VelocityX -= Random.Shared.Next(12) > 7 ? 0 : 3; | ||
else | ||
VelocityX += Random.Shared.Next(12) > 7 ? 0 : 3; | ||
} | ||
else | ||
VelocityX = Random.Shared.Next(12) > 4 ? VelocityX * 1.02f : Math.Abs(VelocityX) * 1.02f; | ||
break; | ||
} | ||
} | ||
} | ||
if (CoolDown != 0) | ||
CoolDown--; | ||
if (VelocityY == 0) | ||
VelocityY += LaunchSpeed; | ||
VertexUtils.UpdateQuad(PositionX, PositionY, 0.03f, 0.03f, ref Vertices, 0); | ||
if (PositionY <= -170) | ||
{ | ||
ResetBall(); | ||
LastScore = Score; | ||
DiscWindow.Instance.Paddle.ResetPaddle(); | ||
} | ||
} | ||
public void OnRendering(QuadBatchRenderer renderer) | ||
{ | ||
DiscWindow.Instance.ScoreText.Text = "Score: " + Score.ToString(); | ||
renderer.AddQuad(Vertices); | ||
} | ||
} |
Oops, something went wrong.