public override void Update(GameSettings settings, GameTime gameTime) { ContactManager cm = World.ContactManager; DynamicTreeBroadPhase dt = cm.BroadPhase as DynamicTreeBroadPhase; if (dt != null) { int height = dt.ComputeHeight(); int leafCount = dt.ProxyCount; int minimumNodeCount = 2 * leafCount - 1; float minimumHeight = (float)Math.Ceiling(Math.Log(minimumNodeCount) / Math.Log(2.0f)); DebugView.DrawString(50, TextLine, "Test of dynamic tree performance in worse case scenario.", height, minimumHeight); TextLine += 15; DebugView.DrawString(50, TextLine, "I know this is slow. I hope to address this in a future update.", height, minimumHeight); TextLine += 15; DebugView.DrawString(50, TextLine, "Dynamic tree height = {0}, min = {1}", height, minimumHeight); TextLine += 15; } base.Update(settings, gameTime); }
internal void DestroyProxies(DynamicTreeBroadPhase broadPhase) { // Destroy proxies in the broad-phase. for (int i = 0; i < ProxyCount; ++i) { broadPhase.RemoveProxy(Proxies[i].ProxyId); Proxies[i].ProxyId = -1; } ProxyCount = 0; }
internal void destroyProxies(DynamicTreeBroadPhase broadPhase) { // Destroy proxies in the broad-phase. for (int i = 0; i < proxyCount; ++i) { broadPhase.removeProxy(proxies[i].proxyId); proxies[i].proxyId = -1; } proxyCount = 0; }
public void Restore(DynamicTreeBroadPhase dynamicTreeBroadPhase) { dynamicTreeBroadPhase._moveBuffer = this._moveBuffer; dynamicTreeBroadPhase._moveCapacity = this._moveCapacity; dynamicTreeBroadPhase._moveCount = this._moveCount; dynamicTreeBroadPhase._pairBuffer = new Pair[this._pairBuffer.Length]; Array.Copy(this._pairBuffer, dynamicTreeBroadPhase._pairBuffer, dynamicTreeBroadPhase._pairBuffer.Length); dynamicTreeBroadPhase._pairCapacity = this._pairCapacity; dynamicTreeBroadPhase._pairCount = this._pairCount; dynamicTreeBroadPhase._proxyCount = this._proxyCount; dynamicTreeBroadPhase._queryProxyId = this._queryProxyId; this.dynamicTreeClone.Restore(dynamicTreeBroadPhase._tree); }
public void Clone(DynamicTreeBroadPhase dynamicTreeBroadPhase) { this._moveBuffer = dynamicTreeBroadPhase._moveBuffer; this._moveCapacity = dynamicTreeBroadPhase._moveCapacity; this._moveCount = dynamicTreeBroadPhase._moveCount; this._pairBuffer = new Pair[dynamicTreeBroadPhase._pairBuffer.Length]; Array.Copy(dynamicTreeBroadPhase._pairBuffer, this._pairBuffer, this._pairBuffer.Length); this._pairCapacity = dynamicTreeBroadPhase._pairCapacity; this._pairCount = dynamicTreeBroadPhase._pairCount; this._proxyCount = dynamicTreeBroadPhase._proxyCount; this._queryProxyId = dynamicTreeBroadPhase._queryProxyId; this.dynamicTreeClone.Clone(dynamicTreeBroadPhase._tree); }
public override void Update(GameSettings settings, GameTime gameTime) { ContactManager cm = World.ContactManager; DynamicTreeBroadPhase dt = (DynamicTreeBroadPhase)cm.BroadPhase; int height = dt.TreeHeight; int leafCount = dt.ProxyCount; float minimumNodeCount = 2 * leafCount - 1; float minimumHeight = (float)Math.Ceiling(Math.Log(minimumNodeCount) / Math.Log(2.0f)); DrawString(string.Format("dynamic tree height = {0}, min = {1}", height, (int)minimumHeight)); DrawString(string.Format("create time = {0} ms, fixture count = {1}", _createTime, _fixtureCount)); base.Update(settings, gameTime); }
public LevelHandler(GameServer root, string episodeName, string levelName) { this.root = root; this.levelFileName = levelName; //this.episodeName = episodeName; difficulty = GameDifficulty.Multiplayer; gravity = DefaultGravity; collisions = new DynamicTreeBroadPhase <ActorBase>(); eventSpawner = new EventSpawner(this); rootObject = new GameObject(); rootObject.AddComponent(new LocalController(this)); AddObject(rootObject); // Load level LoadLevel(levelFileName, episodeName); // Common sounds commonResources = ContentResolver.Current.RequestMetadata("Common/Scenery"); // Preload frequently used metadata ContentResolver.Current.PreloadAsync("Common/Explosions"); ContentResolver.Current.PreloadAsync("Interactive/PlayerJazz"); ContentResolver.Current.PreloadAsync("Interactive/PlayerSpaz"); ContentResolver.Current.PreloadAsync("Interactive/PlayerLori"); ContentResolver.Current.PreloadAsync("Weapon/Blaster"); ContentResolver.Current.PreloadAsync("Weapon/Bouncer"); ContentResolver.Current.PreloadAsync("Weapon/Freezer"); ContentResolver.Current.PreloadAsync("Weapon/Toaster"); ContentResolver.Current.PreloadAsync("Weapon/RF"); ContentResolver.Current.PreloadAsync("Weapon/Seeker"); ContentResolver.Current.PreloadAsync("Weapon/TNT"); ContentResolver.Current.PreloadAsync("Weapon/Pepper"); ContentResolver.Current.PreloadAsync("Weapon/Electro"); ContentResolver.Current.PreloadAsync("Weapon/Thunderbolt"); }
// These support body activation/deactivation. internal void CreateProxies(DynamicTreeBroadPhase broadPhase, ref Transform xf) { Debug.Assert(ProxyCount == 0); // Create proxies in the broad-phase. ProxyCount = Shape.ChildCount; for (int i = 0; i < ProxyCount; ++i) { FixtureProxy proxy = new FixtureProxy(); Shape.ComputeAABB(out proxy.AABB, ref xf, i); proxy.Fixture = this; proxy.ChildIndex = i; //FPE note: This line needs to be after the previous two because FixtureProxy is a struct proxy.ProxyId = broadPhase.AddProxy(ref proxy); Proxies[i] = proxy; } }
public override void Update(GameSettings settings, GameTime gameTime) { ContactManager cm = World.ContactManager; DynamicTreeBroadPhase broadPhase = (DynamicTreeBroadPhase)cm.BroadPhase; int height = broadPhase.TreeHeight; int leafCount = broadPhase.ProxyCount; int minimumNodeCount = 2 * leafCount - 1; float minimumHeight = MathUtils.Ceil(MathUtils.Log(minimumNodeCount) / MathUtils.Log(2.0f)); DrawString($"dynamic tree height = {height}, min = {(int)minimumHeight}"); base.Update(settings, gameTime); DrawString($"create time = {_createTime} ms, fixture count = {_fixtureCount}"); //b2DynamicTree* tree = &World.m_contactManager.m_broadPhase.m_tree; //if (m_stepCount == 400) //{ // tree.RebuildBottomUp(); //} }
internal void Synchronize(DynamicTreeBroadPhase broadPhase, ref Transform transform1, ref Transform transform2) { if (ProxyCount == 0) { return; } for (var i = 0; i < ProxyCount; ++i) { var proxy = Proxies[i]; // Compute an AABB that covers the swept Shape (may miss some rotation effect). AABB aabb1, aabb2; Shape.ComputeAABB(out aabb1, ref transform1, proxy.ChildIndex); Shape.ComputeAABB(out aabb2, ref transform2, proxy.ChildIndex); proxy.AABB.Combine(ref aabb1, ref aabb2); Vector2 displacement = transform2.P - transform1.P; broadPhase.MoveProxy(proxy.ProxyId, ref proxy.AABB, displacement); } }
internal ContactManager(DynamicTreeBroadPhase broadPhase) { this.BroadPhase = broadPhase; OnBroadphaseCollision = AddPair; }
public LevelHandler(App root, LevelInitialization data) { this.root = root; levelFileName = data.LevelName; episodeName = data.EpisodeName; difficulty = data.Difficulty; gravity = DefaultGravity; collisions = new DynamicTreeBroadPhase <ActorBase>(); api = new ActorApi(this); eventSpawner = new EventSpawner(api); rootObject = new GameObject(); rootObject.AddComponent(new LocalController(this)); AddObject(rootObject); // Load level LoadLevel(levelFileName, episodeName); // Create HUD Hud hud = rootObject.AddComponent <Hud>(); // Process carry overs if (data.PlayerCarryOvers != null) { for (int i = 0; i < data.PlayerCarryOvers.Length; i++) { Vector2 spawnPosition = eventMap.GetSpawnPosition(data.PlayerCarryOvers[i].Type); if (spawnPosition == new Vector2(-1, -1)) { spawnPosition = eventMap.GetSpawnPosition(PlayerType.Jazz); if (spawnPosition == new Vector2(-1, -1)) { continue; } } Player player = new Player(); player.OnActivated(new ActorActivationDetails { Api = api, Pos = new Vector3(spawnPosition, PlayerZ), Params = new[] { (ushort)data.PlayerCarryOvers[i].Type, (ushort)i } }); AddPlayer(player); if (i == 0) { player.AttachToHud(hud); } player.ReceiveLevelCarryOver(data.ExitType, ref data.PlayerCarryOvers[i]); } } Player targetPlayer; Vector3 targetPlayerPosition; if (players.Count > 0) { targetPlayer = players[0]; targetPlayerPosition = targetPlayer.Transform.Pos; // Setup all cameras float relativeViewRange = (1f / players.Count); for (int i = 0; i < players.Count; i++) { GameObject camera = new GameObject(/*"MainCamera " + i*/); Transform cameraTransform = camera.AddComponent <Transform>(); Camera cameraInner = camera.AddComponent <Camera>(); cameraInner.NearZ = NearZ; cameraInner.FarZ = FarZ; cameraInner.Projection = ProjectionMode.Orthographic; cameraInner.VisibilityMask = VisibilityFlag.Group0 | VisibilityFlag.ScreenOverlay | (VisibilityFlag)(1 << i); switch (players.Count) { case 1: cameraInner.TargetRect = new Rect(0f, 0f, 1f, 1f); break; case 2: cameraInner.TargetRect = new Rect(0f, i * relativeViewRange, 1f, relativeViewRange); break; case 3: cameraInner.TargetRect = new Rect(0f, i * relativeViewRange, 1f, relativeViewRange); break; case 4: cameraInner.TargetRect = new Rect((i % 2) * 0.5f, (i / 2) * 0.5f, 0.5f, 0.5f); break; } // Create controller CameraController cameraController = camera.AddComponent <CameraController>(); cameraController.ViewBounds = levelBounds; // Bind camera to player cameraInner.RenderingSetup = new LevelRenderSetup(this); Player currentPlayer = players[i]; cameraTransform.Pos = new Vector3(currentPlayer.Transform.Pos.Xy, 0); cameraController.TargetObject = currentPlayer; ((ICmpFixedUpdatable)cameraController).OnFixedUpdate(1f); camera.Parent = rootObject; cameras.Add(camera); if (i == 0) { // First camera is always sound listener DualityApp.Sound.Listener = camera; } } } else { GameObject camera = new GameObject(/*"MainCamera " + i*/); Transform cameraTransform = camera.AddComponent <Transform>(); Camera cameraInner = camera.AddComponent <Camera>(); cameraInner.NearZ = NearZ; cameraInner.FarZ = FarZ; cameraInner.Projection = ProjectionMode.Orthographic; cameraInner.VisibilityMask = VisibilityFlag.Group0 | VisibilityFlag.ScreenOverlay | (VisibilityFlag)(1 << 0); // Create controller CameraController cameraController = camera.AddComponent <CameraController>(); cameraController.ViewBounds = levelBounds; // Bind camera to player cameraInner.RenderingSetup = new LevelRenderSetup(this); cameraTransform.Pos = new Vector3(levelBounds.Center, 0); ((ICmpUpdatable)cameraController).OnUpdate(); camera.Parent = rootObject; cameras.Add(camera); // First camera is always sound listener DualityApp.Sound.Listener = camera; hud.BeginFadeIn(true); } // Common sounds commonResources = ContentResolver.Current.RequestMetadata("Common/Scenery"); }
public LevelHandler(Controller root, LevelInitialization data) { this.root = root; //levelName = data.LevelName; levelFileName = data.LevelName; episodeName = data.EpisodeName; difficulty = data.Difficulty; gravity = DefaultGravity; collisions = new DynamicTreeBroadPhase(); api = new ActorApi(this); eventSpawner = new EventSpawner(api); rootObject = new GameObject("LevelManager"); rootObject.AddComponent(new LocalController(this)); AddObject(rootObject); // Setup camera camera = new GameObject("MainCamera"); Transform cameraTransform = camera.AddComponent <Transform>(); Camera cameraInner = camera.AddComponent <Camera>(); cameraInner.NearZ = NearZ; cameraInner.FarZ = FarZ; cameraInner.Perspective = PerspectiveMode.Flat; CameraController cameraController = camera.AddComponent <CameraController>(); // Load level LoadLevel(levelFileName, episodeName); // Process carry overs if (data.PlayerCarryOvers != null) { for (int i = 0; i < data.PlayerCarryOvers.Length; i++) { Vector2 spawnPosition = eventMap.GetSpawnPosition(data.PlayerCarryOvers[i].Type); if (spawnPosition == new Vector2(-1, -1)) { spawnPosition = eventMap.GetSpawnPosition(PlayerType.Jazz); if (spawnPosition == new Vector2(-1, -1)) { continue; } } Player player = new Player(); player.OnAttach(new ActorInstantiationDetails { Api = api, Pos = new Vector3(spawnPosition, PlayerZ), Params = new[] { (ushort)data.PlayerCarryOvers[i].Type, (ushort)i } }); AddPlayer(player); player.ReceiveLevelCarryOver(data.ExitType, ref data.PlayerCarryOvers[i]); } } Player targetPlayer; Vector3 targetPlayerPosition; if (players.Count > 0) { targetPlayer = players[0]; targetPlayerPosition = targetPlayer.Transform.Pos; } else { Debug.WriteLine("No spawn point found, used default location instead!"); targetPlayerPosition = new Vector3(120, 160, PlayerZ); targetPlayer = new Player(); targetPlayer.OnAttach(new ActorInstantiationDetails { Api = api, Pos = targetPlayerPosition, Params = new[] { (ushort)PlayerType.Jazz, (ushort)0 } }); AddPlayer(targetPlayer); } // Bind camera to player cameraInner.RenderingSetup = new LevelRenderSetup(this); cameraTransform.Pos = new Vector3(targetPlayerPosition.X, targetPlayerPosition.Y, 0); cameraController.TargetObject = targetPlayer; ((ICmpUpdatable)cameraController).OnUpdate(); camera.Parent = rootObject; DualityApp.Sound.Listener = camera; // Attach player to UI targetPlayer.AttachToHud(rootObject.AddComponent <Hud>()); // Common sounds commonResources = ContentResolver.Current.RequestMetadata("Common/Scenery"); }
public bool ChangeLevel(string levelName, MultiplayerLevelType levelType) { string path = Path.Combine(DualityApp.DataDirectory, "Episodes", levelName + ".level"); if (!File.Exists(path)) { return(false); } IFileSystem levelPackage = new CompressedContent(path); lock (sync) { currentLevel = levelName; currentLevelType = levelType; // Load new level using (Stream s = levelPackage.OpenFile(".res", FileAccessMode.Read)) { // ToDo: Cache parser JsonParser json = new JsonParser(); LevelHandler.LevelConfigJson config = json.Parse <LevelHandler.LevelConfigJson>(s); if (config.Version.LayerFormat > LevelHandler.LayerFormatVersion || config.Version.EventSet > LevelHandler.EventSetVersion) { throw new NotSupportedException("Version not supported"); } currentLevelFriendlyName = BitmapFont.StripFormatting(config.Description.Name); Log.Write(LogType.Info, "Loading level \"" + currentLevelFriendlyName + "\" (" + currentLevelType + ")..."); Point2 tileMapSize; using (Stream s2 = levelPackage.OpenFile("Sprite.layer", FileAccessMode.Read)) using (BinaryReader r = new BinaryReader(s2)) { tileMapSize.X = r.ReadInt32(); tileMapSize.Y = r.ReadInt32(); } levelBounds = new Rect(tileMapSize * /*tileMap.Tileset.TileSize*/ 32); collisions = new DynamicTreeBroadPhase <ICollisionable>(); // Read events eventMap = new ServerEventMap(tileMapSize); if (levelPackage.FileExists("Events.layer")) { using (Stream s2 = levelPackage.OpenFile("Events.layer", FileAccessMode.Read)) { eventMap.ReadEvents(s2, config.Version.LayerFormat); } } } // Send request to change level to all players foreach (KeyValuePair <NetConnection, Player> pair in players) { pair.Value.State = PlayerState.NotReady; } playerConnections.Clear(); foreach (KeyValuePair <NetConnection, Player> pair in players) { Send(new LoadLevel { LevelName = currentLevel, LevelType = currentLevelType, AssignedPlayerIndex = pair.Value.Index }, 64, pair.Key, NetDeliveryMethod.ReliableUnordered, PacketChannels.Main); } } return(true); }
/// <summary> /// Call this to draw shapes and other debug draw data. /// </summary> private void DrawDebugData() { if ((Flags & DebugViewFlags.Shape) == DebugViewFlags.Shape) { foreach (Body b in world.BodyList) { FarseerPhysics.Common.Transform xf; b.GetTransform(out xf); foreach (Fixture f in b.FixtureList) { if (b.Enabled == false) { DrawShape(f, xf, InactiveShapeColor); } else if (b.BodyType == BodyType.Static) { DrawShape(f, xf, StaticShapeColor); } else if (b.BodyType == BodyType.Kinematic) { DrawShape(f, xf, KinematicShapeColor); } else if (b.IsAwake == false) { DrawShape(f, xf, SleepingShapeColor); } else { DrawShape(f, xf, DefaultShapeColor); } } } } if ((Flags & DebugViewFlags.ContactPoints) == DebugViewFlags.ContactPoints) { const float axisScale = 0.3f; for (int i = 0; i < _pointCount; ++i) { ContactPoint point = _points[i]; if (point.State == PointState.Add) { DrawPoint(point.Position, 0.1f, new Color(0.3f, 0.95f, 0.3f)); } else if (point.State == PointState.Persist) { DrawPoint(point.Position, 0.1f, new Color(0.3f, 0.3f, 0.95f)); } if ((Flags & DebugViewFlags.ContactNormals) == DebugViewFlags.ContactNormals) { Vector2 p1 = point.Position; Vector2 p2 = p1 + axisScale * point.Normal; DrawSegment(p1, p2, new Color(0.4f, 0.9f, 0.4f)); } } _pointCount = 0; } if ((Flags & DebugViewFlags.PolygonPoints) == DebugViewFlags.PolygonPoints) { foreach (Body body in world.BodyList) { foreach (Fixture f in body.FixtureList) { PolygonShape polygon = f.Shape as PolygonShape; if (polygon != null) { FarseerPhysics.Common.Transform xf; body.GetTransform(out xf); for (int i = 0; i < polygon.Vertices.Count; i++) { Vector2 tmp = MathUtils.Mul(ref xf, polygon.Vertices[i]); DrawPoint(tmp, 0.1f, Color.Red); } } } } } if ((Flags & DebugViewFlags.Joint) == DebugViewFlags.Joint) { foreach (Joint j in world.JointList) { FSDebugView.DrawJoint(this, j); } } if ((Flags & DebugViewFlags.AABB) == DebugViewFlags.AABB) { Color color = new Color(0.9f, 0.3f, 0.9f); DynamicTreeBroadPhase bp = world.ContactManager.BroadPhase; foreach (Body body in world.BodyList) { if (body.Enabled == false) { continue; } foreach (Fixture f in body.FixtureList) { for (int t = 0; t < f.ProxyCount; ++t) { FixtureProxy proxy = f.Proxies[t]; AABB aabb; bp.GetFatAABB(proxy.ProxyId, out aabb); DrawAABB(ref aabb, color); } } } } if ((Flags & DebugViewFlags.CenterOfMass) == DebugViewFlags.CenterOfMass) { foreach (Body b in world.BodyList) { FarseerPhysics.Common.Transform xf; b.GetTransform(out xf); xf.P = b.WorldCenter; DrawTransform(ref xf); } } if ((Flags & DebugViewFlags.Controllers) == DebugViewFlags.Controllers) { for (int i = 0; i < world.ControllerList.Count; i++) { Controller controller = world.ControllerList[i]; BuoyancyController buoyancy = controller as BuoyancyController; if (buoyancy != null) { AABB container = buoyancy.Container; DrawAABB(ref container, Color.LightBlue); } } } if ((Flags & DebugViewFlags.DebugPanel) == DebugViewFlags.DebugPanel) { DrawDebugPanel(); } }