Merge branch 'main' into livingEntitiesHunchMan

This commit is contained in:
polten0 2023-08-17 13:40:24 +03:00
commit 7b6d750bc1
19 changed files with 498 additions and 151 deletions

View file

@ -5,6 +5,7 @@ using System;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection.Metadata;
namespace AnimationsFileCreator namespace AnimationsFileCreator
{ {
@ -62,7 +63,7 @@ namespace AnimationsFileCreator
container.TextureFrameInterval = 1; container.TextureFrameInterval = 1;
container.Id = id; container.Id = id;
string json = JsonConvert.SerializeObject(container); string json = JsonConvert.SerializeObject(container);
StreamWriter writer = new StreamWriter("../../../../DangerousD/Content/animations/" + id); StreamWriter writer = new StreamWriter("../../../../DangerousD/Content/animations/"+id);
writer.WriteLine(json); writer.WriteLine(json);
writer.Close(); writer.Close();
} }

View file

@ -0,0 +1,20 @@
{
"id": "SilasAttack",
"textureName": "MonstersAnimations",
"startSpriteRectangle": {
"X": 1,
"Y": 618,
"Width": 72,
"Height": 80
},
"frameSecond": [
{
"Item1": 0,
"Item2": 5
}
],
"textureFrameInterval": 1,
"framesCount": 2,
"isCycle": true,
"offset": "0, 0"
}

View file

@ -0,0 +1 @@
{"id":"SilasHandMove","textureName":"MonstersAnimations","startSpriteRectangle":{"X":197,"Y":618,"Width":24,"Height":24},"frameSecond":[{"Item1":0,"Item2":8}],"textureFrameInterval":1,"framesCount":2,"isCycle":true,"offset":"0, 0"}

View file

@ -0,0 +1,20 @@
{
"id": "SilasMove",
"textureName": "MonstersAnimations",
"startSpriteRectangle": {
"X": 1,
"Y": 618,
"Width": 72,
"Height": 80
},
"frameSecond": [
{
"Item1": 0,
"Item2": 60
}
],
"textureFrameInterval": 1,
"framesCount": 2,
"isCycle": true,
"offset": "0, 0"
}

View file

@ -10,6 +10,7 @@ using System.Threading.Tasks;
using DangerousD.GameCore.Managers; using DangerousD.GameCore.Managers;
using MonogameLibrary.UI.Base; using MonogameLibrary.UI.Base;
using System.Diagnostics; using System.Diagnostics;
using DangerousD.GameCore.Network;
namespace DangerousD.GameCore.GUI namespace DangerousD.GameCore.GUI
{ {
@ -32,8 +33,32 @@ namespace DangerousD.GameCore.GUI
Elements.Add(new Label(Manager) { rectangle = new Rectangle(screenWidth / 30 * 2, screenHeight / 30 * 5, Elements.Add(new Label(Manager) { rectangle = new Rectangle(screenWidth / 30 * 2, screenHeight / 30 * 5,
screenWidth / 30 * 26, screenHeight / 15 * 10) }); screenWidth / 30 * 26, screenHeight / 15 * 10) });
// Buttons // Buttons and ip textbox
{ {
TextBox searchBarTextBox = new TextBox(Manager)
{
rectangle = new Rectangle(screenWidth / 30 * 14, screenHeight / 30,
screenWidth / 30 * 10, screenHeight / 30 * 3),
text = "ip",
scale = 0.16f,
fontColor = Color.Gray,
fontName = "font2",
textAligment = TextAligment.Left
};
searchBarTextBox.TextChanged += input => {
if (searchBarTextBox.fontColor == Color.Gray)
{
searchBarTextBox.text = ""; searchBarTextBox.fontColor = Color.Black;
}
};
searchBarTextBox.StopChanging += input => {
if (input.Length == 0)
{
searchBarTextBox.fontColor = Color.Gray;
searchBarTextBox.text = "ip";
}
};
Button backButton = new ButtonText(Manager) Button backButton = new ButtonText(Manager)
{ {
rectangle = new Rectangle(screenWidth / 30, screenHeight / 30, 60, 50), rectangle = new Rectangle(screenWidth / 30, screenHeight / 30, 60, 50),
@ -56,6 +81,8 @@ namespace DangerousD.GameCore.GUI
}; };
hostButton.LeftButtonPressed += () => { hostButton.LeftButtonPressed += () => {
AppManager.Instance.NetworkManager.HostInit(AppManager.Instance.IpAddress);
}; };
Button refreshButton = new ButtonText(Manager) Button refreshButton = new ButtonText(Manager)
@ -79,7 +106,7 @@ namespace DangerousD.GameCore.GUI
fontName = "font2" fontName = "font2"
}; };
joinSelectedButton.LeftButtonPressed += () => { joinSelectedButton.LeftButtonPressed += () => {
AppManager.Instance.NetworkManager.ClientInit(AppManager.Instance.IpAddress);
}; };
Button joinByIpButton = new ButtonText(Manager) Button joinByIpButton = new ButtonText(Manager)
{ {
@ -90,34 +117,7 @@ namespace DangerousD.GameCore.GUI
fontName = "font2" fontName = "font2"
}; };
joinByIpButton.LeftButtonPressed += () => { joinByIpButton.LeftButtonPressed += () => {
AppManager.Instance.NetworkManager.ClientInit(searchBarTextBox.text);
};
}
// SearchBar
{
TextBox searchBarTextBox = new TextBox(Manager) {
rectangle = new Rectangle(screenWidth / 30 * 14, screenHeight / 30,
screenWidth / 30 * 10, screenHeight / 30 * 3),
text = "ip",
scale = 0.16f,
fontColor = Color.Gray,
fontName = "font2",
textAligment = TextAligment.Left
};
searchBarTextBox.TextChanged += input => {
if (searchBarTextBox.fontColor == Color.Gray)
{
searchBarTextBox.text = ""; searchBarTextBox.fontColor = Color.Black;
}
};
searchBarTextBox.StopChanging += input => {
if (input.Length == 0)
{
searchBarTextBox.fontColor = Color.Gray;
searchBarTextBox.text = "ip";
}
}; };
} }
} }

View file

@ -35,6 +35,7 @@ internal class MenuGUI : AbstractGui
butSingle.LeftButtonPressed += () => butSingle.LeftButtonPressed += () =>
{ {
AppManager.Instance.ChangeGameState(GameState.Game); AppManager.Instance.ChangeGameState(GameState.Game);
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.SinglePlayer);
}; };
var butMulti = new ButtonText(Manager) { rectangle = new Rectangle((wigth - 300) / 2, 190, 300, 50), text = "Multiplayer", fontName = "ButtonFont" }; var butMulti = new ButtonText(Manager) { rectangle = new Rectangle((wigth - 300) / 2, 190, 300, 50), text = "Multiplayer", fontName = "ButtonFont" };
Elements.Add(butMulti); Elements.Add(butMulti);

View file

@ -1,4 +1,8 @@
using System; using DangerousD.GameCore.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,7 +10,60 @@ using System.Threading.Tasks;
namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
{ {
internal class SilasHands public class SilasHands : CoreEnemy
{ {
public SilasHands(Vector2 position) : base(position)
{
name = "SilasHand";
Width = 48;
Height = 48;
monster_health = 2;
monster_speed = 2;
acceleration = Vector2.Zero;
}
protected override GraphicsComponent GraphicsComponent { get; }=new GraphicsComponent(new List<string>() { "SilasHandMove" }, "SilasHandMove");
public override void Attack()
{
throw new NotImplementedException();
}
public override void Death()
{
throw new NotImplementedException();
}
public override void Move(GameTime gameTime)
{
if (Pos.Y> AppManager.Instance.GameManager.GetPlayer1.Pos.Y)
{
velocity.Y = monster_speed;
}
else
{
velocity.Y = 0;
}
if (Pos.X> AppManager.Instance.GameManager.GetPlayer1.Pos.X)
{
velocity.X = -monster_speed;
}
else
{
velocity.X = monster_speed;
}
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
if ((Pos.X + 20 <= AppManager.Instance.GameManager.GetPlayer1.Pos.X || Pos.X - 20 >= AppManager.Instance.GameManager.GetPlayer1.Pos.X)&&(Pos.Y + 20 <= AppManager.Instance.GameManager.GetPlayer1.Pos.Y || Pos.Y - 20 >= AppManager.Instance.GameManager.GetPlayer1.Pos.Y))
{
AppManager.Instance.GameManager.GetPlayer1.Death(name);
}
GraphicsComponent.Update();
}
} }
} }

View file

@ -1,4 +1,8 @@
using System; using DangerousD.GameCore.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,7 +10,64 @@ using System.Threading.Tasks;
namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
{ {
public class SilasMaster public class SilasMaster : CoreEnemy
{ {
private int attackTime = 60;
private int moveTime = 360;
private int currentTime = 0;
public SilasMaster(Vector2 position) : base(position)
{
name = "SilasMaster";
Width = 144;
Height = 160;
monster_health = 15;
monster_speed = 4;
acceleration = Vector2.Zero;
}
protected override GraphicsComponent GraphicsComponent { get; } = new GraphicsComponent(new List<string>() { "SilasMove", "SilasAttack" }, "SilasMove");
public override void Attack()
{
if (currentTime==0)
{
GraphicsComponent.StartAnimation("SilasAttack");
}
else if (currentTime >= attackTime)
{
GraphicsComponent.StartAnimation("SilasMove");
currentTime = 0;
}
currentTime++;
}
public override void Death()
{
throw new NotImplementedException();
}
public override void Move(GameTime gameTime)
{
if (currentTime == 0)
{
GraphicsComponent.StartAnimation("SilasMove");
}
else if (currentTime >= moveTime)
{
GraphicsComponent.StartAnimation("SilasAttack");
currentTime = 0;
}
currentTime++;
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (GraphicsComponent.CurrentAnimation.Id=="SilasMove")
{
Move(gameTime);
}
else
{
Attack();
}
}
} }
} }

View file

@ -7,23 +7,27 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using DangerousD.GameCore.Managers;
namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
{ {
public class Zombie : CoreEnemy public class Zombie : CoreEnemy
{ {
private bool isGoRight = true; private bool isGoRight = true;
int leftBorder; float leftBorder;
int rightBorder; float rightBorder;
bool isAttaking = false; bool isAttaking = false;
bool isTarget = false;
PhysicsManager physicsManager;
public Zombie(Vector2 position) : base(position) public Zombie(Vector2 position) : base(position)
{ {
Width = 72; Width = 24;
Height = 120; Height = 40;
monster_speed = 3; monster_speed = 3;
name = "Zombie"; name = "Zombie";
leftBorder = (int)position.X - 60; leftBorder = (int)position.X - 100;
rightBorder = (int)position.X + 120; rightBorder = (int)position.X + 100;
physicsManager = new PhysicsManager();
} }
protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "ZombieMoveRight", "ZombieMoveLeft", "ZombieRightAttack", "ZombieLeftAttack" }, "ZombieMoveLeft"); protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "ZombieMoveRight", "ZombieMoveLeft", "ZombieRightAttack", "ZombieLeftAttack" }, "ZombieMoveLeft");
@ -31,6 +35,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
{ {
if (!isAttaking) if (!isAttaking)
{ {
Target();
Move(gameTime); Move(gameTime);
} }
@ -106,5 +111,25 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
} }
base.OnCollision(gameObject); base.OnCollision(gameObject);
} }
public void Target()
{
if(physicsManager.RayCast(this, AppManager.Instance.GameManager.players[0]) == null)
{
if(isGoRight && this._pos.X <= AppManager.Instance.GameManager.players[0].Pos.X)
{
isTarget = true;
leftBorder = Pos.X - 10;
rightBorder = Pos.X + AppManager.Instance.GameManager.players[0].Pos.X;
}
else if(!isGoRight && this._pos.X >= AppManager.Instance.GameManager.players[0].Pos.X)
{
isTarget = true;
rightBorder = Pos.X + 10;
leftBorder = AppManager.Instance.GameManager.players[0].Pos.X;
}
}
}
} }
} }

View file

@ -20,6 +20,9 @@ namespace DangerousD.GameCore.GameObjects.PlayerDeath
protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> {"DeathFromZombie"}, protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> {"DeathFromZombie"},
"DeathFromZombie"); "DeathFromZombie");
public GraphicsComponent Gr => GraphicsComponent;
private void PlayDeath(string deathName) private void PlayDeath(string deathName)
{ {
if (GraphicsComponent.GetCurrentAnimation != "DeathFromZombie") if (GraphicsComponent.GetCurrentAnimation != "DeathFromZombie")

View file

@ -16,10 +16,8 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{ {
Width = 32; Width = 32;
Height = 64; Height = 64;
/*GraphicsComponent.actionOfAnimationEnd += () => AppManager.Instance.InputManager.MovEventJump += AnimationJump;
{
AppManager.Instance.ChangeGameState(GameState.GameOver);
};*/
} }
public bool IsAlive { get { return isAlive; } } public bool IsAlive { get { return isAlive; } }
@ -35,9 +33,19 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
if(monsterName == "Zombie") if(monsterName == "Zombie")
{ {
DeathRectangle deathRectangle = new DeathRectangle(Pos, "DeathFrom" + monsterName); DeathRectangle deathRectangle = new DeathRectangle(Pos, "DeathFrom" + monsterName);
//GraphicsComponent.actionOfAnimationEnd(); deathRectangle.Gr.actionOfAnimationEnd += (a) =>
{
if (a == "DeathFrom" + monsterName)
{
AppManager.Instance.ChangeGameState(GameState.GameOver);
}
};
} }
isAlive = false; isAlive = false;
} }
public void AnimationJump()
{
velocity.Y = -300;
}
} }
} }

View file

@ -11,12 +11,19 @@ namespace DangerousD.GameCore.Graphics
public class GraphicsComponent public class GraphicsComponent
{ {
public Action<string> actionOfAnimationEnd; public event Action<string> actionOfAnimationEnd;
private List<AnimationContainer> animations; private List<AnimationContainer> animations;
private List<Texture2D> textures; private List<Texture2D> textures;
private List<string> texturesNames; private List<string> texturesNames;
private AnimationContainer currentAnimation; private AnimationContainer currentAnimation;
public AnimationContainer CurrentAnimation
{
get
{
return currentAnimation;
}
}
public string LastAnimation { get; set; }
public string GetCurrentAnimation public string GetCurrentAnimation
{ {
get { return currentAnimation.Id; } get { return currentAnimation.Id; }
@ -26,16 +33,21 @@ namespace DangerousD.GameCore.Graphics
//private SpriteBatch _spriteBatch; //private SpriteBatch _spriteBatch;
private int currentFrame; private int currentFrame;
public int CurrentFrame
{
get
{
return currentFrame;
}
}
private int interval; private int interval;
private int lastInterval; private int lastInterval;
private Rectangle sourceRectangle; private Rectangle sourceRectangle;
public GraphicsComponent(List<string> animationsId, string neitralAnimationId) public GraphicsComponent(List<string> animationsId, string neitralAnimationId)
{ {
//this._spriteBatch = _spriteBatch; //this._spriteBatch = _spriteBatch;
currentFrame = 0; currentFrame = 0;
lastInterval = 1; lastInterval = 1;
LoadAnimations(animationsId, neitralAnimationId); LoadAnimations(animationsId, neitralAnimationId);
currentAnimation = neitralAnimation; currentAnimation = neitralAnimation;
SetInterval(); SetInterval();

View file

@ -10,18 +10,20 @@ namespace DangerousD.GameCore.Levels
{ {
public void InitLevel() public void InitLevel()
{ {
new Player(new Vector2(50,100)); //Spider down-up
var Zombie = new Zombie(new Vector2(140, 128)); new Player(new Vector2(80,0));
var Frank = new Frank(new Vector2(384, 128)); var Zombie = new Zombie(new Vector2(250, 128));
var Spider = new Spider(new Vector2(112, 0)); //var Frank = new Frank(new Vector2(384, 128));
var FlameSkull = new FlameSkull(new Vector2(512, 0)); //var Spider = new Spider(new Vector2(112, 0));
var Werewolf = new Werewolf(new Vector2(640, 0)); //var FlameSkull = new FlameSkull(new Vector2(512, 0));
var Ghost = new Ghost(new Vector2(730, 0)); //var Werewolf = new Werewolf(new Vector2(640, 0));
var FrankBalls = new FrankBalls(new Vector2(Frank.Pos.X, Frank.Pos.Y)); //var Ghost = new Ghost(new Vector2(730, 0));
//var FrankBalls = new FrankBalls(new Vector2(Frank.Pos.X, Frank.Pos.Y));
//var SilasHand = new SilasHands(new Vector2(200,64));
//var SilasMaster = new SilasMaster(new Vector2(400, 64));
var HunchMan = new Hunchman(new Vector2(300, 100)); var HunchMan = new Hunchman(new Vector2(300, 100));
//Spider down-up
new GrassBlock(new Vector2(0, 224)); new GrassBlock(new Vector2(0, 224));
for (int i = 0; i < 50; i++) for (int i = 0; i < 50; i++)
@ -29,6 +31,10 @@ namespace DangerousD.GameCore.Levels
new GrassBlock(new Vector2(i*32, 256)); new GrassBlock(new Vector2(i*32, 256));
} }
new GrassBlock(new Vector2(500, 224)); new GrassBlock(new Vector2(500, 224));
Player player = new Player(new Vector2(400, 64));
player.AnimationJump();
//new GrassBlock(new Vector2(500, 224));
} }
} }
} }

View file

@ -14,12 +14,15 @@ using DangerousD.GameCore.Managers;
namespace DangerousD.GameCore namespace DangerousD.GameCore
{ {
public enum GameState { Menu, Options, Lobby, Game, Login, GameOver } public enum GameState { Menu, Options, Lobby, Game, Login, GameOver }
public enum MultiPlayerStatus { SinglePlayer, Host, Client }
public class AppManager : Game public class AppManager : Game
{ {
public static AppManager Instance { get; private set; } public static AppManager Instance { get; private set; }
public string IpAddress { get; private set; } = "127.0.0.1";
private GraphicsDeviceManager _graphics; private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch; private SpriteBatch _spriteBatch;
GameState gameState; public GameState gameState { get; private set; }
public MultiPlayerStatus multiPlayerStatus { get; private set; } = MultiPlayerStatus.SinglePlayer;
IDrawableObject MenuGUI; IDrawableObject MenuGUI;
IDrawableObject OptionsGUI; IDrawableObject OptionsGUI;
IDrawableObject LoginGUI; IDrawableObject LoginGUI;
@ -45,6 +48,8 @@ namespace DangerousD.GameCore
SettingsManager = new SettingsManager(); SettingsManager = new SettingsManager();
SettingsManager.LoadSettings(); SettingsManager.LoadSettings();
NetworkManager.GetReceivingMessages += NetworkSync;
resolution = SettingsManager.Resolution; resolution = SettingsManager.Resolution;
_graphics.PreferredBackBufferWidth = resolution.X; _graphics.PreferredBackBufferWidth = resolution.X;
_graphics.PreferredBackBufferHeight = resolution.Y; _graphics.PreferredBackBufferHeight = resolution.Y;
@ -162,10 +167,41 @@ namespace DangerousD.GameCore
case GameState.Game: case GameState.Game:
GameManager.mapManager.LoadLevel(""); GameManager.mapManager.LoadLevel("");
break; break;
case GameState.GameOver:
break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
} }
public void NetworkSync(NetworkTask networkTask)
{
//TODO
return;
switch (networkTask.operation)
{
case NetworkTaskOperationEnum.TakeDamage:
break;
case NetworkTaskOperationEnum.SendSound:
SoundManager.StartSound(networkTask.name, networkTask.position, GameManager.GetPlayer1.Pos);
break;
case NetworkTaskOperationEnum.CreateEntity:
break;
case NetworkTaskOperationEnum.SendPosition:
break;
case NetworkTaskOperationEnum.ChangeState:
break;
case NetworkTaskOperationEnum.ConnectToHost:
break;
case NetworkTaskOperationEnum.GetClientPlayerId:
break;
default:
break;
}
}
public void SetMultiplayerState(MultiPlayerStatus multiPlayerStatus)
{
this.multiPlayerStatus = multiPlayerStatus;
}
} }
} }

View file

@ -12,7 +12,7 @@ namespace DangerousD.GameCore
{ {
public class GameManager public class GameManager
{ {
public List<GameObject> GetAllGameObjects { get; private set; }
public List<LivingEntity> livingEntities; public List<LivingEntity> livingEntities;
public List<Entity> entities; public List<Entity> entities;
public List<MapObject> mapObjects; public List<MapObject> mapObjects;
@ -23,6 +23,7 @@ namespace DangerousD.GameCore
public Player GetPlayer1 { get; private set; } public Player GetPlayer1 { get; private set; }
public GameManager() public GameManager()
{ {
GetAllGameObjects = new List<GameObject>();
livingEntities = new List<LivingEntity>(); livingEntities = new List<LivingEntity>();
mapObjects = new List<MapObject>(); mapObjects = new List<MapObject>();
entities = new List<Entity>(); entities = new List<Entity>();

View file

@ -136,7 +136,7 @@ namespace DangerousD.GameCore.Managers
Rectangle rectangle; Rectangle rectangle;
Vector2 distance = entity1.Pos - entity2.Pos; Vector2 distance = entity1.Pos - entity2.Pos;
rectangle = new Rectangle((int)entity1.Pos.X, (int)entity1.Pos.Y, entity2.Width, entity2.Height); rectangle = new Rectangle((int)entity1.Pos.X, (int)entity1.Pos.Y - 5, entity2.Width, entity2.Height);
GameObject gameObject = null; GameObject gameObject = null;
double length = distance.Length(); double length = distance.Length();
@ -144,30 +144,36 @@ namespace DangerousD.GameCore.Managers
{ {
rectangle.X = (int)(entity2.Pos.X + (i / length) * distance.X); rectangle.X = (int)(entity2.Pos.X + (i / length) * distance.X);
rectangle.Y = (int)(entity2.Pos.Y + (i / length) * distance.Y); rectangle.Y = (int)(entity2.Pos.Y + (i / length) * distance.Y);
if (i == length - 1)
{
return null;
} }
for (int j = 0; j < AppManager.Instance.GameManager.entities.Count; j++)
for (int i = 0; i < AppManager.Instance.GameManager.entities.Count; i++)
{ {
if (AppManager.Instance.GameManager.entities[i].Rectangle.Intersects(rectangle)) if (AppManager.Instance.GameManager.entities[j].Rectangle.Intersects(rectangle))
{ {
return AppManager.Instance.GameManager.entities[i]; gameObject = AppManager.Instance.GameManager.entities[j];
} }
} }
for (int i = 0; i < AppManager.Instance.GameManager.mapObjects.Count; i++) for (int r = 0; r < AppManager.Instance.GameManager.mapObjects.Count; r++)
{ {
if (AppManager.Instance.GameManager.mapObjects[i].Rectangle.Intersects(rectangle)) if (AppManager.Instance.GameManager.mapObjects[r].Rectangle.Intersects(rectangle))
{ {
return AppManager.Instance.GameManager.mapObjects[i]; gameObject = AppManager.Instance.GameManager.mapObjects[r];
}
}
for (int w = 0; w < AppManager.Instance.GameManager.livingEntities.Count; w++)
{
if (AppManager.Instance.GameManager.livingEntities[w].Rectangle.Intersects(rectangle))
{
gameObject = AppManager.Instance.GameManager.livingEntities[w];
}
} }
} }
if (gameObject == entity1)
for (int i = 0; i < AppManager.Instance.GameManager.livingEntities.Count; i++)
{ {
if (AppManager.Instance.GameManager.livingEntities[i].Rectangle.Intersects(rectangle)) return null;
{
return AppManager.Instance.GameManager.livingEntities[i];
}
} }
return gameObject; return gameObject;
} }
@ -176,35 +182,57 @@ namespace DangerousD.GameCore.Managers
Rectangle rectangle; Rectangle rectangle;
Vector2 direction = entity1.Pos - targetCast; Vector2 direction = entity1.Pos - targetCast;
rectangle = new Rectangle((int)targetCast.X, (int)targetCast.Y, 1, 1); rectangle = new Rectangle((int)targetCast.X, (int)targetCast.Y, 1, 1);
GameObject gameObject = null;
double k = direction.Length(); double k = direction.Length();
for (int i = 0; i < k; i++) for (int i = 0; i < k; i++)
{ {
rectangle.X = (int)(targetCast.X + (i / k) * direction.X); rectangle.X = (int)(targetCast.X + (i / k) * direction.X);
rectangle.Y = (int)(targetCast.Y + (i / k) * direction.X); rectangle.Y = (int)(targetCast.Y + (i / k) * direction.X);
} for (int j = 0; j < AppManager.Instance.GameManager.entities.Count; j++)
for (int i = 0; i < AppManager.Instance.GameManager.entities.Count; i++)
{ {
if (AppManager.Instance.GameManager.entities[i].Rectangle.Intersects(rectangle)) if (AppManager.Instance.GameManager.entities[j].Rectangle.Intersects(rectangle))
{ {
return AppManager.Instance.GameManager.entities[i]; gameObject = AppManager.Instance.GameManager.entities[j];
} }
} }
for (int i = 0; i < AppManager.Instance.GameManager.mapObjects.Count; i++) for (int r = 0; r < AppManager.Instance.GameManager.mapObjects.Count; r++)
{ {
if (AppManager.Instance.GameManager.mapObjects[i].Rectangle.Intersects(rectangle)) if (AppManager.Instance.GameManager.mapObjects[r].Rectangle.Intersects(rectangle))
{ {
return AppManager.Instance.GameManager.mapObjects[i]; gameObject = AppManager.Instance.GameManager.mapObjects[r];
} }
} }
for (int i = 0; i < AppManager.Instance.GameManager.livingEntities.Count; i++) for (int w = 0; w < AppManager.Instance.GameManager.livingEntities.Count; w++)
{ {
if (AppManager.Instance.GameManager.livingEntities[i].Rectangle.Intersects(rectangle)) if (AppManager.Instance.GameManager.livingEntities[w].Rectangle.Intersects(rectangle))
{ {
return AppManager.Instance.GameManager.livingEntities[i]; gameObject = AppManager.Instance.GameManager.livingEntities[w];
} }
} }
}
if (gameObject == entity1)
{
return null; return null;
} }
return gameObject;
}
public List<GameObject> CheckRectangle(Rectangle rectangle, Type type)
{
var gameObjects = AppManager.Instance.GameManager.GetAllGameObjects;
List<GameObject> intersected = new List<GameObject>();
for (int i = 0; i < gameObjects.Count; i++)
{
if (gameObjects[i].GetType() == type)
{
if (gameObjects[i].Rectangle.Intersects(rectangle))
{
intersected.Add(gameObjects[i]);
}
}
}
return intersected;
}
} }
} }

View file

@ -33,8 +33,11 @@ namespace DangerousD.GameCore
sound.SoundEffect.IsLooped = false; sound.SoundEffect.IsLooped = false;
sound.SoundEffect.Play(); sound.SoundEffect.Play();
PlayingSounds.Add(sound); PlayingSounds.Add(sound);
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host)
{
AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(Vector2.Zero, soundName));
}
} }
public void StartSound(string soundName, Vector2 soundPos, Vector2 playerPos) // запустить звук у которого есть позиция public void StartSound(string soundName, Vector2 soundPos, Vector2 playerPos) // запустить звук у которого есть позиция
{ {
var sound = new Sound(Sounds[soundName], soundPos); var sound = new Sound(Sounds[soundName], soundPos);
@ -42,6 +45,10 @@ namespace DangerousD.GameCore
sound.SoundEffect.Volume = (float)sound.GetDistance(playerPos) / MaxSoundDistance; sound.SoundEffect.Volume = (float)sound.GetDistance(playerPos) / MaxSoundDistance;
sound.SoundEffect.Play(); sound.SoundEffect.Play();
PlayingSounds.Add(sound); PlayingSounds.Add(sound);
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host)
{
AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(soundPos, soundName));
}
} }
public void StopAllSounds() // остановка всех звуков public void StopAllSounds() // остановка всех звуков
{ {

View file

@ -20,21 +20,28 @@ namespace DangerousD.GameCore.Network
string state; string state;
private void Init(string IpAddress) private void Init(string IpAddress)
{
try
{ {
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(IpAddress); IPAddress address = IPAddress.Parse(IpAddress);
int port = 8000; int port = 8000;
endPoint = new IPEndPoint(address, port); endPoint = new IPEndPoint(address, port);
} }
catch { }
}
private void AcceptSockets() private void AcceptSockets()
{ {
while (true) while (true)
{
try
{ {
Socket clientSocket = socket.Accept(); Socket clientSocket = socket.Accept();
clientSockets.Add(clientSocket); clientSockets.Add(clientSocket);
Thread receiveThread = new Thread(BeginHostReceive); Thread receiveThread = new Thread(BeginHostReceive);
receiveThread.Start(clientSocket); receiveThread.Start(clientSocket);
Console.WriteLine("Connected"); }
catch { }
} }
} }
@ -42,6 +49,8 @@ namespace DangerousD.GameCore.Network
{ {
Socket clientSocket = clSocket as Socket; Socket clientSocket = clSocket as Socket;
while (clientSocket != null) while (clientSocket != null)
{
try
{ {
byte[] bytesCount = new byte[4]; byte[] bytesCount = new byte[4];
clientSocket.Receive(bytesCount); clientSocket.Receive(bytesCount);
@ -49,8 +58,12 @@ namespace DangerousD.GameCore.Network
StateObject so = new StateObject(clientSocket, Data); StateObject so = new StateObject(clientSocket, Data);
IAsyncResult count = clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so); IAsyncResult count = clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
} }
catch { }
}
} }
public void HostInit(string IpAddress) public void HostInit(string IpAddress)
{
try
{ {
Init(IpAddress); Init(IpAddress);
socket.Bind(endPoint); socket.Bind(endPoint);
@ -58,9 +71,12 @@ namespace DangerousD.GameCore.Network
Thread acceptThread = new Thread(AcceptSockets); Thread acceptThread = new Thread(AcceptSockets);
acceptThread.Start(); acceptThread.Start();
state = "Host"; state = "Host";
Console.WriteLine("Start Accept"); }
catch { }
} }
public void ClientInit(string IpAddress) public void ClientInit(string IpAddress)
{
try
{ {
Init(IpAddress); Init(IpAddress);
socket.Connect(endPoint); socket.Connect(endPoint);
@ -69,11 +85,17 @@ namespace DangerousD.GameCore.Network
Thread ReceivingThread = new Thread(ReceiveMsgFromHost); Thread ReceivingThread = new Thread(ReceiveMsgFromHost);
ReceivingThread.Start(); ReceivingThread.Start();
} }
catch { }
}
public void SendMsg(NetworkTask networkTask) public void SendMsg(NetworkTask networkTask)
{ {
//TODO
return;
byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask)); byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask));
int count = Data.Length; int count = Data.Length;
if (state == "Host") if (state == "Host")
{
try
{ {
foreach (Socket socket in clientSockets) foreach (Socket socket in clientSockets)
{ {
@ -81,15 +103,23 @@ namespace DangerousD.GameCore.Network
socket.Send(Data); socket.Send(Data);
} }
} }
catch { }
}
else else
{
try
{ {
socket.Send(BitConverter.GetBytes(count)); socket.Send(BitConverter.GetBytes(count));
socket.Send(Data); socket.Send(Data);
} }
catch { }
}
} }
private void ReceiveMsgFromHost() private void ReceiveMsgFromHost()
{ {
while (true) while (true)
{
try
{ {
byte[] bytesCount = new byte[4]; byte[] bytesCount = new byte[4];
socket.Receive(bytesCount); socket.Receive(bytesCount);
@ -97,9 +127,13 @@ namespace DangerousD.GameCore.Network
StateObject so = new StateObject(socket, Data); StateObject so = new StateObject(socket, Data);
IAsyncResult count = socket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so); IAsyncResult count = socket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
} }
catch { }
}
} }
private void AsyncReceiveCallback(IAsyncResult ar) private void AsyncReceiveCallback(IAsyncResult ar)
{
try
{ {
StateObject so = ar.AsyncState as StateObject; StateObject so = ar.AsyncState as StateObject;
Socket clientSocket = so.workSocket; Socket clientSocket = so.workSocket;
@ -115,5 +149,7 @@ namespace DangerousD.GameCore.Network
GetReceivingMessages(JsonConvert.DeserializeObject<NetworkTask>(so.sb.ToString())); GetReceivingMessages(JsonConvert.DeserializeObject<NetworkTask>(so.sb.ToString()));
} }
} }
catch { }
}
} }
} }

View file

@ -13,6 +13,7 @@ namespace DangerousD.GameCore.Network
public NetworkTaskOperationEnum operation { get; set; } public NetworkTaskOperationEnum operation { get; set; }
public string name { get; set; } public string name { get; set; }
public int value { get; set; } public int value { get; set; }
public bool isParam { get; set; }
public int objId { get; set; } public int objId { get; set; }
public Vector2 position { get; set; } public Vector2 position { get; set; }
public Vector2 velocity { get; set; } public Vector2 velocity { get; set; }
@ -102,5 +103,28 @@ namespace DangerousD.GameCore.Network
operation = NetworkTaskOperationEnum.GetClientPlayerId; operation = NetworkTaskOperationEnum.GetClientPlayerId;
objId = PlayerId; objId = PlayerId;
} }
/// <summary>
/// Универсальный конструктор для нестандартных операций. То, что не нужно(кроме операции) делать null.
/// </summary>
/// <param name="operation"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="isParam"></param>
/// <param name="objId"></param>
/// <param name="position"></param>
/// <param name="velocity"></param>
/// <param name="type"></param>
public NetworkTask(NetworkTaskOperationEnum operation, string name, int value, bool isParam, int objId, Vector2 position, Vector2 velocity, Type type)
{
this.operation = operation;
this.name = name;
this.value = value;
this.isParam = isParam;
this.objId = objId;
this.position = position;
this.velocity = velocity;
this.type = type;
}
} }
} }