minor overall

This commit is contained in:
SergoDobro 2024-08-17 18:58:49 +03:00
parent fcc8fac8ea
commit 5a6d56128c
9 changed files with 145 additions and 43 deletions

View file

@ -60,6 +60,26 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
} }
public CollisionComponent(GameObject gameObject)
{
this.gameObject = gameObject;
doesStop = false;
this.isTrigger = false;
AppManager.Instance.server.collisionManager.Register(this);
}
public CollisionComponent(GameObject gameObject, bool hasCollision = false, Rectangle? collisionRectangle = null, bool isTrigger = false, Rectangle? triggerRectangle = null)
{
this.gameObject = gameObject;
doesStop = hasCollision;
this.isTrigger = isTrigger;
if (hasCollision)
this.stopRectangle = collisionRectangle.Value;
if (isTrigger)
this.triggerRectanglee = triggerRectangle.Value;
AppManager.Instance.server.collisionManager.Register(this);
}
} }
} }

View file

@ -10,6 +10,8 @@ using Microsoft.Xna.Framework;
using ZoFo.GameCore.GameManagers.MapManager.MapElements; using ZoFo.GameCore.GameManagers.MapManager.MapElements;
using ZoFo.GameCore.GameObjects.Entities; using ZoFo.GameCore.GameObjects.Entities;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities; using ZoFo.GameCore.GameObjects.Entities.LivingEntities;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.ZoFo_graphics;
namespace ZoFo.GameCore.GameManagers.CollisionManager namespace ZoFo.GameCore.GameManagers.CollisionManager
{ {
@ -23,8 +25,9 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
//чекаем коллизии в листе //чекаем коллизии в листе
public void CheckComponentCollision(LivingEntity entity) public void CheckComponentCollision(CollisionComponent componentOfEntity)
{ {
var entity = componentOfEntity.gameObject as LivingEntity;
//for (int i = 0; i < ObjectsWithCollisions.Count; i++) //for (int i = 0; i < ObjectsWithCollisions.Count; i++)
//{ //{
var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК
@ -57,6 +60,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
} }
else else
{ {
entity.position.X += entity.velocity.X; //update player position
newRect.X = tryingRectX.X;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК newRect.X = tryingRectX.X;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК
} }
@ -87,8 +91,13 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
} }
else else
{ {
entity.position.X += entity.velocity.Y;
newRect.Y = tryingRectY.Y;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК newRect.Y = tryingRectY.Y;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК
} }
entity.collisionComponent.stopRectangle = newRect;
entity.graphicsComponent.ObjectDrawRectangle = newRect;
entity.velocity = Vector2.Zero;
} }
//обновление позиции объекта //обновление позиции объекта
@ -97,12 +106,27 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
} }
public void UpdatePositions()
{
foreach (var item in EntitiesWithMovements)
{
CheckComponentCollision(item);
}
}
public CollisionManager()
{
//graphicsComponent
//.ObjectDrawRectangle = new Rectangle(0, 0, 16 * 12, 16 * 16);
EntitiesWithMovements = new List<CollisionComponent>();
ObjectsWithCollisions = new List<CollisionComponent>();
}
//регистрация компонента(его коллизии) //регистрация компонента(его коллизии)
public void Register(CollisionComponent component) public void Register(CollisionComponent component)
{ {
ObjectsWithCollisions.Add(component); ObjectsWithCollisions.Add(component);
if (component.gameObject is Entity) if (component.gameObject is LivingEntity)
{ {
EntitiesWithMovements.Add(component); EntitiesWithMovements.Add(component);
} }

View file

@ -20,5 +20,7 @@ namespace ZoFo.GameCore.GameObjects.Entities
} }
} }
} }

View file

@ -15,10 +15,15 @@ namespace ZoFo.GameCore.GameObjects.Entities
public int Id { get; set; } public int Id { get; set; }
protected Entity(Vector2 position) : base(position) protected Entity(Vector2 position) : base(position)
{ {
} }
public virtual void Update() public virtual void Update()
{ {
}
public override void UpdateLogic()
{
Update();
base.UpdateLogic();
} }
} }
} }

View file

@ -9,6 +9,9 @@ using ZoFo.GameCore.GameManagers.CollisionManager;
namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities; namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities;
public class LivingEntity : Entity public class LivingEntity : Entity
{ {
/// <summary>
/// Переменная для заявки на передвижения, т.е. то, на сколько вы хотите, чтобы в этом кадре переместился объект
/// </summary>
public Vector2 velocity; public Vector2 velocity;
private InputManager inputManager; private InputManager inputManager;

View file

@ -1,8 +1,12 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System; using System;
using System.Collections.Generic;
using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.ZoFo_graphics;
namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
@ -10,17 +14,35 @@ public class Player : LivingEntity
{ {
public Vector2 InputWeaponRotation { get; set; } public Vector2 InputWeaponRotation { get; set; }
public Vector2 InputPlayerRotation { get; set; } public Vector2 InputPlayerRotation { get; set; }
/// <summary>
/// Факт того, что плеер в этом апдейте пытается стрелять
/// </summary>
public bool IsTryingToShoot { get; set; } public bool IsTryingToShoot { get; set; }
Texture2D texture;
private float speed; private float speed;
private int health; private int health;
public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List<string> { "player_running_top_rotate" }, "player_running_top_rotate");
public Player(Vector2 position) : base(position) public Player(Vector2 position) : base(position)
{ {
//InputWeaponRotation = new Vector2(0, 0); //InputWeaponRotation = new Vector2(0, 0);
//InputPlayerRotation = new Vector2(0, 0); //InputPlayerRotation = new Vector2(0, 0);
collisionComponent = new CollisionComponent(this, true, new Rectangle(0, 0, 10, 10));
} }
public void Update(GameTime gameTime) public void Update(GameTime gameTime)
{
MovementLogic();
}
public void MovementLogic()
{
if (InputPlayerRotation.X > 0.9)
{
velocity.X = 5;
}
}
public void HandleNewInput(UpdateInput updateInput)
{ {
} }

View file

@ -20,10 +20,16 @@ public abstract class GameObject
{ {
this.position = position; this.position = position;
graphicsComponent.LoadContent(); graphicsComponent.LoadContent();
graphicsComponent.ObjectDrawRectangle.X = (int)position.X;
graphicsComponent.ObjectDrawRectangle.Y = (int)position.Y;
} }
public virtual void UpdateLogic(GameTime gameTime) public virtual void UpdateLogic()
{ {
PlayAnimation_OnServer(); PlayAnimation_OnServer();
} }

View file

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System; using System;
using System.Diagnostics;
using ZoFo.GameCore.GameManagers.CollisionManager; using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.ZoFo_graphics; using ZoFo.GameCore.ZoFo_graphics;
@ -7,10 +8,15 @@ namespace ZoFo.GameCore.GameObjects.MapObjects.StopObjects;
public class StopObject : MapObject public class StopObject : MapObject
{ {
CollisionComponent collisionComponent; CollisionComponent[] collisionComponent;
protected StopObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName) : base(position, size, sourceRectangle, textureName) protected StopObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName, Rectangle[] collisions) : base(position, size, sourceRectangle, textureName)
{ {
collisionComponent = new CollisionComponent[collisions.Length];
for (int i = 0; i < collisionComponent.Length; i++)
{
collisionComponent[i] = new CollisionComponent(this, true, collisions[i]);
}
// TODO: Написать коллизию, пусть тразмер будет чисто таким же как и текстурка. // TODO: Написать коллизию, пусть тразмер будет чисто таким же как и текстурка.
// Поменяйте уровень защиты конструктора, после снимите в MapManager комментарий в методе LoadMap с создания StopObject-а // Поменяйте уровень защиты конструктора, после снимите в MapManager комментарий в методе LoadMap с создания StopObject-а
} }

View file

@ -8,12 +8,14 @@ using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.MapManager; using ZoFo.GameCore.GameManagers.MapManager;
using ZoFo.GameCore.GameManagers.NetworkManager; using ZoFo.GameCore.GameManagers.NetworkManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates; using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GameObjects; using ZoFo.GameCore.GameObjects;
using ZoFo.GameCore.GameObjects.Entities; using ZoFo.GameCore.GameObjects.Entities;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
using ZoFo.GameCore.GameObjects.MapObjects; using ZoFo.GameCore.GameObjects.MapObjects;
namespace ZoFo.GameCore namespace ZoFo.GameCore
@ -30,6 +32,8 @@ namespace ZoFo.GameCore
} }
#region server logic as App #region server logic as App
#region Net Methods
//TODO Comment pls //TODO Comment pls
public void OnDataSend(string data) public void OnDataSend(string data)
{ {
@ -73,6 +77,10 @@ namespace ZoFo.GameCore
networkManager.Start(players); networkManager.Start(players);
} }
#endregion
#region Game Methods
/// <summary> /// <summary>
/// Запуск игры в комнате /// Запуск игры в комнате
/// </summary> /// </summary>
@ -81,12 +89,13 @@ namespace ZoFo.GameCore
//TODO начинает рассылку и обмен пакетами игры //TODO начинает рассылку и обмен пакетами игры
//Грузит карту //Грузит карту
collisionManager = new CollisionManager();
gameObjects = new List<GameObject>(); gameObjects = new List<GameObject>();
entities = new List<Entity>(); entities = new List<Entity>();
new MapManager().LoadMap(); new MapManager().LoadMap();
AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(40, 40))); AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(40, 40)));
AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(140, 140)));
} }
/// <summary> /// <summary>
@ -98,6 +107,8 @@ namespace ZoFo.GameCore
networkManager.AddData(gameEnded); networkManager.AddData(gameEnded);
networkManager.CloseConnection(); networkManager.CloseConnection();
} }
public CollisionManager collisionManager;
private List<GameObject> gameObjects = new List<GameObject>(); private List<GameObject> gameObjects = new List<GameObject>();
private List<Entity> entities; //entity private List<Entity> entities; //entity
public void Update(GameTime gameTime) public void Update(GameTime gameTime)
@ -106,8 +117,9 @@ namespace ZoFo.GameCore
{ {
foreach (var go in gameObjects) foreach (var go in gameObjects)
{ {
go.UpdateLogic(gameTime); go.UpdateLogic();
} }
collisionManager.UpdatePositions();
ticks = 0; ticks = 0;
networkManager.SendData(); networkManager.SendData();
} }
@ -143,4 +155,6 @@ namespace ZoFo.GameCore
} }
} }
#endregion #endregion
#endregion
} }