Пример #1
0
        public override CustomArrayList<Point> ApplyPattern()
        {
            CustomArrayList<Point> list = new CustomArrayList<Point>();
            if (selection.units.Count() == 1)
            {
                list.AddLast(location);
                return list;
            }

            int rows = (int)Math.Ceiling((selection.units.Count() / (double)width));

            int offset_x = 0 - (((width - 1) * spacing) / 2);
            int offset_y = 0 - (((rows - 1) * spacing) / 2);

            int count = 0;
            for (int y = 0; y < rows; y++)
            {
                for (int x = 0; (x < width) && (count < selection.units.Count()); x++)
                {
                    // Create the rectangle pattern
                    Point newPoint = new Point(location.X + offset_x + x * spacing,
                        location.Y + offset_y + y * spacing);
                    // Rotate the pattern around
                    newPoint = Util.GetPointOnCircle(location, Util.GetHypoteneuseLength(newPoint, this.location),
                        orientation - (Util.GetHypoteneuseAngleRad(newPoint, this.location) * (180 / Math.PI)));
                    // If it collided with a wall, adjust the pattern to fit
                    CollisionMap c = Game1.GetInstance().map.collisionMap;
                    // If this point is in the middle of collision
                    if (c.CollisionAt(newPoint))
                    {
                        // Get all points between the point and the centre
                        Point[] points = c.PixelsBetweenPoints(newPoint, this.location, 1);
                        foreach (Point point in points)
                        {
                            // Get the point that doesn't have collision, and is farthest away from the centre
                            if (!c.CollisionAt(point))
                            {
                                newPoint = point;
                                break;
                            }
                        }
                    }
                    list.AddLast(newPoint);
                    count++;
                }
            }
            return list;
        }
Пример #2
0
        /// <summary>
        /// Spawns the starting units of this player.
        /// </summary>
        public void SpawnStartUnits()
        {
            if (Game1.GetInstance().IsMultiplayerGame() &&
                Game1.CURRENT_PLAYER != this) return;

            if (!Game1.GetInstance().IsMultiplayerGame())
            {
                int unitCount = 25;

                CustomArrayList<Unit> temp_units = new CustomArrayList<Unit>();
                // +1 to compensate for the engineer
                for (int i = 0; i < unitCount + 1; i++)
                {
                    // Fill the list with dummy units
                    Unit u = null;
                    temp_units.AddLast(u);
                }

                UnitSelection selection = new UnitSelection(temp_units);
                UnitGroupPattern pattern = new CirclePattern(startLocation, selection, 90, 0);
                CustomArrayList<Point> points = pattern.ApplyPattern();

                for (int i = 0; i < unitCount; i++)
                {

                    Point p = points.ElementAt(i);
                    if (i % 2 == 0)
                    {
                        //temp_units.AddLast(fastStore.getUnit(Unit.Type.Fast, p.X, p.Y));
                    }
                    else
                    {
                        temp_units.AddLast(rangedStore.getUnit(Unit.Type.Ranged, p.X, p.Y));
                    }

                    // Point p = points.ElementAt(i);
                    //temp_units.AddLast(meleeStore.getUnit(Unit.Type.Melee, p.X, p.Y));
                }
            }

            meleeStore.getUnit(Unit.Type.Engineer, startLocation.X, startLocation.Y);
        }
Пример #3
0
        public void OnMouseClick(MouseEvent m)
        {
            // Bots dont use the mouse, or shouldn't
            if (Game1.CURRENT_PLAYER != this)
            {
                return;
            }

            if ((m.button == MouseEvent.MOUSE_BUTTON_3))
            {
                if (IsPreviewingBuilding())
                {
                    this.RemovePreviewBuildings();
                }
                else
                {
                    previewPatternClick = GetAddedOffsettedMouseLocation(m);
                }
            }

            if (!hud.DefineRectangle().Contains(m.location) &&
                !hud.DefineMiniMapRectangle().Contains(m.location))
            {
                if (m.button == MouseEvent.MOUSE_BUTTON_1)
                {
                    Unit mouseOverUnit = this.GetMouseOverUnit(this.units);
                    if (mouseOverUnit == null)
                    {
                        if (this.currentSelection != null && this.currentSelection.units.Count() != 0 &&
                            !this.hud.IsMouseOverBuilding() && !this.IsPreviewingBuilding())
                        {
                            this.DeselectAllUnits();
                            this.currentSelection = null;
                        }
                    }
                    else
                    {
                        this.DeselectAllUnits();
                        this.DeselectAllBuildings();
                        // Performed a double click!
                        if (Game1.GetInstance().frames - lastBtn1ClickFrames < 20)
                        {
                            CustomArrayList<Unit> selectionUnits = new CustomArrayList<Unit>();
                            for (int i = 0; i < this.units.Count(); i++)
                            {
                                Unit unit = this.units.ElementAt(i);
                                if (Game1.GetInstance().IsOnScreen(unit.DefineDrawRectangle()) && mouseOverUnit.type == unit.type)
                                    selectionUnits.AddLast(unit);
                            }
                            this.currentSelection = new UnitSelection(selectionUnits);
                        }
                        else if (!mouseOverUnit.selected)
                        {
                            CustomArrayList<Unit> selectionUnits = new CustomArrayList<Unit>();
                            selectionUnits.AddLast(mouseOverUnit);
                            this.currentSelection = new UnitSelection(selectionUnits);
                        }
                    }

                    Building mouseOverBuilding = this.IsMouseOverFriendlyBuilding();
                    if (mouseOverBuilding == null)
                    {
                        if (this.buildingSelection != null && this.buildingSelection.buildings.Count() != 0 &&
                            !this.IsPreviewingBuilding())
                        {
                            this.DeselectAllBuildings();
                            this.buildingSelection = null;
                        }
                    }
                    else
                    {
                        this.DeselectAllUnits();
                        this.DeselectAllBuildings();
                        // Performed a double click!
                        if (Game1.GetInstance().frames - lastBtn1ClickFrames < 20)
                        {
                            CustomArrayList<Building> selectionBuildings = new CustomArrayList<Building>();
                            for (int i = 0; i < this.buildings.Count(); i++)
                            {
                                Building b = this.buildings.ElementAt(i);
                                if (Game1.GetInstance().IsOnScreen(b.DefineDrawRectangle()) && mouseOverBuilding.type == b.type)
                                    selectionBuildings.AddLast(b);
                            }
                            this.buildingSelection = new BuildingSelection(selectionBuildings);
                        }
                        else if (!mouseOverBuilding.selected)
                        {
                            CustomArrayList<Building> selectionBuildings = new CustomArrayList<Building>();
                            selectionBuildings.AddLast(mouseOverBuilding);
                            this.buildingSelection = new BuildingSelection(selectionBuildings);
                            this.buildingSelection.SelectAll();
                        }
                    }
                }
            }

            if (m.button == MouseEvent.MOUSE_BUTTON_1)
            {
                lastBtn1ClickFrames = Game1.GetInstance().frames;
            }
        }
Пример #4
0
        public void Reverse()
        {
            CollisionChangedEvent e = new CollisionChangedEvent();
            e.collisionMap = collision;
            e.collisionAdded = false;
            e.changedRect = this.rect;

            foreach (CollisionChangedEvent.QuadPart part in e.changedQuads)
            {
                part.quad.collisionTexture.UpdateCollision(part.rectangle, false);
            }

            CustomArrayList<Node> processedNodes = new CustomArrayList<Node>();
            foreach (Node node in createdNodes)
            {
                CustomArrayList<PathfindingNode> connectedNodes = node.GetConnectedNodes();
                for( int i = 0; i < connectedNodes.Count(); i++ ){
                    Node connectedNode = (Node)connectedNodes.ElementAt(i);
                    if (!processedNodes.Contains(connectedNode))
                    {
                        SmartPathfindingNodeProcessor.GetInstance().Push(connectedNode);
                        processedNodes.AddLast(connectedNode);
                    }
                }
                node.Destroy();
            }
            for( int i = 0; i < this.removedNodes.Count(); i++ ){
                Point p = this.removedNodes.ElementAt(i);
                // Console.Out.WriteLine("Restoring node " + p);
                new Node(collision, p.X, p.Y);
            }

            collision.FireCollisionChangedEvent(e);
        }
Пример #5
0
        /// <summary>
        /// Applies color to the map!
        /// </summary>
        /// <param name="location">The location to add color to.</param>
        /// <param name="radius">The radius to color.</param>
        public void ApplyColor(Vector2 location, int radius)
        {
            Rectangle rect = new Rectangle(
                (int)(location.X - radius), (int)(location.Y - radius),
                (int)(radius * 2), (int)(radius * 2));

            CustomArrayList<Quad> quadList = new CustomArrayList<Quad>();
            double checkInterval = 3.0;

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left, rect.Top)));
            // top line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left + (i * (int)(rect.Width / checkInterval)), rect.Top)));

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Right, rect.Top)));

            // right line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Right, rect.Top + (int)(i * (rect.Height / checkInterval)))));

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left, rect.Bottom)));

            // bottom line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left + (int)(i * (rect.Width / checkInterval)), rect.Bottom)));

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Right, rect.Bottom)));

            // left line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left, rect.Top + (int)(i * (rect.Height / checkInterval)))));

            CustomArrayList<Quad> affectedQuads = new CustomArrayList<Quad>();

            for (int i = 0; i < quadList.Count(); i++)
            {
                Quad quad = quadList.ElementAt(i);
                if (quad != null && !affectedQuads.Contains(quad)) affectedQuads.AddLast(quad);
            }

            // Update each of the quads.
            for (int i = 0; i < affectedQuads.Count(); i++)
            {
                Quad q = affectedQuads.ElementAt(i);

                Rectangle updatedRect = Rectangle.Intersect(q.rectangle, rect);

                updatedRect.X = updatedRect.X - q.rectangle.X;
                updatedRect.Y = updatedRect.Y - q.rectangle.Y;
                q.colorTexture.ColorTexture(updatedRect);
            }
            // this.tree.
        }
Пример #6
0
        /// <summary>
        /// Merges the loaded layers!
        /// </summary>
        public void MergeLayers()
        {
            if (this.layers.Count() == 0)
            {
                throw new Exception("Perform LoadLayers() first.");
            }

            mapTiles = new Texture2D[this.layers.GetFirst().data.GetLength(0), this.layers.GetFirst().data.GetLength(1)];
            for (int i = 0; i < this.layers.GetFirst().data.GetLength(0); i++)
            {
                for (int j = 0; j < this.layers.GetFirst().data.GetLength(1); j++)
                {
                    CustomArrayList<Texture2D> preBlend = new CustomArrayList<Texture2D>();
                    for (int k = 0; k < this.layers.Count(); k++)
                    {
                        int data = this.layers.ElementAt(k).data[i, j];
                        if (data != -1) preBlend.AddLast(this.individualTiles[data]);
                    }
                    mapTiles[i, j] = this.BlendTextures(preBlend.ToArray(), BlendMode.PriorityBlend);

                    // One step closer to completion
                    Game1.GetInstance().currentLoadProgress += 9;
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Calculates a path between the current unit and the point.
        /// </summary>
        /// <param name="p">The point to calculate to.</param>
        /// <returns>The list containing all the points that you should visit.</returns>
        public CustomArrayList<Point> CalculatePath(Point p)
        {
            CustomArrayList<Point> result = new CustomArrayList<Point>();
            long ticks = DateTime.UtcNow.Ticks;
            if (Game1.GetInstance().map.collisionMap.IsCollisionBetween(new Point((int)this.x, (int)this.y), p))
            {
                Game1 game = Game1.GetInstance();
                // Create temp nodes
                Node start = new Node(game.map.collisionMap, (int)this.x, (int)this.y, true);
                Node end = new Node(game.map.collisionMap, p.X, p.Y, true);
                CustomArrayList<PathfindingNode> nodes = new AStar(start, end).FindPath();
                if (nodes != null)
                {
                    // Remove the first node, because that's the node we're currently on ..
                    nodes.RemoveFirst(true);
                    // Clear our current waypoints
                    this.waypoints.Clear();
                    for( int i = 0; i < nodes.Count(); i++ ){
                        result.AddLast(nodes.ElementAt(i).GetLocation());
                    }
                }

                // Nodes can no longer be used
                start.Destroy();
                end.Destroy();
            }
            else
            {
                result.AddLast(p);
            }
            return result;
        }
Пример #8
0
        private void LoadCommands()
        {
            commandObjects = new CustomArrayList<HUDCommandObject>();
            startCommandX = 673;
            startCommandY = 688;

            //No more than 8 seperate Commands!!!
            moveCommand = new HUDCommandObject(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Move), HUDCommandObject.Type.Move, startCommandX, startCommandY, new Color(0, 100, 255, 255), this.color);
            commandObjects.AddLast(moveCommand);
            IncrementStartCommandXY(startCommandX);

            attackCommand = new HUDCommandObject(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Attack), HUDCommandObject.Type.Attack, startCommandX, startCommandY, new Color(255, 0, 12, 255), this.color);
            commandObjects.AddLast(attackCommand);
            IncrementStartCommandXY(startCommandX);

            defendCommand = new HUDCommandObject(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Defend), HUDCommandObject.Type.Defend, startCommandX, startCommandY, new Color(255, 125, 0, 255), this.color);
            commandObjects.AddLast(defendCommand);
            IncrementStartCommandXY(startCommandX);

            stopCommand = new HUDCommandObject(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Stop), HUDCommandObject.Type.Stop, startCommandX, startCommandY, new Color(255, 0, 0, 255), this.color);
            commandObjects.AddLast(stopCommand);
            IncrementStartCommandXY(startCommandX);

            repairCommand = new HUDCommandObject(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Repair), HUDCommandObject.Type.Repair, startCommandX, startCommandY, new Color(255, 187, 0, 255), this.color);
            commandObjects.AddLast(repairCommand);
            IncrementStartCommandXY(startCommandX);
        }
Пример #9
0
        /// <summary>
        /// Inits the draw locations of the polygons
        /// </summary>
        private void InitPathDrawPolygons()
        {
            this.pathDrawPolygons = new Polygon[5];

            CustomArrayList<Vector2> list = new CustomArrayList<Vector2>();
            list.AddLast(new Vector2(0, 0));
            list.AddLast(new Vector2(0, 1080));
            list.AddLast(new Vector2(400, 1080));
            list.AddLast(new Vector2(349, 729));
            list.AddLast(new Vector2(389, 415));
            list.AddLast(new Vector2(273, 0));
            this.pathDrawPolygons[0] = new Polygon(list);

            list = new CustomArrayList<Vector2>();
            list.AddLast(new Vector2(273, 0));
            list.AddLast(new Vector2(389, 415));
            list.AddLast(new Vector2(349, 729));
            list.AddLast(new Vector2(400, 1080));
            list.AddLast(new Vector2(789, 1080));
            list.AddLast(new Vector2(741, 700));
            list.AddLast(new Vector2(743, 241));
            list.AddLast(new Vector2(613, 0));
            this.pathDrawPolygons[1] = new Polygon(list);

            list = new CustomArrayList<Vector2>();
            list.AddLast(new Vector2(613, 0));
            list.AddLast(new Vector2(743, 241));
            list.AddLast(new Vector2(741, 700));
            list.AddLast(new Vector2(789, 1080));
            list.AddLast(new Vector2(1317, 1080));
            list.AddLast(new Vector2(1265, 825));
            list.AddLast(new Vector2(1309, 203));
            list.AddLast(new Vector2(1269, 0));
            this.pathDrawPolygons[2] = new Polygon(list);

            list = new CustomArrayList<Vector2>();
            list.AddLast(new Vector2(1269, 0));
            list.AddLast(new Vector2(1309, 203));
            list.AddLast(new Vector2(1265, 825));
            list.AddLast(new Vector2(1317, 1080));
            list.AddLast(new Vector2(1579, 1080));
            list.AddLast(new Vector2(1575, 795));
            list.AddLast(new Vector2(1523, 0));
            this.pathDrawPolygons[3] = new Polygon(list);

            list = new CustomArrayList<Vector2>();
            list.AddLast(new Vector2(1523, 0));
            list.AddLast(new Vector2(1575, 795));
            list.AddLast(new Vector2(1579, 1080));
            list.AddLast(new Vector2(1920, 1080));
            list.AddLast(new Vector2(1920, 0));

            this.pathDrawPolygons[4] = new Polygon(list);
        }
Пример #10
0
        /// <summary>
        /// Checks whether the HUD should be hidden or not. (if the player this HUD belongs to is not the player that is watching)
        /// Loads the objects to display according to selected units/buildings.
        /// </summary>
        /// <param name="ks">Default KeyboardState</param>
        /// <param name="ms">Default MouseState</param>
        public void Update(KeyboardState ks, MouseState ms)
        {
            if (!draw)
            {
                draw = true;
            }
            CountUnits();

            objects = new CustomArrayList<HUDObject>();

            for (int i = 0; i < this.commandObjects.Count(); i++)
            {
                this.commandObjects.ElementAt(i).disabled = true;
            }

            if (loadForEngineer)
            {
                resourceObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Resources), HUDObject.Type.Resources, startObjectX, startObjectY, color);
                objects.AddLast(resourceObject);
                IncrementStartObjectXY(startObjectX);

                barracksObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Barracks), HUDObject.Type.Barracks, startObjectX, startObjectY, color);
                objects.AddLast(barracksObject);
                IncrementStartObjectXY(startObjectX);

                factoryObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Factory), HUDObject.Type.Factory, startObjectX, startObjectY, color);
                objects.AddLast(factoryObject);
                IncrementStartObjectXY(startObjectX);

                sentryObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Sentry), HUDObject.Type.Sentry, startObjectX, startObjectY, color);
                objects.AddLast(sentryObject);
                IncrementStartObjectXY(startObjectX);

                fortressObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Fortress), HUDObject.Type.Fortress, startObjectX, startObjectY, color);
                objects.AddLast(fortressObject);
                IncrementStartObjectXY(startObjectX);

                for (int i = 0; i < this.commandObjects.Count(); i++)
                {
                    HUDCommandObject co = this.commandObjects.ElementAt(i);
                    if (co.type == HUDCommandObject.Type.Repair || co.type == HUDCommandObject.Type.Move || co.type == HUDCommandObject.Type.Stop || co.type == HUDCommandObject.Type.Defend)
                    {
                        co.disabled = false;
                    }
                }
            }
            if (loadForUnit)
            {
                for (int i = 0; i < this.commandObjects.Count(); i++)
                {
                    HUDCommandObject co = this.commandObjects.ElementAt(i);
                    if (co.type == HUDCommandObject.Type.Attack || co.type == HUDCommandObject.Type.Defend || co.type == HUDCommandObject.Type.Move || co.type == HUDCommandObject.Type.Stop)
                    {
                        co.disabled = false;
                    }
                }
            }
            if (loadForBarracks)
            {
                meleeObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Melee), HUDObject.Type.Melee, startObjectX, startObjectY, color);
                objects.AddLast(meleeObject);
                IncrementStartObjectXY(startObjectX);

                rangedObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Ranged), HUDObject.Type.Ranged, startObjectX, startObjectY, color);
                objects.AddLast(rangedObject);
                IncrementStartObjectXY(startObjectX);

                fastObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Fast), HUDObject.Type.Fast, startObjectX, startObjectY, color);
                objects.AddLast(fastObject);
                IncrementStartObjectXY(startObjectX);
            }
            if (loadForFortress)
            {
                engineerObject = new HUDObject(TextureManager.GetInstance().GetTexture(HUDObject.Type.Engineer), HUDObject.Type.Engineer, startObjectX, startObjectY, color);
                objects.AddLast(engineerObject);
                IncrementStartObjectXY(startObjectX);
            }

            startObjectX = 278;
            startObjectY = 688;
        }
Пример #11
0
 /// <summary>
 /// Loads the mapnames from disk.
 /// </summary>
 /// <returns>The list of names.</returns>
 public CustomArrayList<String> LoadMapNames()
 {
     DirectoryInfo di = new DirectoryInfo(Game1.MAPS_FOLDER_LOCATION);
     CustomArrayList<String> names = new CustomArrayList<String>();
     foreach (FileInfo fi in di.GetFiles())
     {
         if (fi.Extension == ".xml") names.AddLast(fi.Name.Replace(".xml", ""));
     }
     return names;
 }
Пример #12
0
        /*
        /// <summary>
        /// Converts a boolean to a texture.
        /// </summary>
        /// <param name="device">The device to create the texture on</param>
        /// <param name="data">The data</param>
        /// <param name="width">the width</param>
        /// <param name="inversedScale">An invesed scale. 2 means, 2 times as small. 4 times as small, etc. USE IN MULTIPLES OF 2, OR FAIL.</param>
        /// <returns></returns>
        public Texture2D BoolToTexture(GraphicsDevice device, Boolean[] data, int width, int inversedScale)
        {
            if (inversedScale == 1) return BoolToTexture(device, data, width);
            int[] intData = new int[data.Length / ( inversedScale * inversedScale )];
            Texture2D texture = new Texture2D(device, width / inversedScale,
                ( data.Length / width) / inversedScale);

            int newPixelCount = 0;
            int skips = 0;
            for (int i = 0; i < data.Length - 1; i += inversedScale)
            {
                bool skipNext = false;
                if (i % (width * inversedScale) == 0) skipNext = true;
                if (data[i]) intData[newPixelCount] = (int)Color.Black.PackedValue;
                else intData[newPixelCount] = 0;

                newPixelCount++;
                if (skipNext)
                {
                    // Console.Out.Write("Skipping @ " + i + ", " + newPixelCount + ", " + skips);
                    i += width * (inversedScale - 1);
                    // Console.Out.WriteLine(" new: " + i);
                    skips++;
                    continue;
                }
            }
            // Console.Out.WriteLine("Finished, copied " + newPixelCount + " pixels, while the array size is " + intData.Length);
            texture.SetData(intData);
            return texture;
        }*/
        /// <summary>
        /// Updates the collisionmap, without updating or adding pathfinding points. 
        /// DO NOT USE THIS METHOD IN-GAME, ONLY IN THE EDITOR.
        /// </summary>
        /// <param name="rect">The rectangle</param>
        /// <param name="add">Whether to add or to remove the rect.</param>
        public void UpdateCollisionMap(Rectangle rect, Boolean add)
        {
            CollisionChangedEvent e = new CollisionChangedEvent();
            e.collisionMap = this;
            e.changedRect = rect;
            e.collisionAdded = add;

            CustomArrayList<Quad> quadList = new CustomArrayList<Quad>();
            double checkInterval = 3.0;

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left, rect.Top)));
            // top line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left + (i * (int)(rect.Width / checkInterval)), rect.Top)));

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Right, rect.Top)));

            // right line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Right, rect.Top + (int)(i * (rect.Height / checkInterval)))));

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left, rect.Bottom)));

            // bottom line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left + (int)(i * (rect.Width / checkInterval)), rect.Bottom)));

            quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Right, rect.Bottom)));

            // left line
            for (int i = 0; i < checkInterval; i++)
                quadList.AddLast(this.tree.GetQuadByPoint(new Point(rect.Left, rect.Top + (int)(i * (rect.Height / checkInterval)))));

            CustomArrayList<Quad> affectedQuads = new CustomArrayList<Quad>();

            for (int i = 0; i < quadList.Count(); i++)
            {
                Quad quad = quadList.ElementAt(i);
                if (quad != null && !affectedQuads.Contains(quad)) affectedQuads.AddLast(quad);
            }

            // Update each of the quads.
            for( int i = 0; i < affectedQuads.Count(); i++ ){
                Quad q = affectedQuads.ElementAt(i);

                Rectangle updatedRect = Rectangle.Intersect(q.rectangle, rect);
                updatedRect.X = updatedRect.X - q.rectangle.X;
                updatedRect.Y = updatedRect.Y - q.rectangle.Y;
                q.collisionTexture.UpdateCollision(updatedRect, add);
                e.changedQuads.AddLast(new CollisionChangedEvent.QuadPart(q, updatedRect));
            }

            FireCollisionChangedEvent(e);
        }
Пример #13
0
        /// <summary>
        /// Gets the pixels between points.
        /// </summary>
        /// <param name="p1">Point one</param>
        /// <param name="p2">Point two</param>
        /// <param name="spacing">The pixel spacing between the points (default should be 1). Lower than 1 will use 1.</param>
        /// <returns></returns>
        public Point[] PixelsBetweenPoints(Point p1, Point p2, int spacing)
        {
            if (spacing < 1) spacing = 1;
            int xDiff = p2.X - p1.X;
            int yDiff = p2.Y - p1.Y;

            double maxWidth = Math.Max(Math.Abs(xDiff), Math.Abs(yDiff));

            double xDelta = (xDiff / maxWidth) * spacing;
            // Console.Out.WriteLine("xDelta: " + xDelta);
            double yDelta = (yDiff / maxWidth) * spacing;
            // Console.Out.WriteLine("yDelta: " + yDelta);

            CustomArrayList<Point> pointList = new CustomArrayList<Point>();
            Point currentPoint = p1;
            Point previousPoint = new Point(-100, -100);
            double currentX = p1.X, currentY = p1.Y;
            for (int i = 0; i < maxWidth / spacing; i++)
            {
                currentX += xDelta;
                currentY += yDelta;

                currentPoint.X = (int)currentX;
                currentPoint.Y = (int)currentY;

                Point newPoint = new Point(currentPoint.X, currentPoint.Y);
                if (previousPoint != null && !previousPoint.Equals(newPoint))
                {
                    // Console.Out.WriteLine(i + ". Adding point to list: " + newPoint);
                    pointList.AddLast(newPoint);
                }
                previousPoint = newPoint;
            }
            return pointList.ToArray();
        }
Пример #14
0
        /// <summary>
        /// Places nodes around all the edges
        /// </summary>
        public CustomArrayList<Point> GetNodeLocationsAroundEdges()
        {
            Boolean previous = CollisionAt(0);
            CustomArrayList<Point> pointList = new CustomArrayList<Point>();
            for (int i = 0; i < dataLength - 1; i++)
            {
                Boolean current = CollisionAt(i);
                if ((int)(i / mapWidth) % VERTICAL_COLLISION_CHECK_SPACING != 0) { continue; }
                // if (i % mapWidth > mapWidth - 15 || i % mapWidth < 15) { continue; }
                if (i % mapWidth == 0) { previous = current; }
                if (current != previous)
                {
                    if (!current && (i + 10 < dataLength) && !CollisionAt(i + 10)) pointList.AddLast(IndexToPoint(i + 10));
                    else if ((i - 10 > -1) && !CollisionAt(i - 10)) pointList.AddLast(IndexToPoint(i - 10));
                }
                previous = current;
            }

            for (int i = 0; i < mapWidth; i += HORIZONTAL_COLLISION_CHECK_SPACING)
            {
                for (int j = 0; j < mapHeight; j++)
                {
                    int index = i + j * mapWidth;
                    Boolean current = CollisionAt(index);
                    // if (index % mapWidth % HORIZONTAL_COLLISION_CHECK_SPACING != 0) { continue; }
                    if (j == 0) previous = current;
                    // if (index % screenWidth > screenWidth - 15 || index % screenWidth < 15) continue;
                    if (current != previous)
                    {
                        if (!current && (index + (10 * mapWidth) < dataLength)
                            && !CollisionAt(index + (10 * mapWidth)))
                            pointList.AddLast(IndexToPoint(index + (10 * mapWidth)));
                        else if ((index - (10 * mapWidth) > -1) && !CollisionAt(index - (10 * mapWidth)))
                            pointList.AddLast(IndexToPoint(index - (10 * mapWidth)));
                    }
                    previous = current;
                }
            }

            // Remove nodes that are closer than NODE_REMOVE_DISTANCE of each other
            for (int i = 0; i < pointList.Count(); i++)
            {
                Point p1 = pointList.ElementAt(i);
                for (int j = 0; j < pointList.Count(); j++)
                {
                    Point p2 = pointList.ElementAt(j);
                    if (p1 != p2 && PathfindingUtil.GetHypoteneuseLength(p1, p2) < NODE_REMOVE_DISTANCE)
                    {
                        pointList.Remove(p1);
                        i--;
                        j--;
                        break;
                    }
                }
            }
            return pointList;
        }
Пример #15
0
        /// <summary>
        /// Gets the closest node in line of sight from the given point.
        /// </summary>
        /// <param name="p">The point.</param>
        /// <returns>The node, or null if no node is in range.</returns>
        public PathfindingNode ClosestNodeInLOS(Point p)
        {
            PathfindingNodeManager manager = PathfindingNodeManager.GetInstance();
            CustomArrayList<PathfindingNode> inRangeNodes = new CustomArrayList<PathfindingNode>();
            for( int i = 0; i < manager.GetNodeCount(); i++)
            {
                PathfindingNode node = manager.GetNodeAt(i);
                if (this.IsCollisionBetween(p, node.GetLocation()))
                {
                    inRangeNodes.AddLast(node);
                }
            }
            PathfindingNode closestNode = inRangeNodes.ElementAt(0);
            double closestDistance = Double.MaxValue;
            for( int i = 0; i < inRangeNodes.Count(); i++ ){
                PathfindingNode node = inRangeNodes.ElementAt(i);

                double newDistance = PathfindingUtil.GetHypoteneuseLength(closestNode.GetLocation(), node.GetLocation());
                if (closestDistance > newDistance)
                {
                    closestDistance = newDistance;
                    closestNode = node;
                }
            }
            return closestNode;
        }