diff --git a/.gitignore b/.gitignore index 8636995..1537aee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ bin obj .vs -.idea \ No newline at end of file +.idea +DangerousD.sln.DotSettings.user \ No newline at end of file diff --git a/DangerousD/Content/MonstersAnimations.png b/DangerousD/Content/MonstersAnimations.png index 97e3e11..5bc9cca 100644 Binary files a/DangerousD/Content/MonstersAnimations.png and b/DangerousD/Content/MonstersAnimations.png differ diff --git a/DangerousD/Content/animation1.png b/DangerousD/Content/animation1.png new file mode 100644 index 0000000..a599347 Binary files /dev/null and b/DangerousD/Content/animation1.png differ diff --git a/DangerousD/GameCore/GameObjects/Entity.cs b/DangerousD/GameCore/GameObjects/Entity.cs index 6699710..cf2caa9 100644 --- a/DangerousD/GameCore/GameObjects/Entity.cs +++ b/DangerousD/GameCore/GameObjects/Entity.cs @@ -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); - } } } diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index 52c96c1..c4b19cf 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -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); } } } \ No newline at end of file diff --git a/DangerousD/GameCore/GameObjects/IDrawableObject.cs b/DangerousD/GameCore/GameObjects/IDrawableObject.cs index 61df454..f157dcb 100644 --- a/DangerousD/GameCore/GameObjects/IDrawableObject.cs +++ b/DangerousD/GameCore/GameObjects/IDrawableObject.cs @@ -11,4 +11,4 @@ namespace DangerousD.GameCore.GUI void Update(GameTime gameTime); void Draw(SpriteBatch spriteBatch); } -} +} \ No newline at end of file diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/CoreEnemy.cs b/DangerousD/GameCore/GameObjects/LivingEntities/CoreEnemy.cs index fafc936..1b28997 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/CoreEnemy.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/CoreEnemy.cs @@ -29,9 +29,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities Death(); isAlive = false; } - - Move(gameTime); - base.Update(gameTime); } public abstract void Death(); diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs index a46c681..23e010a 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs @@ -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); - } + velocity = new Vector2(monster_speed, 0); } 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; + velocity = new Vector2(-monster_speed, 0); } } diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs index dd0e96e..90266bf 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs @@ -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 { "ZombieMoveRight", "ZombieMoveLeft", "ZombieRightAttack", "ZombieLeftAttack" }, "ZombieMoveLeft");//TODO: Change to player public void Kill() { diff --git a/DangerousD/GameCore/GameObjects/LivingEntity.cs b/DangerousD/GameCore/GameObjects/LivingEntity.cs index db3cc2f..5352489 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntity.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntity.cs @@ -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); } } \ No newline at end of file diff --git a/DangerousD/GameCore/GameObjects/MapObject.cs b/DangerousD/GameCore/GameObjects/MapObject.cs index d000f7b..d4180d0 100644 --- a/DangerousD/GameCore/GameObjects/MapObject.cs +++ b/DangerousD/GameCore/GameObjects/MapObject.cs @@ -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) diff --git a/DangerousD/GameCore/Levels/Level1.cs b/DangerousD/GameCore/Levels/Level1.cs index e718794..0116688 100644 --- a/DangerousD/GameCore/Levels/Level1.cs +++ b/DangerousD/GameCore/Levels/Level1.cs @@ -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 } } } diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index 67a65cf..e7abf16 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -53,6 +53,7 @@ namespace DangerousD.GameCore _spriteBatch = new SpriteBatch(GraphicsDevice); MenuGUI.LoadContent(); LoginGUI.LoadContent(); + //GameObject.te } protected override void Update(GameTime gameTime) diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index 497fa9c..f017120 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -12,17 +12,22 @@ namespace DangerousD.GameCore { public class GameManager { - List livingEntities; - List entities; - List mapObjects; - public MapManager mapManager; - public Player Player { get; set; } + + public List livingEntities; + public List entities; + public List mapObjects; + public MapManager mapManager; + public PhysicsManager physicsManager; + public List players; + public Player GetPlayer1 { get; private set; } public GameManager() { livingEntities = new List(); mapObjects = new List(); entities = new List(); + players = new List(); 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); + + } } } \ No newline at end of file diff --git a/DangerousD/GameCore/Managers/PhysicsManager.cs b/DangerousD/GameCore/Managers/PhysicsManager.cs index ed159a7..e847c45 100644 --- a/DangerousD/GameCore/Managers/PhysicsManager.cs +++ b/DangerousD/GameCore/Managers/PhysicsManager.cs @@ -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 entities, List livingEntities, - List mapObjects) + List 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 livingEntities, + List 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 entities, List 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 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; + } } -} +} \ No newline at end of file