+ Base of a game UI
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MonoGame.Extended;
|
using MonoGame.Extended;
|
||||||
using MonoGame.Extended.Collisions;
|
using MonoGame.Extended.Collisions;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace client
|
namespace client
|
||||||
{
|
{
|
||||||
@@ -56,6 +57,15 @@ namespace client
|
|||||||
// Collisions
|
// Collisions
|
||||||
public void OnCollision(CollisionEventArgs collisionInfo)
|
public void OnCollision(CollisionEventArgs collisionInfo)
|
||||||
{
|
{
|
||||||
|
if (collisionInfo.Other.GetType().Equals(typeof(ScreenBounds)))
|
||||||
|
{
|
||||||
|
Velocity.Y *= -1;
|
||||||
|
}
|
||||||
|
else if (collisionInfo.Other.GetType().Equals(typeof(Player)))
|
||||||
|
{
|
||||||
|
Velocity.X *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
Bounds.Position -= collisionInfo.PenetrationVector;
|
Bounds.Position -= collisionInfo.PenetrationVector;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-19
@@ -28,12 +28,16 @@ namespace client
|
|||||||
*/
|
*/
|
||||||
string gameState = "None";
|
string gameState = "None";
|
||||||
/*
|
/*
|
||||||
1 => Player 1
|
0 => Player 1
|
||||||
2 => Player 2
|
1 => Player 2
|
||||||
3 => Spectator (default)
|
2 => Spectator (default)
|
||||||
*/
|
*/
|
||||||
int controller = 2;
|
int controller = 2;
|
||||||
|
|
||||||
|
// Game params
|
||||||
|
int score1 = 0;
|
||||||
|
int score2 = 0;
|
||||||
|
|
||||||
// Game instances
|
// Game instances
|
||||||
private Player pl1;
|
private Player pl1;
|
||||||
private Player pl2;
|
private Player pl2;
|
||||||
@@ -49,6 +53,10 @@ namespace client
|
|||||||
private CollisionComponent _collisionComponent;
|
private CollisionComponent _collisionComponent;
|
||||||
private readonly List<IEntity> _entities = new List<IEntity>();
|
private readonly List<IEntity> _entities = new List<IEntity>();
|
||||||
|
|
||||||
|
// UIs
|
||||||
|
private GameUI _gameUI;
|
||||||
|
private SpriteFont gameFont;
|
||||||
|
|
||||||
public Game1()
|
public Game1()
|
||||||
{
|
{
|
||||||
_graphics = new GraphicsDeviceManager(this);
|
_graphics = new GraphicsDeviceManager(this);
|
||||||
@@ -105,6 +113,7 @@ namespace client
|
|||||||
pl1 = new Player(
|
pl1 = new Player(
|
||||||
new RectangleF(new Point(15, _graphics.PreferredBackBufferHeight / 2 - 50), new Size2(15, 100)),
|
new RectangleF(new Point(15, _graphics.PreferredBackBufferHeight / 2 - 50), new Size2(15, 100)),
|
||||||
_graphics.PreferredBackBufferHeight,
|
_graphics.PreferredBackBufferHeight,
|
||||||
|
100,
|
||||||
processor
|
processor
|
||||||
);
|
);
|
||||||
_entities.Add(pl1);
|
_entities.Add(pl1);
|
||||||
@@ -112,41 +121,39 @@ namespace client
|
|||||||
pl2 = new Player(
|
pl2 = new Player(
|
||||||
new RectangleF(new Point(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50), new Size2(15, 100)),
|
new RectangleF(new Point(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50), new Size2(15, 100)),
|
||||||
_graphics.PreferredBackBufferHeight,
|
_graphics.PreferredBackBufferHeight,
|
||||||
|
100,
|
||||||
processor
|
processor
|
||||||
);
|
);
|
||||||
_entities.Add(pl2);
|
_entities.Add(pl2);
|
||||||
|
|
||||||
// BALL
|
// BALL
|
||||||
ball = new Ball(
|
ball = new Ball(
|
||||||
new CircleF(new Point(_graphics.PreferredBackBufferWidth / 2 - 10, _graphics.PreferredBackBufferHeight / 2 - 10), 10)
|
new CircleF(new Point(_graphics.PreferredBackBufferWidth / 2, _graphics.PreferredBackBufferHeight / 2), 10)
|
||||||
);
|
);
|
||||||
_entities.Add(ball);
|
_entities.Add(ball);
|
||||||
|
|
||||||
// SCREEN BOUNDS
|
// SCREEN BOUNDS
|
||||||
sb1 = new ScreenBounds(
|
sb1 = new ScreenBounds(
|
||||||
new RectangleF(new Point(0, -1), new Size2(_graphics.PreferredBackBufferWidth, 1))
|
new RectangleF(new Point(0, -1), new Size2(_graphics.PreferredBackBufferWidth, 2))
|
||||||
);
|
);
|
||||||
_entities.Add(sb1);
|
_entities.Add(sb1);
|
||||||
sb2 = new ScreenBounds(
|
sb2 = new ScreenBounds(
|
||||||
new RectangleF(new Point(0, _graphics.PreferredBackBufferHeight), new Size2(_graphics.PreferredBackBufferWidth, 1))
|
new RectangleF(new Point(0, _graphics.PreferredBackBufferHeight - 1), new Size2(_graphics.PreferredBackBufferWidth, 2))
|
||||||
);
|
);
|
||||||
_entities.Add(sb2);
|
_entities.Add(sb2);
|
||||||
|
|
||||||
// KILLZONE
|
// KILLZONE
|
||||||
/*killZone1 = new KillZone(
|
killZone1 = new KillZone(
|
||||||
new Vector2(0, 0),
|
new RectangleF(new Point(-1, 0), new Size2(2, _graphics.PreferredBackBufferHeight)),
|
||||||
15,
|
processor
|
||||||
_graphics.PreferredBackBufferHeight
|
|
||||||
);
|
);
|
||||||
_entities.Add(killZone1);
|
_entities.Add(killZone1);
|
||||||
|
|
||||||
killZone2 = new KillZone(
|
killZone2 = new KillZone(
|
||||||
new Vector2(_graphics.PreferredBackBufferWidth - 15, 0),
|
new RectangleF(new Point(_graphics.PreferredBackBufferWidth - 1, 0), new Size2(2, _graphics.PreferredBackBufferHeight)),
|
||||||
15,
|
processor
|
||||||
_graphics.PreferredBackBufferHeight
|
);
|
||||||
);
|
_entities.Add(killZone2);
|
||||||
killZone2.isLeft = false;
|
|
||||||
_entities.Add(killZone2);*/
|
|
||||||
|
|
||||||
|
|
||||||
foreach (IEntity actor in _entities)
|
foreach (IEntity actor in _entities)
|
||||||
@@ -154,18 +161,25 @@ namespace client
|
|||||||
_collisionComponent.Insert(actor);
|
_collisionComponent.Insert(actor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UI
|
||||||
|
_gameUI = new();
|
||||||
|
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnExiting(object sender, EventArgs args)
|
protected override void OnExiting(object sender, EventArgs args)
|
||||||
{
|
{
|
||||||
client.DisconnectPeer(_server);
|
if (_server != null)
|
||||||
|
{
|
||||||
|
client.DisconnectPeer(_server);
|
||||||
|
}
|
||||||
base.OnExiting(sender, args);
|
base.OnExiting(sender, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void LoadContent()
|
protected override void LoadContent()
|
||||||
{
|
{
|
||||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||||
|
gameFont = Content.Load<SpriteFont>("scoreFont");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
@@ -175,6 +189,8 @@ namespace client
|
|||||||
|
|
||||||
pl1.UpdateStats(controller, gameState, _server);
|
pl1.UpdateStats(controller, gameState, _server);
|
||||||
pl2.UpdateStats(controller, gameState, _server);
|
pl2.UpdateStats(controller, gameState, _server);
|
||||||
|
killZone1.UpdateStat(_server, gameState);
|
||||||
|
killZone2.UpdateStat(_server, gameState);
|
||||||
foreach (IEntity entity in _entities)
|
foreach (IEntity entity in _entities)
|
||||||
{
|
{
|
||||||
entity.Update(gameTime);
|
entity.Update(gameTime);
|
||||||
@@ -182,6 +198,8 @@ namespace client
|
|||||||
|
|
||||||
_collisionComponent.Update(gameTime);
|
_collisionComponent.Update(gameTime);
|
||||||
|
|
||||||
|
_gameUI.Update(score1, score2);
|
||||||
|
|
||||||
base.Update(gameTime);
|
base.Update(gameTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,6 +213,8 @@ namespace client
|
|||||||
entitiy.Draw(_spriteBatch);
|
entitiy.Draw(_spriteBatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_gameUI.Draw(_spriteBatch, gameFont, new Size2(_graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight));
|
||||||
|
|
||||||
_spriteBatch.End();
|
_spriteBatch.End();
|
||||||
base.Draw(gameTime);
|
base.Draw(gameTime);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MonoGame.Extended;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace client
|
||||||
|
{
|
||||||
|
internal class GameUI
|
||||||
|
{
|
||||||
|
private int score1;
|
||||||
|
private int score2;
|
||||||
|
|
||||||
|
public GameUI() { }
|
||||||
|
|
||||||
|
public void Draw(SpriteBatch spriteBatch, SpriteFont font, Size2 windowSize)
|
||||||
|
{
|
||||||
|
// Mid lines
|
||||||
|
for(int i = 0; i < windowSize.Height / 21; i++)
|
||||||
|
{
|
||||||
|
if (i%2==0)
|
||||||
|
{
|
||||||
|
spriteBatch.DrawLine(
|
||||||
|
new Vector2(windowSize.Width / 2, windowSize.Height / 21 * i),
|
||||||
|
new Vector2(windowSize.Width / 2, windowSize.Height / 21 * (i+1)),
|
||||||
|
Color.White
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scores
|
||||||
|
int spacing = 25;
|
||||||
|
spriteBatch.DrawString(font, $"{score1}", new Vector2(windowSize.Width / 2 - font.MeasureString($"{score1}").X - spacing, 5), Color.LightGray);
|
||||||
|
spriteBatch.DrawString(font, $"{score2}", new Vector2(windowSize.Width / 2 + spacing, 5), Color.LightGray);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(int _score1, int _score2)
|
||||||
|
{
|
||||||
|
score1 = _score1;
|
||||||
|
score2 = _score2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
-48
@@ -1,65 +1,46 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using LiteNetLib;
|
||||||
|
using LiteNetLib.Utils;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using System;
|
using MonoGame.Extended;
|
||||||
using System.Collections.Generic;
|
using MonoGame.Extended.Collisions;
|
||||||
using System.Diagnostics;
|
using shared;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace client
|
namespace client
|
||||||
{
|
{
|
||||||
internal class KillZone
|
internal class KillZone : IEntity
|
||||||
{
|
{
|
||||||
private Vector2 Position;
|
public IShapeF Bounds { get; }
|
||||||
private int Width;
|
private NetPacketProcessor _processor;
|
||||||
private int Height;
|
private NetPeer _server;
|
||||||
public bool isLeft = true;
|
private string _gameState;
|
||||||
|
|
||||||
public bool Debug = false;
|
public KillZone(RectangleF rectangleF, NetPacketProcessor processor)
|
||||||
|
|
||||||
public KillZone(Vector2 position, int width, int height)
|
|
||||||
{
|
{
|
||||||
Position = position;
|
Bounds = rectangleF;
|
||||||
Width = width;
|
_processor = processor;
|
||||||
Height = height;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
|
public void Draw(SpriteBatch spriteBatch)
|
||||||
{
|
{
|
||||||
Texture2D texture = new Texture2D(graphicsDevice, Width, Height);
|
spriteBatch.DrawRectangle((RectangleF)Bounds, Color.Transparent);
|
||||||
Color[] data = new Color[Width * 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, Position, Color.White);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public void Collisions(Ball ball)
|
public void Update(GameTime gameTime) { }
|
||||||
|
|
||||||
|
public void OnCollision(CollisionEventArgs collisionInfos)
|
||||||
{
|
{
|
||||||
if (isLeft)
|
if (collisionInfos.Other.GetType().Equals(typeof(Ball)) && _gameState != "Ended")
|
||||||
{
|
{
|
||||||
if (ball.Position.X < Position.X + Width && ball.CanMove)
|
GameStateChange packet = new() { gameState = "Ended" };
|
||||||
{
|
_processor.Send(_server, packet, DeliveryMethod.ReliableOrdered);
|
||||||
//ball.StopMoving();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
if (ball.Position.X > Position.X && ball.CanMove)
|
public void UpdateStat(NetPeer server, string gameState)
|
||||||
{
|
{
|
||||||
//ball.StopMoving();
|
_server = server;
|
||||||
}
|
_gameState = gameState;
|
||||||
}
|
}
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -15,6 +15,7 @@ namespace client
|
|||||||
public Vector2 Velocity;
|
public Vector2 Velocity;
|
||||||
public IShapeF Bounds { get; }
|
public IShapeF Bounds { get; }
|
||||||
public float _screenHeight;
|
public float _screenHeight;
|
||||||
|
public float _height;
|
||||||
|
|
||||||
public bool canMove = false;
|
public bool canMove = false;
|
||||||
private Vector2 DefaultPosition { get; set; }
|
private Vector2 DefaultPosition { get; set; }
|
||||||
@@ -26,10 +27,11 @@ namespace client
|
|||||||
string _gameState = "None";
|
string _gameState = "None";
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
public Player(RectangleF rectangleF, float screenHeight, NetPacketProcessor processor)
|
public Player(RectangleF rectangleF, float screenHeight, float height, NetPacketProcessor processor)
|
||||||
{
|
{
|
||||||
Bounds = rectangleF;
|
Bounds = rectangleF;
|
||||||
_screenHeight = screenHeight;
|
_screenHeight = screenHeight;
|
||||||
|
_height = height;
|
||||||
DefaultPosition = Bounds.Position;
|
DefaultPosition = Bounds.Position;
|
||||||
_processor = processor;
|
_processor = processor;
|
||||||
|
|
||||||
@@ -61,9 +63,9 @@ namespace client
|
|||||||
if (Keyboard.GetState().IsKeyDown(Keys.Down))
|
if (Keyboard.GetState().IsKeyDown(Keys.Down))
|
||||||
{
|
{
|
||||||
float asked = Bounds.Position.Y + Velocity.Y * gameTime.GetElapsedSeconds() * 50;
|
float asked = Bounds.Position.Y + Velocity.Y * gameTime.GetElapsedSeconds() * 50;
|
||||||
if (asked >= _screenHeight)
|
if (asked >= _screenHeight - _height)
|
||||||
{
|
{
|
||||||
asked = _screenHeight;
|
asked = _screenHeight - _height;
|
||||||
}
|
}
|
||||||
Bounds.Position = new Vector2(Bounds.Position.X, asked);
|
Bounds.Position = new Vector2(Bounds.Position.X, asked);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user