Pre 3 player support
This commit is contained in:
parent
a1adb49918
commit
424583837a
6 changed files with 41 additions and 19 deletions
|
@ -27,15 +27,17 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
|
|||
private bool isAttacked = false;
|
||||
public GameObject objectAttack;
|
||||
|
||||
public Player(Vector2 position) : base(position)
|
||||
public Player(Vector2 position, bool isNetworkPlayer = false) : base(position)
|
||||
{
|
||||
Width = 16;
|
||||
Height = 32;
|
||||
|
||||
if (!isNetworkPlayer)
|
||||
{
|
||||
AppManager.Instance.InputManager.ShootEvent += Shoot;
|
||||
|
||||
AppManager.Instance.InputManager.MovEventJump += Jump;
|
||||
AppManager.Instance.InputManager.MovEventDown += MoveDown;
|
||||
}
|
||||
|
||||
velocity = new Vector2(0, 0);
|
||||
rightBorder = (int)position.X + 100;
|
||||
|
|
|
@ -231,16 +231,22 @@ namespace DangerousD.GameCore
|
|||
case NetworkTaskOperationEnum.ChangeState:
|
||||
break;
|
||||
case NetworkTaskOperationEnum.ConnectToHost:
|
||||
Player connectedPlayer = new Player(Vector2.Zero);
|
||||
Player connectedPlayer = new Player(Vector2.Zero, true);
|
||||
NetworkTasks.Add(new NetworkTask(connectedPlayer.id));
|
||||
NetworkTask task = new NetworkTask();
|
||||
NetworkTasks.Add(task.AddConnectedPlayer(GameManager.GetPlayer1.id, GameManager.GetPlayer1.Pos));
|
||||
foreach (Player player in GameManager.players)
|
||||
{
|
||||
if (player.id != connectedPlayer.id)
|
||||
{
|
||||
NetworkTasks.Add(task.AddConnectedPlayer(player.id, player.Pos));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case NetworkTaskOperationEnum.GetClientPlayerId:
|
||||
GameManager.GetPlayer1.id = networkTask.objId;
|
||||
break;
|
||||
case NetworkTaskOperationEnum.AddConnectedPlayer:
|
||||
Player remoteConnectedPlayer = new Player(networkTask.position);
|
||||
Player remoteConnectedPlayer = new Player(networkTask.position, true);
|
||||
remoteConnectedPlayer.id = networkTask.objId;
|
||||
GameManager.players.Add(remoteConnectedPlayer);
|
||||
break;
|
||||
|
|
|
@ -99,7 +99,8 @@ namespace DangerousD.GameCore
|
|||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
AppManager.Instance.DebugHUD.Set("playerId: ", GetPlayer1.id.ToString());
|
||||
Player p = players.Find(x => x.id == 16);
|
||||
if (p != null) AppManager.Instance.DebugHUD.Set("NextPlayerVelocity: ", p.velocity.ToString());
|
||||
if (AppManager.Instance.NetworkTasks.Count > 0)
|
||||
{
|
||||
AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList());
|
||||
|
@ -117,7 +118,6 @@ namespace DangerousD.GameCore
|
|||
{
|
||||
livingEntitiesWithoutPlayers[i].Update(gameTime);
|
||||
}
|
||||
GetPlayer1.Update(gameTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -125,7 +125,6 @@ namespace DangerousD.GameCore
|
|||
{
|
||||
livingEntitiesWithoutPlayers[i].PlayAnimation();
|
||||
}
|
||||
GetPlayer1.Update(gameTime);
|
||||
}
|
||||
GetPlayer1.Update(gameTime);
|
||||
foreach (var item in otherObjects)
|
||||
|
|
|
@ -9,6 +9,7 @@ using Microsoft.Xna.Framework.Graphics;
|
|||
using System.Xml.Serialization;
|
||||
using DangerousD.GameCore.GameObjects;
|
||||
using System.Globalization;
|
||||
using DangerousD.GameCore.GameObjects.LivingEntities;
|
||||
|
||||
namespace DangerousD.GameCore.Managers
|
||||
{
|
||||
|
@ -106,7 +107,16 @@ namespace DangerousD.GameCore.Managers
|
|||
foreach (XmlNode entity in group.ChildNodes)
|
||||
{
|
||||
Type type = Type.GetType($"DangerousD.GameCore.GameObjects.{entityType}");
|
||||
Entity inst = (Entity)Activator.CreateInstance(type, new Vector2(float.Parse(entity.Attributes["x"].Value, CultureInfo.InvariantCulture) + offsetX, float.Parse(entity.Attributes["y"].Value, CultureInfo.InvariantCulture) + offsetY) * _scale);
|
||||
Vector2 vector = new Vector2(float.Parse(entity.Attributes["x"].Value, CultureInfo.InvariantCulture) + offsetX, float.Parse(entity.Attributes["y"].Value, CultureInfo.InvariantCulture) + offsetY);
|
||||
Entity inst;
|
||||
if (type.Equals(typeof(Player)))
|
||||
{
|
||||
inst = (Entity)Activator.CreateInstance(type, vector * _scale, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
inst = (Entity)Activator.CreateInstance(type, vector * _scale);
|
||||
}
|
||||
inst.SetPosition(new Vector2(inst.Pos.X, inst.Pos.Y - inst.Height));
|
||||
inst.Height *= _scale;
|
||||
inst.Width *= _scale;
|
||||
|
|
|
@ -21,9 +21,9 @@ namespace DangerousD.GameCore.Managers
|
|||
item.velocity = item.velocity + item.acceleration * delta;
|
||||
}
|
||||
|
||||
CheckCollisionsLE_MO(livingEntities, mapObjects);
|
||||
CheckCollisionsE_LE(entities, livingEntities);
|
||||
CheckCollisionsLE_LE(livingEntities);
|
||||
CheckCollisionsLE_MO(livingEntities.ToList(), mapObjects);
|
||||
CheckCollisionsE_LE(entities, livingEntities.ToList());
|
||||
CheckCollisionsLE_LE(livingEntities.ToList());
|
||||
|
||||
//entities dont move
|
||||
//Living entities dont move
|
||||
|
|
|
@ -91,7 +91,7 @@ namespace DangerousD.GameCore.Network
|
|||
}
|
||||
catch { }
|
||||
}
|
||||
public void SendMsg(List<NetworkTask> networkTask)
|
||||
public void SendMsg(List<NetworkTask> networkTask, Socket ignoreSocket = null)
|
||||
{
|
||||
byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask));
|
||||
int count = Data.Length;
|
||||
|
@ -100,11 +100,14 @@ namespace DangerousD.GameCore.Network
|
|||
try
|
||||
{
|
||||
foreach (Socket socket in clientSockets)
|
||||
{
|
||||
if (!(socket == ignoreSocket))
|
||||
{
|
||||
socket.Send(BitConverter.GetBytes(count));
|
||||
socket.Send(Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
else
|
||||
|
@ -148,7 +151,9 @@ namespace DangerousD.GameCore.Network
|
|||
}
|
||||
else
|
||||
{
|
||||
GetReceivingMessages(JsonConvert.DeserializeObject<List<NetworkTask>>(so.sb.ToString()));
|
||||
List<NetworkTask> tasks = JsonConvert.DeserializeObject<List<NetworkTask>>(so.sb.ToString());
|
||||
//SendMsg(tasks, clientSocket);
|
||||
GetReceivingMessages(tasks);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
|
Loading…
Add table
Reference in a new issue