public void New() { foreach (int y in Enumerable.Range(0, ScreenHeight)) { foreach (int x in Enumerable.Range(0, ScreenWidth)) { Tiles[x, y] = new Tile( new Vector3(x, y, 0), new Vector3(x + (WorldPosition.X + ScreenWidth), y + (WorldPosition.Y + ScreenHeight), 0), new Vector2(0, 0), 16, 16, "Textures/overworld"); } } }
public void LoadContent(ContentManager contentManager) { Texture = contentManager.Load<Texture2D>(TextureName); int tileWidth = Texture.Width / 16; int tileHeight = Texture.Height / 16; Tiles = new Tile[tileWidth * tileHeight]; int counter = 0; foreach (int y in Enumerable.Range(0, tileHeight)) { foreach (int x in Enumerable.Range(0, tileWidth)) { Tiles[counter] = new Tile(Vector3.Zero, Vector3.Zero, new Vector2(x * 16, y * 16), 16, 16, TextureName); Tiles[counter].LoadContent(contentManager); counter++; } } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (this.IsActive) { _keyboardState = Keyboard.GetState(); _mouse = Mouse.GetState(); Window.Title = string.Format("Windfish Editor (X: {0} Y: {1})", _world.CurrentScreenPosition.X, _world.CurrentScreenPosition.Y); _world.Update(gameTime); if (_keyboardState.IsKeyDown(Keys.Up) && !_lastKeyboardState.IsKeyDown(Keys.Up)) { _world.MoveToScreen(new Vector2(_world.CurrentScreenPosition.X, _world.CurrentScreenPosition.Y - 1)); } if (_keyboardState.IsKeyDown(Keys.Down) && !_lastKeyboardState.IsKeyDown(Keys.Down)) { _world.MoveToScreen(new Vector2(_world.CurrentScreenPosition.X, _world.CurrentScreenPosition.Y + 1)); } if (_keyboardState.IsKeyDown(Keys.Left) && !_lastKeyboardState.IsKeyDown(Keys.Left)) { _world.MoveToScreen(new Vector2(_world.CurrentScreenPosition.X - 1, _world.CurrentScreenPosition.Y)); } if (_keyboardState.IsKeyDown(Keys.Right) && !_lastKeyboardState.IsKeyDown(Keys.Right)) { _world.MoveToScreen(new Vector2(_world.CurrentScreenPosition.X + 1, _world.CurrentScreenPosition.Y)); } if (_keyboardState.IsKeyDown(Keys.F2) && !_lastKeyboardState.IsKeyDown(Keys.F2)) { _world.Save("World.map"); } if (MapViewport.Bounds.Contains(_mouse.Position)) { if (_mouse.LeftButton == ButtonState.Pressed) { Tile tile = (Tile)_primaryTile.Clone(); tile.LocalPosition = new Vector3(_cursorMapPosition.X, _cursorMapPosition.Y, 0); tile.WorldPosition = new Vector3(_cursorMapPosition.X + (_world.CurrentScreenPosition.X * Screen.ScreenWidth), _cursorMapPosition.Y + (_world.CurrentScreenPosition.Y * Screen.ScreenHeight), 0); _world.CurrentScreen.SetTile(_cursorMapPosition.X.ToInt32(), _cursorMapPosition.Y.ToInt32(), tile); } if (_mouse.RightButton == ButtonState.Pressed) { Tile tile = (Tile)_secondaryTile.Clone(); tile.LocalPosition = new Vector3(_cursorMapPosition.X, _cursorMapPosition.Y, 0); tile.WorldPosition = new Vector3(_cursorMapPosition.X + (_world.CurrentScreenPosition.X * Screen.ScreenWidth), _cursorMapPosition.Y + (_world.CurrentScreenPosition.Y * Screen.ScreenHeight), 0); _world.CurrentScreen.SetTile(_cursorMapPosition.X.ToInt32(), _cursorMapPosition.Y.ToInt32(), tile); } } if (TilesViewport.Bounds.Contains(_mouse.Position)) { _cursorMapPosition = new Vector2((_mouse.Position.X - TilesViewport.Bounds.X) / 32, _mouse.Position.Y / 32); int rowLength = TilesViewport.Width / 32; if (_mouse.LeftButton == ButtonState.Pressed) { int index = (int)(_cursorMapPosition.X + rowLength * _cursorMapPosition.Y); if (index < _tileset.Tiles.Length) { _primaryTile = _tileset.Tiles[index]; } } if (_mouse.RightButton == ButtonState.Pressed) { int index = (int)(_cursorMapPosition.X + rowLength * _cursorMapPosition.Y); if (index < _tileset.Tiles.Length) { _secondaryTile = _tileset.Tiles[index]; } } } _lastKeyboardState = _keyboardState; base.Update(gameTime); } else { Debug.Print("Not Active"); } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); _tileset = new Tileset("Textures/overworld"); _tileset.LoadContent(Content); _primaryTile = _tileset.Tiles[0]; _secondaryTile = _tileset.Tiles[1]; _world.LoadContent(Content); // TODO: use this.Content to load your game content here }
public void SetTile(int x, int y, Tile tile) { if (x >= 0 && x < ScreenWidth && y >= 0 && y < ScreenHeight) { Tiles[x, y] = tile; } }
public Screen(Vector2 worldPosition) { WorldPosition = worldPosition; Tiles = new Tile[ScreenWidth, ScreenHeight]; GameObjects = new List<GameObject>(); }