/// <summary> /// Checks if this object is adjacent to another object /// </summary> /// <param name="o">The object to check if this object is adjacent to</param> /// <returns>True if this object is adjacent to the other and false otherwise</returns> public bool isAdjacent(WorldObject o) { int[] loc = this.getRelativeLocation(o); int locX = loc[0]; int locY = loc[1]; bool b = false; if (Math.Abs(locX) <= 1 && Math.Abs(locY) <= 1) { b = true; } return b; }
/// <summary> /// Gets the direction to a specifed WorldObject from this one /// </summary> /// <param name="o">The object to get the direction to</param> /// <returns>The direction from this object to get to the specified object</returns> public Direction getDirectionTo(WorldObject o) { int[] relativeLoc = getRelativeLocation(o); Vector2 v = new Vector2(relativeLoc[0], relativeLoc[1]); if (Math.Abs(v.X) > Math.Abs(v.Y)) { Vector2 temp = new Vector2(v.X / Math.Abs(v.X), v.Y / Math.Abs(v.X)); v = temp; } else { Vector2 temp = new Vector2(v.X / Math.Abs(v.Y), v.Y / Math.Abs(v.Y)); v = temp; } Direction d = Direction.EAST; //now work out the direction if (v.X == 1) // --> { if (v.Y > 0.5) // V { d = Direction.SOUTHEAST; } else if (v.Y < -0.5) // ^ { d = Direction.NORTHEAST; } else { d = Direction.EAST; } } else if (v.X == -1) // <-- { if (v.Y > 0.5) // V { d = Direction.SOUTHWEST; } else if (v.Y < -0.5) // ^ { d = Direction.NORTHWEST; } else { d = Direction.WEST; } } else if (v.Y == 1) // V { if (v.X > 0.5) // --> { d = Direction.SOUTHEAST; } else if (v.X < -0.5) // <-- { d = Direction.SOUTHWEST; } else { d = Direction.SOUTH; } } else if (v.Y == -1) // ^ { if (v.X > 0.5) // --> { d = Direction.NORTHEAST; } else if (v.X < -0.5) // <-- { d = Direction.NORTHWEST; } else { d = Direction.NORTH; } } return d; }
/// <summary> /// Gets the relative location of a specified WorldObject from this one, that is, gets the /// location of the specified WorldObject as if this WorldObject was as 0,0 /// </summary> /// <param name="o">The WorldObject to get the relative location of</param> /// <returns>The relative location of the specified object as a 2 element array of ints</returns> public int[] getRelativeLocation(WorldObject o) { int locX = o.getLocationXY()[0] - this.getLocationXY()[0]; int locY = o.getLocationXY()[1] - this.getLocationXY()[1]; return new int[] { locX, locY }; }
//a^2 + b^2 = c^2 /// <summary> /// Gets the euclidean distance of this object from a given object /// </summary> /// <param name="o">The object to get the distance from</param> /// <returns>The euclidean distance from this object to the specified object as a /// double</returns> public double getEuclideanDistanceFrom(WorldObject o) { int[] xy = getRelativeLocation(o); int a2 = xy[0] * xy[0]; int b2 = xy[1] * xy[1]; return Math.Sqrt(a2 + b2); }
//a + b = c /// <summary> /// Gets the manhattan distance of this object from a given object /// </summary> /// <param name="o">The object to calculate the distance to</param> /// <returns>The distance as an int</returns> public int getDistanceFrom(WorldObject o) { int[] xy = getRelativeLocation(o); return Math.Abs(xy[0]) + Math.Abs(xy[1]); }
/// <summary> /// Gets the danger of a given WorldObject based on a list of other WorldObjects /// </summary> /// <param name="o">The WorldObject to get the danger for</param> /// <param name="things">The list of WorldObjects to use to find the danger of the other</param> /// <returns>An int value representing the danger of the given object, a lower value being more dangerous</returns> private int getDanger(WorldObject o, List<WorldObject> things) { int danger = 0; foreach (WorldObject b in things) { danger += o.getDistanceFrom(b); } return danger; }
/// <summary> /// Moves towards a given world object /// </summary> /// <param name="thing">The thing to move towards</param> public void moveTowards(WorldObject thing) { if (inCombat) { if (getSpeed() + rand(10) < inCombatWith.getSpeed() + rand(10)) { damage(inCombatWith.getStrength() - getDefence()); } outOfCombat(); } int i = 0; int maxMoves = speed; while(i < maxMoves && !isAdjacent(thing)) { Direction d = getDirectionTo(thing); move(d); i++; } }