private bool checkNextTile(MapTile mapTile, int x, int y) { //See if there is a door we need to open checkDoors(mapTile, x, y); //See if there is character to fight if (mapTile.ObjectTile != null && mapTile.ObjectTile.Category == "character") { if (mapTile.ObjectTile.Shortcut == "pri") { //Game is won Sounds.Kiss(); _gameState.GameIsWon = true; return(false); //Don't want to walk on her } Sounds.Fight(); _heroSpriteFighting = true; _startFightTime = -1; int heroDamage = 0; //A monsters attack ability is 1/2 their max health. Compare that to your armour //If you outclass them then there is still a chance of a lucky hit if (_random.Next((mapTile.ObjectTile.Health / 2) + 1) >= _gameState.Armour || (mapTile.ObjectTile.Health / 2 < _gameState.Armour && _random.Next(10) == 0)) { //Monsters do damage up to their max health - if they hit you. heroDamage = _random.Next(mapTile.ObjectTile.Health) + 1; _gameState.Health -= heroDamage; if (_gameState.Health <= 0) { _gameState.Health = 0; _heroSprite = new Sprite(null, _heroPosition.X * Tile.TileSizeX + Area.AreaOffsetX, _heroPosition.Y * Tile.TileSizeY + Area.AreaOffsetY, _tiles["bon"].Bitmap, _tiles["bon"].Rectangle, _tiles["bon"].NumberOfFrames); _heroSprite.ColorKey = Color.FromArgb(75, 75, 75); } } //Hero _popups.Clear(); _popups.Add(new textPopup((int)_heroSprite.Location.X + 40, (int)_heroSprite.Location.Y + 20, (heroDamage != 0) ? heroDamage.ToString() : "Pud³o")); //A monsters armour is 1/5 of their max health if (_random.Next(_gameState.Attack + 1) >= (mapTile.ObjectTile.Health / 5)) { //Hero damage is up to twice the attack rating if (damageMonster(_random.Next(_gameState.Attack * 2) + 1, mapTile, x, y)) { //Monster is dead now return(true); } } else { _popups.Add(new textPopup((int)mapTile.Sprite.Location.X + 40, (int)mapTile.Sprite.Location.Y + 20, "Pud³o")); } //Monster not dead return(false); } //If the next tile is a blocker then we can't move if (mapTile.Tile.IsBlock) { return(false); } return(true); }
public void KeyDown(Keys key) { //Ignore keypresses while we are animating or fighting if (!_heroSpriteAnimating && !_heroSpriteFighting) { switch (key) { case Keys.Right: //Are we at the edge of the map? if (_heroPosition.X < Area.MapSizeX - 1) { //Can we move to the next tile or not (blocking tile or monster) if (checkNextTile(_currentArea.Map[_heroPosition.X + 1, _heroPosition.Y], _heroPosition.X + 1, _heroPosition.Y)) { _heroSprite.Velocity = new PointF(100, 0); _heroSprite.Flip = true; _heroSpriteAnimating = true; _direction = HeroDirection.Right; _heroPosition.X++; setDestination(); } } else if (_currentArea.EastArea != "-") { //Edge of map - move to next area _currentArea = _world[_currentArea.EastArea]; _heroPosition.X = 0; setDestination(); _heroSprite.Location = _heroDestination; } break; case Keys.Left: //Are we at the edge of the map? if (_heroPosition.X > 0) { //Can we move to the next tile or not (blocking tile or monster) if (checkNextTile(_currentArea.Map[_heroPosition.X - 1, _heroPosition.Y], _heroPosition.X - 1, _heroPosition.Y)) { _heroSprite.Velocity = new PointF(-100, 0); _heroSprite.Flip = false; _heroSpriteAnimating = true; _direction = HeroDirection.Left; _heroPosition.X--; setDestination(); } } else if (_currentArea.WestArea != "-") { _currentArea = _world[_currentArea.WestArea]; _heroPosition.X = Area.MapSizeX - 1; setDestination(); _heroSprite.Location = _heroDestination; } break; case Keys.Up: //Are we at the edge of the map? if (_heroPosition.Y > 0) { //Can we move to the next tile or not (blocking tile or monster) if (checkNextTile(_currentArea.Map[_heroPosition.X, _heroPosition.Y - 1], _heroPosition.X, _heroPosition.Y - 1)) { _heroSprite.Velocity = new PointF(0, -100); _heroSpriteAnimating = true; _direction = HeroDirection.Up; _heroPosition.Y--; setDestination(); } } else if (_currentArea.NorthArea != "-") { //Edge of map - move to next area _currentArea = _world[_currentArea.NorthArea]; _heroPosition.Y = Area.MapSizeY - 1; setDestination(); _heroSprite.Location = _heroDestination; } break; case Keys.Down: //Are we at the edge of the map? if (_heroPosition.Y < Area.MapSizeY - 1) { //Can we move to the next tile or not (blocking tile or monster) if (checkNextTile(_currentArea.Map[_heroPosition.X, _heroPosition.Y + 1], _heroPosition.X, _heroPosition.Y + 1)) { _heroSprite.Velocity = new PointF(0, 100); _heroSpriteAnimating = true; _direction = HeroDirection.Down; _heroPosition.Y++; setDestination(); } } else if (_currentArea.SouthArea != "-") { //Edge of map - move to next area _currentArea = _world[_currentArea.SouthArea]; _heroPosition.Y = 0; setDestination(); _heroSprite.Location = _heroDestination; } break; case Keys.P: //Potion - if we have any if (_gameState.Potions > 0) { Sounds.Magic(); _gameState.Potions--; _heroSpriteFighting = true; _startFightTime = -1; //All monsters on the screen take maximum damage _popups.Clear(); for (int i = 0; i < Area.MapSizeX; i++) { for (int j = 0; j < Area.MapSizeY; j++) { MapTile mapTile = _currentArea.Map[i, j]; if (mapTile.ObjectTile != null && mapTile.ObjectTile.Category == "character") { damageMonster(_gameState.Attack * 2, mapTile, i, j); } } } } break; } } }