public UnitEntity(AbstractPlayerData player, Unit unit, string name) { Point = unit.Point; _unit = unit; _player = player; _model = MainApplication.ManagerInstance.Content.Load<Model>("Content\\Models\\" + name); _animations.Add("Idle", new ModelAnimation("Content\\Animations\\" + name + "\\Idle")); Status = ModelStatus.Standing; for (int i = 0; i < unit.Formation.Positions; ++i) { Vector3 startOffset = unit.Formation.GetPosition(i); _items.Add(new UnitItem(Position + startOffset, startOffset, Rotation)); } _scaleMatrix = Matrix.CreateScale(Scale * 0.3f); int boneCount = _model.Bones.Count; this.boneTransforms = new Matrix[boneCount]; _model.CopyBoneTransformsTo(this.boneTransforms); this.boneTransformsOriginal = new Matrix[boneCount]; _model.CopyBoneTransformsTo(this.boneTransformsOriginal); this.absoluteBoneTransforms = new Matrix[boneCount]; SetAnimation("Idle"); }
public void DeleteUnit(Unit unit) { _units.Remove(unit); }
void unit_Moved(Unit sender, HexPoint oldPosition, HexPoint newPosition) { _needToUpdateInfluenceMaps = true; }
public void AddUnit(string unitname, int x, int y) { UnitData data = Provider.GetUnit(unitname); Unit unit = new Unit(new HexPoint(x, y), this, data); //string script = MainApplication.ManagerInstance.Content.Load<string>("Content/Scripts/Marine"); //unit.AssignScript(script); unit.Moved += new UnitMovedHandler(unit_Moved); unit.WorkFinished += delegate(Unit u, HexPoint pt, Improvement imp) { if (!u.IsAutomated) { GameFacade.getInstance().SendNotification( GameNotification.Message, NotificationType.ImprovementReady, string.Format(Strings.TXT_KEY_NOTIFICATION_BUILD_IMPROVEMENT, u.Player.Civilization.Title, imp.Title, pt), u.Player.Civilization, MessageFilter.Friends | MessageFilter.Self, new List<object>() { imp, pt }); } _needToUpdateInfluenceMaps = true; GameFacade.getInstance().SendNotification(GameNotification.UpdateImprovements); }; _units.Add(unit); }
/// <summary> /// Calculates a path from start to end. When no path can be found in /// reasonable time the search is aborted and an incomplete path is returned. /// When refresh is not set to true a cached path is returned where possible. /// </summary> /// <param name="start">start position in 2d map space</param> /// <param name="end">end position in 2d map space</param> /// <param name="refresh">force to recalculate the path</param> /// <returns></returns> public Path CalculatePath(Unit unit, HexPoint start, HexPoint end, bool refresh = false) { // swap points to calculate the path backwards (from end to start) HexPoint temp = end; end = start; start = temp; // Check whether the requested path is already known PathRequest request = new PathRequest(unit,start, end); if (!refresh && knownPaths.ContainsKey(request)) { return knownPaths[request].Copy(); } // priority queue of nodes that yet have to be explored sorted in // ascending order by node costs (F) PriorityQueue<PathNode> open = new PriorityQueue<PathNode>(); // list of nodes that have already been explored LinkedList<IGridCell> closed = new LinkedList<IGridCell>(); // start is to be explored first PathNode startNode = new PathNode(unit,null, map[start], end); open.Enqueue(startNode); int steps = 0; PathNode current; do { // abort if calculation is too expensive if (++steps > stepLimit) return null; // examine the cheapest node among the yet to be explored current = open.Dequeue(); // Finish? if (current.Cell.Matches(end)) { // paths which lead to the requested goal are cached for reuse Path path = new Path(current); if (knownPaths.ContainsKey(request)) { knownPaths[request] = path.Copy(); } else { knownPaths.Add(request, path.Copy()); } return path; } // Explore all neighbours of the current cell ICollection<IGridCell> neighbours = map.GetNeighbourCells(current.Cell); foreach (IGridCell cell in neighbours) { // discard nodes that are not of interest if (closed.Contains(cell) || (cell.Matches(end) == false && !cell.IsWalkable(unit))) { continue; } // successor is one of current's neighbours PathNode successor = new PathNode(unit,current, cell, end); PathNode contained = open.Find(successor); if (contained != null && successor.F >= contained.F) { // This cell is already in the open list represented by // another node that is cheaper continue; } else if (contained != null && successor.F < contained.F) { // This cell is already in the open list but on a more expensive // path -> "integrate" the node into the current path contained.Predecessor = current; contained.Update(); open.Update(contained); } else { // The cell is not in the open list and therefore still has to // be explored open.Enqueue(successor); } } // add current to the list of the already explored nodes closed.AddLast(current.Cell); } while (open.Peek() != null); return null; }
public PathRequest(Unit unit, HexPoint start, HexPoint end) { data = unit.Data; this.start = start; this.end = end; }
public bool IsWalkable(Unit unit) { return CanEnter(unit) && IsSpotted(unit.Player); }
public int Weight(Unit unit) { return 1; }
public MarineModelEntity(AbstractPlayerData player, Unit unit) : base(player, unit, "PlayerMarine_mdla") { Scale = new Vector3(0.2f); }
public Path FindPath(Unit unit, HexPoint pt) { if (finder == null) finder = new Pathfinder(this); return finder.CalculatePath(unit, unit.Point, pt); }