+ Base of a game UI
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.Collisions;
|
||||
using System;
|
||||
|
||||
namespace client
|
||||
{
|
||||
@@ -56,6 +57,15 @@ namespace client
|
||||
// Collisions
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
+35
-15
@@ -28,12 +28,16 @@ namespace client
|
||||
*/
|
||||
string gameState = "None";
|
||||
/*
|
||||
1 => Player 1
|
||||
2 => Player 2
|
||||
3 => Spectator (default)
|
||||
0 => Player 1
|
||||
1 => Player 2
|
||||
2 => Spectator (default)
|
||||
*/
|
||||
int controller = 2;
|
||||
|
||||
// Game params
|
||||
int score1 = 0;
|
||||
int score2 = 0;
|
||||
|
||||
// Game instances
|
||||
private Player pl1;
|
||||
private Player pl2;
|
||||
@@ -49,6 +53,10 @@ namespace client
|
||||
private CollisionComponent _collisionComponent;
|
||||
private readonly List<IEntity> _entities = new List<IEntity>();
|
||||
|
||||
// UIs
|
||||
private GameUI _gameUI;
|
||||
private SpriteFont gameFont;
|
||||
|
||||
public Game1()
|
||||
{
|
||||
_graphics = new GraphicsDeviceManager(this);
|
||||
@@ -105,6 +113,7 @@ namespace client
|
||||
pl1 = new Player(
|
||||
new RectangleF(new Point(15, _graphics.PreferredBackBufferHeight / 2 - 50), new Size2(15, 100)),
|
||||
_graphics.PreferredBackBufferHeight,
|
||||
100,
|
||||
processor
|
||||
);
|
||||
_entities.Add(pl1);
|
||||
@@ -112,41 +121,39 @@ namespace client
|
||||
pl2 = new Player(
|
||||
new RectangleF(new Point(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50), new Size2(15, 100)),
|
||||
_graphics.PreferredBackBufferHeight,
|
||||
100,
|
||||
processor
|
||||
);
|
||||
_entities.Add(pl2);
|
||||
|
||||
// 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);
|
||||
|
||||
// SCREEN BOUNDS
|
||||
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);
|
||||
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);
|
||||
|
||||
// KILLZONE
|
||||
/*killZone1 = new KillZone(
|
||||
new Vector2(0, 0),
|
||||
15,
|
||||
_graphics.PreferredBackBufferHeight
|
||||
killZone1 = new KillZone(
|
||||
new RectangleF(new Point(-1, 0), new Size2(2, _graphics.PreferredBackBufferHeight)),
|
||||
processor
|
||||
);
|
||||
_entities.Add(killZone1);
|
||||
|
||||
killZone2 = new KillZone(
|
||||
new Vector2(_graphics.PreferredBackBufferWidth - 15, 0),
|
||||
15,
|
||||
_graphics.PreferredBackBufferHeight
|
||||
new RectangleF(new Point(_graphics.PreferredBackBufferWidth - 1, 0), new Size2(2, _graphics.PreferredBackBufferHeight)),
|
||||
processor
|
||||
);
|
||||
killZone2.isLeft = false;
|
||||
_entities.Add(killZone2);*/
|
||||
_entities.Add(killZone2);
|
||||
|
||||
|
||||
foreach (IEntity actor in _entities)
|
||||
@@ -154,18 +161,25 @@ namespace client
|
||||
_collisionComponent.Insert(actor);
|
||||
}
|
||||
|
||||
// UI
|
||||
_gameUI = new();
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
protected override void OnExiting(object sender, EventArgs args)
|
||||
{
|
||||
if (_server != null)
|
||||
{
|
||||
client.DisconnectPeer(_server);
|
||||
}
|
||||
base.OnExiting(sender, args);
|
||||
}
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
gameFont = Content.Load<SpriteFont>("scoreFont");
|
||||
}
|
||||
|
||||
protected override void Update(GameTime gameTime)
|
||||
@@ -175,6 +189,8 @@ namespace client
|
||||
|
||||
pl1.UpdateStats(controller, gameState, _server);
|
||||
pl2.UpdateStats(controller, gameState, _server);
|
||||
killZone1.UpdateStat(_server, gameState);
|
||||
killZone2.UpdateStat(_server, gameState);
|
||||
foreach (IEntity entity in _entities)
|
||||
{
|
||||
entity.Update(gameTime);
|
||||
@@ -182,6 +198,8 @@ namespace client
|
||||
|
||||
_collisionComponent.Update(gameTime);
|
||||
|
||||
_gameUI.Update(score1, score2);
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
@@ -195,6 +213,8 @@ namespace client
|
||||
entitiy.Draw(_spriteBatch);
|
||||
}
|
||||
|
||||
_gameUI.Draw(_spriteBatch, gameFont, new Size2(_graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight));
|
||||
|
||||
_spriteBatch.End();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
-47
@@ -1,65 +1,46 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using LiteNetLib;
|
||||
using LiteNetLib.Utils;
|
||||
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;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.Collisions;
|
||||
using shared;
|
||||
|
||||
namespace client
|
||||
{
|
||||
internal class KillZone
|
||||
internal class KillZone : IEntity
|
||||
{
|
||||
private Vector2 Position;
|
||||
private int Width;
|
||||
private int Height;
|
||||
public bool isLeft = true;
|
||||
public IShapeF Bounds { get; }
|
||||
private NetPacketProcessor _processor;
|
||||
private NetPeer _server;
|
||||
private string _gameState;
|
||||
|
||||
public bool Debug = false;
|
||||
|
||||
public KillZone(Vector2 position, int width, int height)
|
||||
public KillZone(RectangleF rectangleF, NetPacketProcessor processor)
|
||||
{
|
||||
Position = position;
|
||||
Width = width;
|
||||
Height = height;
|
||||
Bounds = rectangleF;
|
||||
_processor = processor;
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
|
||||
public void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
Texture2D texture = new Texture2D(graphicsDevice, Width, Height);
|
||||
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);
|
||||
spriteBatch.DrawRectangle((RectangleF)Bounds, Color.Transparent);
|
||||
}
|
||||
|
||||
/*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);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStat(NetPeer server, string gameState)
|
||||
{
|
||||
//ball.StopMoving();
|
||||
_server = server;
|
||||
_gameState = gameState;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ball.Position.X > Position.X && ball.CanMove)
|
||||
{
|
||||
//ball.StopMoving();
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -15,6 +15,7 @@ namespace client
|
||||
public Vector2 Velocity;
|
||||
public IShapeF Bounds { get; }
|
||||
public float _screenHeight;
|
||||
public float _height;
|
||||
|
||||
public bool canMove = false;
|
||||
private Vector2 DefaultPosition { get; set; }
|
||||
@@ -26,10 +27,11 @@ namespace client
|
||||
string _gameState = "None";
|
||||
|
||||
// Constructors
|
||||
public Player(RectangleF rectangleF, float screenHeight, NetPacketProcessor processor)
|
||||
public Player(RectangleF rectangleF, float screenHeight, float height, NetPacketProcessor processor)
|
||||
{
|
||||
Bounds = rectangleF;
|
||||
_screenHeight = screenHeight;
|
||||
_height = height;
|
||||
DefaultPosition = Bounds.Position;
|
||||
_processor = processor;
|
||||
|
||||
@@ -61,9 +63,9 @@ namespace client
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.Down))
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user