public void RevokeControl(IMessageRider token) { if (IsOwner) { if (Controller) { EntityProxy proxy; // force a replication of this Controller.controlling.Remove(this); Controller.entityChannel.ForceSync(this, out proxy); Controller = null; // clear out everything CommandLastExecuted = null; CommandSequence = 0; CommandQueue.Clear(); // set token if (proxy != null) { proxy.ControlTokenLost = token; proxy.ControlTokenGained = null; } } Freeze(false); } else { NetLog.Error("You can not revoke control of {0}, you are not the owner", this); return; } }
public void ResetInput() { lastMovementFromKeyPressTime = 0; commandQueue.Clear(); lastInputString = ""; isInputEnabled = true; }
public void ReleaseControlInternal(IMessageRider token) { NetAssert.True(Flags & EntityFlags.HAS_CONTROL); Flags &= ~EntityFlags.HAS_CONTROL; CommandQueue.Clear(); CommandSequence = 0; CommandLastExecuted = null; ControlLostToken = token; ControlGainedToken = null; // call to serializer Serializer.OnControlLost(); // call to user behaviours foreach (IEntityBehaviour eb in Behaviours) { if (ReferenceEquals(eb.entity, this.UnityObject)) { eb.ControlLost(); } } // call user event GlobalEventListenerBase.ControlOfEntityLostInvoke(UnityObject); // de-freeze Freeze(false); }
/// <summary> /// Выполняет преобразование коллекции строк <paramref name="lines"/> и возвращает флаг, указывающий /// на успешность выполнения преобразования. /// </summary> /// <returns>Результат преобразования: истина, если преобразование выполнено без ошибок</returns> public bool Parse(string[] lines) { Errors.Clear(); CommandQueue.Clear(); lines = lines.Select(line => RemoveComment(line.Replace(" ", ""))).ToArray(); for (currentLineNumber = GetProgramStartIndex(lines) + 1; currentLineNumber <= lines.Length; currentLineNumber++) { try { ParseLine(lines[currentLineNumber - 1].ToUpper()); } catch (EndOfCodeException) { break; } } if (Errors.Count != 0) { CommandQueue.Clear(); } return(Errors.Count == 0); }
public void AssignControl(Connection connection, IMessageRider token) { if (IsOwner) { if (HasControl) { ReleaseControl(token); } EntityProxy proxy; CommandLastExecuted = null; CommandSequence = 0; CommandQueue.Clear(); Controller = connection; Controller.controlling.Add(this); Controller.entityChannel.CreateOnRemote(this, out proxy); Controller.entityChannel.ForceSync(this); // set token proxy.ControlTokenLost = null; proxy.ControlTokenGained = token; Freeze(false); } else { NetLog.Error("You can not assign control of {0}, you are not the owner", this); } }
public void TestClear() { q.Enqueue(new CommandInfo(AppToUCICommand.IsReady)); q.Enqueue(new CommandInfo(AppToUCICommand.IsReady)); q.Enqueue(new CommandInfo(AppToUCICommand.IsReady)); q.Clear(); Assert.IsEmpty(q); }
public override void Panic() { lock (_thread_interlock) { CommandQueue.Clear(); CommandQueue.Add(new PanicCommand()); } }
public override void Reset() { lock (_thread_interlock) { CommandQueue.Clear(); CommandQueue.Add(new ReadyCommand()); } }
public void RenderAll(float DeltaTime) { foreach (RenderingCommand command in CommandQueue) { ExecuteRenderCommand(command); } CommandQueue.Clear(); foreach (IRenderable renderable in Renderables) { renderable?.Render(this); } }
private void Initialize() { last_command = null; last_command_time = 0; command_retries = 0; lock (_thread_interlock) { _state = State.init; CommandQueue.Clear(); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.InputOverride, Data = ErosTek.ET232.Constants.OverrideAll, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_A, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_B, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.ModeOverride, Data = ErosTek.ET232.Constants.ForceMode | (byte)IntenseMode.Mode, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_MA, Data = IntenseMode.StartingMA, Retry = true, }); draining_queue = true; } }
protected override void GetCommands(Entity entity) { commandQueue.Clear(); if (InputSource == null) { return; } if (entity.CanExecuteActionOfType(GameInfo.ActionType.Move)) { if (InputSource.DashKeyPressed && _dashCooldownTimer <= 0) { uint duration = 2; Vector2 velocity = InputSource.MoveVector * 4; Action <Entity> dash = (dashingEntity) => { if (!entity.CanExecuteActionOfType(GameInfo.ActionType.Move)) { return; } entity.LockActionType(GameInfo.ActionType.Move, 1); entity.Velocity = velocity; uint _counter = 0; Action <Entity> OnFixedUpdate = null; OnFixedUpdate = (e) => { if (_counter == duration) { entity.Velocity = Vector2.zero; entity.UnlockActionType(GameInfo.ActionType.Move, 1); entity.OnFixedUpdate -= OnFixedUpdate; return; } _counter++; }; entity.OnFixedUpdate += OnFixedUpdate; _dashCooldownTimer = (int)DashCooldown; }; commandQueue.Issue(dash); return; } commandQueue.Issue(Behaviours.GetEntityMoveCommandFromInput(entity, InputSource)); } _dashCooldownTimer--; }
public override void Reset() { lock (_thread_interlock) { CommandQueue.Clear(); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_A, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_B, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.InputOverride, Data = ErosTek.ET232.Constants.OverrideNone, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.ModeOverride, Data = ErosTek.ET232.Constants.ForceReset, Retry = true, }); draining_queue = true; } }
public override void ThreadFunction() { ErrorCallback(this, "Network processing startup.", "", false); bool should_exit = false; byte[] remote_bytes = Array.Empty <byte>(); NetworkHeader read_header = null; Utf8JsonWriter jsonwriter; lock (_thread_interlock) { jsonwriter = new Utf8JsonWriter(DeviceStream); CommandQueue.Clear(); } byte[] read_buffer = new byte[block_size]; void DataReader() { try { lock (_thread_interlock) DeviceStream.BeginRead(read_buffer, 0, read_buffer.Length, delegate(IAsyncResult ar) { try { lock (_thread_interlock) { if (DeviceStream == null) { return; } int len = DeviceStream.EndRead(ar); int pos = remote_bytes.Length; Array.Resize(ref remote_bytes, pos + len); Buffer.BlockCopy(read_buffer, 0, remote_bytes, pos, len); } } catch (Exception e) { bool exiting; lock (_thread_interlock) exiting = _thread_end; if (!exiting) { ErrorCallback(this, "Network error.", e.Message, false); } } DataReader(); }, null); } catch (Exception e) { bool exiting; lock (_thread_interlock) exiting = _thread_end; if (!exiting) { ErrorCallback(this, "Network error.", e.Message, false); } } } DataReader(); while (!should_exit) { Thread.Yield(); long consumed = 0; if (read_header == null) { try { lock (_thread_interlock) { if (remote_bytes.Length > 0) { Utf8JsonReader jsonreader = new Utf8JsonReader(new ReadOnlySpan <byte>(remote_bytes)); read_header = JsonSerializer.Deserialize <NetworkHeader>(ref jsonreader); consumed = jsonreader.BytesConsumed; } } } catch (JsonException) { } } else { CommandBase received_command = null; try { lock (_thread_interlock) { if (remote_bytes.Length > 0) { Utf8JsonReader jsonreader = new Utf8JsonReader(new ReadOnlySpan <byte>(remote_bytes)); switch (read_header.ObjectType) { case TransmissionType.Nothing: break; case TransmissionType.Ready: received_command = JsonSerializer.Deserialize <ReadyCommand>(ref jsonreader); break; case TransmissionType.Panic: received_command = JsonSerializer.Deserialize <PanicCommand>(ref jsonreader); break; case TransmissionType.Levels: received_command = JsonSerializer.Deserialize <LevelsCommand>(ref jsonreader); break; case TransmissionType.Raw: received_command = JsonSerializer.Deserialize <RawCommand>(ref jsonreader); break; case TransmissionType.Mode: received_command = JsonSerializer.Deserialize <ModeCommand>(ref jsonreader); break; case TransmissionType.Response: received_command = JsonSerializer.Deserialize <DeviceResponse>(ref jsonreader); break; case TransmissionType.Request: break; case TransmissionType.Error: received_command = JsonSerializer.Deserialize <ErrorCommand>(ref jsonreader); break; } consumed = jsonreader.BytesConsumed; } } } catch (JsonException) { } if (received_command != null) { switch (read_header.ObjectType) { case TransmissionType.Nothing: break; case TransmissionType.Ready: QueueStateCallback(this, 0); break; case TransmissionType.Panic: MassLevelSetCallback(this, 0, 0, 0, true); ErrorCallback(this, "PANIC!", "Panic received from remote!", true); break; case TransmissionType.Levels: LevelsCommand levels = (LevelsCommand)received_command; LastA = levels.A; LastB = levels.B; MassLevelSetCallback(this, levels.A, levels.B, levels.MA, levels.Mode == ControlMode.absolute); break; case TransmissionType.Raw: RawCommand raw = (RawCommand)received_command; if (raw.Address == (int)AddressByte.Pot_A) { LastA = raw.Data; } if (raw.Address == (int)AddressByte.Pot_B) { LastB = raw.Data; } StateUpdatedCallback(this, raw.Address, raw.Data); break; case TransmissionType.Mode: ModeCommand mode = (ModeCommand)received_command; StateUpdatedCallback(this, (int)AddressByte.ModeOverride, mode.Mode); StateUpdatedCallback(this, (int)AddressByte.Pot_MA, mode.MA); break; case TransmissionType.Request: break; case TransmissionType.Response: DeviceResponse response = (DeviceResponse)received_command; DataReturnedCallback(this, response.Address, response.Data); double current_unixtime = UnixTime.Current(); LevelEvent level = new LevelEvent() { unixtime = current_unixtime }; lock (_thread_interlock) { if (response.Address == (int)AddressByte.PulseAmp_A) { level.level = (int)Math.Sqrt(LastA * response.Data); AmpHistoryA.Add(level); } if (response.Address == (int)AddressByte.PulseAmp_B) { level.level = (int)Math.Sqrt(LastB * response.Data); AmpHistoryB.Add(level); } } break; case TransmissionType.Error: ErrorCommand error = (ErrorCommand)received_command; ErrorCallback(this, error.Error, error.Details, false); break; } read_header = null; } } if (consumed > 0) { lock (_thread_interlock) { byte[] remaining = new byte[remote_bytes.Length - consumed]; Buffer.BlockCopy(remote_bytes, (int)consumed, remaining, 0, remaining.Length); remote_bytes = remaining; } } lock (_thread_interlock) { if (CommandQueue.Count > 0) { CommandBase command = CommandQueue.First(); CommandQueue.RemoveAt(0); NetworkHeader write_header = new NetworkHeader() { ObjectType = command.ObjectType }; try { JsonSerializer.Serialize(jsonwriter, write_header, typeof(NetworkHeader)); jsonwriter.Flush(); jsonwriter.Reset(); switch (command.ObjectType) { case TransmissionType.Nothing: break; case TransmissionType.Ready: JsonSerializer.Serialize(jsonwriter, (ReadyCommand)command, typeof(ReadyCommand)); break; case TransmissionType.Panic: JsonSerializer.Serialize(jsonwriter, (PanicCommand)command, typeof(PanicCommand)); break; case TransmissionType.Levels: JsonSerializer.Serialize(jsonwriter, (LevelsCommand)command, typeof(LevelsCommand)); break; case TransmissionType.Raw: JsonSerializer.Serialize(jsonwriter, (RawCommand)command, typeof(RawCommand)); break; case TransmissionType.Mode: JsonSerializer.Serialize(jsonwriter, (ModeCommand)command, typeof(ModeCommand)); break; case TransmissionType.Request: JsonSerializer.Serialize(jsonwriter, (RequestCommand)command, typeof(RequestCommand)); break; case TransmissionType.Response: JsonSerializer.Serialize(jsonwriter, (ResponseCommand)command, typeof(ResponseCommand)); break; case TransmissionType.Error: JsonSerializer.Serialize(jsonwriter, (ErrorCommand)command, typeof(ErrorCommand)); break; } jsonwriter.Flush(); jsonwriter.Reset(); } catch (JsonException) { } catch (IOException e) when(e.InnerException is SocketException) { _thread_end = true; ErrorCallback(this, "Network error.", e.InnerException.Message, false); } catch (IOException e) { _thread_end = true; ErrorCallback(this, "Network error.", e.Message, false); } catch (Exception e) { _thread_end = true; ErrorCallback(this, "Network error.", e.Message, false); } if (CommandQueue.Count == 0 && command.ObjectType != TransmissionType.Ready) { CommandQueue.Add(new ReadyCommand()); } } should_exit = _thread_end; } } try { lock (_thread_interlock) { if (DeviceStream != null) { DeviceStream.Close(); DeviceStream.Dispose(); } DeviceStream = null; } } catch (Exception e) { ErrorCallback(this, "Network error.", e.Message, false); } ErrorCallback(this, "Network processing shutdown.", "", false); }
protected override void Update(GameTime gameTime) { if (!Level.DisplayingRemainingLivesScreen && !Level.DisplayingWinScreen) { foreach (IController input in inputs) { input.Update(); } if (paused) { return; } Player.Update(); if (GhostPlayer != null) { GhostPlayer.Update(); } foreach (IEntity e in Entities) { e.Update(); } DetectCollisions(); GameCamera.SpotlightCameraPosition = new Vector2(Player.Location.X + Player.Sprite.Width, Player.Location.Y + Player.Sprite.Height); GameCamera.Update(gameTime); foreach (ICommand c in CommandQueue) { c.Execute(); } CommandQueue.Clear(); ticks--; if (ticks % GameUtility.FramesPerSecond == 0) { Stats.Time--; } if (Stats.Coins >= GameUtility.NumCoinsToIncreaseLives) { Stats.Lives++; Stats.Coins -= GameUtility.NumCoinsToIncreaseLives; } } else { CommandQueue.Clear(); GameCamera.SpotlightCameraPosition = new Vector2(Player.Location.X + Player.Sprite.Width, Player.Location.Y + Player.Sprite.Height); GameCamera.Update(gameTime); foreach (IController input in inputs) { input.Update(); } foreach (ICommand c in CommandQueue) { c.Execute(); } } base.Update(gameTime); }