public SpawnPoint(SpawnController spawner, string type, Vector2 position) { SpawnController = spawner; EntityType = type; Position = position; OriginalPosition = position; }
internal SpawnPoint(SpawnController spawner, Squared.Tiled.Object obj) { SpawnController = spawner; Size = new Vector2(obj.Width, obj.Height); Position = new Vector2(obj.X, obj.Y) + Size / 2; OriginalPosition = new Vector2(obj.X, obj.Y) + Size / 2; Name = obj.Name; Properties = obj.Properties; EntityType = obj.Type; }
public GameEnvironment(Controller ctrl) : base(ctrl) { Pressure = 0.0f; Sound.StopAll(true); // Camera. Camera = new Camera2D(this); //Camera.Position = new Vector2(1280.0f, k_idealScreenSize.Y * 0.5f); Camera.MoveSpeed = defaultCameraMoveSpeed; Camera.ResetEffectScale(1.0f); // Set at slightly higher than 1.0 so we can do a zoom out pressure effect. // Window. Controller.Window.ClientSizeChanged += WindowSizeChanged; WindowSizeChanged(null, null); Controller.IsMouseVisible = true; // Collision. CollisionWorld = new Physics.Dynamics.World(Vector2.Zero); // Create a new SpriteBatch which can be used to draw textures. m_spriteBatch = new SpriteBatch(ctrl.GraphicsDevice); ParticleRenderer = new SpriteBatchRenderer { GraphicsDeviceService = ctrl.Graphics }; ParticleRenderer.LoadContent(contentManager); // Create collision notification callbacks. CollisionWorld.ContactManager.PreSolve += PreSolve; CollisionWorld.ContactManager.BeginContact += BeginContact; CollisionWorld.ContactManager.EndContact += EndContact; CollisionWorld.ContactManager.ContactFilter += ContactFilter; // HUD. HUD = new Menus.HUD(this); // Effects. m_tintEffect = contentManager.Load <Effect>("TintEffect"); FadeOut = new FaderOuter(this); SpawnController = new SpawnController(this); // Farseer freaks out unless we call Update here when changing Environments. FIXME: Why? Update(0.0f); }
internal SpawnPoint(SpawnController spawner, Squared.Tiled.Object obj) { SpawnController = spawner; Size = new Vector2(obj.Width, obj.Height); Position = new Vector2(obj.X, obj.Y) + Size / 2; OriginalPosition = new Vector2(obj.X, obj.Y) + Size / 2; Name = obj.Name; Properties = obj.Properties; EntityType = obj.Type; // Immediately spawn some entities. switch (EntityType) { case "spawn": case "boss": AlwaysSpawned = true; Spawn(); break; } }
/// <summary> /// Update the Environment each frame. /// </summary> /// <param name="elapsedTime">Time since last Update() call.</param> public override void Update(float elapsedTime) { // Toggle debug view = F1. if (Keyboard.GetState().IsKeyDown(Keys.F1) && !OldKeyboard.GetState().IsKeyDown(Keys.F1)) { if (m_debugView != null) { m_debugView = null; } else { m_debugView = new Physics.DebugViewXNA(CollisionWorld); } } //Pause with Escape if (!paused) { if (Keyboard.GetState().IsKeyDown(Keys.Escape) && !OldKeyboard.GetState().IsKeyDown(Keys.Escape) || (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Start) && !OldGamePad.GetState().IsButtonDown(Buttons.Start)) ) { pause(new GamePauseMenu(Controller, this)); Sound.PlayCue("scroll"); } } // Debug Menu = F10. if (Keyboard.GetState().IsKeyDown(Keys.F10) && !OldKeyboard.GetState().IsKeyDown(Keys.F10)) { Controller.ChangeEnvironment(new Menus.DebugMenu(Controller)); } // Exit game. if (Keyboard.GetState().IsKeyDown(Keys.F5) && !OldKeyboard.GetState().IsKeyDown(Keys.F5)) { Controller.Exit(); } if (!paused) { HUD.enabled = true; if (elapsedTime > 0.0f) { // Update physics. CollisionWorld.Step(elapsedTime); if (SpawnController != null) { SpawnController.Update(elapsedTime); } // Update entities. Camera.Update(elapsedTime); base.Update(elapsedTime); // Particle keepalive. Update remaining and try to remove if they are done. ParticleKeepalive.RemoveAll(ent => { ent.Update(elapsedTime); if (ent.Effect.ActiveParticlesCount == 0) { ent.Remove(); return(true); } else { return(false); } }); } } else { Camera.Update(elapsedTime); m_popUp.Update(elapsedTime); HUD.enabled = false; } HUD.Update(elapsedTime); FadeOut.Update(elapsedTime); if (Balloon != null) { heatMultiplier = (this.ScreenVirtualSize.Y / 2 - Balloon.Position.Y) * 0.0007f; } else { heatMultiplier = 0; } }
/// <summary> /// Load a map from file and create collision objects for it. Appends map horizontally if one exists. /// </summary> /// <param name="filename">File to load map from.</param> public void LoadOrExtendMap(string filename, bool spawnPlayer = false) { Tiled.Map map = Tiled.Map.Load(Path.Combine(Controller.Content.RootDirectory, filename), Controller.Content); // Destroy and re-create collision body for map. if (CollisionBody == null) { CreateCollisionBody(CollisionWorld, Physics.Dynamics.BodyType.Static); } Vector2 tileHalfSize = new Vector2(map.TileWidth, map.TileHeight) / 2; Vector2 tileSize = new Vector2(map.TileWidth, map.TileHeight); bool[,] levelCollision = new bool[map.Width, map.Height]; float defaultZVal = ZSettings.Ground; // 2 = collision. 1 = no collision. 0 = unknown. List <byte> collision = new List <byte>(); foreach (KeyValuePair <string, Tiled.Layer> layer in map.Layers) { defaultZVal -= 0.001f; for (int x = 0; x < layer.Value.Width; ++x) { for (int y = 0; y < layer.Value.Height; ++y) { int tileId = layer.Value.GetTile(x, y); if (tileId >= collision.Count || collision[tileId] == 0) { Tiled.Tileset.TilePropertyList props = GetTileProperties(map, tileId); // The only way to add new elements at arbitrary indices is to fill up the rest of the array. Do so. for (int i = collision.Count; i < tileId + 1; ++i) { collision.Add(0); } if (props != null && props.ContainsKey("collision")) { collision[tileId] = (byte)(props["collision"].Equals("true", StringComparison.OrdinalIgnoreCase) ? 2 : 1); } else { collision[tileId] = 1; } } levelCollision[x, y] |= (collision[tileId] > 1); } } float z = defaultZVal; if (layer.Value.Properties.ContainsKey("zindex")) { if (!float.TryParse(layer.Value.Properties["zindex"], out z)) { z = defaultZVal; } } MapLayer ml = new MapLayer(this, map, layer.Key, z); ml.Position = new Vector2(m_mapStart, 0.0f); AddChild(ml); } // Go through collision and try to create large horizontal collision shapes. for (int y = 0; y < map.Height; ++y) { int firstX = 0; bool hasCollision = false; for (int x = 0; x < map.Width; ++x) { if (levelCollision[x, y]) { if (hasCollision) { continue; } else { hasCollision = true; firstX = x; } } else { if (hasCollision) { hasCollision = false; int tilesWide = x - firstX; if (tilesWide == 1) { continue; } for (int i = firstX; i <= x; ++i) { levelCollision[i, y] = false; } AddCollisionRectangle( tileHalfSize * new Vector2(tilesWide, 1.0f) , new Vector2(tileSize.X * (x - (float)tilesWide / 2) + m_mapStart, tileSize.Y * (y + 0.5f)) ); } } } // Create final collision. if (hasCollision) { for (int i = firstX; i < map.Width; ++i) { levelCollision[i, y] = false; } int tilesWide = map.Width - firstX; AddCollisionRectangle( tileHalfSize * new Vector2(tilesWide, 1.0f) , new Vector2(tileSize.X * (map.Width - (float)tilesWide / 2) + m_mapStart, tileSize.Y * (y + 0.5f)) ); } } // Go through collision and try to create large vertical collision shapes. for (int x = 0; x < map.Width; ++x) { int firstY = 0; bool hasCollision = false; for (int y = 0; y < map.Height; ++y) { if (levelCollision[x, y]) { if (hasCollision) { continue; } else { hasCollision = true; firstY = y; } } else { if (hasCollision) { hasCollision = false; int tilesTall = y - firstY; AddCollisionRectangle( tileHalfSize * new Vector2(1.0f, tilesTall) , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y - (float)tilesTall / 2)) ); } } } // Create final collision. if (hasCollision) { int tilesTall = map.Height - firstY; AddCollisionRectangle( tileHalfSize * new Vector2(1.0f, tilesTall) , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (map.Height - (float)tilesTall / 2)) ); } } SpawnController.CreateSpawnPoints(map.ObjectGroups.Values, new Vector2(m_mapStart, 0.0f), spawnPlayer); m_mapStart += map.Width * map.TileWidth; }
/// <summary> /// Load a map from file and create collision objects for it. /// </summary> /// <param name="filename">File to load map from.</param> public void LoadMap(string filename) { m_map = Tiled.Map.Load(Path.Combine(Controller.Content.RootDirectory, filename), Controller.Content); // Destroy and re-create collision body for map. DestroyCollisionBody(); CreateCollisionBody(CollisionWorld, Physics.Dynamics.BodyType.Static); Vector2 tileHalfSize = new Vector2(m_map.TileWidth, m_map.TileHeight) / 2; Vector2 tileSize = new Vector2(m_map.TileWidth, m_map.TileHeight); bool[,] levelCollision = new bool[m_map.Width, m_map.Height]; foreach (Tiled.Layer layer in m_map.Layers.Values) { for (int x = 0; x < layer.Width; ++x) for (int y = 0; y < layer.Height; ++y) { int tileId = layer.GetTile(x, y) - 1; if (tileId < 0) continue; int row = tileId / 15; int col = tileId - row * 15; if (row >= 11 || col >= 15) continue; levelCollision[x, y] = (collision[row, col] != 0); } } // Go through collision and try to create large horizontal collision shapes. for (int y = 0; y < m_map.Height; ++y) { int firstX = 0; bool hasCollision = false; for (int x = 0; x < m_map.Width; ++x) { if (levelCollision[x, y]) { if (hasCollision) continue; else { hasCollision = true; firstX = x; } } else { if (hasCollision) { hasCollision = false; int tilesWide = x - firstX; if (tilesWide == 1) continue; for (int i = firstX; i <= x; ++i) levelCollision[i, y] = false; AddCollisionRectangle( tileHalfSize * new Vector2(tilesWide, 1.0f) , new Vector2(tileSize.X * (x - (float) tilesWide / 2), tileSize.Y * (y + 0.5f)) ); } } } // Create final collision. if (hasCollision) { for (int i = firstX; i < m_map.Width; ++i) levelCollision[i, y] = false; int tilesWide = m_map.Width - firstX; AddCollisionRectangle( tileHalfSize * new Vector2(tilesWide, 1.0f) , new Vector2(tileSize.X * (m_map.Width - (float) tilesWide / 2), tileSize.Y * (y + 0.5f)) ); } } // Go through collision and try to create large vertical collision shapes. for (int x = 0; x < m_map.Width; ++x) { int firstY = 0; bool hasCollision = false; for (int y = 0; y < m_map.Height; ++y) { if (levelCollision[x, y]) { if (hasCollision) continue; else { hasCollision = true; firstY = y; } } else { if (hasCollision) { hasCollision = false; int tilesTall = y - firstY; AddCollisionRectangle( tileHalfSize * new Vector2(1.0f, tilesTall) , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y - (float) tilesTall / 2)) ); } } } // Create final collision. if (hasCollision) { int tilesTall = m_map.Height - firstY; AddCollisionRectangle( tileHalfSize * new Vector2(1.0f, tilesTall) , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (m_map.Height - (float) tilesTall / 2)) ); } } SpawnController = new SpawnController(this, m_map.ObjectGroups.Values); }