Beispiel #1
0
 public MovementAI(SpritePolygon collisionPolygon, Enemy parent)
 {
     nodeMap     = NodeMapRepository.GetNodeMap(collisionPolygon);
     pathFinder  = new PathFinder();
     this.parent = parent;
     sightRange  = 50;
 }
Beispiel #2
0
        public static NodeMap CreateFromTileMap(TileMap tileMap, SpritePolygon collisionPolygon)
        {
            NodeMap nodeMap = new NodeMap(tileMap.MapWidthTiles, tileMap.MapHeightTiles, tileMap.TileHeight, tileMap.TileWidth);

            nodeMap.generationPolygon = (SpritePolygon)collisionPolygon.Clone();

            for (int i = 0; i < nodeMap.XSize; i++)
            {
                for (int j = 0; j < nodeMap.YSize; j++)
                {
                    Vector2 position = new Vector2
                    {
                        X = nodeMap.Nodes[i, j].X,
                        Y = nodeMap.Nodes[i, j].Y
                    };
                    collisionPolygon.MoveTo(position);
                    Collision collision = tileMap.CheckCollisionWithTerrain(collisionPolygon);
                    if (collision == Collision.Solid || collision == Collision.Water)
                    {
                        nodeMap.Nodes[i, j].IsFree    = false;
                        nodeMap.Nodes[i, j].IsTerrain = true;
                    }
                }
            }

            return(nodeMap);
        }
Beispiel #3
0
        public Collision CheckCollisionWithTerrain(SpritePolygon spritePolygon)
        {
            var collision = Collision.None;

            try
            {
                foreach (var layer in map.Layers)
                {
                    //if (layer.Name == "accesories") continue;
                    foreach (var point in spritePolygon.CollisionPoints)
                    {
                        var currentTile = GetCurrentTilesGid(point, layer);
                        if (CollisionPolygons.TryGetValue(currentTile, out Tuple <Polygon, Collision> terrainPolygon))
                        {
                            var tileOrigin       = GetCurrentTileOrigin(point);
                            var polygonTileCoord = TransformPolygonToTileCoord(spritePolygon, tileOrigin);
                            if (polygonTileCoord.Intersects(terrainPolygon.Item1))
                            {
                                collision = terrainPolygon.Item2;
                            }
                        }
                    }
                }
            }
            catch (ArgumentOutOfRangeException) //being out of map counts as collision
            {
                return(Collision.Solid);
            }
            return(collision);
        }
Beispiel #4
0
 public Sprite(Texture2D texture2D, Vector2 position)
 {
     frame            = State.Content.Load <Texture2D>("frame");
     this.texture     = texture2D;
     this.Position    = position;
     Scale            = 1f;
     collisionPolygon = new SpritePolygon(new Rectangle((int)Position.X,
                                                        (int)Position.Y,
                                                        (int)(texture.Width * Scale),
                                                        (int)(texture.Height * Scale)));
 }
Beispiel #5
0
 public Sprite(Dictionary <string, Animation> animations, Vector2 position)
 {
     this.animations = animations;
     frame           = State.Content.Load <Texture2D>("frame");
     Scale           = 1f;
     //if you are changing this, there might be trouble in Character origin, so dont do that unless it is necessary
     animationManager = new AnimationManager(animations.First().Value);
     this.Position    = position;
     collisionPolygon = new SpritePolygon(new Rectangle((int)Position.X,
                                                        (int)Position.Y,
                                                        (int)(animationManager.Animation.FrameWidth * Scale),
                                                        (int)(animationManager.Animation.FrameHeight * Scale)));
 }
Beispiel #6
0
        public static NodeMap GetNodeMap(SpritePolygon collisionPolygon)
        {
            var polygon = (SpritePolygon)collisionPolygon.Clone();

            polygon.MoveTo(Vector2.Zero);

            if (nodeMaps.TryGetValue(polygon, out NodeMap nodeMap))
            {
                return(nodeMap);
            }

            var map = NodeMap.CreateFromTileMap(GameState.TileMap, polygon);

            polygon.MoveTo(Vector2.Zero);
            nodeMaps.Add(polygon, map);
            return(map);
        }
Beispiel #7
0
        private bool CanGoStraightTo(Node node)
        {
            bool    canGo     = false;
            Vector2 direction = new Vector2()
            {
                X = node.X - (float)Math.Floor(parent.Position.X),
                Y = node.Y - (float)Math.Floor(parent.Position.Y)
            };

            direction.Normalize();
            direction *= 8;
            SpritePolygon collisionPolygon = (SpritePolygon)parent.CollisionPolygon.Clone();;

            while (GameState.TileMap.CheckCollisionWithTerrain(collisionPolygon) == Collision.None)
            {
                if (DistanceFrom(node, collisionPolygon.Vertices[0]) <= direction.Length())
                {
                    canGo = true;
                    break;
                }
                collisionPolygon.Translate(direction);
            }
            return(canGo);
        }