недоделанный RayCast с 1 перегрузкой
This commit is contained in:
parent
246e90c933
commit
4a09635ff7
4 changed files with 88 additions and 30 deletions
|
@ -8,23 +8,11 @@ namespace DangerousD.GameCore.GameObjects
|
||||||
{
|
{
|
||||||
public abstract class Entity : GameObject
|
public abstract class Entity : GameObject
|
||||||
{
|
{
|
||||||
private Vector2 targetPosition;
|
|
||||||
public float speed;
|
|
||||||
|
|
||||||
public Entity(Vector2 position) : base(position) {}
|
public Entity(Vector2 position) : base(position) {}
|
||||||
|
|
||||||
|
|
||||||
public void SetPosition(Vector2 position) { targetPosition = position; }
|
|
||||||
|
|
||||||
public override void Update(GameTime gameTime)
|
|
||||||
{
|
|
||||||
if (Vector2.Distance(Pos, targetPosition) > 0.5f)
|
|
||||||
{
|
|
||||||
Vector2 dir = targetPosition - Pos;
|
|
||||||
dir.Normalize();
|
|
||||||
Pos += dir * speed;
|
|
||||||
}
|
|
||||||
base.Update(gameTime);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,23 @@ namespace DangerousD.GameCore.GameObjects;
|
||||||
|
|
||||||
public abstract class LivingEntity : Entity
|
public abstract class LivingEntity : Entity
|
||||||
{
|
{
|
||||||
|
private Vector2 targetPosition;
|
||||||
|
public Vector2 velocity;
|
||||||
|
public Vector2 acceleration;
|
||||||
public LivingEntity(Vector2 position) : base(position)
|
public LivingEntity(Vector2 position) : base(position)
|
||||||
{
|
{
|
||||||
|
acceleration = new Vector2(0, 10);
|
||||||
|
}
|
||||||
|
public void SetPosition(Vector2 position) { targetPosition = position; }
|
||||||
|
|
||||||
|
public override void Update(GameTime gameTime)
|
||||||
|
{
|
||||||
|
if (Vector2.Distance(Pos, targetPosition) > 0.5f)
|
||||||
|
{
|
||||||
|
Vector2 dir = targetPosition - Pos;
|
||||||
|
dir.Normalize();
|
||||||
|
Pos += dir * velocity;
|
||||||
|
}
|
||||||
|
base.Update(gameTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,9 +11,9 @@ namespace DangerousD.GameCore
|
||||||
{
|
{
|
||||||
public class GameManager
|
public class GameManager
|
||||||
{
|
{
|
||||||
List<LivingEntity> livingEntities;
|
public List<LivingEntity> livingEntities;
|
||||||
List<Entity> entities;
|
public List<Entity> entities;
|
||||||
List<MapObject> mapObjects;
|
public List<MapObject> mapObjects;
|
||||||
public MapManager mapManager;
|
public MapManager mapManager;
|
||||||
|
|
||||||
public GameManager()
|
public GameManager()
|
||||||
|
|
|
@ -10,15 +10,19 @@ namespace DangerousD.GameCore.Managers
|
||||||
{
|
{
|
||||||
internal class PhysicsManager
|
internal class PhysicsManager
|
||||||
{
|
{
|
||||||
|
|
||||||
public void UpdateCollisions(List<Entity> entities, List<LivingEntity> livingEntities,
|
public void UpdateCollisions(List<Entity> entities, List<LivingEntity> livingEntities,
|
||||||
List<MapObject> mapObjects)
|
List<MapObject> mapObjects, GameTime gameTime)
|
||||||
{
|
{
|
||||||
|
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
|
foreach (var item in livingEntities)
|
||||||
CheckCollisions(livingEntities,mapObjects);
|
{
|
||||||
|
item.velocity = item.velocity + item.acceleration * delta;
|
||||||
|
}
|
||||||
|
CheckCollisions(livingEntities, mapObjects);
|
||||||
OnCollision(entities, livingEntities);
|
OnCollision(entities, livingEntities);
|
||||||
OnCollision(livingEntities);
|
OnCollision(livingEntities);
|
||||||
|
|
||||||
//entities dont move
|
//entities dont move
|
||||||
//Living entities dont move
|
//Living entities dont move
|
||||||
//mapObjects dont move
|
//mapObjects dont move
|
||||||
|
@ -39,33 +43,38 @@ namespace DangerousD.GameCore.Managers
|
||||||
{
|
{
|
||||||
if (livingEntities[i].Rectangle.Right > mapObjects[j].Rectangle.Left)
|
if (livingEntities[i].Rectangle.Right > mapObjects[j].Rectangle.Left)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
livingEntities[i].velocity.X = 0;
|
||||||
|
|
||||||
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X - (livingEntities[i].Rectangle.Right - mapObjects[j].Rectangle.Left),
|
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X - (livingEntities[i].Rectangle.Right - mapObjects[j].Rectangle.Left),
|
||||||
livingEntities[i].Pos.Y));
|
livingEntities[i].Pos.Y));
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (livingEntities[i].Rectangle.Left < mapObjects[j].Rectangle.Right)
|
else if (livingEntities[i].Rectangle.Left < mapObjects[j].Rectangle.Right)
|
||||||
{
|
{
|
||||||
|
livingEntities[i].velocity.X = 0;
|
||||||
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X + mapObjects[j].Rectangle.Right - livingEntities[i].Rectangle.Left,
|
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X + mapObjects[j].Rectangle.Right - livingEntities[i].Rectangle.Left,
|
||||||
livingEntities[i].Pos.Y));
|
livingEntities[i].Pos.Y));
|
||||||
}
|
}
|
||||||
else if (livingEntities[i].Rectangle.Bottom > mapObjects[j].Rectangle.Top)
|
else if (livingEntities[i].Rectangle.Bottom > mapObjects[j].Rectangle.Top)
|
||||||
{
|
{
|
||||||
|
livingEntities[i].velocity.Y = 0;
|
||||||
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X,
|
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X,
|
||||||
livingEntities[i].Pos.Y - (livingEntities[i].Rectangle.Bottom - mapObjects[j].Rectangle.Top)));
|
livingEntities[i].Pos.Y - (livingEntities[i].Rectangle.Bottom - mapObjects[j].Rectangle.Top)));
|
||||||
}
|
}
|
||||||
else if (livingEntities[i].Rectangle.Top < mapObjects[j].Rectangle.Bottom)
|
else if (livingEntities[i].Rectangle.Top < mapObjects[j].Rectangle.Bottom)
|
||||||
{
|
{
|
||||||
|
livingEntities[i].velocity.Y = 0;
|
||||||
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X,
|
livingEntities[i].SetPosition(new Vector2(livingEntities[i].Pos.X,
|
||||||
livingEntities[i].Pos.Y + (mapObjects[j].Rectangle.Bottom - livingEntities[i].Rectangle.Top)));
|
livingEntities[i].Pos.Y + (mapObjects[j].Rectangle.Bottom - livingEntities[i].Rectangle.Top)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void OnCollision(List<Entity>entities,List<LivingEntity> livingEntities )
|
public void OnCollision(List<Entity> entities, List<LivingEntity> livingEntities)
|
||||||
{
|
{
|
||||||
for (int i = 0; i <entities.Count ; i++)
|
for (int i = 0; i < entities.Count; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < livingEntities.Count; j++)
|
for (int j = 0; j < livingEntities.Count; j++)
|
||||||
{
|
{
|
||||||
|
@ -76,13 +85,13 @@ namespace DangerousD.GameCore.Managers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void OnCollision(List<LivingEntity> livingEntities)
|
public void OnCollision(List<LivingEntity> livingEntities)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < livingEntities.Count; i++)
|
for (int i = 0; i < livingEntities.Count; i++)
|
||||||
{
|
{
|
||||||
for (int j = i+1; j < livingEntities.Count; j++)
|
for (int j = i + 1; j < livingEntities.Count; j++)
|
||||||
{
|
{
|
||||||
if (livingEntities[i].Rectangle.Intersects(livingEntities[j].Rectangle))
|
if (livingEntities[i].Rectangle.Intersects(livingEntities[j].Rectangle))
|
||||||
{
|
{
|
||||||
|
@ -92,6 +101,51 @@ namespace DangerousD.GameCore.Managers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public GameObject RayCast(LivingEntity entity1, LivingEntity entity2, )
|
||||||
|
{
|
||||||
|
|
||||||
|
Rectangle rectangle;
|
||||||
|
Vector2 distance = entity1.Pos - entity2.Pos;
|
||||||
|
rectangle = new Rectangle((int)entity1.Pos.X, (int)entity1.Pos.Y, entity2.Width, entity2.Height);
|
||||||
|
GameObject gameObject = null;
|
||||||
|
double length = distance.Length();
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
rectangle.X = (int)(entity2.Pos.X + (i / length) * distance.X);
|
||||||
|
rectangle.Y = (int)(entity2.Pos.Y + (i / length) * distance.Y);
|
||||||
|
|
||||||
|
//if (rectangle.Intersects(GameManager.Rectangle))
|
||||||
|
//{
|
||||||
|
// return game
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
return gameObject;
|
||||||
|
}
|
||||||
|
public GameObject RayCast(LivingEntity entity1, Vector2 targetCast)
|
||||||
|
{
|
||||||
|
Rectangle rectangle;
|
||||||
|
Vector2 direction = entity1.Pos - targetCast;
|
||||||
|
rectangle = new Rectangle((int)entity1.Pos.X, (int)entity1.Pos.Y, 1, 1);
|
||||||
|
GameObject gameObject = null;
|
||||||
|
double k = direction.Length();
|
||||||
|
|
||||||
|
for (int i = 0; i < k; i++)
|
||||||
|
{
|
||||||
|
rectangle.X = (int)(entity1.Pos.X + (i / k) * direction.X);
|
||||||
|
rectangle.Y = (int)(entity1.Pos.Y + (i / k) * direction.X);
|
||||||
|
if (gameObject != null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
return gameObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue