/// Checks wether there is line of sight from the current /// position to the desired spot on the containing map. public bool canSee(int x, int y) { if (ContainingMap == null) { return(false); } int tx = (int)Position.X; int ty = (int)Position.Y; List <Point> ray = MapUtils.getRay(tx, ty, x, y); foreach (Point p in ray) { // if(ContainingMap.getEntities(p.X, p.Y).Count > 0) // return false; foreach (Tile tile in ContainingMap.getTiles(p.X, p.Y)) { if (tile == null || tile.Solid) { return(false); } } } return(true); }
public Point?nextMove(bool newPath) { if (AP < MoveAP) { return(Point.Zero); } if (newPath) { step = 0; Pathfinder.Path.Clear(); Pathfinder.Validator vl = pos => { bool playerAdjacent = false; Point p = new Point(0, -1); for (int s = 0; s < 4; ++s) { if (ContainingMap.getEntities(pos + MapUtils.rotatePoint(p, (Facing)s)) .Where(e => e is Player).Count() > 0) { playerAdjacent = true; break; } } return(playerAdjacent && ContainingMap .getTiles(pos).Where(t => t.Solid) .Count() == 0); }; Point tl = ContainingMap.BattleMap.Position.ToPoint() - ContainingMap.Viewport; Point br = ContainingMap.BattleMap.Position.ToPoint() + ContainingMap.Viewport; Pathfinder.getShortestPath(tl, br, vl); return(null); } if (!Pathfinder.IsAtWork) { if (step < Pathfinder.Path.Count) { return(Pathfinder.Path[step++]); } else { return(null); } } return(null); }
/// Updates this projectile and the hit ability /// after a collision public override void update(GameTime gameTime) { base.update(gameTime); if (collided) { if (Ability == null || !Ability.IsExecuting) { ContainingMap.removeEntity(this); onProjectileRemoved(); } else if (Ability != null) { Ability.update(gameTime); } } }
/// Helper to check wether any targets are in sight /// and on the spots this ability is executed on /// and returns them in a list private List <Creature> checkAbility(Ability ability) { List <Creature> targets = new List <Creature>(); for (int s = 0; s < 4; ++s) { ability.getTargetSpots() .Where(spot => canSee(Position.ToPoint() + MapUtils.rotatePoint(spot, (Facing)s))) .ToList().ForEach(spot => ContainingMap .getEntities(Position.ToPoint() + MapUtils.rotatePoint(spot, (Facing)s)) .Where(e => e is Player) .Cast <Player>().ToList() .ForEach(targets.Add)); } return(targets); }
protected virtual void onMoveFinished(MoveEventArgs args) { MoveFinishedHandler handler = MoveFinishedEvent; if (handler != null) { handler(this, args); } // trigger tile stepped event(s) if (ContainingMap != null) { ContainingMap.getTiles(args.Target.X, args.Target.Y).ForEach(tile => { tile.onTileStepped(new TileEventArgs( this, ContainingMap, args.Target)); }); } }
/// Returns the first map object found below the cursor public MapObject getObject() { if (ContainingMap == null) { return(null); } int x = (int)Position.X; int y = (int)Position.Y; List <Entity> entities = ContainingMap.getEntities(x, y); if (entities.Count > 1) { return(entities.Find(e => e != this)); } return(ContainingMap.getTiles(x, y) .OrderByDescending(t => t.MapLayer) .FirstOrDefault()); }
/// Checks wether a player is within /// range of the passed radius public bool playerInRange(int r) { int px = (int)Position.X; int py = (int)Position.Y; for (int x, y = -r; y <= r; ++y) { for (x = -r; x <= r; ++x) { if (canSee(px + x, py + y) && ContainingMap.getEntities(px + x, py + y) .Any(e => e is Player)) { return(true); } } } return(false); }
/// Moves this object distanceX tiles on the x-axis and /// distanceY tiles on the y-axis relative to its own /// position. Collisions are resolved by the passed /// collision resolver. public virtual bool move(int distanceX, int distanceY, CollisionResolver resolve) { if (!Moving && (distanceX != 0 || distanceY != 0)) { // TODO sprite is updated with delay // when turning, not sure why Facing = distanceY > 0 ? Facing.South : distanceY < 0 ? Facing.North : distanceX > 0 ? Facing.East : distanceX < 0 ? Facing.West : Facing; int positionX = (int)Position.X; int positionY = (int)Position.Y; Target = new Point( positionX + distanceX, positionY + distanceY); List <MapObject> targets = ContainingMap .getTiles(Target.X, Target.Y).Cast <MapObject>() .Concat(ContainingMap.getEntities(Target.X, Target.Y)).ToList(); Point?newDistance = resolve(distanceX, distanceY, targets); if (newDistance != null) { move(newDistance.Value.X, newDistance.Value.Y, resolve); return(false); } Moving = true; onMoveStarted(new MoveEventArgs( Position.ToPoint(), Target)); return(true); } return(false); }
public Creature(string spriteSheet) : base(spriteSheet) { Abilities = new List <Ability>(); Effects = new List <AbilityEffect>(); clazz = new Class(); Stats = new Stats(); initRNG(); // TODO reinit handler on deserialization MoveStartedEvent += (sender, args) => { if (ContainingMap == null) { return; } if (ContainingMap.getEntities(args.Position) .Where(e => e is Creature).Count() < 2) { ContainingMap.getTile(args.Position).Occupied = false; } ContainingMap.getTile(args.Target).Occupied = true; }; }
public override void update(GameTime gameTime) { if (timer > Duration) { ContainingMap.removeEntity(this); return; } base.update(gameTime); int elapsed = gameTime.ElapsedGameTime.Milliseconds; timer += elapsed; float fragment = elapsed / (float)Duration; offset.X += path.X * fragment; offset.Y += path.Y * fragment; alpha = timer < Duration / 2 ? 2f * timer / Duration : 2f * (Duration - timer) / Duration; scale = alpha; }
/// Selects a random target positon within the /// roaming radius of the spawn position. Returns /// true if the position is a tile that an entity /// can step on false otherwise private bool evaluateTarget() { Point target = new Point( RNG.Next( SpawnPosition.X - RoamingRadius, SpawnPosition.X + RoamingRadius + 1), RNG.Next( SpawnPosition.Y - RoamingRadius, SpawnPosition.Y + RoamingRadius + 1) ); if (!ContainingMap.getTiles(target).Any(t => t.Solid)) { Pathfinder.getShortestPath( SpawnPosition - new Point(RoamingRadius, RoamingRadius), SpawnPosition + new Point(RoamingRadius, RoamingRadius), (point) => point.Equals(target) ); return(true); } return(false); }
/// Updates this creature and any abilities /// that are currently executed public override void update(GameTime gameTime) { if (HP <= 0) { die(); } bool abilityRunning = false; Abilities.Where(a => a.IsExecuting).ToList().ForEach(a => { abilityRunning = true; a.update(gameTime); }); if (!dieing) { base.update(gameTime); } else if (!abilityRunning && (ActiveAnimation == null || !ActiveAnimation.IsRunning)) { ContainingMap.BattleMap.Participants.Remove(this); ContainingMap.removeEntity(this); } }
/// Returns a list of entities below the cursor public List <Entity> getEntities() { return(ContainingMap == null ? new List <Entity>() : ContainingMap.getEntities((int)Position.X, (int)Position.Y)); }