diff --git a/client/Ball.cs b/client/Ball.cs index 917159e..2ed1c43 100644 --- a/client/Ball.cs +++ b/client/Ball.cs @@ -1,6 +1,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended; +using MonoGame.Extended.Timers; using System; using System.Collections.Generic; using System.Linq; @@ -11,29 +12,36 @@ namespace client { internal class Ball { - private Vector2 Position { get; set; } - private int Radius { get; set; } - private float InitialSpeed { get; set; } - private float Acceleration { get; set; } - private int Delta { get; set; } // Delta time = milliseconds + public Vector2 Position = new Vector2(0, 0); + public Vector2 DefaultPosition = new Vector2(0,0); + public int Radius = 5; + public bool CanMove = false; + + // Movement + private float MoveX = 0f; + private float MoveY = 0f; + private float Speed = 100f; + + Random random = new Random(); // Constructors - public Ball(GraphicsDevice graphicsDevice) + public Ball(GraphicsDevice graphicsDevice, GameTime gameTime) { - this.Position = new Vector2(0,0); - this.Radius = 5; - this.InitialSpeed = 0.5f; - this.Acceleration = 0.1f; - this.Delta = 100; } - public Ball(Vector2 position, int radius, float initialSpeed, float acceleration, int delta) + public Ball(Vector2 position, int radius, float speed) { this.Position = position; + this.DefaultPosition = position; this.Radius = radius; - this.InitialSpeed = initialSpeed; - this.Acceleration = acceleration; - this.Delta = delta; + this.Speed = speed; + } + + // Default Position + public Ball GetBackToDefaultPos() + { + this.Position = this.DefaultPosition; + return this; } // Draw ball @@ -53,11 +61,64 @@ namespace client // Start | Stop ball moving public Ball StartMoving() { + this.MoveX = random.Next((int)-this.Speed, (int)this.Speed); + this.MoveY = random.Next((int)-this.Speed, (int)this.Speed); + this.CanMove = true; + return this; + } + + public Ball Move(GameTime gameTime, float screenHeight, float screenWidth, Player p1, Player p2) + { + if (this.CanMove) + { + // X + if (this.Position.X < 0) + { + this.MoveX -= this.Speed; + } + else + { + this.MoveX += this.Speed; + } + // Y + if (this.Position.Y < 0) + { + this.MoveY -= this.Speed; + } + else + { + this.MoveY += this.Speed; + } + + this.Position.X += this.MoveX; + this.Position.Y += this.MoveY; + + // Collisions + if (this.Position.Y < this.Radius*2 && this.MoveY < 0) + { + this.MoveY = Math.Abs(this.MoveY); + } + if (this.Position.Y > screenHeight - this.Radius*2 && this.MoveY > 0) + { + this.MoveY = -this.MoveY; + } + + if ( + (this.Position.X < p1.Position.X + p1.width && this.Position.X > p1.Position.X && this.Position.Y > p1.Position.Y && this.Position.Y < p1.Position.Y + p1.height) || + (this.Position.X+ this.Radius*2 > p2.Position.X && this.Position.X + this.Radius*2 < p2.Position.X + p2.width && this.Position.Y > p2.Position.Y && this.Position.Y < p2.Position.Y + p2.height) + ) + { + this.MoveX = -this.MoveX; + } + + } + return this; } public Ball StopMoving() { + this.CanMove = false; return this; } } diff --git a/client/Game1.cs b/client/Game1.cs index 646d3f6..3d31fd6 100644 --- a/client/Game1.cs +++ b/client/Game1.cs @@ -9,6 +9,8 @@ namespace client private Player pl1; private Player pl2; private Ball ball; + private KillZone killZone1; + private KillZone killZone2; private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; @@ -28,7 +30,7 @@ namespace client 15, 100, 100f - ); + ).SetControllable(true); pl2 = new Player( new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50), @@ -41,11 +43,27 @@ namespace client ball = new Ball( new Vector2(_graphics.PreferredBackBufferWidth / 2 - 5, _graphics.PreferredBackBufferHeight / 2 - 5), 10, - 0.5f, - 0.1f, - 100 + 0.2f ); + // KILLZONE + killZone1 = new KillZone( + new Vector2(0, 0), + 15, + _graphics.PreferredBackBufferHeight + ); + + killZone2 = new KillZone( + new Vector2(_graphics.PreferredBackBufferWidth - 15, 0), + 15, + _graphics.PreferredBackBufferHeight + ); + killZone2.isLeft = false; + + + // TEST + ball.StartMoving(); + base.Initialize(); } @@ -56,9 +74,13 @@ namespace client protected override void Update(GameTime gameTime) { - /*if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) - Exit();*/ - + pl1.InputsControls(gameTime, _graphics.PreferredBackBufferHeight); + + ball.Move(gameTime, _graphics.PreferredBackBufferHeight, _graphics.PreferredBackBufferWidth, pl1, pl2); + + killZone1.Collisions(ball); + killZone2.Collisions(ball); + base.Update(gameTime); } @@ -72,6 +94,9 @@ namespace client ball.Draw(_spriteBatch, GraphicsDevice); + killZone1.Draw(_spriteBatch, GraphicsDevice); + killZone2.Draw(_spriteBatch, GraphicsDevice); + _spriteBatch.End(); base.Draw(gameTime); } diff --git a/client/KillZone.cs b/client/KillZone.cs new file mode 100644 index 0000000..4fa61c0 --- /dev/null +++ b/client/KillZone.cs @@ -0,0 +1,64 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace client +{ + internal class KillZone + { + private Vector2 Position; + private int Width; + private int Height; + public bool isLeft = true; + + public bool Debug = false; + + public KillZone(Vector2 position, int width, int height) + { + Position = position; + Width = width; + Height = height; + } + + public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice) + { + Texture2D texture = new Texture2D(graphicsDevice, this.Width, this.Height); + Color[] data = new Color[this.Width * this.Height]; + for (int i = 0; i < data.Length; ++i) + { + if (Debug) + { + data[i] = Color.Red; + } else + { + data[i] = Color.Transparent; + } + } + texture.SetData(data); + spriteBatch.Draw(texture, this.Position, Color.White); + } + + public void Collisions(Ball ball) + { + if (this.isLeft) + { + if (ball.Position.X < this.Position.X + this.Width && ball.CanMove) + { + ball.StopMoving(); + } + } + else + { + if (ball.Position.X > this.Position.X && ball.CanMove) + { + ball.StopMoving(); + } + } + } + } +} diff --git a/client/Player.cs b/client/Player.cs index 0663efe..543d9fc 100644 --- a/client/Player.cs +++ b/client/Player.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; using System; using System.Collections.Generic; using System.Linq; @@ -11,7 +12,7 @@ namespace client { internal class Player { - private Vector2 Position { get; set; } + public Vector2 Position { get; set; } private Vector2 DefaultPosition { get; set; } public int width; public int height; @@ -62,6 +63,13 @@ namespace client return this; } + // Set controllable by input + public Player SetControllable(bool state) + { + this.canMove = state; + return this; + } + // Draw player on screen public Player DrawPlayer(SpriteBatch spriteBatch, GraphicsDevice _graphicsDevice) { @@ -76,5 +84,33 @@ namespace client spriteBatch.Draw(texture, this.Position, Color.White); return this; } + + // Inputs controls + public Player InputsControls(GameTime gameTime, float screenHeight) + { + if (this.canMove) + { + if (Keyboard.GetState().IsKeyDown(Keys.Up)) + { + Vector2 asked = new Vector2(this.Position.X, this.Position.Y - this.speed * (float)gameTime.ElapsedGameTime.TotalSeconds); + if (asked.Y <= 0) + { + asked.Y = 0f; + } + this.Position = asked; + } + + if (Keyboard.GetState().IsKeyDown(Keys.Down)) + { + Vector2 asked = new Vector2(this.Position.X, this.Position.Y + this.speed * (float)gameTime.ElapsedGameTime.TotalSeconds); + if (asked.Y >= screenHeight - this.height) + { + asked.Y = screenHeight - this.height; + } + this.Position = asked; + } + } + return this; + } } }