void FixedUpdate() { switch (state) { case PoolState.standby: if (position == 2) { cooldown -= Time.deltaTime; if (cooldown < 1) { state = PoolState.active; position = 1; setTarget(moveDistance); } } break; case PoolState.active: transform.Translate(moveIncrement * speed); if (transform.position.y < moveTarget.y + 0.1 && transform.position.y > moveTarget.y - 0.1) { transform.position = new Vector3(transform.position.x, moveTarget.y, transform.position.z); if (destroy) { Destroy(gameObject); return; } state = PoolState.standby; } break; } }
public override void Activate() { state = PoolState.active; position = 2; cooldown = refillCooldown; setTarget(-moveDistance); }
void HitBall(int x, int y) { poolState = PoolState.Moving; pnlTable.Cursor = Cursors.WaitCursor; double v = 20 * (progressBar1.Value / 100.0); progressBar1.Value = 0; double dx = x - balls[0].X + Ball.Radius; double dy = y - balls[0].Y + Ball.Radius; double h = (double)(Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2))); double sin = dy / h; double cos = dx / h; //balls[0].Velocity = velocity; balls[0].IsBallInHole = false; balls[0].XVelocity = v * cos; balls[0].YVelocity = v * sin; timer1.Enabled = true; using (StreamReader reader = new StreamReader(@"Shot01.wav")) { GCHandle myHandle = GCHandle.Alloc(reader.BaseStream, GCHandleType.Normal); SoundPlayerAsync.PlaySound(reader.BaseStream); } //SoundPlayer wavPlayer = new SoundPlayer(); //wavPlayer.SoundLocation = @"Shot01.wav"; //wavPlayer.LoadCompleted += new AsyncCompletedEventHandler(wavPlayer_LoadCompleted); //wavPlayer.LoadAsync(); }
public System.Collections.Generic.IEnumerator <float> InitializePool(ScenePoolController sceneController) { if (state != PoolState.NotInitialized) { Debugging.Logger.LogError($"{sceneController.name} is trying to initialize a pool ({name}) that is already initialized by: {(controller != null ? controller.name : "NULLCONTROLLER")}.", sceneController); /*if (controller == null) { * Debug.LogError("Initializing a pool that is already initialized, but the controller is null", sceneController); * }*/ } else { state = PoolState.Initializing; //isTakingInstance = false; controller = sceneController; Debugging.AssertionHelper.AssertNotNull(m_prefab, () => $"m_prefab of {name} is null", this); pooledObjects = new System.Collections.Generic.Queue <Poolable>(m_initialPoolSize); inGameObjects = new System.Collections.Generic.List <Poolable>(m_initialPoolSize); removeStartIndex = m_prefab.gameObject.name.Length + 1; nameBuilder = new System.Text.StringBuilder(m_prefab.gameObject.name, removeStartIndex + DefaultNumberDecimalPlaces); nameBuilder.Append('_'); currentSize = 0; requestedSize = 0; for (int i = 0; i < m_initialPoolSize; i++) { yield return(MEC.Timing.WaitUntilDone(MEC.Timing.RunCoroutine(CreateInitialInstances(m_worldPositionStays)))); } state = PoolState.Initialized; } }
public void FinalizePool(ScenePoolController scenePoolController) { if (state == PoolState.Initialized && scenePoolController == controller) { state = PoolState.NotInitialized; controller = null; } else { Debugging.Logger.LogError("You can only finalize a pool with the owner of the pool and when it had initialized.", scenePoolController.gameObject); } }
internal static string ToSerializedValue(this PoolState value) { switch (value) { case PoolState.Active: return("active"); case PoolState.Deleting: return("deleting"); } return(null); }
public void StopServices(bool needs) { lock (this) { if (_ps == PoolState.Run) { lock (useConns) { if (needs == true)//必须退出 { useConns.Clear(); } else if (useConns.Count != 0) { throw new ResCallBackException(); //连接池资源未全部回收 } } _timer.Stop(); while (_isThreadCheckRun) { Thread.Sleep(50); } //等待timer事件结束 _createThreadProcessRun = false; while (_threadCreate.ThreadState != ThreadState.WaitSleepJoin) { Thread.Sleep(50); } //等待可能存在的创建线程结束 lock (connList) { for (int i = 0; i < connList.Count; i++) { connList[i].Dispose(); } connList.Clear(); } _ps = PoolState.Stop; } else { throw new PoolNotRunException();//服务未启动 } } UpdatePoolState();//更新属性 }
// constructors public ExclusiveConnectionPool( ServerId serverId, EndPoint endPoint, ConnectionPoolSettings settings, IConnectionFactory connectionFactory, IEventSubscriber eventSubscriber, IConnectionExceptionHandler connectionExceptionHandler) { _serverId = Ensure.IsNotNull(serverId, nameof(serverId)); _endPoint = Ensure.IsNotNull(endPoint, nameof(endPoint)); _settings = Ensure.IsNotNull(settings, nameof(settings)); _connectionFactory = Ensure.IsNotNull(connectionFactory, nameof(connectionFactory)); _connectionExceptionHandler = Ensure.IsNotNull(connectionExceptionHandler, nameof(connectionExceptionHandler)); Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber)); _maintenanceHelper = new MaintenanceHelper(MaintainSize, _settings.MaintenanceInterval); _poolState = new PoolState(EndPointHelper.ToString(_endPoint)); _checkOutReasonCounter = new CheckOutReasonCounter(); _maxConnectingQueue = new SemaphoreSlimSignalable(settings.MaxConnecting); _connectionHolder = new ListConnectionHolder(eventSubscriber, _maxConnectingQueue); _maxConnectionsQueue = new SemaphoreSlimSignalable(settings.MaxConnections); _serviceStates = new ServiceStates(); #pragma warning disable 618 _waitQueueFreeSlots = settings.WaitQueueSize; #pragma warning restore 618 eventSubscriber.TryGetEventHandler(out _checkingOutConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkedOutConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkingOutConnectionFailedEventHandler); eventSubscriber.TryGetEventHandler(out _checkingInConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkedInConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addingConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addedConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _openingEventHandler); eventSubscriber.TryGetEventHandler(out _openedEventHandler); eventSubscriber.TryGetEventHandler(out _closingEventHandler); eventSubscriber.TryGetEventHandler(out _closedEventHandler); eventSubscriber.TryGetEventHandler(out _readyEventHandler); eventSubscriber.TryGetEventHandler(out _addingConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addedConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _clearingEventHandler); eventSubscriber.TryGetEventHandler(out _clearedEventHandler); eventSubscriber.TryGetEventHandler(out _connectionCreatedEventHandler); }
/// <summary> /// 初始化函数 /// </summary> protected void InitConnectionPool(string connectionString, ConnTypeEnum cte, int maxConnection, int minConnection, int seepConnection, int keepRealConnection) { if (cte == ConnTypeEnum.None) { throw new ConnTypeExecption();//参数不能是None } _ps = PoolState.UnInitialize; this._connString = connectionString; this._connType = cte; this._minConnection = minConnection; this._seepConnection = seepConnection; this._keepRealConnection = keepRealConnection; this._maxConnection = maxConnection; //连接状态检查线程 this.threadCheck = new Thread(new ThreadStart(checkThreadProcess)); this.threadCreate = new Thread(new ThreadStart(createThreadProcess)); }
/// <summary> /// 停止服务,线程安全 /// <param name="needs">是否必须退出;如果指定为false与StartServices()功能相同,如果指定为true。将未收回的连接资源关闭,这将是危险的。认为可能你的程序正在使用此资源。</param> /// </summary> public void StopServices(bool needs) { lock (this) { if (_ps == PoolState.Run) { lock (hs_UseConn) { if (needs == true)//必须退出 { hs_UseConn.Clear(); } else if (hs_UseConn.Count != 0) { throw new ResCallBackException(); //连接池资源未全部回收 } } time.Stop(); while (isThreadCheckRun) { Thread.Sleep(50); } //等待timer事件结束 createThreadProcessRun = false; while (threadCreate.ThreadState != ThreadState.WaitSleepJoin) { Thread.Sleep(50); } //等待可能存在的创建线程结束 lock (al_All) { for (int i = 0; i < al_All.Count; i++) { al_All[i].Dispose(); } al_All.Clear(); } _ps = PoolState.Stop; } else { throw new PoolNotRunException();//服务未启动 } } UpdateAttribute();//更新属性 }
/// <summary> /// 初始化函数 /// </summary> protected void InitConnectionPool(string connectionString, ConnectionType cte, int maxConnection, int minConnection, int seepConnection, int keepConnection, int keepRealConnection) { if (cte == ConnectionType.None) { throw new ConnTypeExecption();//参数不能是None } _ps = PoolState.UnInitialize; this._connString = connectionString; this._connType = cte; this._minConnection = minConnection; this._seepConnection = seepConnection; this._keepRealConnection = keepRealConnection; this._maxConnection = maxConnection; this._timer = new Timer(500); this._timer.Stop(); this._timer.Elapsed += timer_Elapsed; this._threadCreate = new Thread(new ThreadStart(CreateThreadProcess)); }
/// <summary> /// 初始化函数 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <param name="cte">数据库连接类型</param> /// <param name="maxConnection">最大连接数,最大可以创建的连接数目</param> /// <param name="minConnection">最小连接数</param> /// <param name="seepConnection">每次创建连接的连接数</param> /// <param name="keepConnection">保留连接数,当空闲连接不足该数值时,连接池将创建seepConnection个连接</param> /// <param name="keepRealConnection">当空闲的实际连接不足该值时创建连接,直到达到最大连接数</param> protected void InitConnectionPool(string connectionString, ConnTypeEnum cte, int maxConnection, int minConnection, int seepConnection, int keepConnection, int keepRealConnection) { if (cte == ConnTypeEnum.None) { throw new ConnTypeExecption();//参数不能是None } _ps = PoolState.UnInitialize; this._connString = connectionString; this._connType = cte; this._minConnection = minConnection; this._seepConnection = seepConnection; this._keepRealConnection = keepRealConnection; this._maxConnection = maxConnection; this.time = new System.Timers.Timer(500); this.time.Stop(); this.time.Elapsed += new System.Timers.ElapsedEventHandler(time_Elapsed); this.threadCreate = new Thread(new ThreadStart(createThreadProcess)); //启动服务 StartServices(); }
/// <summary> /// 创建线程 /// </summary> private void createThreadProcess() { bool join = false; int createThreadProcessTemp_inside = createThreadProcessTemp; _ps = PoolState.Initialize; while (true) { join = false; _ps = PoolState.Run; if (createThreadProcessRun == false) { //遇到终止命令 try { threadCreate.Join(); } catch (Exception e) { } } else { if (createThreadMode == 0) { //------------------------begin mode 创建模式 lock (al_All) { if (al_All.Count < createThreadProcessTemp_inside) { al_All.Add(CreateConnection(_connString, _connType)); } else { join = true; } } //------------------------end mode } else if (createThreadMode == 1) { //------------------------begin mode 增加模式 lock (al_All) { if (createThreadProcessTemp_inside != 0) { createThreadProcessTemp_inside--; al_All.Add(CreateConnection(_connString, _connType)); } else { join = true; } } //------------------------end mode } else { join = true; } //------------------------------------------------------------------------- if (join == true) { UpdateAttribute();//更新属性 try { createThreadProcessTemp = 0; threadCreate.Join(); } catch (Exception e) { createThreadProcessTemp_inside = createThreadProcessTemp; }//得到传入的变量 } } } }
private void MoveBalls() { foreach (Ball ball in balls) { if (Math.Abs(ball.X) < 5 && Math.Abs(ball.Y) < 5 && Math.Abs(ball.XVelocity) < 10 && Math.Abs(ball.YVelocity) < 10) { ball.X = ball.Y = 0; ball.XVelocity = ball.YVelocity = 0; } } bool conflicted = true; int totalPoints1 = 0; int totalPoints2 = 0; while (conflicted) { conflicted = false; //foreach (Ball ball in balls) //{ // //int xOffset = (int)(ball.X + Ball.Radius + ball.XVelocity) - pnlTable.Width; // int xOffset = (int)(ball.X + Ball.Radius) - pnlTable.Width; // if (xOffset > 0) // { // ball.X = ball.X - xOffset * 2; // ball.XVelocity *= -1 * absorption; // conflicted = true; // } // //xOffset = (int)(ball.X + ball.XVelocity); // xOffset = (int)(ball.X - Ball.Radius); // if (xOffset < 0) // { // ball.X = ball.X - xOffset * 2; // ball.XVelocity *= -1 * absorption; // conflicted = true; // } // //int yOffset = (int)(ball.Y + Ball.Radius * 2 + ball.YVelocity - pnlTable.Height); // int yOffset = (int)(ball.Y + Ball.Radius) - pnlTable.Height; // if (yOffset > 0) // { // ball.Y = ball.Y - yOffset * 2; // ball.YVelocity *= -1 * absorption; // conflicted = true; // } // //yOffset = (int)(ball.Y + Ball.Radius + ball.YVelocity); // yOffset = (int)(ball.Y - Ball.Radius); // if (yOffset < 0) // { // ball.Y = ball.Y - yOffset * 2; // ball.YVelocity *= -1 * absorption; // conflicted = true; // } //} //foreach (Ball ballA in balls) //{ bool someCollision = true; while (someCollision) { someCollision = false; foreach (Ball ballA in balls) { foreach (DiagonalBorder diagonalBorder in diagonalBorders) { if (diagonalBorder.Colliding(ballA)) { //listBox1.Items.Add(diagonalBorder.ToString()); //listBox1.SelectedIndex = listBox1.Items.Count - 1; //tableGraphics.DrawLine(new Pen(Brushes.White), diagonalBorder.X1, diagonalBorder.Y1, diagonalBorder.X2, diagonalBorder.Y2); diagonalBorder.ResolveCollision(ballA); } } RectangleCollision borderCollision = RectangleCollision.None; foreach (TableBorder tableBorder in tableBorders) { borderCollision = tableBorder.Colliding(ballA); if (borderCollision != RectangleCollision.None) { //listBox1.Items.Add(tableBorder.ToString()); //listBox1.SelectedIndex = listBox1.Items.Count - 1; someCollision = true; tableBorder.ResolveCollision(ballA, borderCollision); } } foreach (Ball ballB in balls) { if (ballA.Id.CompareTo(ballB.Id) != 0) { if (ballA.Colliding(ballB)) { ballA.PlaySound(); while (ballA.Colliding(ballB)) { someCollision = true; ballA.ResolveCollision(ballB); ballA.X += (int)ballA.Velocity.X; ballA.Y += (int)ballA.Velocity.Y; ballB.X += (int)ballB.Velocity.X; ballB.Y += (int)ballB.Velocity.Y; } } } } double absXVelocity = Math.Abs(ballA.XVelocity); double absYVelocity = Math.Abs(ballA.YVelocity); double signalXVelocity = ballA.XVelocity >= 0 ? 1 : -1; double signalYVelocity = ballA.YVelocity >= 0 ? 1 : -1; absXVelocity = absXVelocity * (1 - friction) - 0.01; absYVelocity = absYVelocity * (1 - friction) - 0.01; if (absXVelocity < 0) { absXVelocity = 0; } if (absYVelocity < 0) { absYVelocity = 0; } ballA.XVelocity = absXVelocity * signalXVelocity; ballA.YVelocity = absYVelocity * signalYVelocity; MoveBall((int)ballA.X, (int)ballA.Y, false); } } conflicted = false; } double totalVelocity = 0; foreach (Ball ball in balls) { ball.Position.X += ball.XVelocity; ball.Position.Y += ball.YVelocity; totalVelocity += ball.XVelocity; totalVelocity += ball.YVelocity; } if (poolState == PoolState.Moving && totalVelocity == 0) { foreach (Ball ball in balls) { MoveBall((int)ball.X, (int)ball.Y, true); } listBox1.Items.Add(snapShotCount.ToString()); snapShotCount = 0; poolState = PoolState.AwaitingShot; pnlTable.Cursor = Cursors.SizeAll; } foreach (Ball ball in balls) { if (ball.IsBallInHole) { //MoveBalls(); } foreach (Hole hole in holes) { if (hole.IsBallInHole(ball) && (ball.Velocity.X != 0 || ball.Velocity.Y != 0)) { //MoveBalls(); //MessageBox.Show(string.Format("Ball {0} in hole {1}", ball.Id, hole.Id)); } } } }
/// <summary> /// 创建连接 /// </summary> public void CreateThreadProcess() { bool join = false; int createThreadProcessNum_inside = _createThreadProcessNum; _ps = PoolState.Initialize; while (true) { join = false; _ps = PoolState.Run; if (_createThreadProcessRun == false) //终止命令 { try { _threadCreate.Join(); } catch (Exception) { throw; } } else { if (_createThreadMode == CreateThreadMode.StaticCreateMode) { lock (connList) { if (connList.Count < createThreadProcessNum_inside) { connList.Add(CreateConnection(_connString, _connType)); } else { join = true; } } } else if (_createThreadMode == CreateThreadMode.DynamicCreateMode) { lock (connList) { if (createThreadProcessNum_inside != 0) { createThreadProcessNum_inside--; connList.Add(CreateConnection(_connString, _connType)); } } } else { join = true; } if (join) { UpdatePoolState(); try { _createThreadProcessNum = 0; _threadCreate.Join(); } catch (Exception) { createThreadProcessNum_inside = _createThreadProcessNum; } } } } }
public GetBucketCapacitySummarySpectraS3Request WithPoolState(PoolState? poolState) { this._poolState = poolState; if (poolState != null) { this.QueryParams.Add("pool_state", poolState.ToString()); } else { this.QueryParams.Remove("pool_state"); } return this; }
public static string ToSerialString(this PoolState value) => value switch {