commit
572d784fc3
4 changed files with 110 additions and 37 deletions
|
@ -0,0 +1,6 @@
|
||||||
|
namespace ZoFo.GameCore.GameManagers.CollisionManager
|
||||||
|
{
|
||||||
|
public class CollectionComponent
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,34 +1,41 @@
|
||||||
using System;
|
using Microsoft.Xna.Framework;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection.Metadata;
|
using System.Reflection.Metadata;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using ZoFo.GameCore.GameObjects;
|
||||||
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
|
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
|
||||||
|
|
||||||
namespace ZoFo.GameCore.GameManagers.CollisionManager
|
namespace ZoFo.GameCore.GameManagers.CollisionManager
|
||||||
{
|
{
|
||||||
public class CollisionComponent
|
public class CollisionComponent
|
||||||
{
|
{
|
||||||
//поля
|
//==ПОЛЯ==
|
||||||
public Rectangle Bounds { get; set; }
|
|
||||||
|
public GameObject gameObject { get; set; }
|
||||||
|
//public Rectangle Bounds { get; set; }
|
||||||
|
|
||||||
|
//public Rectangle Rectangle => new Rectangle();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//остановлен ли перс
|
|
||||||
bool doesStop;
|
bool doesStop;
|
||||||
Rectangle stopRectangle;
|
public Rectangle stopRectangle;
|
||||||
|
|
||||||
// triggers for rectangle
|
// triggers for rectangle
|
||||||
bool isTrigger;
|
bool isTrigger;
|
||||||
Rectangle triggerRectangle;
|
public Rectangle triggerRectanglee;
|
||||||
|
|
||||||
//delegate
|
//delegate
|
||||||
public delegate void EventHandler(object sender, EventArgs e);
|
public delegate void EventHandler(object sender, EventArgs e);
|
||||||
|
|
||||||
public CollisionComponent(int x, int y, int width, int height)
|
//public CollisionComponent(int x, int y, int width, int height)
|
||||||
{
|
//{
|
||||||
Bounds = new Rectangle(x, y, width, height);
|
// Bounds = new Rectangle(x, y, width, height);
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//events
|
//events
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using Microsoft.VisualBasic;
|
using Microsoft.VisualBasic;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -9,51 +8,106 @@ using ZoFo.GameCore.GameObjects;
|
||||||
using ZoFo.GameCore.GameManagers.CollisionManager;
|
using ZoFo.GameCore.GameManagers.CollisionManager;
|
||||||
using Microsoft.Xna.Framework;
|
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.LivingEntities;
|
||||||
|
|
||||||
namespace ZoFo.GameCore.GameManagers.CollisionManager
|
namespace ZoFo.GameCore.GameManagers.CollisionManager
|
||||||
{
|
{
|
||||||
public class CollisionManager
|
public class CollisionManager
|
||||||
{
|
{
|
||||||
public List<CollisionComponent> CollisionComponent;
|
//листики
|
||||||
public List<CollisionComponent> TriggerComponent;
|
|
||||||
|
|
||||||
public void RegisterComponent()
|
public List<CollisionComponent> ObjectsWithCollisions;
|
||||||
{
|
public List<CollisionComponent> EntitiesWithMovements;
|
||||||
|
public List<CollisionComponent> ObjectsWithTriggers;
|
||||||
}
|
|
||||||
|
|
||||||
public static bool CheckComponentCollision(List<CollisionComponent> collisionComponents, CollisionComponent component)
|
|
||||||
|
//чекаем коллизии в листе
|
||||||
|
public void CheckComponentCollision(LivingEntity entity)
|
||||||
{
|
{
|
||||||
foreach (var collisionComponent in collisionComponents)
|
//for (int i = 0; i < ObjectsWithCollisions.Count; i++)
|
||||||
{
|
//{
|
||||||
if (component.Bounds.IntersectsWith(collisionComponent.Bounds))
|
var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК
|
||||||
|
var newRect = currentRect; // задаём значение старого РЕК новому РЕК
|
||||||
|
bool flagRemovedObject = false; //флаг удаления
|
||||||
|
|
||||||
|
|
||||||
|
var collidedX = false; // соприкосновение
|
||||||
|
var tryingRectX = currentRect;//переменная для попытки перемещения по X
|
||||||
|
|
||||||
|
tryingRectX.Offset((int)(entity.velocity.X), 0);//задаём значения для tryingRectX по X и по Y
|
||||||
|
|
||||||
|
foreach (var item in ObjectsWithCollisions)//фильтрация
|
||||||
{
|
{
|
||||||
return true;
|
if (Math.Abs(item.stopRectangle.X - entity.collisionComponent.stopRectangle.X) < 550
|
||||||
|
&& Math.Abs(item.stopRectangle.Y - entity.collisionComponent.stopRectangle.Y) < 550
|
||||||
|
&& tryingRectX.Intersects(item.stopRectangle))
|
||||||
|
|
||||||
|
{
|
||||||
|
collidedX = true;// меняем значение соприкосновения на true
|
||||||
|
entity.OnCollision(item);//подписываем entity на ивент коллизии
|
||||||
|
|
||||||
|
break;// выход
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collidedX)// срабатывает, если перемещение блокируется
|
||||||
|
{
|
||||||
|
entity.velocity.X = 0;// задаём значение смещения entity на 0
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newRect.X = tryingRectX.X;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК
|
||||||
|
}
|
||||||
|
|
||||||
|
//==ПОВТОРЯЕМ ТОЖЕ САМОЕ ДЛЯ Y==
|
||||||
|
|
||||||
|
var collidedY = false; // соприкосновение
|
||||||
|
var tryingRectY = currentRect;//переменная для попытки перемещения по X
|
||||||
|
|
||||||
|
tryingRectY.Offset(new Point(0, (int)entity.velocity.Y));//задаём значения для tryingRectX по X и по Y
|
||||||
|
|
||||||
|
foreach (var item in ObjectsWithCollisions)//фильтрация
|
||||||
|
{
|
||||||
|
if (Math.Abs(item.stopRectangle.X - entity.collisionComponent.stopRectangle.X) < 550
|
||||||
|
&& Math.Abs(item.stopRectangle.Y - entity.collisionComponent.stopRectangle.Y) < 550
|
||||||
|
&& tryingRectY.Intersects(item.stopRectangle))
|
||||||
|
|
||||||
|
{
|
||||||
|
collidedY = true;// меняем значение соприкосновения на true
|
||||||
|
entity.OnCollision(item);//подписываем entity на ивент коллизии
|
||||||
|
|
||||||
|
break;// выход
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
if (collidedY)// срабатывает, если перемещение блокируется
|
||||||
|
{
|
||||||
|
entity.velocity.Y = 0;// задаём значение смещения entity на 0
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newRect.Y = tryingRectY.Y;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateComponentCollision(List<CollisionComponent> collisionComponents)
|
//обновление позиции объекта
|
||||||
|
public void UpdateObjectsPositions()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdatePositions()
|
|
||||||
|
//регистрация компонента(его коллизии)
|
||||||
|
public void Register(CollisionComponent component)
|
||||||
{
|
{
|
||||||
|
ObjectsWithCollisions.Add(component);
|
||||||
|
if (component.gameObject is Entity)
|
||||||
|
{
|
||||||
|
EntitiesWithMovements.Add(component);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//public void GetObjectInArea(Rectangle area)
|
|
||||||
//{
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
//public void Register(Rectangle rectangle)
|
|
||||||
//{
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System;
|
||||||
using ZoFo.GameCore.GameObjects.Entities;
|
using ZoFo.GameCore.GameObjects.Entities;
|
||||||
using ZoFo.GameCore.ZoFo_graphics;
|
using ZoFo.GameCore.ZoFo_graphics;
|
||||||
using ZoFo.GameCore.GameManagers;
|
using ZoFo.GameCore.GameManagers;
|
||||||
|
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
|
||||||
|
@ -26,6 +27,11 @@ public class LivingEntity : Entity
|
||||||
}*/
|
}*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
public void OnCollision(CollisionComponent component)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue