diff options
| author | william341 <[email protected]> | 2017-06-18 16:43:30 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-06-18 16:43:30 -0700 |
| commit | ba80dcf3f80018cbb041b62ad8a40268427d1311 (patch) | |
| tree | f0bd18b2355d34c07c744c3cd82a4725a799eecd /ShiftOS.WinForms/Applications/Pong.cs | |
| parent | 771c20cfb3a703e0f1550fdcf9eb07b78298c944 (diff) | |
| parent | 12acff8742f4c64976bfabee1b70dc515190fc7c (diff) | |
| download | shiftos_thereturn-ba80dcf3f80018cbb041b62ad8a40268427d1311.tar.gz shiftos_thereturn-ba80dcf3f80018cbb041b62ad8a40268427d1311.tar.bz2 shiftos_thereturn-ba80dcf3f80018cbb041b62ad8a40268427d1311.zip | |
Merge pull request #2 from shiftos-game/master
wew
Diffstat (limited to 'ShiftOS.WinForms/Applications/Pong.cs')
| -rw-r--r-- | ShiftOS.WinForms/Applications/Pong.cs | 1290 |
1 files changed, 307 insertions, 983 deletions
diff --git a/ShiftOS.WinForms/Applications/Pong.cs b/ShiftOS.WinForms/Applications/Pong.cs index 84177b7..53d410e 100644 --- a/ShiftOS.WinForms/Applications/Pong.cs +++ b/ShiftOS.WinForms/Applications/Pong.cs @@ -1,1143 +1,467 @@ -/* - * MIT License - * - * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using System; +using System; using System.Collections.Generic; using System.ComponentModel; -using System.Data; using System.Drawing; +using System.Data; using System.Linq; using System.Text; -using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using Newtonsoft.Json; using ShiftOS.Engine; -using ShiftOS.Objects; using ShiftOS.WinForms.Tools; namespace ShiftOS.WinForms.Applications { - [MultiplayerOnly] [Launcher("Pong", true, "al_pong", "Games")] [WinOpen("pong")] + [DefaultTitle("Pong")] [DefaultIcon("iconPong")] public partial class Pong : UserControl, IShiftOSWindow { - //I can assure you guaranteed that there is an acorn somewhere, in this place, and the sailors are looking for it - int xVel = 7; - int yVel = 8; - int computerspeed = 8; - int level = 1; - int secondsleft = 60; - int casualposition; - double xveldec = 3.0; - double yveldec = 3.0; - double incrementx = 0.4; - double incrementy = 0.2; - int levelxspeed = 3; - int levelyspeed = 3; - int beatairewardtotal; - int beataireward = 1; - int[] levelrewards = new int[50]; - int totalreward; - int countdown = 3; - - bool aiShouldIsbeEnabled = true; - public Pong() { InitializeComponent(); - } - - private void Pong_Load(object sender, EventArgs e) - { - setuplevelrewards(); - } - - - - // Move the paddle according to the mouse position. - private void pongMain_MouseMove(object sender, MouseEventArgs e) - { - var loc = this.PointToClient(MousePosition); - if (IsMultiplayerSession) + paddleWidth = pnlcanvas.Width / 30; + drawTimer = new Timer(); + drawTimer.Interval = 1; + drawTimer.Tick += (o, a) => + { + UpdateBall(); + pnlcanvas.Refresh(); + }; + counterTimer = new Timer(); + counterTimer.Interval = 1000; + counterTimer.Tick += (o, a) => { - if (IsLeader) + if(secondsleft > 0) { - paddleHuman.Location = new Point(paddleHuman.Location.X, (loc.Y) - (paddleHuman.Height / 2)); - ServerManager.Forward(OpponentGUID, "pong_mp_setopponenty", paddleHuman.Top.ToString()); + secondsleft--; } else { - paddleComputer.Location = new Point(paddleComputer.Location.X, (loc.Y) - (paddleComputer.Height / 2)); - ServerManager.Forward(OpponentGUID, "pong_mp_setopponenty", paddleComputer.Top.ToString()); + LevelComplete(); } - } - else + }; +#if DEBUG + this.KeyDown += (o, a) => { - paddleHuman.Location = new Point(paddleHuman.Location.X, (loc.Y) - (paddleHuman.Height / 2)); - } - } - - private void CenterPanels() - { - pnlfinalstats.CenterParent(); - pnlgamestats.CenterParent(); - pnlhighscore.CenterParent(); - pnlintro.CenterParent(); - pnllose.CenterParent(); - lblcountdown.CenterParent(); - lblbeatai.Left = (this.Width - lblbeatai.Width) / 2; - SetupStats(); - } - - public void SetupStats() - { - lblstatsX.Location = new Point(5, this.Height - lblstatsX.Height - 5); - lblstatsY.Location = new Point(this.Width - lblstatsY.Width - 5, this.Height - lblstatsY.Height - 5); - lblstatscodepoints.Top = this.Height - lblstatscodepoints.Height - 5; - lblstatscodepoints.Left = (this.Width - lblstatscodepoints.Width) / 2; + if(a.KeyCode == Keys.D) + { + drawAiBall = !drawAiBall; + } + }; +#endif } - int OpponentY = 0; + private double ballX = 0.0f; + private double ballY = 0.0f; - bool IsLeader = true; - - int LeaderX = 0; - int LeaderY = 0; - - // ERROR: Handles clauses are not supported in C# - private void gameTimer_Tick(object sender, EventArgs e) - { - if (this.Left < Screen.PrimaryScreen.Bounds.Width) - { - ball.BackColor = SkinEngine.LoadedSkin.ControlTextColor; - paddleComputer.BackColor = SkinEngine.LoadedSkin.ControlTextColor; - paddleHuman.BackColor = SkinEngine.LoadedSkin.ControlTextColor; - - //Check if paddle upgrade has been bought and change paddles accordingly - //if (ShiftoriumFrontend.UpgradeInstalled("pong_increased_paddle_size")) - //{ - // paddleHuman.Height = 150; - // paddleComputer.Height = 150; - //} - //I don't know the point of this but I'm fucking 86ing it. - Michael - - //Set the computer player to move according to the ball's position. - if (IsMultiplayerSession == true) - { - //If we're multiplayer, then we want to set the computer Y to the opponent's Y. - //If we're the leader, we set the AI paddle, else we set the player paddle. - if (IsLeader) - paddleComputer.Top = OpponentY; - else - paddleHuman.Top = OpponentY; - } - else - { - if (aiShouldIsbeEnabled) - if (ball.Location.X > (this.Width - (this.Width / 3)) - xVel * 10 && xVel > 0) - { - if (ball.Location.Y > paddleComputer.Location.Y + 50) - { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y + computerspeed); - } - if (ball.Location.Y < paddleComputer.Location.Y + 50) - { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y - computerspeed); - } - casualposition = rand.Next(-150, 201); - } - else - { - //used to be me.location.y - except it's fucking C# and this comment is misleading as fuck. OH WAIT! I didn't write it! And none of the current devs did either! - Michael - if (paddleComputer.Location.Y > this.Size.Height / 2 - paddleComputer.Height + casualposition) - { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y - computerspeed); - } - //Rylan is hot. Used to be //used to be me.location.y - if (paddleComputer.Location.Y < this.Size.Height / 2 - paddleComputer.Height + casualposition) - { - paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y + computerspeed); - } - } - } - //Set Xvel and Yvel speeds from decimal - if (xVel > 0) - xVel = (int)Math.Round(xveldec); - if (xVel < 0) - xVel = (int)-Math.Round(xveldec); - if (yVel > 0) - yVel = (int)Math.Round(yveldec); - if (yVel < 0) - yVel = (int)-Math.Round(yveldec); - - bool BallPhysics = true; - - if (IsMultiplayerSession) - { - //Logic for moving the ball in Multiplayer. - if (IsLeader) - { - ball.Location = new Point(ball.Location.X + xVel, ball.Location.Y + yVel); - } - else - { - //Move it to the leader's ball position. - ball.Location = new Point(LeaderX, LeaderY); - BallPhysics = false; - } - } - else - {// Move the game ball. - ball.Location = new Point(ball.Location.X + xVel, ball.Location.Y + yVel); - } - if (BallPhysics) - { - // Check for top wall. - if (ball.Location.Y < 0) - { - ball.Location = new Point(ball.Location.X, 0); - yVel = -yVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); - } + private double aiBallX = 0.0f; + private double aiBallY = 0.0f; - // Check for bottom wall. - if (ball.Location.Y > pgcontents.Height - ball.Height) - { - ball.Location = new Point(ball.Location.X, pgcontents.Height - ball.Size.Height); - yVel = -yVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); - } + private double speedFactor = 0.0125; - // Check for player paddle. - if (ball.Bounds.IntersectsWith(paddleHuman.Bounds)) - { - ball.Location = new Point(paddleHuman.Location.X + ball.Size.Width + 1, ball.Location.Y); - //randomly increase x or y speed of ball - switch (rand.Next(1, 3)) - { - case 1: - xveldec = xveldec + incrementx; - break; - case 2: - if (yveldec > 0) - yveldec = yveldec + incrementy; - if (yveldec < 0) - yveldec = yveldec - incrementy; - break; - } - xVel = -xVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.writesound); + private double xVel = 1; + private double yVel = 1; - } + private double aiXVel = 1; + private double aiYVel = 1; - // Check for computer paddle. - if (ball.Bounds.IntersectsWith(paddleComputer.Bounds)) - { - ball.Location = new Point(paddleComputer.Location.X - paddleComputer.Size.Width - 1, ball.Location.Y); - xveldec = xveldec + incrementx; - xVel = -xVel; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.writesound); - } - } + private int paddleWidth; + Timer counterTimer = null; - // Check for left wall. - if (ball.Location.X < -100) - { - //If we are in multiplayer, and not the leader, we won. - if (IsMultiplayerSession) - { - if (IsLeader) - { - //We lost. - NotifyLoseToTarget(); - } - else - { - //We won. - NotifyWinToTarget(); - Win(); - } - } - else - { - ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); - paddleComputer.Location = new Point(paddleComputer.Location.X, ball.Location.Y); - if (xVel > 0) - xVel = -xVel; - pnllose.Show(); - gameTimer.Stop(); - counter.Stop(); - lblmissedout.Text = Localization.Parse("{YOU_MISSED_OUT_ON}:") + Environment.NewLine + lblstatscodepoints.Text.Replace(Localization.Parse("{CODEPOINTS}: "), "") + Localization.Parse(" {CODEPOINTS}"); - if (ShiftoriumFrontend.UpgradeInstalled("pong_upgrade_2")) - { - totalreward = levelrewards[level - 1] + beatairewardtotal; - double onePercent = (totalreward / 100); - lblbutyougained.Show(); - lblbutyougained.Text = Localization.Parse("{BUT_YOU_GAINED}:") + Environment.NewLine + onePercent.ToString("") + (Localization.Parse(" {CODEPOINTS}")); - SaveSystem.TransferCodepointsFrom("pong", (totalreward / 100)); - } - else - { - lblbutyougained.Hide(); - } - } - } + private long codepointsToEarn = 0; + private int level = 1; - // Check for right wall. - if (ball.Location.X > this.Width - ball.Size.Width - paddleComputer.Width + 100) - { - if (IsMultiplayerSession) - { - //If we are the leader we won. - if (IsLeader) - { - NotifyWinToTarget(); - Win(); - } - else - { - NotifyLoseToTarget(); - } - } - else - { - Win(); - } - } - if (IsMultiplayerSession) - { - if (IsLeader) - { - ServerManager.Forward(OpponentGUID, "pong_mp_setballpos", JsonConvert.SerializeObject(ball.Location)); - } - } + private double playerY = 0.0; + private double opponentY = 0.0; + private int secondsleft = 60; + bool doAi = true; + bool doBallCalc = true; - if (IsLeader) - { - //lblstats.Text = "Xspeed: " & Math.Abs(xVel) & " Yspeed: " & Math.Abs(yVel) & " Human Location: " & paddleHuman.Location.ToString & " Computer Location: " & paddleComputer.Location.ToString & Environment.NewLine & " Ball Location: " & ball.Location.ToString & " Xdec: " & xveldec & " Ydec: " & yveldec & " Xinc: " & incrementx & " Yinc: " & incrementy - lblstatsX.Text = "X vel: " + xveldec; - lblstatsY.Text = "Y vel: " + yveldec; - lblstatscodepoints.Text = "Codepoints: " + (levelrewards[level - 1] + beatairewardtotal).ToString(); - lbllevelandtime.Text = "Level: " + level + " - " + secondsleft + " seconds left"; + private string header = ""; - if (xVel > 20 || xVel < -20) - { - paddleHuman.Width = Math.Abs(xVel); - paddleComputer.Width = Math.Abs(xVel); - } - else - { - paddleHuman.Width = 20; - paddleComputer.Width = 20; - } - } - if (!IsMultiplayerSession) - { - computerspeed = Math.Abs(yVel); - } - } - } + private string counter = ""; - public void ServerMessageReceivedHandler(ServerMessage msg) + private void Bounce(Rectangle ball, Rectangle paddle) { - if (IsMultiplayerSession) - { - if (msg.Name == "pong_mp_setballpos") - { - var pt = JsonConvert.DeserializeObject<Point>(msg.Contents); - LeaderX = pt.X; - LeaderY = pt.Y; - } - else if (msg.Name == "pong_mp_left") - { - this.Invoke(new Action(() => - { - AppearanceManager.Close(this); - })); - Infobox.Show("Opponent has closed Pong.", "The opponent has closed Pong, therefore the connection between you two has dropped."); - } - else if (msg.Name == "pong_mp_youlose") - { - this.Invoke(new Action(LoseMP)); - } - else if (msg.Name == "pong_mp_setopponenty") - { - int y = Convert.ToInt32(msg.Contents); - OpponentY = y; - } - else if (msg.Name == "pong_handshake_matchmake") - { - if (!PossibleMatchmakes.Contains(msg.Contents)) - PossibleMatchmakes.Add(msg.Contents); - this.Invoke(new Action(ListMatchmakes)); - } - else if (msg.Name == "pong_handshake_resendid") - { - ServerManager.Forward("all", "pong_handshake_matchmake", YouGUID); - } - else if (msg.Name == "pong_handshake_complete") - { - IsLeader = true; + // reverse x velocity to send the ball the other way + xVel = -xVel; - OpponentGUID = msg.Contents; - LeaveMatchmake(); - this.Invoke(new Action(() => - { - pnlmultiplayerhandshake.Hide(); - StartLevel(); - })); - } - else if(msg.Name == "pong_mp_levelcompleted") - { - level = Convert.ToInt32(msg.Contents) + 1; - this.Invoke(new Action(CompleteLevel)); - } - else if (msg.Name == "pong_handshake_chosen") - { - IsLeader = false; - LeaveMatchmake(); - OpponentGUID = msg.Contents; - YouGUID = ServerManager.thisGuid.ToString(); - //Start the timers. - counter.Start(); - SendFollowerGUID(); - this.Invoke(new Action(() => - { - pnlmultiplayerhandshake.Hide(); - })); - } - else if(msg.Name == "pong_mp_cashedout") - { - this.Invoke(new Action(() => - { - btncashout_Click(this, EventArgs.Empty); - })); - Infobox.Show("Cashed out.", "The other player has cashed out their Codepoints. Therefore, we have automatically cashed yours out."); - } - else if(msg.Name == "pong_mp_startlevel") - { - OpponentAgrees = true; - if(YouAgree == false) - { - Infobox.PromptYesNo("Play another level?", "The opponent wants to play another level. Would you like to as well?", (answer)=> - { - YouAgree = answer; - ServerManager.Forward(OpponentGUID, "pong_mp_level_callback", YouAgree.ToString()); - }); - } - } - else if(msg.Name == "pong_mp_level_callback") - { - bool agreed = bool.Parse(msg.Contents); - OpponentAgrees = agreed; - if (OpponentAgrees) - { - if (IsLeader) - { - //this.Invoke(new Action(())) - } - } - } - else if (msg.Name == "pong_handshake_left") - { - if (this.PossibleMatchmakes.Contains(msg.Contents)) - this.PossibleMatchmakes.Remove(msg.Contents); - this.Invoke(new Action(ListMatchmakes)); - } - else if(msg.Name == "pong_mp_clockupdate") - { - secondsleft = Convert.ToInt32(msg.Contents); - } - else if (msg.Name == "pong_mp_youwin") - { - this.Invoke(new Action(Win)); - } - } + // adjust y velocity based on where the ball hit the paddle + yVel = linear((ball.Top + (ball.Height / 2)), paddle.Top, paddle.Bottom, -1, 1); } - bool OpponentAgrees = false; - bool YouAgree = false; - - public void ListMatchmakes() + public void UpdateBall() { - lvotherplayers.Items.Clear(); - var c = new ColumnHeader(); - c.Width = lvotherplayers.Width; - c.Text = "Player"; - lvotherplayers.Columns.Clear(); - lvotherplayers.Columns.Add(c); - - lvotherplayers.FullRowSelect = true; - foreach (var itm in PossibleMatchmakes) + if (doBallCalc) { - if (itm != YouGUID) - { - var l = new ListViewItem(); - l.Text = itm; - lvotherplayers.Items.Add(l); - } - } + double ballXLocal = linear(ballX, -1.0, 1.0, 0, pnlcanvas.Width); + double ballYLocal = linear(ballY, -1.0, 1.0, 0, pnlcanvas.Height); - if (PossibleMatchmakes.Count > 0) - { - lbmpstatus.Text = "Select a player."; - } - else - { - lbmpstatus.Text = "Waiting for players..."; - } - } + ballXLocal -= ((double)paddleWidth / 2); + ballYLocal -= ((double)paddleWidth / 2); - public void NotifyLoseToTarget() - { - ServerManager.Forward(OpponentGUID, "pong_mp_youwin", null); - } + double aiBallXLocal = linear(aiBallX, -1.0, 1.0, 0, pnlcanvas.Width); + double aiBallYLocal = linear(aiBallY, -1.0, 1.0, 0, pnlcanvas.Height); - public void NotifyWinToTarget() - { - ServerManager.Forward(OpponentGUID, "pong_mp_youlose", null); - } + aiBallXLocal -= ((double)paddleWidth / 2); + aiBallYLocal -= ((double)paddleWidth / 2); - public void LeaveMatchmake() - { - ServerManager.Forward("all", "pong_handshake_left", YouGUID); - } - - List<string> PossibleMatchmakes = new List<string>(); - - public void SendLeaderGUID(string target) - { - ServerManager.Forward(target, "pong_handshake_chosen", YouGUID); - } + double playerYLocal = linear(playerY, -1.0, 1.0, 0, pnlcanvas.Height); + double opponentYLocal = linear(opponentY, -1.0, 1.0, 0, pnlcanvas.Height); - public void StartMultiplayer() - { - IsMultiplayerSession = true; - YouGUID = ServerManager.thisGuid.ToString(); - ServerManager.SendMessage("pong_handshake_matchmake", YouGUID); - StartMatchmake(); - } + int paddleHeight = pnlcanvas.Height / 5; - public void StartMatchmake() - { - pnlmultiplayerhandshake.Show(); - pnlmultiplayerhandshake.CenterParent(); - pnlmultiplayerhandshake.BringToFront(); - ServerManager.Forward("all", "pong_handshake_resendid", null); + Rectangle ballRect = new Rectangle((int)ballXLocal, (int)ballYLocal, paddleWidth, paddleWidth); - } + Rectangle aiBallRect = new Rectangle((int)aiBallXLocal, (int)aiBallYLocal, paddleWidth, paddleWidth); - public void SendFollowerGUID() - { - ServerManager.Forward(OpponentGUID, "pong_handshake_complete", YouGUID); - } + Rectangle playerRect = new Rectangle((int)paddleWidth, (int)(playerYLocal - (int)(paddleHeight / 2)), (int)paddleWidth, (int)paddleHeight); + Rectangle opponentRect = new Rectangle((int)(pnlcanvas.Width - (paddleWidth * 2)), (int)(opponentYLocal - (int)(paddleHeight / 2)), (int)paddleWidth, (int)paddleHeight); - public void LoseMP() - { - ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); - if(IsLeader) - if (xVel > 0) - xVel = -xVel; - lblbeatai.Show(); - lblbeatai.Text = "The opponent has beaten you!"; - tmrcountdown.Start(); - gameTimer.Stop(); - counter.Stop(); + //Top and bottom walls: + if (ballRect.Top <= 0 || ballRect.Bottom >= pnlcanvas.Height) + yVel = -yVel; //reverse the Y velocity - } + //top and bottom walls - ai + if (aiBallRect.Top <= 0 || aiBallRect.Bottom >= pnlcanvas.Height) + aiYVel = -aiYVel; //reverse the Y velocity - public void Win() - { - ball.Location = new Point(this.Size.Width / 2 + 200, this.Size.Height / 2); - paddleComputer.Location = new Point(paddleComputer.Location.X, ball.Location.Y); - if (xVel > 0) - xVel = -xVel; - beatairewardtotal = beatairewardtotal + beataireward; - lblbeatai.Show(); - lblbeatai.Text = Localization.Parse($"{{PONG_BEAT_AI_REWARD_SECONDARY}}: {beataireward}"); - tmrcountdown.Start(); - gameTimer.Stop(); - counter.Stop(); - } + //Left wall + if (ballRect.Left <= 0) + { + //You lose. + codepointsToEarn = 0; + Lose(); + } - public void CompleteLevel() - { - if (SaveSystem.CurrentSave.UniteAuthToken != null) - { - try + //Right wall + if (ballRect.Right >= pnlcanvas.Width) { - var unite = new ShiftOS.Unite.UniteClient("http://getshiftos.ml", SaveSystem.CurrentSave.UniteAuthToken); - if (unite.GetPongLevel() < level) - unite.SetPongLevel(level); + //You win. + codepointsToEarn += CalculateAIBeatCP(); + Win(); } - catch { } - } - //Only set these stats if the user is the leader. - if (IsLeader) - { - secondsleft = 60; - level = level + 1; - generatenextlevel(); - } - pnlgamestats.Show(); - pnlgamestats.BringToFront(); - pnlgamestats.Location = new Point((pgcontents.Width / 2) - (pnlgamestats.Width / 2), (pgcontents.Height / 2) - (pnlgamestats.Height / 2)); + //Enemy paddle: + if (ballRect.IntersectsWith(opponentRect)) + { + //check if the ball x is greater than the player paddle's middle coordinate + if (ballRect.Right >= opponentRect.Left) + { + Bounce(ballRect, opponentRect); + aiXVel = xVel; + aiYVel = yVel; + doAi = false; + } - counter.Stop(); - gameTimer.Stop(); + } - } - // ERROR: Handles clauses are not supported in C# - private void counter_Tick(object sender, EventArgs e) - { - if (IsLeader) - { - if (this.Left < Screen.PrimaryScreen.Bounds.Width) + //Enemy paddle - AI: + if (aiBallRect.IntersectsWith(opponentRect)) { - secondsleft = secondsleft - 1; - if (secondsleft == 0) + //check if the ball x is greater than the player paddle's middle coordinate + if (aiBallRect.Right >= opponentRect.Left) { - CompleteLevel(); + doAi = false; } - lblstatscodepoints.Text = "Codepoints: " + (levelrewards[level - 1] + beatairewardtotal).ToString(); } - } - SetupStats(); - } - [Obsolete("This method does nothing. Use UniteClient for highscore queries.")] - public void SendHighscores() - { - } - // ERROR: Handles clauses are not supported in C# - private void btnplayon_Click(object sender, EventArgs e) - { - xveldec = levelxspeed; - yveldec = levelyspeed; - - secondsleft = 60; + //Player paddle: + if (ballRect.IntersectsWith(playerRect)) + { + if (ballRect.Left <= playerRect.Right) + { + Bounce(ballRect, playerRect); + aiXVel = xVel; + aiYVel = yVel; + + //reset the ai location + aiBallX = ballX; + aiBallY = ballY; + doAi = true; + } - tmrcountdown.Start(); - lblbeatai.Text = Localization.Parse($"{{PONG_BEAT_AI_REWARD}}: {beataireward}"); - pnlgamestats.Hide(); - lblbeatai.Show(); - ball.Location = new Point(paddleHuman.Location.X + paddleHuman.Width + 50, paddleHuman.Location.Y + paddleHuman.Height / 2); - if (xVel < 0) - xVel = Math.Abs(xVel); - lbllevelandtime.Text = Localization.Parse("{LEVEL}: " + level + " - " + secondsleft + " {SECONDS_LEFT}"); - } + } - //Increase the ball speed stats for the next level - private void generatenextlevel() - { - lbllevelreached.Text = Localization.Parse("{YOU_REACHED_LEVEL} " + level + "!"); - lblpreviousstats.Text = Localization.Parse("{INITIAL_H_VEL}: " + levelxspeed + Environment.NewLine + "{INITIAL_V_VEL}: " + levelyspeed + Environment.NewLine + "{INC_H_VEL}: " + incrementx + Environment.NewLine + "{INC_V_VEL}: " + incrementy); - switch (rand.Next(1, 3)) - { - case 1: - levelxspeed = levelxspeed + 1; - break; - case 2: - levelxspeed = levelxspeed + 2; - break; - } - switch (rand.Next(1, 3)) - { - case 1: - levelyspeed = levelyspeed + 1; - break; - case 2: - levelyspeed = levelyspeed + 2; - break; - } - switch (rand.Next(1, 6)) - { - case 1: - incrementx = incrementx + 0.1; - break; - case 2: - incrementx = incrementx + 0.2; - break; - case 3: - incrementy = incrementy + 0.1; - break; - case 4: - incrementy = incrementy + 0.2; - break; - case 5: - incrementy = incrementy + 0.3; - break; - } + ballX += xVel * speedFactor; + ballY += yVel * speedFactor; - lblnextstats.Text = Localization.Parse("{INITIAL_H_VEL}: " + levelxspeed + Environment.NewLine + "{INITIAL_V_VEL}: " + levelyspeed + Environment.NewLine + "{INC_H_VEL}: " + incrementx + Environment.NewLine + "{INC_V_VEL}: " + incrementy); + aiBallX += aiXVel * (speedFactor * 1.5); + aiBallY += aiYVel * (speedFactor * 1.5); - if (level < 15) - { - if (ShiftoriumFrontend.UpgradeInstalled("pong_upgrade")) - { - beataireward = level * 10; - } else - { - beataireward = level * 5; - } - } - else - { - if (ShiftoriumFrontend.UpgradeInstalled("pong_upgrade")) - { - double br = levelrewards[level - 1] / 10; - beataireward = (int)Math.Round(br) * 10; - } else + if (doAi == true) { - double br = levelrewards[level - 1] / 10; - beataireward = (int)Math.Round(br) * 5; + if (opponentY != aiBallY) + { + if (opponentY < aiBallY) + { + if (opponentY < 0.9) + opponentY += speedFactor * level; + } + else + { + if (opponentY > -0.9) + opponentY -= speedFactor * level; + } + } } } - - totalreward = levelrewards[level - 1] + beatairewardtotal; - - btncashout.Text = Localization.Parse("{CASH_OUT_WITH_CODEPOINTS}"); - btnplayon.Text = Localization.Parse("{PONG_PLAY_ON_FOR_MORE}"); } - private void setuplevelrewards() + public void Lose() { - if (ShiftoriumFrontend.UpgradeInstalled("pong_upgrade")) - { - levelrewards[0] = 0; - levelrewards[1] = 40; - levelrewards[2] = 120; - levelrewards[3] = 280; - levelrewards[4] = 580; - levelrewards[5] = 800; - levelrewards[6] = 1200; - levelrewards[7] = 1800; - levelrewards[8] = 2400; - levelrewards[9] = 3200; - levelrewards[10] = 4000; - levelrewards[11] = 5000; - levelrewards[12] = 6000; - levelrewards[13] = 8000; - levelrewards[14] = 10000; - levelrewards[15] = 12000; - levelrewards[16] = 16000; - levelrewards[17] = 20000; - levelrewards[18] = 26000; - levelrewards[19] = 32000; - levelrewards[20] = 40000; - levelrewards[21] = 50000; - levelrewards[22] = 64000; - levelrewards[23] = 80000; - levelrewards[24] = 100000; - levelrewards[25] = 120000; - levelrewards[26] = 150000; - levelrewards[27] = 180000; - levelrewards[28] = 220000; - levelrewards[29] = 280000; - levelrewards[30] = 360000; - levelrewards[31] = 440000; - levelrewards[32] = 540000; - levelrewards[33] = 640000; - levelrewards[34] = 800000; - levelrewards[35] = 1000000; - levelrewards[36] = 1280000; - levelrewards[37] = 1600000; - levelrewards[38] = 2000000; - levelrewards[39] = 3000000; - levelrewards[40] = 4000000; - } else + InitializeCoordinates(); + counterTimer.Stop(); + secondsleft = 60; + level = 1; + speedFactor = 0.0125; + pnlgamestart.Show(); + pnlgamestart.BringToFront(); + pnlgamestart.CenterParent(); + Infobox.Show("{TITLE_PONG_YOULOSE}", "{PROMPT_PONGLOST}"); + doAi = false; + doBallCalc = false; + } + + public void LevelComplete() + { + level++; + doAi = false; + doBallCalc = false; + counterTimer.Stop(); + pnllevelwon.CenterParent(); + pnllevelwon.Show(); + pnllevelwon.BringToFront(); + lbltitle.Text = Localization.Parse("{PONG_LEVELREACHED}", new Dictionary<string, string> { - levelrewards[0] = 0; - levelrewards[1] = 20; - levelrewards[2] = 60; - levelrewards[3] = 140; - levelrewards[4] = 290; - levelrewards[5] = 400; - levelrewards[6] = 600; - levelrewards[7] = 900; - levelrewards[8] = 1200; - levelrewards[9] = 1600; - levelrewards[10] = 2000; - levelrewards[11] = 2500; - levelrewards[12] = 3000; - levelrewards[13] = 4000; - levelrewards[14] = 5000; - levelrewards[15] = 6000; - levelrewards[16] = 8000; - levelrewards[17] = 10000; - levelrewards[18] = 13000; - levelrewards[19] = 16000; - levelrewards[20] = 20000; - levelrewards[21] = 25000; - levelrewards[22] = 32000; - levelrewards[23] = 40000; - levelrewards[24] = 50000; - levelrewards[25] = 60000; - levelrewards[26] = 75000; - levelrewards[27] = 90000; - levelrewards[28] = 110000; - levelrewards[29] = 140000; - levelrewards[30] = 180000; - levelrewards[31] = 220000; - levelrewards[32] = 270000; - levelrewards[33] = 320000; - levelrewards[34] = 400000; - levelrewards[35] = 500000; - levelrewards[36] = 640000; - levelrewards[37] = 800000; - levelrewards[38] = 1000000; - levelrewards[39] = 1500000; - levelrewards[40] = 2000000; - } + ["%level"] = level.ToString() + }); + lbltitle.Height = (int)CreateGraphics().MeasureString(lbltitle.Text, lbltitle.Font).Height; + codepointsToEarn += CalculateAIBeatCP() * 2; + speedFactor += speedFactor / level; + secondsleft = 60; } - // ERROR: Handles clauses are not supported in C# - private void countdown_Tick(object sender, EventArgs e) + public long CalculateAIBeatCP() { - if (this.Left < Screen.PrimaryScreen.Bounds.Width) - { - switch (countdown) - { - case 0: - countdown = 3; - lblcountdown.Hide(); - lblbeatai.Hide(); - gameTimer.Start(); - counter.Start(); - tmrcountdown.Stop(); - break; - case 1: - lblcountdown.Text = "1"; - countdown = countdown - 1; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); - break; - case 2: - lblcountdown.Text = "2"; - countdown = countdown - 1; - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); - break; - case 3: - lblcountdown.Text = "3"; - countdown = countdown - 1; - lblcountdown.Show(); - ShiftOS.Engine.AudioManager.PlayStream(Properties.Resources.typesound); - break; - } - - } + return 2 * (10 * level); } - // ERROR: Handles clauses are not supported in C# - private void btncashout_Click(object sender, EventArgs e) + public void Win() { - pnlgamestats.Hide(); - pnlfinalstats.Show(); - lblfinalcodepointswithtext.Text = Localization.Parse("{YOU_WON} " + totalreward + " {CODEPOINTS}!"); - lblfinallevelreached.Text = Localization.Parse("{CODEPOINTS_FOR_BEATING_LEVEL}: ") + (level - 1).ToString(); - lblfinallevelreward.Text = levelrewards[level - 1].ToString(); - lblfinalcomputerreward.Text = beatairewardtotal.ToString(); - lblfinalcodepoints.Text = totalreward + Localization.Parse(" {CODEPOINTS_SHORT}"); - SaveSystem.TransferCodepointsFrom("pong", totalreward); - if (!string.IsNullOrWhiteSpace(SaveSystem.CurrentSave.UniteAuthToken)) + header = Localization.Parse("{PONG_BEATAI}", new Dictionary<string, string> { - var unite = new ShiftOS.Unite.UniteClient("http://getshiftos.ml", SaveSystem.CurrentSave.UniteAuthToken); - if (unite.GetPongCP() < totalreward) - { - unite.SetPongCP(totalreward); - } - } - if (IsMultiplayerSession) + ["%amount"] = CalculateAIBeatCP().ToString() + }); + InitializeCoordinates(); + counterTimer.Stop(); + new System.Threading.Thread(() => { - ServerManager.Forward(OpponentGUID, "pong_mp_cashedout", null); - StopMultiplayerSession(); - } + doBallCalc = false; + for (int i = 3; i > 0; i--) + { + counter = i.ToString(); + Engine.AudioManager.PlayStream(Properties.Resources.writesound); + System.Threading.Thread.Sleep(1000); + } + doBallCalc = true; + header = ""; + counter = ""; + Desktop.InvokeOnWorkerThread(() => + { + counterTimer.Start(); + }); + }).Start(); } - public void StopMultiplayerSession() + public void InitializeCoordinates() { - IsMultiplayerSession = false; - IsLeader = true; - OpponentGUID = ""; - YouGUID = ""; + ballX = 0; + ballY = 0; + opponentY = 0; + xVel = 1; + aiBallX = 0; + aiBallY = 0; + aiXVel = xVel; + aiYVel = yVel; + doAi = true; } - private void newgame() + private bool drawAiBall = true; + + private void pnlcanvas_Paint(object sender, PaintEventArgs e) { - pnlfinalstats.Hide(); - pnllose.Hide(); - pnlintro.Hide(); - level = 1; - totalreward = 0; - if (ShiftoriumFrontend.UpgradeInstalled("pong_upgrade")) - { - beataireward = 10; - } else - { - beataireward = 5; - } - beatairewardtotal = 0; - secondsleft = 60; - lblstatscodepoints.Text = Localization.Parse("{CODEPOINTS}: "); - //reset stats text - lblstatsX.Text = Localization.Parse("{H_VEL}: "); - lblstatsY.Text = Localization.Parse("{V_VEL}: "); - - levelxspeed = 3; - levelyspeed = 3; - - incrementx = 0.4; - incrementy = 0.2; - - xveldec = levelxspeed; - yveldec = levelyspeed; - - tmrcountdown.Start(); - lblbeatai.Text = Localization.Parse($"{{PONG_BEAT_AI_REWARD}}: {beataireward}"); - pnlgamestats.Hide(); - lblbeatai.Show(); - ball.Location = new Point(paddleHuman.Location.X + paddleHuman.Width + 50, (paddleHuman.Location.Y + paddleHuman.Height) / 2); - if (xVel < 0) - xVel = Math.Abs(xVel); - lbllevelandtime.Text = Localization.Parse("{{LEVEL}}: " + level + " - " + secondsleft + " {SECONDS_LEFT}"); - } + paddleWidth = pnlcanvas.Width / 30; + double ballXLocal = linear(ballX, -1.0, 1.0, 0, pnlcanvas.Width); + double ballYLocal = linear(ballY, -1.0, 1.0, 0, pnlcanvas.Height); - public void btnhighscore_Click(object s, EventArgs a) - { - pnlhighscore.BringToFront(); - SetupHighScores(); - } + ballXLocal -= ((double)paddleWidth / 2); + ballYLocal -= ((double)paddleWidth / 2); - bool IsMultiplayerSession = false; + double aiballXLocal = linear(aiBallX, -1.0, 1.0, 0, pnlcanvas.Width); + double aiballYLocal = linear(aiBallY, -1.0, 1.0, 0, pnlcanvas.Height); - string YouGUID = ""; - string OpponentGUID = ""; + aiballXLocal -= ((double)paddleWidth / 2); + aiballYLocal -= ((double)paddleWidth / 2); - public void SetupHighScores() - { - lbhighscore.Items.Clear(); - lbhighscore.View = View.Details; - lbhighscore.FullRowSelect = true; - lbhighscore.Columns.Clear(); - var n = new ColumnHeader(); - n.Text = "Player"; - n.Width = lbhighscore.Width / 3; - var l = new ColumnHeader(); - l.Text = "Level"; - l.Width = n.Width; - var c = new ColumnHeader(); - c.Text = "Codepoints"; - c.Width = n.Width; - lbhighscore.Columns.Add(n); - lbhighscore.Columns.Add(l); - lbhighscore.Columns.Add(c); - - var t = new Thread(() => - { - try - { - var unite = new ShiftOS.Unite.UniteClient("http://getshiftos.ml", SaveSystem.CurrentSave.UniteAuthToken); - var hs = unite.GetPongHighscores(); - foreach (var score in hs.Highscores) - { - if(this.ParentForm.Visible == false) - { - Thread.CurrentThread.Abort(); - } - string username = unite.GetDisplayNameId(score.UserId); - this.Invoke(new Action(() => - { - var name_item = new ListViewItem(); - name_item.Text = username; - lbhighscore.Items.Add(name_item); - name_item.SubItems.Add(score.Level.ToString()); - name_item.SubItems.Add(score.CodepointsCashout.ToString()); - })); - } - } - catch - { - try - { - if (this.ParentForm.Visible == true) - { - Infobox.Show("Service unavailable.", "The Pong Highscore service is unavailable at this time."); - this.Invoke(new Action(pnlgamestats.BringToFront)); - } - } - catch { } //JUST. ABORT. THE. FUCKING. THREAD. - return; - } - }); - t.Start(); - pnlhighscore.Show(); - } + e.Graphics.Clear(pnlcanvas.BackColor); - // ERROR: Handles clauses are not supported in C# - private void btnplayagain_Click(object sender, EventArgs e) - { - newgame(); - } + //draw the ai ball + if (drawAiBall) + e.Graphics.FillEllipse(new SolidBrush(Color.Gray), new RectangleF((float)aiballXLocal, (float)aiballYLocal, (float)paddleWidth, (float)paddleWidth)); - // ERROR: Handles clauses are not supported in C# - private void btnlosetryagain_Click(object sender, EventArgs e) - { - newgame(); - } - public void StartLevel() - { - newgame(); - } - - // ERROR: Handles clauses are not supported in C# - private void btnstartgame_Click(object sender, EventArgs e) - { - newgame(); - } + //draw the ball + if (doBallCalc) + e.Graphics.FillEllipse(new SolidBrush(pnlcanvas.ForeColor), new RectangleF((float)ballXLocal, (float)ballYLocal, (float)paddleWidth, (float)paddleWidth)); - Random rand = new Random(); - // ERROR: Handles clauses are not supported in C# - private void tmrstoryline_Tick(object sender, EventArgs e) - { - // Random chance of showing getshiftnet storyline - int i = rand.Next(0, 100); + double playerYLocal = linear(playerY, -1.0, 1.0, 0, pnlcanvas.Height); + double opponentYLocal = linear(opponentY, -1.0, 1.0, 0, pnlcanvas.Height); + + int paddleHeight = pnlcanvas.Height / 5; + + int paddleStart = paddleWidth; - if (i >= 25 && i <= 50) + //draw player paddle + e.Graphics.FillRectangle(new SolidBrush(pnlcanvas.ForeColor), new RectangleF((float)paddleWidth, (float)(playerYLocal - (float)(paddleHeight / 2)), (float)paddleWidth, (float)paddleHeight)); + + //draw opponent + e.Graphics.FillRectangle(new SolidBrush(pnlcanvas.ForeColor), new RectangleF((float)(pnlcanvas.Width - (paddleWidth*2)), (float)(opponentYLocal - (float)(paddleHeight / 2)), (float)paddleWidth, (float)paddleHeight)); + + string cp_text = Localization.Parse("{PONG_STATUSCP}", new Dictionary<string, string> { - tmrstoryline.Stop(); - } + ["%cp"] = codepointsToEarn.ToString() + }); - } + var tSize = e.Graphics.MeasureString(cp_text, SkinEngine.LoadedSkin.Header3Font); - // ERROR: Handles clauses are not supported in C# - private void me_closing(object sender, FormClosingEventArgs e) - { - tmrstoryline.Stop(); - } + var tLoc = new PointF((pnlcanvas.Width - (int)tSize.Width) / 2, + (pnlcanvas.Height - (int)tSize.Height) + + ); + e.Graphics.DrawString(cp_text, SkinEngine.LoadedSkin.Header3Font, new SolidBrush(pnlcanvas.ForeColor), tLoc); + tSize = e.Graphics.MeasureString(counter, SkinEngine.LoadedSkin.Header2Font); - private void Label6_Click(object sender, EventArgs e) - { + tLoc = new PointF((pnlcanvas.Width - (int)tSize.Width) / 2, + (pnlcanvas.Height - (int)tSize.Height) / 2 - } + ); + e.Graphics.DrawString(counter, SkinEngine.LoadedSkin.Header2Font, new SolidBrush(pnlcanvas.ForeColor), tLoc); + tSize = e.Graphics.MeasureString(header, SkinEngine.LoadedSkin.Header2Font); - private void Label8_Click(object sender, EventArgs e) - { + tLoc = new PointF((pnlcanvas.Width - (int)tSize.Width) / 2, + (pnlcanvas.Height - (int)tSize.Height) / 4 - } + ); + e.Graphics.DrawString(header, SkinEngine.LoadedSkin.Header2Font, new SolidBrush(pnlcanvas.ForeColor), tLoc); - private void pgcontents_Paint(object sender, PaintEventArgs e) { + string l = Localization.Parse("{PONG_STATUSLEVEL}", new Dictionary<string, string> + { + ["%level"] = level.ToString(), + ["%time"] = secondsleft.ToString() + }); + tSize = e.Graphics.MeasureString(l, SkinEngine.LoadedSkin.Header3Font); + + tLoc = new PointF((pnlcanvas.Width - (int)tSize.Width) / 2, + (tSize.Height) + ); + e.Graphics.DrawString(l, SkinEngine.LoadedSkin.Header3Font, new SolidBrush(pnlcanvas.ForeColor), tLoc); - } - private void ball_MouseEnter(object sender, EventArgs e) { - aiShouldIsbeEnabled = false; } - private void ball_MouseLeave(object sender, EventArgs e) { - aiShouldIsbeEnabled = true; + static public double linear(double x, double x0, double x1, double y0, double y1) + { + if ((x1 - x0) == 0) + { + return (y0 + y1) / 2; + } + return y0 + (x - x0) * (y1 - y0) / (x1 - x0); } + Timer drawTimer = null; + public void OnLoad() { - pnlintro.BringToFront(); - pnlintro.Show(); - pnlhighscore.Hide(); - pnlgamestats.Hide(); - pnlfinalstats.Hide(); - CenterPanels(); - lblbeatai.Hide(); - ServerManager.MessageReceived += this.ServerMessageReceivedHandler; + doAi = false; + doBallCalc = false; + pnlgamestart.Show(); + pnlgamestart.BringToFront(); + pnlgamestart.CenterParent(); + drawTimer.Start(); } public void OnSkinLoad() { - CenterPanels(); - this.SizeChanged += (o, a) => - { - CenterPanels(); - }; } public bool OnUnload() { - if(IsMultiplayerSession == true) - { - if(!string.IsNullOrWhiteSpace(OpponentGUID)) - ServerManager.Forward(OpponentGUID, "pong_mp_left", null); - LeaveMatchmake(); - } - ServerManager.MessageReceived -= this.ServerMessageReceivedHandler; - + drawTimer.Stop(); + counterTimer.Stop(); return true; } public void OnUpgrade() { - CenterPanels(); } - private void button2_Click(object sender, EventArgs e) + private void pnlcanvas_MouseMove(object sender, MouseEventArgs e) { - pnlhighscore.Hide(); + playerY = linear(e.Y, 0, pnlcanvas.Height, -1, 1); } - private void btnmatchmake_Click(object sender, EventArgs e) + private void button1_Click(object sender, EventArgs e) { - this.StartMultiplayer(); - pnlintro.Hide(); - lblbeatai.Text = "Beat the other player to earn Codepoints."; - lblcountdown.Text = "Waiting for another player..."; - lblcountdown.Left = (this.Width - lblcountdown.Width) / 2; + pnllevelwon.Hide(); + doAi = true; + doBallCalc = true; + counterTimer.Start(); } - private void lvotherplayers_DoubleClick(object sender, EventArgs e) + private void btncashout_Click(object sender, EventArgs e) { - if(lvotherplayers.SelectedItems.Count > 0) + pnllevelwon.Hide(); + SaveSystem.CurrentSave.Codepoints += (ulong)codepointsToEarn; + level = 1; + speedFactor = 0.0125; + Infobox.Show("{TITLE_CODEPOINTSTRANSFERRED}", Localization.Parse("{PROMPT_CODEPOINTSTRANSFERRED}", new Dictionary<string, string> { - SendLeaderGUID(lvotherplayers.SelectedItems[0].Text); - } + ["%transferrer"] = "Pong", + ["%amount"] = codepointsToEarn.ToString() + })); + codepointsToEarn = 0; + pnlgamestart.Show(); + pnlgamestart.BringToFront(); + pnlgamestart.CenterParent(); + + } + + private void button2_Click(object sender, EventArgs e) + { + counterTimer.Start(); + doAi = true; + doBallCalc = true; + pnlgamestart.Hide(); } } } |
