Exemple #1
0
        List<Planet> LoadMap(string map)
        {
            List<Planet> mapPlanets = new List<Planet>();
            XmlReader xmlReader = XmlReader.Create(map);
            xmlReader.ReadStartElement();
            while (xmlReader.Read())
            {
                /*Read planet header*/
                if (xmlReader.LocalName == "Planet" && xmlReader.IsStartElement())
                {
                    /*Read the ID element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("ID");
                    int ID = xmlReader.ReadContentAsInt();

                    /*Read the owner element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("Owner");
                    string owner = xmlReader.ReadContentAsString();

                    /*Read the forces element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("Forces");
                    int forces = xmlReader.ReadContentAsInt();

                    /*Read the growth element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("Growth");
                    int growth = xmlReader.ReadContentAsInt();

                    /*Read the growth cooldown element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("GrowthCooldown");
                    int growthcd = xmlReader.ReadContentAsInt();

                    /*Read the size element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("Size");
                    float size = xmlReader.ReadContentAsFloat();

                    /*Read the Position element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("Position");
                    Vector2 Position = new Vector2();

                    /*Read the X element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("X");
                    Position.X = xmlReader.ReadContentAsInt();

                    /*Read the Y element*/
                    xmlReader.Read();
                    xmlReader.MoveToElement();
                    xmlReader.ReadStartElement("Y");
                    Position.Y = xmlReader.ReadContentAsInt();

                    Planet p = new Planet();
                    p.ID = ID;
                    p.Position = Position;
                    p.Growth = growth;
                    p.GrowthCounter = p.GrowthReset = growthcd;
                    p.Owner=(PlayerType)Enum.Parse(typeof(PlayerType),owner,false);
                    p.Forces.Add(p.Owner, forces);
                    p.PlanetSize = size;
                    p.InStateOfWar = false;
                    if (p.PlanetSize <= 0.45f)
                        p.Texture = Planet[0];
                    else if (p.PlanetSize >= 0.45f && p.PlanetSize <= 0.7f)
                        p.Texture = Planet[2];
                    else
                        p.Texture = Planet[1];
                    mapPlanets.Add(p);
                }

            }

            return mapPlanets;
        }
Exemple #2
0
        public void SendFleet(int Ammount, Planet Origin, Planet Destination)
        {
            if (Origin.ID == Destination.ID)
                return;
            if (Origin.Forces[Origin.Owner] < Ammount)
                return;
            Origin.Forces[Origin.Owner] -= Ammount;

            Fleet fleet = new Fleet();
            fleet.Owner = Origin.Owner;
            //Put the ship on the edge
            Vector2 Normalized = Destination.Position - Origin.Position;
            Normalized.Normalize();
            fleet.Rotation = (float)Math.Atan2(Normalized.Y, Normalized.X) + MathHelper.ToRadians(90);
            Normalized *= Origin.PlanetSize * 64;

            fleet.Position = Origin.Position+Normalized;
            fleet.Origin = Origin;
            fleet.Destination = Destination;
            fleet.Count = Ammount;

            int ExtraShipCount = 0;
            if (fleet.Count-10 >= 10)
                ExtraShipCount = (fleet.Count-10) / 10;
            fleet.Positions = new Vector2[ExtraShipCount];
            Normalized.Normalize();

            for (int i = 0; i < ExtraShipCount; i++)
            {
                float Angle = MathHelper.ToRadians((float)(rnd.Next(360) + rnd.NextDouble()));
                float Distance=8+(float)Math.Sqrt(rnd.NextDouble())*((1+(float)Math.Sqrt(ExtraShipCount)/2)*8);
                fleet.Positions[i] =  new Vector2((float)Math.Cos(Angle)*Distance,(float)Math.Sin(Angle)*Distance);

                foreach (Vector2 p in fleet.Positions)
                {
                    if (p == null||p==fleet.Positions[i])
                        continue;

                    if(Vector2.Distance(p,fleet.Positions[i])<16)
                    {
                        Angle = MathHelper.ToRadians((float)(rnd.Next(360) + rnd.NextDouble()));
                        Distance = 8 + (float)Math.Sqrt(rnd.NextDouble()) * ((1 + (float)Math.Sqrt(ExtraShipCount)/2) * 8);
                        fleet.Positions[i] = new Vector2((float)Math.Cos(Angle) * Distance, (float)Math.Sin(Angle) * Distance);
                    }
                }
            }

            State.Fleets.Add(fleet);
        }