private void AddLocalChangeBehaviourCommand(Entity entity, Behaviour newBehaviour) { Debug.Assert(entityManager.HasComponent <GroupBehaviour>(entity), "the current selected entity doesn't have a group behaviour component. can't recieve a change behaviour command"); CommandStorageSystem.TryAddLocalCommand(new ChangeBehaviourCommand() { Target = entity, NewBehaviour = new GroupBehaviour() { Value = newBehaviour } }, World.Active); }
private void SendCommandsToNetwork() { if (OfflineMode.OffLineMode) { return; } int turnToExecuteTheCommands = MainSimulationLoopSystem.CurrentLockstepTurn + MainSimulationLoopSystem.COMMANDS_DELAY; byte eventCode; object[] content; Debug.Log($"On CommandNetworkSender, volatile commands to send {CommandStorageSystem.AreVolatileCommands()}"); if (CommandStorageSystem.AreVolatileCommands()) { eventCode = (byte)NetworkEventTypes.CommandEventCode; object[] commands = CommandStorageSystem.GetAllVolatileCommandsSerialized(); content = new object[] { turnToExecuteTheCommands, commands[0], commands[1], commands[2] }; } else { eventCode = (byte)NetworkEventTypes.EmptyCommandEventCode; content = new object[] { turnToExecuteTheCommands }; } RaiseEventOptions raiseOptions = new RaiseEventOptions() { Receivers = ReceiverGroup.Others }; SendOptions sendOptions = new SendOptions() { Reliability = true }; PhotonNetwork.RaiseEvent(eventCode, content, raiseOptions, sendOptions); }
//event data layout: //it must be a object array. //and always the first element is the turn of execution. private void CommandCallback(EventData photonEvent) { //null check is important object[] eventData = (object[])photonEvent.CustomData; int turnToExecute = (int)eventData[0]; object[] serializedMoveCommands = (object[])eventData[1]; MoveCommand[] moveCommands; if (serializedMoveCommands != null) { moveCommands = new MoveCommand[serializedMoveCommands.Length]; for (int i = 0; i < serializedMoveCommands.Length; i++) { moveCommands[i] = CommandUtils.DeserializeMoveCommand((object[])serializedMoveCommands[i]); } } else { moveCommands = null; } object[] serializedChangeBehaviourCommand = (object[])eventData[2]; ChangeBehaviourCommand[] changeBehaviourCommands; if (serializedChangeBehaviourCommand != null) { changeBehaviourCommands = new ChangeBehaviourCommand[serializedChangeBehaviourCommand.Length]; for (int i = 0; i < changeBehaviourCommands.Length; i++) { changeBehaviourCommands[i] = CommandUtils.DeserializeChangeBehaviourCommand((object[])serializedChangeBehaviourCommand[i]); } } else { changeBehaviourCommands = null; } object[] serializedGatherCommand = (object[])eventData[3]; GatherCommand[] gatherCommands; if (serializedGatherCommand != null) { gatherCommands = new GatherCommand[serializedGatherCommand.Length]; for (int i = 0; i < gatherCommands.Length; i++) { gatherCommands[i] = CommandUtils.DeserializeGatherCommand((object[])serializedGatherCommand[i]); } } else { gatherCommands = null; } //otros commandos CommandStorageSystem.QueueNetworkedCommands(turnToExecute, moveCommands, changeBehaviourCommands, gatherCommands); CreateCommandLockstepCheck(turnToExecute); SendCommandConfirmationEvent(turnToExecute); }
//this system manages the volatile commands of each lockstep turn. //sends them to the network and queues them into the definitive dictionary. protected override void OnUpdate() { SendCommandsToNetwork(); CommandStorageSystem.QueueVolatileCommands(MainSimulationLoopSystem.CurrentLockstepTurn + MainSimulationLoopSystem.COMMANDS_DELAY); }
protected override void OnUpdate() { if (Input.GetMouseButtonDown(1)) { var currentSelected = SelectionSystem.CurrentSelection; if (MapManager.ActiveMap == null) { Debug.Log("we need the active map"); return; } else if (currentSelected == null) { Debug.Log("There is not an entity selected"); return; } //early out if the entity is of other team or doesn't have team. if (!EntityManager.HasComponent <Team>(currentSelected.entity)) { return; } var selectedTeam = EntityManager.GetComponentData <Team>(currentSelected.entity).Number; if (!GameManager.PlayerTeams.Contains(selectedTeam)) { return; } var currentSelectedEntity = currentSelected.entity; if (EntityManager.HasComponent <Commandable>(currentSelectedEntity)) { //here we see the default command for the commandable var defaultCommandType = EntityManager.GetComponentData <Commandable>(currentSelectedEntity).DeafaultCommand; Hex clickHex = MapManager.ActiveMap.layout.PixelToHex(Input.mousePosition, Camera.main); switch (defaultCommandType) { case CommandType.MOVE_COMMAND: var moveCommand = new MoveCommand() { Target = currentSelectedEntity, Destination = new DestinationHex() { FinalDestination = clickHex } }; CommandStorageSystem.TryAddLocalCommand(moveCommand, World.Active); break; case CommandType.GATHER_COMMAND: //gather si es que se cliquea a un recurso, si no solo moverse. if (ResourceSourceManagerSystem.TryGetResourceAtHex(clickHex, out ResourceSourceAndEntity source)) { var gatherCommand = new GatherCommand() { Target = currentSelectedEntity, TargetPos = clickHex }; CommandStorageSystem.TryAddLocalCommand(gatherCommand, World.Active); } else { var moveCommand2 = new MoveCommand() { Target = currentSelectedEntity, Destination = new DestinationHex() { FinalDestination = clickHex } }; CommandStorageSystem.TryAddLocalCommand(moveCommand2, World.Active); } break; default: Debug.LogError("commandable doesn't have a valid default command"); break; } } else { Debug.Log("The selected entity cannot recieve commands. It isn't commandable!"); } } }