public void TestBehindFalse() { var timer = new CyclicTimer(1); Thread.Sleep(1500); Assert.IsFalse(timer.IsBehind()); }
public void TestBehindTrue() { var timer = new CyclicTimer(1); Thread.Sleep(2000); Assert.IsTrue(timer.IsBehind()); }
private void wait_until_new_code_has_been_submitted(CyclicTimer timer, CancellationToken cancel_token) { for (; ;) { timer.WaitUntilNextFrame(); if (has_new_code_been_submitted() || cancel_token.IsCancellationRequested) { break; } } }
private void OnValidate() { spawn_timer = new CyclicTimer(SpawnPulseDelta); var half_size = Size / 2; var p = transform.position; bottom = new Vector2 { x = p.x, y = p.z } -half_size; top = new Vector2 { x = p.x, y = p.z } +half_size; }
public void Cmd_equip_weapon_impl(GameObject go) { var weapon = go.GetComponent <WeaponController>(); if (!can_be_equipped(weapon)) { return; } equipped_weapon = weapon; clip_size = 0; OnClipSizeChanged.Invoke(clip_size); max_clip_size = weapon.ClipSize; fire_rate_delta = time_since_last_fired_shot = weapon.FireRateDelta; clip_regen_timer = new CyclicTimer(weapon.ClipRegenDelta); ik_equip.SwapWeapon(weapon); Rpc_change_weapon(weapon.gameObject); }
public void Test1() { var timer = new CyclicTimer(1); timer.WaitUntilNextFrame(); Assert.IsTrue(!timer.HasPassedLastFrame()); Assert.IsFalse(timer.IsBehind()); Thread.Sleep(1000); Assert.IsTrue(timer.HasPassedLastFrame()); Assert.IsFalse(timer.IsBehind()); var now = DateTime.Now; timer.WaitUntilNextFrame(); var delta = DateTime.Now - now; Assert.IsTrue(delta.TotalSeconds > 1, delta.TotalSeconds.ToString()); Assert.IsFalse(timer.IsBehind()); }
void run_env() { Script.DefaultOptions.DebugPrint = (s) => MainThreadEventQueue.Enqueue(() => LogController.Instance?.Log(s)); var engine = new Script(); var cancel_token = cancel_source.Token; var ud_v3 = UserData.RegisterType(typeof(Vector3)); var ud_q = UserData.RegisterType(typeof(Quaternion)); UserData.RegisterType(typeof(PlayerState)); UserData.RegisterType(typeof(EnvState)); UserData.RegisterType(typeof(Weapon)); var ud_weapon_type = UserData.RegisterType(typeof(WeaponType)); UserData.RegisterType(typeof(Bullet)); UserData.RegisterType(typeof(OtherPlayer)); UserData.RegisterType(typeof(Elevation)); for (; ;) { var timer = new CyclicTimer(1f / 10f); try { if (cancel_token.IsCancellationRequested) { break; } var local_snapshot = current_snapshot; //Really important local_* is used instead of snapshot here. Basicly the state is viewed as consumed from here on. var local_ps = local_snapshot.player_state; var local_env = local_snapshot.env_state; //Set up state engine.Globals["vector3"] = UserData.CreateStatic(ud_v3); engine.Globals["quaternion"] = UserData.CreateStatic(ud_q); engine.Globals["WEAPON_TYPE"] = UserData.CreateStatic(ud_weapon_type); engine.Globals["player"] = local_ps; engine.Globals["environment"] = local_env; //Run script engine.DoString(Code, codeFriendlyName: "Script"); //Run commands from script if (local_ps.new_dest.HasValue) //SetDestination { var v = local_ps.new_dest.Value; local_ps.new_dest = null; MainThreadEventQueue.Enqueue(() => local_navmesh_network.SetDestination(v)); } if (local_ps.use_weapon) //UseWeapon { if (local_ps.equipped_weapon != null) { local_ps.use_weapon = false; MainThreadEventQueue.Enqueue(() => equipment_controller?.CmdUseWeapon()); } } if (local_ps.weapon_to_pick_up != null) //PickUpWeapon { var weapon = local_ps.weapon_to_pick_up; local_ps.weapon_to_pick_up = null; MainThreadEventQueue.Enqueue(() => bag?.PickUp(weapon.controller)); //TODO: Error message: weapon already picked up or owned by other } if (local_ps.weapon_to_drop != null) //DropWeapon { var weapon = local_ps.weapon_to_drop; local_ps.weapon_to_drop = null; MainThreadEventQueue.Enqueue(() => bag?.Drop(weapon.controller)); //TODO: Error message: weapon not ours } if (local_ps.weapon_to_equip != null) //EquipWeapon { var weapon = local_ps.weapon_to_equip; local_ps.weapon_to_equip = null; MainThreadEventQueue.Enqueue(() => equipment_controller?.EquipWeapon(weapon.controller)); //TODO: Error message: weapon not picked up } if (local_ps.new_rotation.HasValue) //Aim { var rot = local_ps.new_rotation.Value; local_ps.new_rotation = null; MainThreadEventQueue.Enqueue(() => rotator?.CmdSetRotation(rot)); } timer.WaitUntilNextFrame(); } catch (SyntaxErrorException e) { MainThreadEventQueue.Enqueue(() => LogController.Instance?.Log(e.DecoratedMessage)); wait_until_new_code_has_been_submitted(timer, cancel_token); } catch (ScriptRuntimeException e) { MainThreadEventQueue.Enqueue(() => LogController.Instance?.Log(e.DecoratedMessage)); wait_until_new_code_has_been_submitted(timer, cancel_token); } catch (Exception e) { UnityEngine.Debug.LogError(e); MainThreadEventQueue.Enqueue(() => LogController.Instance?.Log(e.Message)); wait_until_new_code_has_been_submitted(timer, cancel_token); } } UnityEngine.Debug.Log("Ended running scripts"); }