/// <summary> /// Creates the bucket. /// </summary> /// <param name="request">The request.</param> /// <returns>System.String.</returns> private HttpStatusCode CreateBucket(CreateBucketRequest request) { try { request.BucketName = request.BucketName.ToLower(); if (string.IsNullOrEmpty(request.BucketName)) { return(HttpStatusCode.BadRequest); } if (Buckets.ContainsKey(request.BucketName)) { return(HttpStatusCode.BadRequest); } Buckets.Add(request.BucketName, DataBucket.CreateNewBucket(request.BucketName, DbLocation, request.MaxRecordSize, request.MaxRecordsPerBin)); return(HttpStatusCode.OK); } catch (Exception ex) { return(HttpStatusCode.InternalServerError); } }
/// <summary> /// 根据Bound 粗测阶段 /// </summary> /// <param name="bodies"></param> /// <param name="forceUpdate"></param> private void UpdateBroadphase(List <Body> bodies, bool forceUpdate) { var gridChanged = false; foreach (var body in bodies .Where(body => forceUpdate || !body.Sleep) .Where( body => !(body.Bounds.Max.X < World.Bounds.Min.X) && !(body.Bounds.Min.X > World.Bounds.Max.X) && !(body.Bounds.Max.Y < World.Bounds.Min.Y) && !(body.Bounds.Min.Y > World.Bounds.Max.Y))) { var newRegion = GetRegion(body); if (body.Region == null || newRegion.GetId() != body.Bounds.GetId() || forceUpdate) { if (body.Region == null || forceUpdate) { body.Region = newRegion; } var union = newRegion.Union(body.Region); for (var x = union.Min.X; x <= union.Max.X; x++) { for (var y = union.Min.Y; y <= union.Max.Y; y++) { var bucketId = x + "," + y; var bucketExists = Buckets.ContainsKey(bucketId); var isInsideNewRegion = newRegion.Contains(x, y); var isInsideOldRegion = body.Region.Contains(x, y); if (!isInsideNewRegion && isInsideOldRegion) { if (bucketExists) { var bucket = Buckets[bucketId]; bucket.Remove(body); foreach (var pair in from _body in bucket select Helper.GetPairId(body, _body) into pairId where _tmpPairs.ContainsKey(pairId) select _tmpPairs[pairId]) { pair.Count--; } } } if (body.Region == newRegion || (isInsideNewRegion && !isInsideOldRegion) || forceUpdate) { var bucket = bucketExists ? Buckets[bucketId] : new List <Body>(); if (!bucketExists) { Buckets.Add(bucketId, bucket); } foreach (var bodyB in bucket) { if (body.Id.Value == bodyB.Id.Value || (body.Static && bodyB.Static)) { continue; } var pairId = Helper.GetPairId(body, bodyB); TmpPair pair; if (_tmpPairs.TryGetValue(pairId, out pair)) { pair.Count++; } else { _tmpPairs.Add(pairId, new TmpPair(body, bodyB, 1)); } } bucket.Add(body); } } } body.Region = newRegion; gridChanged = true; } } if (gridChanged) { _tmpPairsList = _tmpPairs.Values.Where(x => x.Count > 0).ToList(); _tmpPairs = _tmpPairs.Where(x => x.Value.Count > 0).ToDictionary(x => x.Key, x => x.Value); } }