public void OnDownArrowWasPressed() { if(!isMoving) { direction = XNAGameObjectDirection.Down; desiredTile = currentTile.NearbyTiles[direction]; if (desiredTile.IsWalkable) { frame.Y = 0; isMoving = true; } } }
public Map(ContentManager contentManager) { int[,] map = new int[100, 100]; for (int x = 0; x < map.GetLength(0); x++) { for (int y = 0; y < map.GetLength(1); y++) { double r = new Random().NextDouble(); if (NumericHelper.GetRandomNumber() < Settings.AUTOMATA_CHANCE_TO_STAY_ALIVE) { map[y, x] = 1; } } } for (int i = 0; i < Settings.GAME_OF_LIFE_NUMBER_OF_STEPS; i++) { map = GameOfLifeHelper.DoTransitionFromOldMap(map); } Tiles = new Tile[map.GetLength(1), map.GetLength(0)]; for (int y = 0; y < map.GetLength(0); y++) { for (int x = 0; x < map.GetLength(1); x++) { Tile tile = new Tile() { Frame = new Rectangle(x * Settings.TILE_WIDTH, y * Settings.TILE_HEIGHT, Settings.TILE_WIDTH, Settings.TILE_HEIGHT), IsWalkable = map[y, x] == 0, Texture = contentManager.Load<Texture2D>(map[y, x] == 0 ? "tile1" : "tile2"), MapPosition = new Vector2(x, y) }; Tiles[x, y] = tile; } } while (StarterTile == null) { int randX = (int)Math.Floor((new Random().NextDouble() * map.GetLength(0))); int randY = (int)Math.Floor((new Random().NextDouble() * map.GetLength(1))); if (GameOfLifeHelper.GetMooreNeighborsAlive(randX, randY, map) >= Settings.GOOD_NUMBER_OF_ALIVE_CELLS_TO_BE_A_CAVE && Tiles[randX, randY].IsWalkable) StarterTile = Tiles[randX, randY]; } for (int x = 0; x < Tiles.GetLength(0); x++) { for (int y = 0; y < Tiles.GetLength(1); y++) { Tile tile = Tiles[x, y]; if (tile != null) { if (y > 0 && Tiles[x, y - 1] != null) tile.NearbyTiles.Add(XNAGameObjectDirection.Up, Tiles[x, y - 1]); if (x > 0 && Tiles[x - 1, y] != null) tile.NearbyTiles.Add(XNAGameObjectDirection.Left, Tiles[x - 1, y]); if (x + 1 < Tiles.GetLength(0) && Tiles[x + 1, y] != null) tile.NearbyTiles.Add(XNAGameObjectDirection.Right, Tiles[x + 1, y]); if (y + 1 < Tiles.GetLength(1) && Tiles[x, y + 1] != null) tile.NearbyTiles.Add(XNAGameObjectDirection.Down, Tiles[x, y + 1]); } } } }
public void OnRightArrowWasPressed() { if (!isMoving) { direction = XNAGameObjectDirection.Right; desiredTile = currentTile.NearbyTiles[direction]; if (desiredTile.IsWalkable) { frame.Y = frame.Height * 2; isMoving = true; } } }
public Hero(int frameWidth, int frameHeight, Tile initialTile) : base(new Rectangle(0, 0, frameWidth, frameHeight), new Vector2((int)initialTile.Frame.X, (int)initialTile.Frame.Y)) { isMoving = false; currentTile = initialTile; }
public override void Update(GameTime gameTime) { timeSizeLastFrame += gameTime.ElapsedGameTime.Milliseconds; if (isMoving) { if (timeSizeLastFrame > Settings.FPS) { timeSizeLastFrame = 0; this.frame.X += this.frame.Width; if (this.frame.X >= this.frame.Width * Settings.NUMBER_OF_FRAMES_PER_SHEET) { this.frame.X = 0; } } if (position.X == desiredTile.Frame.X && position.Y == desiredTile.Frame.Y) { currentTile = desiredTile; isMoving = false; } else { if (position.Y != desiredTile.Frame.Y) { if (direction == XNAGameObjectDirection.Up) { position.Y--; } if (direction == XNAGameObjectDirection.Down) { position.Y++; } } if (position.X != desiredTile.Frame.X) { if (direction == XNAGameObjectDirection.Left) { position.X--; } if (direction == XNAGameObjectDirection.Right) { position.X++; } } } } }