Merge branch 'livingEntities' into livingEntitiesVlad

This commit is contained in:
N4K 2023-08-16 15:21:14 +03:00
commit 3b79ff9baf
15 changed files with 215 additions and 52 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ bin
obj
.vs
.idea
DangerousD.sln.DotSettings.user

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View file

@ -8,23 +8,11 @@ namespace DangerousD.GameCore.GameObjects
{
public abstract class Entity : GameObject
{
private Vector2 targetPosition;
public float speed;
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);
}
}
}

View file

@ -48,9 +48,12 @@ namespace DangerousD.GameCore
GraphicsComponent.Update();
}
//static Texture2D debugTexture;
public virtual void Draw(SpriteBatch spriteBatch)
{
GraphicsComponent.DrawAnimation(Rectangle, spriteBatch);
//debug
//spriteBatch.Draw(debugTexture, Rectangle, Color.White);
}
}
}

View file

@ -29,9 +29,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
Death();
isAlive = false;
}
Move(gameTime);
base.Update(gameTime);
}
public abstract void Death();

View file

@ -19,7 +19,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
{
Width = 72;
Height = 120;
monster_speed = 20;
monster_speed = 10;
GraphicsComponent.StartAnimation("ZombieLeftAttack");
name = "Zombie";
leftBorder = (int)position.X;
@ -29,12 +29,16 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
public override void Update(GameTime gameTime)
{
if (AppManager.Instance.GameManager.GetPlayer1.Pos.X>Pos.X)
isGoRight = true;
else
isGoRight = false;
Move(gameTime);
var player = AppManager.Instance.GameManager.Player;
if(Pos.X + 20 <= player.Pos.X || Pos.X - 20 >= player.Pos.X)
if(Pos.X + 20 <= AppManager.Instance.GameManager.GetPlayer1.Pos.X || Pos.X - 20 >= AppManager.Instance.GameManager.GetPlayer1.Pos.X)
{
Attack();
player.Death(name);
AppManager.Instance.GameManager.GetPlayer1.Death(name);
}
base.Update(gameTime);
@ -46,13 +50,13 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
{
GraphicsComponent.StopAnimation();
GraphicsComponent.StartAnimation("ZombieRightAttack");
AppManager.Instance.GameManager.Player.Death(name);
AppManager.Instance.GameManager.players[0].Death(name);
}
else if (!isGoRight)
{
GraphicsComponent.StopAnimation();
GraphicsComponent.StartAnimation("ZombieLeftAttack");
AppManager.Instance.GameManager.Player.Death(name);
AppManager.Instance.GameManager.players[0].Death(name);
}
}
@ -63,34 +67,19 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
public override void Move(GameTime gameTime)
{
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
double delta = gameTime.ElapsedGameTime.TotalSeconds;
if (isGoRight)
{
if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveRight")
{
GraphicsComponent.StartAnimation("ZombieMoveRight");
velocity = new Vector2(monster_speed, 0);
_pos = new Vector2(Pos.X + monster_speed * (delta), Pos.Y);
}
}
else if (!isGoRight)
{
if(GraphicsComponent.GetCurrentAnimation != "ZombieMoveLeft")
{
GraphicsComponent.StartAnimation("ZombieMoveLeft");
velocity = new Vector2(-monster_speed, 0);
_pos = new Vector2(Pos.X - monster_speed * (delta), Pos.Y);
}
}
if(Pos.X <= leftBorder)
{
isGoRight = true;
}
if(Pos.X >= rightBorder)
{
isGoRight = false;
}
}

View file

@ -12,9 +12,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{
public Player(Vector2 position) : base(position)
{
Width = 32;
Height = 64;
}
protected override GraphicsComponent GraphicsComponent => throw new NotImplementedException();
protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "ZombieMoveRight", "ZombieMoveLeft", "ZombieRightAttack", "ZombieLeftAttack" }, "ZombieMoveLeft");//TODO: Change to player
public void Kill()
{

View file

@ -4,7 +4,23 @@ namespace DangerousD.GameCore.GameObjects;
public abstract class LivingEntity : Entity
{
private Vector2 targetPosition;
public Vector2 velocity;
public Vector2 acceleration;
public LivingEntity(Vector2 position) : base(position)
{
acceleration = new Vector2(0, 10);
}
public void SetPosition(Vector2 position) { targetPosition = position; _pos = position; } //TODO befrend targetpos and physics engine
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);
}
}

View file

@ -3,7 +3,7 @@ using System.Security.Cryptography.X509Certificates;
namespace DangerousD.GameCore.GameObjects;
internal abstract class MapObject : GameObject
public abstract class MapObject : GameObject
{
public bool IsColliderOn;
public MapObject(Vector2 position) : base(position)

View file

@ -10,11 +10,25 @@ namespace DangerousD.GameCore.Levels
{
public void InitLevel()
{
<<<<<<< HEAD
var Трава = new GrassBlock(new Vector2(0, 128));
var Death = new TestAnimationDeath(new Vector2(128, 128));
//var Zombie = new Zombie(new Vector2(256, 128));
var Frank = new Frank(new Vector2(384, 128));
var FlameSkull = new FlameSkull(new Vector2(512, 128));
=======
new Player(new Vector2(0,0));
var Zombie = new Zombie(new Vector2(256, 128));
var Frank = new Frank(new Vector2(384, 128));
new GrassBlock(new Vector2(0, 224));
for (int i = 0; i < 50; i++)
{
new GrassBlock(new Vector2(i*32, 256));
}
new GrassBlock(new Vector2(500, 224));
>>>>>>> livingEntities
}
}
}

View file

@ -53,6 +53,7 @@ namespace DangerousD.GameCore
_spriteBatch = new SpriteBatch(GraphicsDevice);
MenuGUI.LoadContent();
LoginGUI.LoadContent();
//GameObject.te
}
protected override void Update(GameTime gameTime)

View file

@ -12,17 +12,22 @@ namespace DangerousD.GameCore
{
public class GameManager
{
List<LivingEntity> livingEntities;
List<Entity> entities;
List<MapObject> mapObjects;
public List<LivingEntity> livingEntities;
public List<Entity> entities;
public List<MapObject> mapObjects;
public MapManager mapManager;
public Player Player { get; set; }
public PhysicsManager physicsManager;
public List<Player> players;
public Player GetPlayer1 { get; private set; }
public GameManager()
{
livingEntities = new List<LivingEntity>();
mapObjects = new List<MapObject>();
entities = new List<Entity>();
players = new List<Player>();
mapManager = new MapManager();
physicsManager = new PhysicsManager();
mapManager.Init();
}
@ -34,6 +39,11 @@ namespace DangerousD.GameCore
entities.Add(gameObject as Entity);
if (gameObject is MapObject)
mapObjects.Add(gameObject as MapObject);
if (gameObject is Player)
{
players.Add(gameObject as Player);
GetPlayer1= players[0];
}
}
public void Draw(SpriteBatch _spriteBatch)
@ -54,6 +64,10 @@ namespace DangerousD.GameCore
item.Update(gameTime);
foreach (var item in livingEntities)
item.Update(gameTime);
physicsManager.UpdateCollisions(entities, livingEntities, mapObjects, gameTime);
}
}
}

View file

@ -4,14 +4,26 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
namespace DangerousD.GameCore.Managers
{
internal class PhysicsManager
public class PhysicsManager
{
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)
{
item.velocity = item.velocity + item.acceleration * delta;
}
CheckCollisions(livingEntities, mapObjects);
OnCollision(entities, livingEntities);
OnCollision(livingEntities);
//entities dont move
//Living entities dont move
//mapObjects dont move
@ -21,5 +33,131 @@ namespace DangerousD.GameCore.Managers
//OnCollision
}
public void CheckCollisions(List<LivingEntity> livingEntities,
List<MapObject> mapObjects)
{
for (int i = 0; i < livingEntities.Count; i++)
{
var currentEntity = livingEntities[i];
Rectangle oldRect = currentEntity.Rectangle;
oldRect.Offset((int)currentEntity.velocity.X / 2, 0);
for (int j = 0; j < mapObjects.Count; j++)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0);
break;
}
}
oldRect.Offset((int)currentEntity.velocity.X / 2, 0);
for (int j = 0; j < mapObjects.Count; j++)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0);
break;
}
}
oldRect.Offset(0, (int)currentEntity.velocity.Y/2);
for (int j = 0; j < mapObjects.Count; j++)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2);
break;
}
}
oldRect.Offset(0, (int)currentEntity.velocity.Y / 2);
for (int j = 0; j < mapObjects.Count; j++)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2);
break;
}
}
currentEntity.SetPosition(new Vector2(oldRect.X, oldRect.Y));
}
}
public void OnCollision(List<Entity> entities, List<LivingEntity> livingEntities)
{
for (int i = 0; i < entities.Count; i++)
{
for (int j = 0; j < livingEntities.Count; j++)
{
if (livingEntities[j].Rectangle.Intersects(entities[i].Rectangle))
{
livingEntities[j].OnCollision();
entities[i].OnCollision();
}
}
}
}
public void OnCollision(List<LivingEntity> livingEntities)
{
for (int i = 0; i < livingEntities.Count; i++)
{
for (int j = i + 1; j < livingEntities.Count; j++)
{
if (livingEntities[i].Rectangle.Intersects(livingEntities[j].Rectangle))
{
livingEntities[i].OnCollision();
livingEntities[j].OnCollision();
}
}
}
}
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;
}
}
}