+ KillZone & Ball Movements (bases)
This commit is contained in:
+76
-15
@@ -1,6 +1,7 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MonoGame.Extended;
|
using MonoGame.Extended;
|
||||||
|
using MonoGame.Extended.Timers;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -11,29 +12,36 @@ namespace client
|
|||||||
{
|
{
|
||||||
internal class Ball
|
internal class Ball
|
||||||
{
|
{
|
||||||
private Vector2 Position { get; set; }
|
public Vector2 Position = new Vector2(0, 0);
|
||||||
private int Radius { get; set; }
|
public Vector2 DefaultPosition = new Vector2(0,0);
|
||||||
private float InitialSpeed { get; set; }
|
public int Radius = 5;
|
||||||
private float Acceleration { get; set; }
|
public bool CanMove = false;
|
||||||
private int Delta { get; set; } // Delta time = milliseconds
|
|
||||||
|
// Movement
|
||||||
|
private float MoveX = 0f;
|
||||||
|
private float MoveY = 0f;
|
||||||
|
private float Speed = 100f;
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
// Constructors
|
// 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.Position = position;
|
||||||
|
this.DefaultPosition = position;
|
||||||
this.Radius = radius;
|
this.Radius = radius;
|
||||||
this.InitialSpeed = initialSpeed;
|
this.Speed = speed;
|
||||||
this.Acceleration = acceleration;
|
}
|
||||||
this.Delta = delta;
|
|
||||||
|
// Default Position
|
||||||
|
public Ball GetBackToDefaultPos()
|
||||||
|
{
|
||||||
|
this.Position = this.DefaultPosition;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw ball
|
// Draw ball
|
||||||
@@ -53,11 +61,64 @@ namespace client
|
|||||||
// Start | Stop ball moving
|
// Start | Stop ball moving
|
||||||
public Ball StartMoving()
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ball StopMoving()
|
public Ball StopMoving()
|
||||||
{
|
{
|
||||||
|
this.CanMove = false;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-6
@@ -9,6 +9,8 @@ namespace client
|
|||||||
private Player pl1;
|
private Player pl1;
|
||||||
private Player pl2;
|
private Player pl2;
|
||||||
private Ball ball;
|
private Ball ball;
|
||||||
|
private KillZone killZone1;
|
||||||
|
private KillZone killZone2;
|
||||||
|
|
||||||
private GraphicsDeviceManager _graphics;
|
private GraphicsDeviceManager _graphics;
|
||||||
private SpriteBatch _spriteBatch;
|
private SpriteBatch _spriteBatch;
|
||||||
@@ -28,7 +30,7 @@ namespace client
|
|||||||
15,
|
15,
|
||||||
100,
|
100,
|
||||||
100f
|
100f
|
||||||
);
|
).SetControllable(true);
|
||||||
|
|
||||||
pl2 = new Player(
|
pl2 = new Player(
|
||||||
new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50),
|
new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50),
|
||||||
@@ -41,11 +43,27 @@ namespace client
|
|||||||
ball = new Ball(
|
ball = new Ball(
|
||||||
new Vector2(_graphics.PreferredBackBufferWidth / 2 - 5, _graphics.PreferredBackBufferHeight / 2 - 5),
|
new Vector2(_graphics.PreferredBackBufferWidth / 2 - 5, _graphics.PreferredBackBufferHeight / 2 - 5),
|
||||||
10,
|
10,
|
||||||
0.5f,
|
0.2f
|
||||||
0.1f,
|
|
||||||
100
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 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();
|
base.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,8 +74,12 @@ namespace client
|
|||||||
|
|
||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
/*if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
pl1.InputsControls(gameTime, _graphics.PreferredBackBufferHeight);
|
||||||
Exit();*/
|
|
||||||
|
ball.Move(gameTime, _graphics.PreferredBackBufferHeight, _graphics.PreferredBackBufferWidth, pl1, pl2);
|
||||||
|
|
||||||
|
killZone1.Collisions(ball);
|
||||||
|
killZone2.Collisions(ball);
|
||||||
|
|
||||||
base.Update(gameTime);
|
base.Update(gameTime);
|
||||||
}
|
}
|
||||||
@@ -72,6 +94,9 @@ namespace client
|
|||||||
|
|
||||||
ball.Draw(_spriteBatch, GraphicsDevice);
|
ball.Draw(_spriteBatch, GraphicsDevice);
|
||||||
|
|
||||||
|
killZone1.Draw(_spriteBatch, GraphicsDevice);
|
||||||
|
killZone2.Draw(_spriteBatch, GraphicsDevice);
|
||||||
|
|
||||||
_spriteBatch.End();
|
_spriteBatch.End();
|
||||||
base.Draw(gameTime);
|
base.Draw(gameTime);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+37
-1
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using Microsoft.Xna.Framework.Input;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -11,7 +12,7 @@ namespace client
|
|||||||
{
|
{
|
||||||
internal class Player
|
internal class Player
|
||||||
{
|
{
|
||||||
private Vector2 Position { get; set; }
|
public Vector2 Position { get; set; }
|
||||||
private Vector2 DefaultPosition { get; set; }
|
private Vector2 DefaultPosition { get; set; }
|
||||||
public int width;
|
public int width;
|
||||||
public int height;
|
public int height;
|
||||||
@@ -62,6 +63,13 @@ namespace client
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set controllable by input
|
||||||
|
public Player SetControllable(bool state)
|
||||||
|
{
|
||||||
|
this.canMove = state;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
// Draw player on screen
|
// Draw player on screen
|
||||||
public Player DrawPlayer(SpriteBatch spriteBatch, GraphicsDevice _graphicsDevice)
|
public Player DrawPlayer(SpriteBatch spriteBatch, GraphicsDevice _graphicsDevice)
|
||||||
{
|
{
|
||||||
@@ -76,5 +84,33 @@ namespace client
|
|||||||
spriteBatch.Draw(texture, this.Position, Color.White);
|
spriteBatch.Draw(texture, this.Position, Color.White);
|
||||||
return this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user