diff --git a/DangerousD/Content/Content.mgcb b/DangerousD/Content/Content.mgcb index 713c20a..3c483a8 100644 --- a/DangerousD/Content/Content.mgcb +++ b/DangerousD/Content/Content.mgcb @@ -259,7 +259,19 @@ /processorParam:TextureFormat=Color /build:textboxbackground6-1.png -#begin tiles.png +#begin MonstersAnimations.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:MonstersAnimations.png + +#begin PC_Computer_Dangerous_Dave_In_The_Haunted_Mansion_Death_Sequences.png /importer:TextureImporter /processor:TextureProcessor /processorParam:ColorKeyColor=255,0,255,255 @@ -271,6 +283,18 @@ /processorParam:TextureFormat=Color /build:tiles.png +#begin playerAnimation.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:playerAnimation.png + #begin wall.jpg /importer:TextureImporter /processor:TextureProcessor diff --git a/DangerousD/Content/animations/HunchmanAttackLeft b/DangerousD/Content/animations/HunchmanAttackLeft new file mode 100644 index 0000000..f6b0634 --- /dev/null +++ b/DangerousD/Content/animations/HunchmanAttackLeft @@ -0,0 +1 @@ +{"id":"HunchmanAttackLeft","textureName":"MonstersAnimations","startSpriteRectangle":{"X":101,"Y":124,"Width":40,"Height":24},"frameSecond":[{"Item1":0,"Item2":10}],"textureFrameInterval":1,"framesCount":3,"isCycle":true,"offset":"0, 0"} diff --git a/DangerousD/Content/animations/HunchmanAttackRight b/DangerousD/Content/animations/HunchmanAttackRight new file mode 100644 index 0000000..482d7f3 --- /dev/null +++ b/DangerousD/Content/animations/HunchmanAttackRight @@ -0,0 +1 @@ +{"id":"HunchmanAttackRight","textureName":"MonstersAnimations","startSpriteRectangle":{"X":101,"Y":99,"Width":40,"Height":24},"frameSecond":[{"Item1":0,"Item2":10}],"textureFrameInterval":1,"framesCount":3,"isCycle":true,"offset":"0, 0"} diff --git a/DangerousD/Content/animations/HunchmanMoveLeft b/DangerousD/Content/animations/HunchmanMoveLeft new file mode 100644 index 0000000..47d627e --- /dev/null +++ b/DangerousD/Content/animations/HunchmanMoveLeft @@ -0,0 +1 @@ +{"id":"HunchmanMoveLeft","textureName":"MonstersAnimations","startSpriteRectangle":{"X":1,"Y":124,"Width":24,"Height":24},"frameSecond":[{"Item1":0,"Item2":10}],"textureFrameInterval":1,"framesCount":4,"isCycle":true,"offset":"0, 0"} diff --git a/DangerousD/Content/animations/HunchmanMoveRight b/DangerousD/Content/animations/HunchmanMoveRight new file mode 100644 index 0000000..a289fa5 --- /dev/null +++ b/DangerousD/Content/animations/HunchmanMoveRight @@ -0,0 +1,3 @@ + +{"id":"HunchmanMoveRight","textureName":"MonstersAnimations","startSpriteRectangle":{"X":1,"Y":99,"Width":24,"Height":24},"frameSecond":[{"Item1":0,"Item2":10}],"textureFrameInterval":1,"framesCount":4,"isCycle":true,"offset":"0, 0"} + diff --git a/DangerousD/Content/animations/playerJumpLeft b/DangerousD/Content/animations/playerJumpLeft new file mode 100644 index 0000000..763ccd7 --- /dev/null +++ b/DangerousD/Content/animations/playerJumpLeft @@ -0,0 +1 @@ +{"id":"playerJumpLeft","textureName":"playerAnimation","startSpriteRectangle":{"X":101,"Y":34,"Width":24,"Height":32},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":3,"isCycle":false,"offset":"0, 0"} diff --git a/DangerousD/Content/animations/playerJumpRight b/DangerousD/Content/animations/playerJumpRight new file mode 100644 index 0000000..816b2d3 --- /dev/null +++ b/DangerousD/Content/animations/playerJumpRight @@ -0,0 +1 @@ +{"id":"playerJumpRight","textureName":"playerAnimation","startSpriteRectangle":{"X":126,"Y":1,"Width":24,"Height":32},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":3,"isCycle":false,"offset":"0, 0"} diff --git a/DangerousD/Content/animations/playerMoveLeft b/DangerousD/Content/animations/playerMoveLeft new file mode 100644 index 0000000..d167573 --- /dev/null +++ b/DangerousD/Content/animations/playerMoveLeft @@ -0,0 +1,20 @@ +{ + "id": "playerMoveLeft", + "textureName": "playerAnimation", + "startSpriteRectangle": { + "X": 26, + "Y": 34, + "Width": 24, + "Height": 32 + }, + "frameSecond": [ + { + "Item1": 0, + "Item2": 8 + } + ], + "textureFrameInterval": 1, + "framesCount": 4, + "isCycle": true, + "offset": "0, 0" +} diff --git a/DangerousD/Content/animations/playerMoveRight b/DangerousD/Content/animations/playerMoveRight new file mode 100644 index 0000000..42d7327 --- /dev/null +++ b/DangerousD/Content/animations/playerMoveRight @@ -0,0 +1,20 @@ +{ + "id": "playerMoveRight", + "textureName": "playerAnimation", + "startSpriteRectangle": { + "X": 26, + "Y": 1, + "Width": 24, + "Height": 32 + }, + "frameSecond": [ + { + "Item1": 0, + "Item2": 8 + } + ], + "textureFrameInterval": 1, + "framesCount": 4, + "isCycle": true, + "offset": "0, 0" +} diff --git a/DangerousD/Content/animations/playerRightStay b/DangerousD/Content/animations/playerRightStay new file mode 100644 index 0000000..bbeb120 --- /dev/null +++ b/DangerousD/Content/animations/playerRightStay @@ -0,0 +1 @@ +{"id":"playerRightStay","textureName":"playerAnimation","startSpriteRectangle":{"X":1,"Y":1,"Width":24,"Height":32},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":1,"isCycle":true,"offset":"0, 0"} diff --git a/DangerousD/Content/animations/playerStayLeft b/DangerousD/Content/animations/playerStayLeft new file mode 100644 index 0000000..993f1e1 --- /dev/null +++ b/DangerousD/Content/animations/playerStayLeft @@ -0,0 +1 @@ +{"id":"playerStayLeft","textureName":"playerAnimation","startSpriteRectangle":{"X":1,"Y":34,"Width":24,"Height":32},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":1,"isCycle":true,"offset":"0, 0"} diff --git a/DangerousD/Content/playerAnimation.png b/DangerousD/Content/playerAnimation.png new file mode 100644 index 0000000..ed061f3 Binary files /dev/null and b/DangerousD/Content/playerAnimation.png differ diff --git a/DangerousD/GameCore/GUI/HUD.cs b/DangerousD/GameCore/GUI/HUD.cs index 092829a..07e446a 100644 --- a/DangerousD/GameCore/GUI/HUD.cs +++ b/DangerousD/GameCore/GUI/HUD.cs @@ -1,42 +1,40 @@ using System; using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; +using MonogameLibrary.UI.Base; using MonogameLibrary.UI.Elements; +using System.Xml.Linq; +using DangerousD.GameCore.Managers; +using DangerousD.GameCore; +using System.Collections.Generic; namespace DangerousD.GameCore.GUI { - public class HUD : IDrawableObject - { - - - public HUD() - { - } - - public void Draw(SpriteBatch spriteBatch) + public class HUD : AbstractGui + { + int ammout = 0; + List rects = new List { }; + int wigth = AppManager.Instance.inGameResolution.X; + int height = AppManager.Instance.inGameResolution.Y; + protected override void CreateUI() { - ; - } - - public void Initialize(GraphicsDevice graphicsDevice) - { - throw new NotImplementedException(); - } - - public void Initialize() - { - throw new NotImplementedException(); - } - - public void LoadContent() - { - var content = AppManager.Instance.Content; + DrawableUIElement background = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, wigth, height), mainColor = Color.Transparent }; + Elements.Add(background); + Rect rect = new Rect(Manager) { rectangle = new Rectangle(wigth / 35, height / 35, 120, 70), mainColor = Color.DarkRed }; + Elements.Add(rect); + Label label = new Label(Manager) { rectangle = new Rectangle(wigth / 34, height / 30, 120, 20), text = "ammout", fontName = "font2", scale = 0.2f, mainColor = Color.Transparent, fontColor = Color.Black }; + Elements.Add(label); } - - public void Update(GameTime gameTime) + public override void Update(GameTime gameTime) { - throw new NotImplementedException(); + + rects.Clear(); + for (int i = 0; i < ammout; i++) + { + rects.Add(new Rect(Manager) { rectangle = new Rectangle(wigth / 29 + i * 13, height / 17, 5, 20), mainColor = Color.Yellow }); + rects[i].LoadTexture(AppManager.Instance.Content); + } + base.Update(gameTime); } } } diff --git a/DangerousD/GameCore/GUI/OptionsGUI.cs b/DangerousD/GameCore/GUI/OptionsGUI.cs index 2236613..6fc95c7 100644 --- a/DangerousD/GameCore/GUI/OptionsGUI.cs +++ b/DangerousD/GameCore/GUI/OptionsGUI.cs @@ -63,7 +63,8 @@ namespace DangerousD.GameCore.GUI Button bTExit = new Button(Manager) { fontName = "Font2", scale = 0.72f, text = "<-", rectangle = new Rectangle(wigth / 30, height / 30, (int)(40 * 2.4), (int)(40 * 2.4)), textureName = "textboxbackground1-1" }; Elements.Add(bTExit); - bTExit.LeftButtonPressed += () => { + bTExit.LeftButtonPressed += () => + { AppManager.Instance.ChangeGameState(GameState.Menu); }; } diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index 96ff4ad..ceb5e5a 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -14,6 +14,7 @@ namespace DangerousD.GameCore { protected Vector2 _pos; public Vector2 Pos => _pos; + public int id; public int Width { get; set; } public int Height { get; set; } public Rectangle Rectangle => new Rectangle((int)Pos.X, (int)Pos.Y, Width, Height); @@ -38,10 +39,14 @@ namespace DangerousD.GameCore { } - public void LoadContent() + public void PlayAnimation() { GraphicsComponent.LoadContent(); } + public void LoadContent() + { + PlayAnimation(); + } public virtual void Update(GameTime gameTime) { diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Hunchman.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Hunchman.cs index e309d9e..ba342ec 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Hunchman.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Hunchman.cs @@ -4,7 +4,9 @@ using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; +using System.Security.Authentication.ExtendedProtection; using System.Text; using System.Threading.Tasks; @@ -12,15 +14,71 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters { public class Hunchman : CoreEnemy { + GameManager gameManager; + bool isAttacking; public Hunchman(Vector2 position) : base(position) { + Width = 48; + Height = 48; + monster_speed = -2; + monster_health = 1; + name = "HunchMan"; + velocity = new Vector2(monster_speed, 0); + gameManager = AppManager.Instance.GameManager; + isAttacking = false; + isAlive = true; } - protected override GraphicsComponent GraphicsComponent { get; } = new(new List { "HunchmanMoveLeft", "HunchmanMoveRight", "HunchmanAttackLeft", "HunchmanAttackRight" }, "HunchmanMoveRight"); + protected override GraphicsComponent GraphicsComponent { get; } = new(new List + { "HunchmanMoveLeft", "HunchmanMoveRight", "HunchmanAttackLeft", "HunchmanAttackRight" }, "HunchmanMoveLeft"); + + public override void Update(GameTime gameTime) + { + // P.S. Всё в классе можешь смело удалять и переписывать с нуля. + gameManager = AppManager.Instance.GameManager; + + if (!isAttacking) + { + Attack(); + Move(gameTime); + } + else + { + velocity.X = 0; + } + Death(); + + } public override void Attack() { - + GameObject gameObject; + foreach (var player in gameManager.players) + { + if (player.Pos.Y + player.Height >= Pos.Y && player.Pos.Y <= Pos.Y + Height) + { + gameObject = gameManager.physicsManager.RayCast(this, player); + if (gameObject is null) + { + isAttacking = true; + GraphicsComponent.StopAnimation(); + if (velocity.X > 0) + { + if (GraphicsComponent.GetCurrentAnimation != "HunchmanAttackRight") + { + GraphicsComponent.StartAnimation("HunchmanAttackRight"); + } + } + else if (velocity.X < 0) + { + if (GraphicsComponent.GetCurrentAnimation != "HunchmanAttackLeft") + { + GraphicsComponent.StartAnimation("HunchmanAttackLeft"); + } + } + } + } + } } public override void Attack(GameTime gameTime) @@ -30,12 +88,45 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters public override void Death() { + if (monster_health <= 0) + { + } } public override void Move(GameTime gameTime) { + if (gameManager.physicsManager.RayCast(this, new Vector2(Pos.X + Width + 10, Pos.Y + Height)) is not null) + { + monster_speed *= -1; + } + + velocity.X = monster_speed; + if (velocity.X > 0) + { + if (GraphicsComponent.GetCurrentAnimation != "HunchmanMoveRight") + { + GraphicsComponent.StartAnimation("HunchmanMoveRight"); + } + + } + + else if (velocity.X < 0) + { + if (GraphicsComponent.GetCurrentAnimation != "HunchmanMoveLeft") + { + GraphicsComponent.StartAnimation("HunchmanMoveLeft"); + } + } + + } + + public override void OnCollision(GameObject gameObject) + { + monster_speed *= -1; + _pos.X += 5 * monster_speed; + Debug.WriteLine("Collision"); } public override void Target() diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs index f2dc98a..4ab4494 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs @@ -13,7 +13,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters { public class Zombie : CoreEnemy { - private bool isGoRight = true; float leftBorder; float rightBorder; bool isAttaking = false; @@ -25,9 +24,18 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters Height = 40; monster_speed = 3; name = "Zombie"; - leftBorder = (int)position.X - 100; - rightBorder = (int)position.X + 100; + leftBorder = (int)position.X - 50; + rightBorder = (int)position.X + 50; physicsManager = new PhysicsManager(); + Random random = new Random(); + if(random.Next(0, 2) == 0) + { + isGoRight = true; + } + else + { + isGoRight = false; + } } protected override GraphicsComponent GraphicsComponent { get; } = new(new List { "ZombieMoveRight", "ZombieMoveLeft", "ZombieRightAttack", "ZombieLeftAttack" }, "ZombieMoveLeft"); @@ -38,7 +46,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters Target(); Move(gameTime); } - + fixBorder(); base.Update(gameTime); } @@ -50,7 +58,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters { if (GraphicsComponent.GetCurrentAnimation != "ZombieRightAttack") { - GraphicsComponent.StartAnimation("ZombieAttackRight"); + GraphicsComponent.StartAnimation("ZombieRightAttack"); } AppManager.Instance.GameManager.players[0].Death(name); } @@ -71,7 +79,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters public override void Move(GameTime gameTime) { - float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; if (isGoRight) { if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveRight") @@ -113,7 +120,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters } public override void Target() { - if (AppManager.Instance.GameManager.physicsManager.CheckRectangle(new Rectangle((int)Pos.X - 50, (int)Pos.Y, Width + 100, Height), typeof(Player)) != null) + if (AppManager.Instance.GameManager.physicsManager.CheckRectangle(new Rectangle((int)Pos.X - 50, (int)Pos.Y, Width + 100, Height), typeof(Player)).Count > 0) { if (isGoRight && this._pos.X <= AppManager.Instance.GameManager.players[0].Pos.X) { @@ -130,10 +137,19 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters } } } - public override void Attack(GameTime gameTime) + public void fixBorder() { - throw new NotImplementedException(); + if(leftBorder <= 0) + { + leftBorder = 0; + } + if(rightBorder >= 800) + { + rightBorder = 760; + } } - + + public override void Attack(GameTime gameTime) + {} } } diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Player/DeathRectangle.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Player/DeathRectangle.cs index 1867b60..7dda1b1 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Player/DeathRectangle.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Player/DeathRectangle.cs @@ -13,8 +13,8 @@ namespace DangerousD.GameCore.GameObjects.PlayerDeath { public DeathRectangle(Vector2 pos, string DeathType) : base(pos) { - Height = 128; - Width = 128; + Height = 48; + Width = 48; PlayDeath(DeathType); } diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs index 122f360..03c24b6 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Player/Player.cs @@ -8,12 +8,15 @@ using System.Threading.Tasks; using DangerousD.GameCore.GameObjects.PlayerDeath; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Graphics; +using DangerousD.GameCore.Network; namespace DangerousD.GameCore.GameObjects.LivingEntities { public class Player : LivingEntity { bool isAlive = true; + bool isRight; + string stayAnimation; bool isJump = false; public int health; public bool isGoRight = false; @@ -21,6 +24,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities public int rightBorder; public int leftBorder; public bool isVisible = true; + private bool isAttacked = false; public GameObject objectAttack; public Player(Vector2 position) : base(position) @@ -30,7 +34,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities AppManager.Instance.InputManager.ShootEvent += Shoot; - AppManager.Instance.InputManager.MovEventJump += AnimationJump; + AppManager.Instance.InputManager.MovEventJump += Jump; AppManager.Instance.InputManager.MovEventDown += MoveDown; velocity = new Vector2(0, 0); @@ -38,9 +42,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities leftBorder = (int)position.X - 100; } + public bool IsAlive { get { return isAlive; } } - protected override GraphicsComponent GraphicsComponent { get; } = new(new List { "ZombieMoveRight", "ZombieMoveLeft", "ZombieRightAttack", "ZombieLeftAttack", "DeathFromZombie" }, "ZombieMoveLeft");//TODO: Change to player + protected override GraphicsComponent GraphicsComponent { get; } = new(new List { "playerMoveLeft", "playerMoveRight", "DeathFromZombie", "playerRightStay", "playerStayLeft", + "playerJumpRight" , "playerJumpLeft"}, "playerStayLeft"); public void Attack() { @@ -51,10 +57,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities } public override void OnCollision(GameObject gameObject) { - if (gameObject is Player) - { - isVisible = false; - } base.OnCollision(gameObject); } public override void Draw(SpriteBatch spriteBatch) @@ -66,20 +68,21 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities } public void Death(string monsterName) { - /*if(monsterName == "Zombie") + isAttacked = true; + if(monsterName == "Zombie") { DeathRectangle deathRectangle = new DeathRectangle(Pos, "DeathFrom" + monsterName); deathRectangle.Gr.actionOfAnimationEnd += (a) => { if (a == "DeathFrom" + monsterName) { - AppManager.Instance.ChangeGameState(GameState.GameOver); + AppManager.Instance.ChangeGameState(GameState.Death); } }; } - isAlive = false;*/ + isAlive = false; } - public void AnimationJump() + public void Jump() { if (isOnGround) { @@ -95,8 +98,15 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities public override void Update(GameTime gameTime) { GraphicsComponent.CameraPosition = (_pos-new Vector2(200, 350)).ToPoint(); + if (!isAttacked) + { + Move(gameTime); + } + else + { + velocity.X = 0; + } base.Update(gameTime); - Move(gameTime); } public void Move(GameTime gameTime) @@ -105,16 +115,16 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities velocity.X = 5 * AppManager.Instance.InputManager.VectorMovementDirection.X; if (AppManager.Instance.InputManager.VectorMovementDirection.X > 0) { - if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveRight")//идёт направо + if (GraphicsComponent.GetCurrentAnimation != "playerMoveRight")//идёт направо { - GraphicsComponent.StartAnimation("ZombieMoveRight"); + GraphicsComponent.StartAnimation("playerMoveRight"); } } else if (AppManager.Instance.InputManager.VectorMovementDirection.X < 0)//идёт налево { - if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveLeft") + if (GraphicsComponent.GetCurrentAnimation != "playerMoveLeft") { - GraphicsComponent.StartAnimation("ZombieMoveLeft"); + GraphicsComponent.StartAnimation("playerMoveLeft"); } } else if(AppManager.Instance.InputManager.VectorMovementDirection.X == 0)//стоит @@ -124,6 +134,11 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities GraphicsComponent.StartAnimation("ZombieMoveLeft"); } } + if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.SinglePlayer) + { + NetworkTask task = new NetworkTask(id, Pos); + AppManager.Instance.NetworkTasks.Add(task); + } } public void MoveDown() { diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index e781ae1..cd8bfe4 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -10,11 +10,15 @@ using DangerousD.GameCore.Graphics; using DangerousD.GameCore.Network; using MonogameLibrary.UI.Base; using DangerousD.GameCore.Managers; +using DangerousD.GameCore.GameObjects.LivingEntities; +using DangerousD.GameCore.GameObjects; namespace DangerousD.GameCore { public enum MultiPlayerStatus { SinglePlayer, Host, Client } - public enum GameState { Menu, Options, Lobby, Game, Login, Death } + public enum GameState { Menu, Options, Lobby, Game, Login, Death, HUD, + GameOver + } public class AppManager : Game { public static AppManager Instance { get; private set; } @@ -30,7 +34,9 @@ namespace DangerousD.GameCore IDrawableObject LoginGUI; IDrawableObject LobbyGUI; IDrawableObject DeathGUI; + IDrawableObject HUD; public DebugHUD DebugHUD; + public List NetworkTasks = new List(); public GameManager GameManager { get; private set; } = new(); public AnimationBuilder AnimationBuilder { get; private set; } = new AnimationBuilder(); @@ -56,13 +62,14 @@ namespace DangerousD.GameCore resolution = SettingsManager.Resolution; _graphics.PreferredBackBufferWidth = resolution.X; _graphics.PreferredBackBufferHeight = resolution.Y; - _graphics.IsFullScreen = true; + _graphics.IsFullScreen = false; gameState = GameState.Menu; MenuGUI = new MenuGUI(); LoginGUI = new LoginGUI(); OptionsGUI = new OptionsGUI(); LobbyGUI = new LobbyGUI(); DeathGUI = new DeathGUI(); + HUD = new HUD(); DebugHUD = new DebugHUD(); UIManager.resolution = resolution; UIManager.resolutionInGame = inGameResolution; @@ -76,7 +83,7 @@ namespace DangerousD.GameCore DebugHUD.Initialize(); OptionsGUI.Initialize(); - + HUD.Initialize(); LobbyGUI.Initialize(); DeathGUI.Initialize(); base.Initialize(); @@ -91,6 +98,7 @@ namespace DangerousD.GameCore OptionsGUI.LoadContent(); LobbyGUI.LoadContent(); DeathGUI.LoadContent(); + HUD.LoadContent(); GameObject.debugTexture = new Texture2D(GraphicsDevice, 1, 1); GameObject.debugTexture.SetData(new Color[] { new Color(1, 0,0,0.25f) }); SoundManager.LoadSounds(); @@ -123,6 +131,9 @@ namespace DangerousD.GameCore case GameState.Death: DeathGUI.Update(gameTime); break; + case GameState.HUD: + HUD.Update(gameTime); + break; case GameState.Game: GameManager.Update(gameTime); break; @@ -156,6 +167,9 @@ namespace DangerousD.GameCore case GameState.Death: DeathGUI.Draw(_spriteBatch); break; + case GameState.HUD: + HUD.Draw(_spriteBatch); + break; case GameState.Game: _spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp); GameManager.Draw(_spriteBatch); @@ -211,18 +225,29 @@ namespace DangerousD.GameCore case NetworkTaskOperationEnum.CreateEntity: break; case NetworkTaskOperationEnum.SendPosition: + LivingEntity entity = GameManager.livingEntities.Find(x => x.id == networkTask.objId); + entity.SetPosition(networkTask.position); break; case NetworkTaskOperationEnum.ChangeState: break; case NetworkTaskOperationEnum.ConnectToHost: + Player connectedPlayer = new Player(Vector2.Zero); + NetworkTasks.Add(new NetworkTask(connectedPlayer.id)); + NetworkTask task = new NetworkTask(); + NetworkTasks.Add(task.AddConnectedPlayer(GameManager.GetPlayer1.id, GameManager.GetPlayer1.Pos)); break; case NetworkTaskOperationEnum.GetClientPlayerId: + GameManager.GetPlayer1.id = networkTask.objId; + break; + case NetworkTaskOperationEnum.AddConnectedPlayer: + Player remoteConnectedPlayer = new Player(networkTask.position); + remoteConnectedPlayer.id = networkTask.objId; + GameManager.players.Add(remoteConnectedPlayer); break; default: break; } } - } public void SetMultiplayerState(MultiPlayerStatus multiPlayerStatus) { diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index 342813b..7e4851a 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -8,14 +8,18 @@ using System; using System.Collections.Generic; using System.Text; using DangerousD.GameCore.GameObjects.LivingEntities.Monsters; +using System.Linq; +using DangerousD.GameCore.GUI; +using DangerousD.GameCore.Network; namespace DangerousD.GameCore { public class GameManager { public List GetAllGameObjects { get; private set; } - + private int currentEntityId = 0; public List livingEntities; + public List livingEntitiesWithoutPlayers; public List entities; public List mapObjects; public List BackgroundObjects; @@ -24,35 +28,43 @@ namespace DangerousD.GameCore public PhysicsManager physicsManager; public List players; public List otherObjects = new(); - + public Player GetPlayer1 { get; private set; } public GameManager() { others = new List(); GetAllGameObjects = new List(); livingEntities = new List(); + livingEntitiesWithoutPlayers = new List(); mapObjects = new List(); BackgroundObjects = new List(); entities = new List(); players = new List(); mapManager = new MapManager(1); physicsManager = new PhysicsManager(); - - } - + } internal void Register(GameObject gameObject) { GetAllGameObjects.Add(gameObject); + if (gameObject is Entity) + { + gameObject.id = currentEntityId; + currentEntityId++; + } if (gameObject is Player objPl) { livingEntities.Add(gameObject as LivingEntity); players.Add(objPl); - GetPlayer1 = players[0]; + if (GetPlayer1 is null) + { + GetPlayer1 = players[players.Count - 1]; + } } else if (gameObject is LivingEntity objLE) { + livingEntitiesWithoutPlayers.Add(objLE); livingEntities.Add(objLE); } else if (gameObject is Entity objE) @@ -71,7 +83,6 @@ namespace DangerousD.GameCore otherObjects.Add(gameObject); } } - public void Draw(SpriteBatch _spriteBatch) { foreach (var item in BackgroundObjects) @@ -88,21 +99,39 @@ namespace DangerousD.GameCore public void Update(GameTime gameTime) { + AppManager.Instance.DebugHUD.Set("playerId: ", GetPlayer1.id.ToString()); + if (AppManager.Instance.NetworkTasks.Count > 0) + { + AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList()); + AppManager.Instance.NetworkTasks.Clear(); + } foreach (var item in BackgroundObjects) item.Update(gameTime); foreach (var item in mapObjects) item.Update(gameTime); foreach (var item in entities) item.Update(gameTime); - - for (int i = 0; i < livingEntities.Count; i++) - livingEntities[i].Update(gameTime); + if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.Client) + { + for (int i = 0; i < livingEntitiesWithoutPlayers.Count; i++) + { + livingEntitiesWithoutPlayers[i].Update(gameTime); + } + GetPlayer1.Update(gameTime); + } + else + { + for (int i = 0; i < livingEntitiesWithoutPlayers.Count; i++) + { + livingEntitiesWithoutPlayers[i].PlayAnimation(); + } + GetPlayer1.Update(gameTime); + } + GetPlayer1.Update(gameTime); foreach (var item in otherObjects) item.Update(gameTime); physicsManager.UpdateCollisions(entities, livingEntities, mapObjects, gameTime); - - } } -} +} \ No newline at end of file diff --git a/DangerousD/GameCore/Managers/SoundManager.cs b/DangerousD/GameCore/Managers/SoundManager.cs index 7da1b17..8e2df9e 100644 --- a/DangerousD/GameCore/Managers/SoundManager.cs +++ b/DangerousD/GameCore/Managers/SoundManager.cs @@ -47,7 +47,7 @@ namespace DangerousD.GameCore PlayingSounds.Add(sound); if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host) { - AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(Vector2.Zero, soundName)); + AppManager.Instance.NetworkTasks.Add(new Network.NetworkTask(Vector2.Zero, soundName)); } } public void StartSound(string soundName, Vector2 soundPos, Vector2 playerPos) // запустить звук у которого есть позиция @@ -59,7 +59,7 @@ namespace DangerousD.GameCore PlayingSounds.Add(sound); if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host) { - AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(soundPos, soundName)); + AppManager.Instance.NetworkTasks.Add(new Network.NetworkTask(soundPos, soundName)); } } public void StopAllSounds() // остановка всех звуков diff --git a/DangerousD/GameCore/Network/NetworkManager.cs b/DangerousD/GameCore/Network/NetworkManager.cs index caf16a1..292e210 100644 --- a/DangerousD/GameCore/Network/NetworkManager.cs +++ b/DangerousD/GameCore/Network/NetworkManager.cs @@ -25,7 +25,7 @@ namespace DangerousD.GameCore.Network { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address = IPAddress.Parse(IpAddress); - int port = 8000; + int port = 51873; endPoint = new IPEndPoint(address, port); } catch { } @@ -71,6 +71,7 @@ namespace DangerousD.GameCore.Network Thread acceptThread = new Thread(AcceptSockets); acceptThread.Start(); state = "Host"; + AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host); } catch { } } @@ -84,10 +85,13 @@ namespace DangerousD.GameCore.Network Thread.Sleep(10); Thread ReceivingThread = new Thread(ReceiveMsgFromHost); ReceivingThread.Start(); + NetworkTask connectionTask = new NetworkTask("Player"); + AppManager.Instance.NetworkTasks.Add(connectionTask); + AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client); } catch { } } - public void SendMsg(NetworkTask networkTask) + public void SendMsg(List networkTask) { byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask)); int count = Data.Length; diff --git a/DangerousD/GameCore/Network/NetworkTask.cs b/DangerousD/GameCore/Network/NetworkTask.cs index 1dbb508..8611d78 100644 --- a/DangerousD/GameCore/Network/NetworkTask.cs +++ b/DangerousD/GameCore/Network/NetworkTask.cs @@ -19,6 +19,7 @@ namespace DangerousD.GameCore.Network public Vector2 velocity { get; set; } public Type type { get; set; } + public NetworkTask() { } /// /// Нанести урон сущности /// @@ -126,5 +127,12 @@ namespace DangerousD.GameCore.Network this.velocity = velocity; this.type = type; } + public NetworkTask AddConnectedPlayer(int connectedPlayerId, Vector2 playerPosition) + { + operation = NetworkTaskOperationEnum.AddConnectedPlayer; + objId = connectedPlayerId; + position = playerPosition; + return this; + } } } diff --git a/DangerousD/GameCore/Network/NetworkTaskOperationEnum.cs b/DangerousD/GameCore/Network/NetworkTaskOperationEnum.cs index 86cb112..7fad430 100644 --- a/DangerousD/GameCore/Network/NetworkTaskOperationEnum.cs +++ b/DangerousD/GameCore/Network/NetworkTaskOperationEnum.cs @@ -9,6 +9,6 @@ namespace DangerousD.GameCore.Network [Serializable] public enum NetworkTaskOperationEnum { - TakeDamage, SendSound, CreateEntity, SendPosition, ChangeState, ConnectToHost, GetClientPlayerId + TakeDamage, SendSound, CreateEntity, SendPosition, ChangeState, ConnectToHost, GetClientPlayerId, AddConnectedPlayer } } diff --git a/MonogameLibrary/UI/Base/DrawableUIElement.cs b/MonogameLibrary/UI/Base/DrawableUIElement.cs index 58e739b..8835ce0 100644 --- a/MonogameLibrary/UI/Base/DrawableUIElement.cs +++ b/MonogameLibrary/UI/Base/DrawableUIElement.cs @@ -15,7 +15,7 @@ namespace MonogameLibrary.UI.Base protected Texture2D texture; protected int layerIndex; protected UIManager Manager; - public string textureName; + public string textureName = ""; public Rectangle rectangle = new Rectangle(0, 0, 10, 10); public Color mainColor = Color.White; diff --git a/MonogameLibrary/UI/Elements/Rect.cs b/MonogameLibrary/UI/Elements/Rect.cs new file mode 100644 index 0000000..d776fb7 --- /dev/null +++ b/MonogameLibrary/UI/Elements/Rect.cs @@ -0,0 +1,68 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Enums; +using MonogameLibrary.UI.Interfaces; +using System; +using System.Collections.Generic; +using System.Text; +using static MonogameLibrary.UI.Elements.Button; + +namespace MonogameLibrary.UI.Elements +{ + public class Rect : DrawableTextedUiElement, IInteractable + { + public delegate void OnButtonPressed(); + public event OnButtonPressed? RightButtonPressed; + public event OnButtonPressed? LeftButtonPressed; + protected HoverState hoverState = HoverState.None; + + public Rect(UIManager manager, int layerIndex = 0) : base(manager, layerIndex) + { + } + + public virtual bool InteractUpdate(MouseState mouseState, MouseState prevmouseState) + { + //if (Manager.) + if (rectangle.Intersects(new Rectangle(mouseState.Position, Point.Zero))) + { + if (mouseState.LeftButton == ButtonState.Pressed || mouseState.RightButton == ButtonState.Pressed) + { + hoverState = HoverState.Pressing; + } + else + { + hoverState = HoverState.Hovering; + } + if (prevmouseState.LeftButton == ButtonState.Pressed) + { + if (mouseState.LeftButton != prevmouseState.LeftButton) + { + hoverState = HoverState.Pressing; + LeftButtonPressed?.Invoke(); + return true; + } + } + else if(prevmouseState.RightButton == ButtonState.Pressed) + { + if (mouseState.RightButton != prevmouseState.RightButton) + { + RightButtonPressed?.Invoke(); + return true; + } + } + } + else + { + hoverState = HoverState.None; + } + return false; + } + public override void Draw(SpriteBatch _spriteBatch) + { + _spriteBatch.Draw(texture, rectangle, Color.White); + DrawText(_spriteBatch); + } + } +}