private static Message ChaseTarget(Tile target, Tile chaser, TileSet statics) { var movementVector = chaser.Location.NormalizedVector(target.Location); var bestMove = new[]{ chaser.Location + new Point(0, movementVector.Y), //vertical chaser.Location + new Point(movementVector.X, 0)} //horizontal .OrderBy(m => m.Distance(target.Location)) // order on distance from player .FirstOrDefault(m => !statics.CollisionDetection(chaser, m).Collision); //pick first where there is no collision return bestMove != default(Point) ? chaser.BuildMoveMessage(bestMove) : null; }
public void SetupBehaviours(TileSet mobiles, TileSet statics, List<Func<Message, Message>> gameMessageReactor) { var player = mobiles.WithName(GameConstants.PLAYER); var chaseBehaviour = MonsterAi.ChasePlayer(player, statics); //Setup player behaviour player.OnCollision.Add(collision => EatenByMonster(player, collision)); player.OnCollision.Add(collision => CrushedByBoulder(player, collision)); player.OnCollision.Add(collision => FellInHole(player, collision)); //Setup strong monster behaviour mobiles.WithType(GameConstants.STRONG_MONSTER).AddUpdateBehaviour(chaseBehaviour); mobiles.WithType(GameConstants.STRONG_MONSTER).AddCollisionRules( strongMonster => collision => CrushedByBoulder(strongMonster, collision)); mobiles.WithType(GameConstants.STRONG_MONSTER).AddCollisionRules( strongMonster => collision => FellInHole(strongMonster, collision)); //Setup weak monster behaviour mobiles.WithType(GameConstants.WEAK_MONSTER).AddUpdateBehaviour(chaseBehaviour); mobiles.WithType(GameConstants.WEAK_MONSTER).AddCollisionRules( weakMonster => collision => CrushedByBoulder(weakMonster, collision)); mobiles.WithType(GameConstants.WEAK_MONSTER).AddCollisionRules( weakMonster => collision => FellInHole(weakMonster, collision)); mobiles.WithType(GameConstants.WEAK_MONSTER).AddCollisionRules( weakMonster => collision => EatenByMonster(weakMonster, collision)); mobiles.WithType(GameConstants.PORTAL).AddCollisionRules(portal => collision => portal.BuildLevelCompleteMessage()); gameMessageReactor.Add(message => (Deaded(message, GameConstants.WEAK_MONSTER) || Deaded(message, GameConstants.PLAYER) || Deaded(message, GameConstants.STRONG_MONSTER)) ? statics[mobiles.WithName(message.Name).Location].BuildSplatMessage() : null); gameMessageReactor.Add(message => Deaded(message, GameConstants.HOLE) ? statics[mobiles.WithName(message.Name).Location].BuildCrashMessage() : null); //Setup boulder behaviour mobiles.WithType(GameConstants.BOULDER).AddCollisionRules(boulder => collision => Pushed(boulder,collision)); mobiles.WithType(GameConstants.BOULDER).AddCollisionRules(boulder => collision => FellInHole(boulder,collision)); //Setup hole behaviour mobiles.WithType(GameConstants.HOLE).AddCollisionRules(boulder => collision => CrushedByBoulder(boulder,collision)); }
public MessageHandler(TileSet mobiles) { _mobiles = mobiles; }
public static Tile WithName(this TileSet tileSet, string name) { return(tileSet.Tiles.FirstOrDefault(m => m.Name == name)); }
public static IEnumerable <Tile> GetActiveTiles(this TileSet tileSet) { return(tileSet.Tiles.Where(m => m.IsActive)); }
public static IEnumerable <Tile> WithType(this TileSet tileSet, string type) { return(tileSet.Tiles.Where(m => m.Type == type)); }
public MessageHandler(TileSet mobiles, TileSet statics) { _mobiles = mobiles; _statics = statics; }