Ejemplo n.º 1
0
        public void Update(Control control, Map map, Player player, GameTime time, Package package)
        {
            UpdatePlayers(control, map);

            NetOutgoingMessage msgout = NetClient.CreateMessage();
            msgout.Write(BuildCommand(player));
            NetClient.SendMessage(msgout, NetDeliveryMethod.Unreliable);

            NetIncomingMessage msg;
            while ((msg = NetClient.ReadMessage()) != null)
            {
                switch(msg.MessageType)
                {
                    case NetIncomingMessageType.DiscoveryResponse:
                        NetClient.Connect(msg.SenderEndpoint);

                        Player newplayer = new Player();
                        newplayer.Initialize(PlayerTexture, BarsTexture, package, map.Spawn);
                        Messages.Add("new player");
                        Players.Add(newplayer);
                        break;
                    case NetIncomingMessageType.Data:

                        HandleData(msg);

                        break;
                    default:
                       // Messages.Add(msg.ReadString());
                        break;
                }
            }
        }
Ejemplo n.º 2
0
        public void UpdatePlayers(Control control, Map map)
        {
            foreach (var player in Players)
            {
                player.Update(control, map);

                // Update Player
            }
        }
Ejemplo n.º 3
0
        public void Update(Map map)
        {
            Random random = new Random();

            for (int x = 1; x < map.SizeX; x++)
            {
                for (int y = 1; y < map.SizeY; y++)
                {

                    // If square empty, attempt to place flowers and grass
                    if (map.Blocks[x,y] != null && map.Blocks[x,y - 1] == null && Flora[x, y - 1] == null)
                    {
                        int rand = random.Next(0, 600);
                        if (rand > 0 && rand < 20)
                        {
                            Flora[x, y - 1] = new Flora(Types[1]);
                        }
                        else if (rand == 21)
                        {
                            Flora[x, y - 1] = new Flora(Types[10]);
                        }
                        else if (rand == 22)
                        {
                            Flora[x, y - 1] = new Flora(Types[11]);
                        }
                        else if (rand == 23)
                        {
                            Flora[x, y - 1] = new Flora(Types[12]);
                        }
                    }

                    // If square is empty, attempt to place vines
                    if (map.Blocks[x,y] != null && map.getBlock(x, y + 1) == null)
                    {
                        int rand = random.Next(0, 30);
                        if (rand == 1)
                        {
                            Flora[x, y + 1] = new Flora(Types[30]);
                        }
                    }

                    //if(Flora[x,y]

                    if (Flora[x, y] != null && Flora[x,y].Type.GrowInto != 0)
                    {
                        Flora[x, y].GrowUpto++;
                        if (Flora[x, y].GrowUpto >= Flora[x, y].Type.GrowAt)
                            { Flora[x, y] = new Flora( Types[Flora[x,y].Type.GrowInto] ); }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void Update(Control control, Map map, Player player, GameTime time, Package package)
        {
            UpdatePlayers(control, map);

            NetOutgoingMessage msgout = NetServer.CreateMessage();
            msgout.Write(BuildCommand(player));
            NetServer.SendToAll(msgout, NetDeliveryMethod.Unreliable);

            Time = time;
            NetIncomingMessage msg;
            while ((msg = NetServer.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                    case NetIncomingMessageType.DiscoveryRequest:
                        NetServer.SendDiscoveryResponse(null, msg.SenderEndpoint);
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                        if (status == NetConnectionStatus.Connected)
                        {
                            Player newplayer = new Player();
                            newplayer.Initialize(PlayerTexture, BarsTexture, package, map.Spawn);

                            Players.Add(newplayer);

                            Message(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!");
                        }
                        else if(status == NetConnectionStatus.Disconnected)
                        {
                            Message(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " disconnected!");
                        }
                        break;
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        Messages.Add(new ChatMessage(msg.ReadString(), time));
                        break;
                    case NetIncomingMessageType.Data:
                        HandleData(msg);
                        break;
                    default:
                        Messages.Add(new ChatMessage("Unhandled request ("+msg.MessageType+"): " + msg.ReadString(), time));
                        break;
                }
                NetServer.Recycle(msg);
            }
        }
Ejemplo n.º 5
0
        public Map Generate(Map map)
        {
            // Define Areas
            Areas = new BlockGenerationArea[10];
            Areas[0] = new BlockGenerationArea(0, 10, 200, 70, new FlatBlockGenerator());

            // Do Generation
            foreach (var area in Areas)
            {
                if (area == null) { continue; }
                area.Generate(map);
            }

            return map;
        }
Ejemplo n.º 6
0
        public void Initialize(Map map, Texture2D floraTexture)
        {
            Sprite = floraTexture;
            Flora = new Flora[map.SizeX, map.SizeY];
            Types = new FloraType[50];

            Types[1] = new FloraType("Grass", new Rectangle(0, 0, 24, 24), null, 10, 2);
            Types[2] = new FloraType("Grass", new Rectangle(24, 0, 24, 24), null, 10, 3);
            Types[3] = new FloraType("Grass", new Rectangle(48, 0, 24, 24), null, 10, 4);
            Types[4] = new FloraType("Grass", new Rectangle(72, 0, 24, 24), null, 10, 5);
            Types[5] = new FloraType("Grass", new Rectangle(96, 0, 24, 24), null, 10, 6);
            Types[6] = new FloraType("Grass", new Rectangle(120, 0, 24, 24), null, 10, 0);

            Types[10] = new FloraType("Cyan Flower", new Rectangle(0, 24, 24, 24));
            Types[11] = new FloraType("Pink Flower", new Rectangle(24, 24, 24, 24));
            Types[12] = new FloraType("Blue Flower", new Rectangle(24, 24, 24, 24));
            Types[13] = new FloraType("Orange Flower", new Rectangle(24, 24, 24, 24));
            Types[14] = new FloraType("Purple Flower", new Rectangle(24, 24, 24, 24));

            Types[30] = new FloraType("Vine", new Rectangle(0, 48, 24,24));
            Types[31] = new FloraType("Vine", new Rectangle(24, 48, 24, 24));
            Types[32] = new FloraType("Vine", new Rectangle(48, 48, 24, 24));
            Types[33] = new FloraType("Vine", new Rectangle(72, 48, 24, 24));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Standard Update Function
        /// </summary>
        public void Update( Map map )
        {
            Vector2 originalPosition = new Vector2(Area.X, Area.Y);

            // Turn intentions into changes in velocity
            if (Intention.Left && Velocity.X > MaxSpeed * -1) // If moving left and not at the current max speed
            {
                Velocity.X -= Acceleration;
                if (Velocity.X < MaxSpeed * -1) { Velocity.X = MaxSpeed * -1; } // If accelatred too fast, level it out to the max speed
            }
            if (Intention.Right && Velocity.X < MaxSpeed)  // If moving right and not at the current max speed
            {
                Velocity.X += Acceleration;
                if (Velocity.X > MaxSpeed) { Velocity.X = MaxSpeed; } // If accelatred too fast, level it out to the max speed
            }
            if (Intention.Jumping && JumpPower >= 0) // If trying to jump and still has jump power left
            {
                Velocity.Y -= JumpSpeed;
                JumpPower -= JumpSpeed;
                if (JumpPower < 0) { Falling = true; } else { Falling = false; } // Once jump runs out then active gravity
            }
            else
            {
                // Check for solid footing
                Solid = SolidFooting(Area,map);
                if (!Solid)
                {
                    Falling = true;
                }
                else
                {
                    Falling = false;
                }
            }

            if (Falling) {
                if (Velocity.Y < 1)
                { // Less gravity
                    Velocity.Y += Gravity / 1.5f;
                }
                else
                {
                    Velocity.Y += Gravity;
                }
            }

            if (!Intention.Left && !Intention.Right) // If not moving left or right, apply drag
            {
                if(Velocity.X < 0)
                {
                    if (Falling) { Velocity.X += Drag * 0.3f; } else { Velocity.X += Drag; }
                    if (Velocity.X > 0) { Velocity.X = 0; }
                } else {
                    if (Falling) { Velocity.X -= Drag * 0.3f; } else { Velocity.X -= Drag; }
                    if (Velocity.X < 0) { Velocity.X = 0; }
                }
            }

            // Terminal speed
            if (Velocity.X > TerminalSpeed) { Velocity.X = TerminalSpeed; }
            if (Velocity.X < TerminalSpeed * -1) { Velocity.X = TerminalSpeed * -1; }
            if (Velocity.Y > TerminalSpeed) { Velocity.Y = TerminalSpeed; }
            if (Velocity.Y < TerminalSpeed * -1) { Velocity.Y = TerminalSpeed * -1; }

            // Direct
            if (Intention.Left) { Direction = "left"; }
            if (Intention.Right) { Direction = "right"; }

            // Apply Movement
            Area.X += (int)Velocity.X;
            Area.Y += (int)Velocity.Y;

            // Collisions
            bool collisionSolved = false;
            int collisionRuns = 0;
            while (!collisionSolved)
            {
                collisionRuns++;
                collisionSolved = SolveCollision(map);
                if (collisionRuns > 20) { Falling = false; break; }
            }

            // Finishing Changes
            Moved = new Vector2(Area.X, Area.Y) - originalPosition;
        }
Ejemplo n.º 8
0
        public void Update(Control control, Camera camera, Player player, NpcPackage npcs, Ui.FloatingTextCollection floatingText, ref Map map)
        {
            #region Initialize Weapons

            if (player.Inventory.Quickbar.UsingItem.Type == "useweapon")
            {
                ItemUse use = player.Inventory.Quickbar.UsingItem;
                Weapon weapon = GetWeaponById(int.Parse(use.Value));

                if (weapon.Type.Name == "Sword" && !player.Inventory.Quickbar.IsUsingItem)
                {
                    LiveWeapons.Add(new LiveSword(GetWeaponById(player.Inventory.Quickbar.UsingItem.IntValue),new Vector2(player.Movement.Area.Center.X, player.Movement.Area.Center.Y - 5), player.Movement.Direction));
                    player.Inventory.Quickbar.IsUsingItem = true;
                }

                if (weapon.Type.Name == "Arrow")
                {
                    if(control.currentMouse.LeftButton == ButtonState.Pressed){
                        if(!player.Inventory.Quickbar.IsUsingItem) {
                            player.Inventory.Quickbar.IsUsingItem = true;
                            use.Charge = 10f;
                        } else {
                            use.Charge += 0.1f;
                        }
                    } else {
                        LiveProjectile liveWeapon = new LiveProjectile(Weapons[2], new Vector2(player.Movement.Area.Center.X, player.Movement.Area.Center.Y), (float)use.Charge, control, camera);
                        use.Charge = 0;
                        LiveWeapons.Add(liveWeapon);
                        player.Inventory.Quickbar.IsUsingItem = false;
                        Player = player;
                    }
                }
            }

            #endregion

            #region Update weapons

            for ( int i = 0; i < LiveWeapons.Count; i++)
            {
                if (LiveWeapons[i].Weapon.Type.Name == "Sword")
                {
                    LiveSword weapon = LiveWeapons[i];
                    weapon.Rotation += LiveWeapons[i].Speed;
                    LiveWeapons[i].Location += player.Movement.Moved;
                    if ((LiveWeapons[i].SwingEnd > 0 && LiveWeapons[i].Rotation > LiveWeapons[i].SwingEnd) || (LiveWeapons[i].SwingEnd < 0 && LiveWeapons[i].Rotation < LiveWeapons[i].SwingEnd))
                    {
                        LiveWeapons.RemoveAt(i);
                        player.Inventory.Quickbar.IsUsingItem = false;
                    }
                } else if(LiveWeapons[i].Weapon.Type.Name == "Arrow")
                {
                    LiveProjectile weapon = LiveWeapons[i];
                    if (weapon.Movement.Velocity != Vector2.Zero)
                    {
                        weapon.Movement.Update(map);
                    }
                    else
                    {
                        weapon.TimeToLive -= 1;
                        if (weapon.TimeToLive <= 0) { LiveWeapons.RemoveAt(i); }
                    }
                }
            }

            #endregion

            #region Check for weapon collisions

            // Npc Collisions
            for (int w = 0; w < LiveWeapons.Count; w++)
            {

                var weapon = LiveWeapons[w];
                for (int i = 0; i < npcs.ActiveNpcs.Count; i++)
                {

                    if (weapon.Weapon.Type.Name == "Sword")
                    {
                        LiveSword sword = weapon;
                        if (weapon.currentLocation.Intersects(npcs.ActiveNpcs[i].Movement.Area))
                        {
                            if (npcs.Damage(npcs.ActiveNpcs[i], 10, weapon.Location))
                            {
                                floatingText.Add("10", weapon.Location);
                            }
                        }
                    }
                    else if(weapon.Weapon.Type.Name == "Arrow" && weapon.Movement.Velocity != Vector2.Zero)
                    {
                        LiveProjectile projectile = weapon;
                        if (projectile.Movement.Area.Intersects(npcs.ActiveNpcs[i].Movement.Area))
                        {
                            if (npcs.Damage(npcs.ActiveNpcs[i], 10, weapon.Location))
                            {
                                floatingText.Add("10", new Vector2(projectile.Movement.Area.Center.X, projectile.Movement.Area.Center.Y));
                                LiveWeapons.RemoveAt(w);
                            }
                        }
                    }
                }

                // X/Y Collisions
                int startx = (int)MathHelper.Clamp(camera.X * -1 / 24f, 0, map.SizeX);
                int endx = startx + camera.Width / 24;
                int starty = (int)MathHelper.Clamp(camera.Y * -1 / 24f, 0, map.SizeY);
                int endy = starty + camera.Height / 24;

                for (int x = startx; x < endx; x++)
                {
                    for (int y = starty; y < endy; y++)
                    {

                        Rectangle area = new Rectangle(x * 24, y * 24, 24,24);

                        if (map.Flora.Flora[x, y] != null)
                        {
                            if (weapon.Weapon.Type.Name == "Sword")
                            {
                                if (weapon.currentLocation.Intersects(area)) { map.Flora.Flora[x, y] = null; }
                            }
                            else if (weapon.Weapon.Type.Name == "Arrow")
                            {
                                if (weapon.Movement.Area.Intersects(area)) { map.Flora.Flora[x, y] = null; }
                            }
                        }

                    }
                }

            }

            #endregion
        }
Ejemplo n.º 9
0
 public void Pathfind(Vector2 from, Vector2 to, Map map)
 {
     Pathfind(new Point((int)from.X, (int)from.Y), new Point((int)to.X, (int)to.Y), map);
 }
Ejemplo n.º 10
0
        protected override void Initialize()
        {

            Player = new Player();
            Map = new Map();
            Chat = new Chat();
            ItemPackage = new ItemPackage();
            WeaponPackage = new WeaponPackage();
            Control = new Control();
            NpcPackage = new NpcPackage();
            Ui = new Ui.Ui();
            Lighting.Initialize();

            base.Initialize();
        }
Ejemplo n.º 11
0
        public void Update(Control control, Map map)
        {
            Movement.Update(map);
            Animation.Update(Movement);

            if (Invunerable > 0) { Invunerable -= 1; }

            HealthRegenCounter++;
            if (HealthRegenCounter >= 30)
            { HealthRegenCounter = 0; if (Health < 100) { Health++; } }

            // Killing
            if (Movement.Area.Y > map.SizeY * 24)
            {
                Damage(5000);
            }
        }
Ejemplo n.º 12
0
        public void Update( Map map, Player player, Control control, Camera camera, SpriteFont font )
        {
            for (int i = 0; i < ActiveNpcs.Count; i++)
            {
                //ActiveNpcs[i].Npc.Ai.Update(ActiveNpcs[i].Movement, player); ai off

                ActiveNpc npc = ActiveNpcs[i];

                // Follow the path
                if (npc.CurrentPath != null)
                {
                    if (npc.CurrentDestination == Point.Zero)
                        npc.CurrentDestination = npc.CurrentPath.Pop();

                    if (npc.Movement.Area.Intersects(new Rectangle(npc.CurrentDestination.X * 24, npc.CurrentDestination.Y * 24, 24, 24)))
                    {
                        if (npc.CurrentPath.Count > 0)
                            { npc.CurrentDestination = npc.CurrentPath.Pop(); }
                        else
                            { npc.CurrentPath = null; npc.CurrentDestination = Point.Zero; }

                    }
                }

                if (npc.CurrentDestination != Point.Zero)
                    MovementChase(npc.Movement, npc.CurrentDestination);
                else
                    npc.Movement.Intention.Stop();

                ActiveNpcs[i].Movement.Update(map);
                ActiveNpcs[i].Type.Race.Animation.Update(ActiveNpcs[i].Movement);

                #region Interaction

                if (control.MousePos.Intersects(camera.FromRectangle(npc.Movement.Area)))
                {
                    control.State = Control.CursorStates.Interact;
                    if (control.Click(false))
                        Interaction.Start(npc);
                }

                if (Interaction.State != NpcInteraction.NpcInteractionState.None)
                {
                    if (Geometry.Range(player.Movement.Area, npc.Movement.Area) > 7)
                        Interaction.End();
                }

                Interaction.Update(camera, control, font);

                #endregion

                #region Damage / Combat

                if(npc.Movement.Area.Intersects(player.Movement.Area))
                {
                    player.Damage(5);
                }

                #endregion

                ActiveNpcs[i].Invunerable -= 1;
            }
        }
Ejemplo n.º 13
0
 private Point[] PathfindNeighbours(Point loc, Map map)
 {
     Point[] points = new Point[4];
     if (map.getBlock(loc.X, loc.Y + 1) == null) { points[1] = new Point(loc.X, loc.Y + 1); } else { points[0] = Point.Zero; }
     if (map.getBlock(loc.X, loc.Y - 1) == null) { points[0] = new Point(loc.X, loc.Y - 1); } else { points[1] = Point.Zero; }
     if (map.getBlock(loc.X - 1, loc.Y) == null) { points[2] = new Point(loc.X - 1, loc.Y); } else { points[2] = Point.Zero; }
     if (map.getBlock(loc.X + 1, loc.Y) == null) { points[3] = new Point(loc.X + 1, loc.Y); } else { points[3] = Point.Zero; }
     return points;
 }
Ejemplo n.º 14
0
 public void Generate(Map map)
 {
     map = Generator.Generate(map, this);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Solves a collision for the movement object.
        /// </summary>
        /// <param name="map">The Map</param>
        /// <returns>Returns false if the collision was not fully solved</returns>
        public bool SolveCollision(Map map)
        {
            int BlockAtX = Area.Center.X / 24;
            int BlockAtY = Area.Center.Y / 24;
            int totalWidth = 0;
            int totalHeight = 0;
            int averageWidthW = 0;
            int averageWidthH = 0;
            int averageHeightW = 0;
            int averageHeightH = 0;
            Vector2 collisionCenterH = Vector2.Zero;
            Vector2 collisionCenterW = Vector2.Zero;

            #region Block Collisions

            for (int x = BlockAtX - 5; x < BlockAtX + 5; x++)
            {
                for (int y = BlockAtY - 5; y < BlockAtY + 5; y++)
                {
                    Block block = map.getBlock(x, y);
                    if (block == null) { continue; }
                    Rectangle blockArea = new Rectangle(x * 24, y * 24, 24, 24);
                    Rectangle intersection = Rectangle.Intersect(Area, blockArea);

                    if (intersection != Rectangle.Empty) // Only handle intersection if it exists
                    {

                        totalHeight += intersection.Height;
                        totalWidth += intersection.Width;
                        if (totalWidth > totalHeight)
                        {
                            if (collisionCenterW == Vector2.Zero)
                            {
                                averageWidthW += intersection.Width;
                                averageHeightW += intersection.Height;
                                collisionCenterW = new Vector2(blockArea.Center.X, blockArea.Center.Y);
                            }
                            else
                            {
                                averageWidthW = (averageWidthW + intersection.Width) / 2;
                                averageHeightW = (averageHeightW + intersection.Height) / 2;
                                collisionCenterW = new Vector2((collisionCenterW.X + blockArea.Center.X) / 2, (collisionCenterW.Y + blockArea.Center.Y) / 2);
                            }
                        }
                        else
                        {
                            if (collisionCenterH == Vector2.Zero)
                            {
                                averageWidthH += intersection.Width;
                                averageHeightH += intersection.Height;
                                collisionCenterH = new Vector2(blockArea.Center.X, blockArea.Center.Y);
                            }
                            else
                            {
                                averageWidthH = (averageWidthH + intersection.Width) / 2;
                                averageHeightH = (averageHeightH + intersection.Height) / 2;
                                collisionCenterH = new Vector2((collisionCenterH.X + blockArea.Center.X) / 2, (collisionCenterH.Y + blockArea.Center.Y) / 2);
                            }
                        }
                    }
                }
            }

            #endregion

            #region Entity Collisions

            foreach (Entity entity in map.Entities.Entities)
            {
                if (!entity.Solid) { continue; }
                Rectangle intersection = Rectangle.Intersect(Area, entity.Area);
                if (intersection != new Rectangle())
                {
                    totalHeight += intersection.Height;
                    totalWidth += intersection.Width;
                    if (totalWidth > totalHeight)
                    {
                        if (collisionCenterW == Vector2.Zero)
                        {
                            averageWidthW += intersection.Width;
                            averageHeightW += intersection.Height;
                            collisionCenterW = new Vector2(entity.Area.Center.X, entity.Area.Center.Y);
                        }
                        else
                        {
                            averageWidthW = (averageWidthW + intersection.Width) / 2;
                            averageHeightW = (averageHeightW + intersection.Height) / 2;
                            collisionCenterW = new Vector2((collisionCenterW.X + entity.Area.Center.X) / 2, (collisionCenterW.Y + entity.Area.Center.Y) / 2);
                        }
                    }
                    else
                    {
                        if (collisionCenterH == Vector2.Zero)
                        {
                            averageWidthH += intersection.Width;
                            averageHeightH += intersection.Height;
                            collisionCenterH = new Vector2(entity.Area.Center.X, entity.Area.Center.Y);
                        }
                        else
                        {
                            averageWidthH = (averageWidthH + intersection.Width) / 2;
                            averageHeightH = (averageHeightH + intersection.Height) / 2;
                            collisionCenterH = new Vector2((collisionCenterH.X + entity.Area.Center.X) / 2, (collisionCenterH.Y + entity.Area.Center.Y) / 2);
                        }
                    }
                }
            }

            #endregion

            if (totalWidth > 0 || totalHeight > 0){
                if (totalWidth > totalHeight ) // Horizontal
                {
                    if (collisionCenterW.Y < Area.Center.Y) // Colliding from the top
                    { Area.Y += averageHeightW; Velocity.Y = 0; }
                    else // Colliding from the bottom
                    {
                        Area.Y -= averageHeightW;
                        JumpPower = MaxJumpPower;
                        Falling = false;
                        if (Velocity.Y > 0) { Velocity.Y = 0; } }
                }
                else
                {
                    if (collisionCenterH.X > Area.Center.X) // Colliding from the right
                    { Area.X -= averageWidthH; if (Velocity.X > 0) { Velocity.X = 0; } }
                    else // Colliding from the left
                    { Area.X += averageWidthH; if (Velocity.X < 0) { Velocity.X = 0; } }
                }
                return false;
            }

            return true;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Checks to see if a rectangle has solid footing mover has solid footing
        /// </summary>
        /// <param name="area">The area which is being checked</param>
        /// <param name="map">Checks against all collidables</param>
        /// <returns>Returns whether the area has solid footing or not</returns>
        public bool SolidFooting(Rectangle area, Map map)
        {
            int BlockAtX = Area.Center.X / 24;
            int BlockAtY = Area.Center.Y / 24;
            int totalWidth = 0;
            int totalHeight = 0;
            Vector2 collisionCenter = Vector2.Zero;
            area.Y++;

            for (int x = BlockAtX - 5; x < BlockAtX + 5; x++)
            {
                for (int y = BlockAtY - 5; y < BlockAtY + 5; y++)
                {
                    Block block = map.getBlock(x, y);
                    if (block == null) { continue; }
                    Rectangle blockArea = new Rectangle(x * 24, y * 24, 24, 24);
                    Rectangle intersection = Rectangle.Intersect(area, blockArea);

                    if (intersection != Rectangle.Empty)
                    {
                        totalHeight += intersection.Height;
                        totalWidth += intersection.Width;
                        if (collisionCenter == Vector2.Zero)
                        {
                            collisionCenter = new Vector2(blockArea.Center.X, blockArea.Center.Y);
                        }
                        else
                        {
                            collisionCenter = new Vector2((collisionCenter.X + blockArea.Center.X) / 2, (collisionCenter.Y + blockArea.Center.Y) / 2);
                        }
                    }
                }
            }

            if (totalWidth > 0 || totalHeight > 0)
            {
                if (totalWidth > totalHeight) // Horizontal
                {
                    if (collisionCenter.Y > area.Center.Y) // Colliding from the bottom
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Ejemplo n.º 17
0
        public Stack<Point> Pathfind(Point from, Point goal, Map map)
        {
            int search_limit = 2000;
            List<Point> closed = new List<Point>();
            List<Point> open = new List<Point>();
            Point[,] movements = new Point[map.SizeX, map.SizeY];
            open.Add( from );

            int[,] g_score = new int[map.SizeX, map.SizeY];
            int[,] h_score = new int[map.SizeX, map.SizeY];
            int[,] f_score = new int[map.SizeX, map.SizeY];
            g_score[from.X, from.Y] = 0;
            h_score[from.X, from.Y] = PathfindEstimate(from, goal);
            f_score[from.X, from.Y] = h_score[from.X, from.Y] + g_score[from.X, from.Y];

            while (open.Count > 0 && search_limit > 0)
            {
                search_limit--;
                // Get node with lowest f score
                int lowest_score = 0;
                int lowest_score_index = 0;
                int lowest_y = 0;
                for (int i = 0; i < open.Count; i++)
                {
                    if (lowest_score == 0 || f_score[ open[i].X, open[i].Y ] < lowest_score || (open[i].Y > lowest_y && f_score[ open[i].X, open[i].Y ] <= lowest_score)) {
                        lowest_score = f_score[open[i].X, open[i].Y];
                        lowest_score_index = i;
                        lowest_y = open[i].Y;
                    }
                }
                Point current = open[lowest_score_index];

                if (current == goal)
                    return PathfindConstruct(movements, goal);

                open.Remove(current);
                closed.Add(current);
                Point[] neighbours = PathfindNeighbours(current, map);
                foreach (Point neighbour in neighbours)
                {
                    if (closed.Contains(neighbour) || neighbour.X < 0 || neighbour.Y < 0 || neighbour == Point.Zero) { continue; }
                    int tmp_g_score = g_score[current.X, current.Y] + PathfindEstimate(neighbour, goal);
                    bool tmp_is_better = false;

                    if (!open.Contains(neighbour))
                    {
                        open.Add(neighbour);
                        h_score[neighbour.X, neighbour.Y] = PathfindEstimate(neighbour, goal);
                        tmp_is_better = true;
                    }
                    else if (tmp_g_score < g_score[neighbour.X, neighbour.Y])
                    {
                        tmp_is_better = true;
                    }

                    if (tmp_is_better)
                    {
                        movements[neighbour.X, neighbour.Y] = current;
                        g_score[neighbour.X, neighbour.Y] = tmp_g_score;
                        f_score[neighbour.X, neighbour.Y] = g_score[neighbour.X, neighbour.Y] + h_score[neighbour.X, neighbour.Y];
                    }
                }

            }
            return null;
        }