// The runLogic function contains the main logic of the scene. // Each given time, the server will create a ball and send its datas to every player connected. // When a ball lives for more than 30 seconds, the server will ask every player to destroy the corresponding ball. // private async Task RunLogic() { long lastUpdate = _env.Clock; _isRunning = true; while (_isRunning == true) { if (lastUpdate + 100 < _env.Clock) { lastUpdate = _env.Clock; Ball newBall = new Ball(_ids, _env.Clock, _rand); _scene.Broadcast("create_ball", s => { var writer = new BinaryWriter(s, Encoding.UTF8, false); writer.Write(_ids); writer.Write(newBall.x); writer.Write(newBall.y); writer.Write(newBall.vx); writer.Write(newBall.vy); writer.Write(newBall.creationTime); writer.Write(newBall.oscillationTime); }, PacketPriority.MEDIUM_PRIORITY, PacketReliability.RELIABLE); _balls.TryAdd(_ids, newBall); _ids++; foreach (Ball ball in _balls.Values) { Ball temp; if (_env.Clock > ball.creationTime + 30000) { _scene.Broadcast("destroy_ball", s => { var writer = new BinaryWriter(s, Encoding.UTF8, false); writer.Write(ball.id); }, PacketPriority.MEDIUM_PRIORITY, PacketReliability.RELIABLE); _balls.TryRemove(ball.id, out temp); } } if (_ids >= 2000000) { _ids = 0; } } await Task.Delay(100); } }
private bool TryFindHitBall(float x, float y, long timestamp, out Ball hitBall) { hitBall = null; foreach (Ball ball in _balls.Values) { if (ball.IsClicked(x, y, timestamp, _scene)) { hitBall = ball; if (((_env.Clock - hitBall.creationTime) / hitBall.oscillationTime) % 2 == 0) { return true; break; } } } return false; }