Esempio n. 1
0
        public Vector2 GetLocation(SpaceShip Ship, float Scalar)
        {
            SpaceShipUnit unit = Ship.Unit;

            if (unit == null || unit.Units.Count == 1 || unit.Units[0] == Ship)
            {
                return(Ship.Location);
            }

            int index = 0;

            while (index < unit.Units.Count && index <= slots.Count)
            {
                if (unit.Units[index] == Ship)
                {
                    break;
                }
                index++;
            }

            if (index < slots.Count)
            {
                return(unit.Units[0].Location + (slots[index].Rotate((float)unit.Units[0].Angle * DEG2RAD) * Scalar));
            }

            return(Ship.Location);
        }
Esempio n. 2
0
        public void Load(ContentManager content)
        {
            random = new Random();
            space_ship = new SpaceShip();
            space_ship.Load(content);
            float y=-50;
            active_obstacles = new List<Obstacle>();
            for (int i = 0; i < 29; i++)
            {
                active_obstacles.Add(new Obstacle());
                active_obstacles[i].Load(content);

            }
            active_obstacles[0].Init(0,-50);
            for (int i = 1;i<29;i++)
            {
                y += 80 * active_obstacles[i - 1].scale;
                active_obstacles[i].Init(0, y);
            }

            last_obstacle = 0;
            //active_obstacles[0].Init(0, -50);
            //active_obstacles[1].Init(0, -50 + (float)Math.Pow(1.02, 1 )/20);
            //active_obstacles[2].Init(0, -50 + (float)Math.Pow(1.02, 1 )/20 + (float)Math.Pow(1.02, 2) / 20);
            //active_obstacles[3].Init(0, -50 + (float)Math.Pow(1.02, 1) / 20 + (float)Math.Pow(1.02, 2) / 20 + (float)Math.Pow(1.02, 3) / 20);
            //active_obstacles[4].Init(0, -50 + (float)Math.Pow(1.02, 1) / 20 + (float)Math.Pow(1.02, 2) / 20 + (float)Math.Pow(1.02, 3) / 20 + (float)Math.Pow(1.02, 4 )/20);
            //active_obstacles[5].Init(0, -50 + (float)Math.Pow(1.02, 1) / 20 + (float)Math.Pow(1.02, 2) / 20 + (float)Math.Pow(1.02, 3) / 20 + (float)Math.Pow(1.02, 4) / 20 + (float)Math.Pow(1.02, 5) / 20);
            frame_counter = 1;
            newest = 0;
        }
Esempio n. 3
0
 public void Add(SpaceShip Ship)
 {
     Units.Add(Ship);
     Ship.Unit = this;
     if (Ship.Hardpoints != null)
     {
         foreach (SpaceShipHardpoint hp in Ship.Hardpoints)
         {
             Add(hp);
         }
     }
 }
Esempio n. 4
0
        public static SpaceShipUnit FromXml(XmlResource Xml, SpaceShipUnit DstObject)
        {
            if (Xml == null)
            {
                throw new ArgumentNullException("Xml");
            }
            SpaceShipUnit Result = DstObject;

            if (DstObject == null)
            {
                Result = new SpaceShipUnit();
            }

            XmlNode obj = Xml.Xml.LastChild;

            string        baseName   = GetXmlText(obj, "Base", string.Empty);
            SpaceShipUnit baseObject = Result;

            if (!string.IsNullOrEmpty(baseName))
            {
                try
                {
                    baseObject = SpaceShipUnit.FromXml(ResourceManager.Get <XmlResource>(baseName), null);
                }
                catch (KeyNotFoundException e)
                {
                    baseObject = Result;
                    Console.WriteLine("XML Error: Failed to locate XML base " + baseName);
                }
            }

            List <SpaceObject> units = GetXmlNested(obj, "ships", null);

            if (units != null && units.Count > 0)
            {
                Result.Units.Clear();
                foreach (SpaceObject o in units)
                {
                    SpaceShip ss = o as SpaceShip;
                    if (ss != null)
                    {
                        Result.Add(ss);
                    }
                }
            }

            Result.UiImage = ResourceManager.Get <TextureResource>(GetXmlText(obj, "image", baseObject.UiImage.Name));

            return(Result);
        }
Esempio n. 5
0
        private static int CommandSpawnUnit(List <string> arg)
        {
            if (arg == null || arg.Count < 1)
            {
                Debug.WriteLine("%WARNING%Usage: \"spawn-unit <xml name>\"");
                return(1);
            }

            SpaceShipUnit newUnit = new SpaceShipUnit();

            for (int i = 0; i < 7; i++)
            {
                SpaceShip newShip = SpaceShip.FromXml(ResourceManager.Get <XmlResource>(arg[0]), null);
                newShip.Location = new Vector2(0, 0);
                newShip.Faction  = 2;
                newShip.Hitbox   = new Hitbox(newShip.Texture.Texture.width);
                newUnit.Add(newShip);
            }
            newUnit.UiImage = ResourceManager.Get <TextureResource>(@"thumbnail\barb");
            GameManager.Add(newUnit);

            return(0);
        }
Esempio n. 6
0
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferWidth  = screenWidth;
            graphics.PreferredBackBufferHeight = screenHeight;

            ScreenRectangle = new Rectangle(
                0,
                0,
                screenWidth,
                screenHeight);

            Content.RootDirectory = "Content";

            Components.Add(new InputHandler(this));

            stateManager = new GameStateManager(this);
            Components.Add(stateManager);

            TitleScreen              = new TitleScreen(this, stateManager);
            StartMenuScreen          = new StartMenuScreen(this, stateManager);
            GamePlayScreen           = new GamePlayScreen(this, stateManager);
            AdminScreen              = new AdminScreen(this, stateManager);
            CharacterGeneratorScreen = new CharacterGeneratorScreen(this, stateManager);
            EndGameScreen            = new EndGameScreen(this, stateManager);
            PauseScreen              = new PauseScreen(this, stateManager);
            highscoreScreen          = new HighscoreScreen(this, stateManager);
            upgradeScreen            = new UpgradeScreen(this, stateManager);
            saveHistory              = new SaveHistoryScreen(this, stateManager);
            initPrice = new InitialPriceScreen(this, stateManager);

            spaceShip = new SpaceShip();
            board     = new Board();

            stateManager.ChangeState(TitleScreen);
        }
Esempio n. 7
0
        public static SpaceShipHardpoint FromXml(XmlResource Xml, SpaceShipHardpoint DstObject)
        {
            if (Xml == null)
            {
                throw new ArgumentNullException("Xml");
            }
            SpaceShipHardpoint Result = DstObject;

            if (DstObject == null)
            {
                Result = new SpaceShipHardpoint();
            }
            Result = SpaceShip.FromXml(Xml, Result) as SpaceShipHardpoint;

            XmlNode obj = Xml.Xml.LastChild;

            string             baseName   = GetXmlText(obj, "Base", string.Empty);
            SpaceShipHardpoint baseObject = Result;

            if (!string.IsNullOrEmpty(baseName))
            {
                try
                {
                    baseObject = SpaceShipHardpoint.FromXml(ResourceManager.Get <XmlResource>(baseName), null);
                }
                catch (KeyNotFoundException e)
                {
                    baseObject = Result;
                    Console.WriteLine("XML Error: Failed to locate XML base " + baseName);
                }
            }

            string[] offsetRaw = GetXmlText(obj, "Offset", "0,0").Split(',');
            Result.Offset  = new Vector2(float.Parse(offsetRaw[0]), float.Parse(offsetRaw[1]));
            Result.Texture = ResourceManager.Get <TextureResource>(GetXmlText(obj, "Texture", baseObject.Texture.Name));
            return(Result);
        }
Esempio n. 8
0
        private static List <SpaceObject> GetXmlNested(XmlNode Parent, string Name, List <SpaceObject> Default)
        {
            if (Parent.HasChildNodes)
            {
                XmlNodeList children = Parent.ChildNodes;
                foreach (XmlNode node in children)
                {
                    if (node.Name.ToUpperInvariant() == Name.ToUpperInvariant())
                    {
                        List <SpaceObject> result = new List <SpaceObject>();
                        if (node.ChildNodes.Count > 0)
                        {
                            children = node.ChildNodes;
                            foreach (XmlNode childNode in children)
                            {
                                if (childNode.Name.ToUpperInvariant() == "spaceship".ToUpperInvariant())
                                {
                                    SpaceShip child = SpaceShip.FromXml(new XmlResource()
                                    {
                                        Xml = new XmlDocument()
                                        {
                                            InnerXml = childNode.OuterXml
                                        }
                                    }, null);
                                    result.Add(child);
                                }
                            }
                        }

                        return(result);
                    }
                }
            }

            return(Default);
        }
Esempio n. 9
0
 public SpaceShipUnit(SpaceShip Ship)
 {
     Add(Ship);
 }
Esempio n. 10
0
        public override void Tick(double Delta)
        {
            if (!Active)
            {
                return;
            }

            isLeader = Unit == null || Unit.Formation == null || this == Unit.Leader;

            if (!isLeader)
            {
                leader    = Unit.Leader;
                Stance    = leader.Stance;
                Objective = leader.Objective;
                Goal      = leader.Goal;
                Behavior  = leader.Behavior;
            }

            if (isLeader || Behavior == Behaviors.Attacking)
            {
                if (Behavior == Behaviors.Idle)
                {
                    Throttle = 0;
                    if (Velocity.Length() > 0)
                    {
                        Velocity = Velocity.Length() * 0.95f * Vector2.Normalize(Velocity);
                    }
                    AngularAcceleration = 0;

                    if (Shield <= 0 && RNG.Next(100 * 60) < ShieldRebootProbability)
                    {
                        Shield += 1;
                    }

                    if (Stance == Stances.Defend)
                    {
                        if (CombatRange <= 0)
                        {
                            CombatRange = MathF.Min(Texture.Texture.width, Texture.Texture.height) * 10;
                        }

                        if (Hardpoints == null) // If I've got hardpoints, let them deal with it.
                        {
                            SpaceObject[] nearTargets = GetTargets(Location, CombatRange);
                            if (nearTargets.Length > 0)
                            {
                                if (RandomOffset >= nearTargets.Length || RandomOffset < 0 || RNG.NextDouble() < 0.001)
                                {
                                    RandomOffset = RandomOffset = (int)Math.Floor(RNG.NextDouble() * nearTargets.Length);
                                }

                                Objective = nearTargets[RandomOffset];

                                float distance = Vector2.Distance(Objective.Location, Location);
                                if (RNG.Next(100) <= 200 / distance)
                                {
                                    attackOffset = new Vector2(RNG.Next((int)(distance / 50)) - RNG.Next((int)(distance / 50)), RNG.Next((int)(distance / 50)) - RNG.Next((int)(distance / 50)));
                                }
                                Goal = (Objective as SpaceShip).GetLead(1 + distance / 50) + attackOffset;

                                double angleOffset = (AngleToPoint(this.Location, Goal) - Angle) + 90;
                                if (angleOffset > 180)
                                {
                                    angleOffset -= 360;
                                }
                                if (angleOffset < -180)
                                {
                                    angleOffset += 360;
                                }

                                if (angleOffset > 1)
                                {
                                    AngularAcceleration = TurnSpeed * Delta;
                                }
                                else if (angleOffset < -1)
                                {
                                    AngularAcceleration = -TurnSpeed * Delta;
                                }
                                else
                                {
                                    AngularAcceleration = TurnSpeed * Math.Abs(Math.Pow(angleOffset, 2)) * angleOffset * Delta;
                                    if (shotCooldown <= 0)
                                    {
                                        Shoot();
                                    }
                                }
                            }
                        }
                    }
                }
                else if (Behavior == Behaviors.Attacking)
                {
                    if (Objective == this || Objective.Active == false)
                    {
                        Behavior = Behaviors.Idle;
                    }

                    float distance = Vector2.Distance(Objective.Location, Location);
                    if (RNG.Next(100) <= 200 / distance)
                    {
                        attackOffset = new Vector2(RNG.Next((int)(distance / 50)) - RNG.Next((int)(distance / 50)), RNG.Next((int)(distance / 50)) - RNG.Next((int)(distance / 50)));
                    }

                    if (Objective is SpaceShip)
                    {
                        Goal = (Objective as SpaceShip).GetLead(1 + distance / 50) + attackOffset;
                    }
                    else
                    {
                        Goal = Objective.Location + attackOffset;
                    }

                    double angleOffset = (AngleToPoint(this.Location, Goal) - Angle) + 90;
                    if (angleOffset > 180)
                    {
                        angleOffset -= 360;
                    }
                    if (angleOffset < -180)
                    {
                        angleOffset += 360;
                    }

                    if (angleOffset > 1)
                    {
                        AngularAcceleration = TurnSpeed * Delta;
                    }
                    else if (angleOffset < -1)
                    {
                        AngularAcceleration = -TurnSpeed * Delta;
                    }
                    else
                    {
                        AngularAcceleration = TurnSpeed * Math.Abs(Math.Pow(angleOffset, 2)) * angleOffset * Delta;
                        if (shotCooldown <= 0 && distance < CombatRange)
                        {
                            Shoot();
                        }
                    }

                    Throttle = Math.Pow(Math.Clamp((180 - angleOffset) / 180, 0.0, 1.0), 2);
                }
                else if (Behavior == Behaviors.Going)
                {
                    Vector2 goal = Goal;

                    /*if (Unit != null && Unit.Formation != null && this != Unit.Units[0])
                     * {
                     *  goal = Unit.Formation.GetLocation(this, Texture.Texture.width * 0.5f);
                     * }*/

                    double angleOffset = (AngleToPoint(this.Location, goal) - Angle) + 90;
                    if (angleOffset > 180)
                    {
                        angleOffset -= 360;
                    }
                    if (angleOffset < -180)
                    {
                        angleOffset += 360;
                    }

                    if (angleOffset > 1)
                    {
                        AngularAcceleration = TurnSpeed * Delta;
                    }
                    else if (angleOffset < -1)
                    {
                        AngularAcceleration = -TurnSpeed * Delta;
                    }
                    else
                    {
                        AngularAcceleration = TurnSpeed * Math.Abs(Math.Pow(angleOffset, 2)) * angleOffset * Delta;
                    }

                    Throttle = Math.Pow(Math.Clamp((180 - angleOffset) / 180, 0.0, 1.0), 2);

                    if (Vector2.Distance(Location, goal) < Velocity.Length() * 60 * 10)
                    {
                        if (Vector2.Distance(Location, goal) < Texture.Texture.height)
                        {
                            Behavior = Behaviors.Idle;
                        }
                        else
                        {
                            Throttle *= (Vector2.Distance(Location, goal) / ((Velocity.Length() * 120) + 1)) * 0.75;
                        }
                    }

                    if (Stance == Stances.Defend)
                    {
                        if (CombatRange <= 0)
                        {
                            CombatRange = MathF.Min(Texture.Texture.width, Texture.Texture.height) * 10;
                        }

                        if (Hardpoints == null) // If I've got hardpoints, let them deal with it.
                        {
                            SpaceObject[] nearTargets = GetTargets(Location, MathF.Sqrt(Velocity.Length()) * 0.75f * CombatRange);
                            if (nearTargets.Length > 0)
                            {
                                if (RandomOffset >= nearTargets.Length || RandomOffset < 0 || RNG.NextDouble() < 0.001)
                                {
                                    RandomOffset = RandomOffset = (int)Math.Floor(RNG.NextDouble() * nearTargets.Length);
                                }

                                float distance = Vector2.Distance(nearTargets[RandomOffset].Location, Location);
                                if (RNG.Next(100) <= 200 / distance)
                                {
                                    attackOffset = new Vector2(RNG.Next((int)(distance / 50)) - RNG.Next((int)(distance / 50)), RNG.Next((int)(distance / 50)) - RNG.Next((int)(distance / 50)));
                                }
                                Vector2 incidentalGoal = (nearTargets[RandomOffset] as SpaceShip).GetLead(1 + distance / 50) + attackOffset;

                                angleOffset = (AngleToPoint(this.Location, incidentalGoal) - Angle) + 90;
                                if (angleOffset > 180)
                                {
                                    angleOffset -= 360;
                                }
                                if (angleOffset < -180)
                                {
                                    angleOffset += 360;
                                }

                                if (Math.Abs(angleOffset) < 5)
                                {
                                    //AngularAcceleration = TurnSpeed * Math.Abs(Math.Pow(angleOffset, 2)) * angleOffset * Delta;
                                    if (shotCooldown <= 0)
                                    {
                                        Shoot();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Vector2 goal           = Unit.Formation.GetLocation(this, Texture.Texture.width * 0.5f);
                Vector2 screenGoal     = UiManager.WorldToScreen(goal);
                Vector2 screenLocation = UiManager.WorldToScreen(Location);
                Debug.DrawLine((int)screenGoal.X, (int)screenGoal.Y, (int)screenLocation.X, (int)screenLocation.Y, new Color(255, 255, 255, 64));

                if (Vector2.Distance(Location, goal) > Texture.Texture.width)
                {
                    double angleOffset = (AngleToPoint(this.Location, goal) - Angle) + 90;
                    if (angleOffset > 180)
                    {
                        angleOffset -= 360;
                    }
                    if (angleOffset < -180)
                    {
                        angleOffset += 360;
                    }

                    if (angleOffset > 1)
                    {
                        AngularAcceleration = TurnSpeed * Delta;
                    }
                    else if (angleOffset < -1)
                    {
                        AngularAcceleration = -TurnSpeed * Delta;
                    }
                    else
                    {
                        AngularAcceleration = TurnSpeed * Math.Abs(Math.Pow(angleOffset, 2)) * angleOffset * Delta;
                    }

                    Throttle = Math.Pow(Math.Clamp((180 - angleOffset) / 180, 0.0, 1.0), 2);

                    if (Vector2.Distance(Location, goal) < Velocity.Length() * 60 * 10)
                    {
                        Throttle *= (Vector2.Distance(Location, goal) / ((Velocity.Length() * 120) + 1)) * 0.75;
                    }

                    //if (Behavior == Behaviors.Idle)
                    //{ Throttle = Throttle * 0.1; }
                }
                else
                {
                    Throttle = 0;
                }
            }

            double  radians = (Angle - 90) * (Math.PI / 180);
            Vector2 thrust  = new Vector2((float)Math.Cos(radians), (float)Math.Sin(radians));

            thrust       = thrust * (float)(MaxThrust * Throttle);
            Acceleration = thrust * (float)Delta / (float)Mass;

            // Nudge away from other ships
            SpaceObject[] neighbors = GetNeighbors(Location, Texture.Texture.width);
            if (neighbors.Length > 0)
            {
                for (int i = 0; i < neighbors.Length; i++)
                {
                    Vector2 nudge = Location - neighbors[i].Location;
                    nudge = (Vector2.Normalize(nudge) * (Velocity.Length() + 0.1f)) / (Vector2.Distance(neighbors[i].Location, Location) * 2.0f + 0.001f);
                    //nudge += new Vector2((float)RNG.NextDouble(), (float)RNG.NextDouble()) / (Vector2.Distance(neighbors[i].Location, Location) + 0.1f) ;
                    Acceleration += nudge / (float)Mass;
                }
            }

            /*
             * if (!isLeader)
             * {
             *  Vector2 nudge = Unit.Formation.GetLocation(this, Texture.Texture.width * 0.5f);
             *  nudge = nudge - Location;
             *  if (nudge.X != 0 || nudge.Y != 0)
             *  {
             *      if (nudge.Length() < Texture.Texture.width)
             *      {
             *          double angleOffset = leader.Angle - Angle;
             *          if (angleOffset > 180) { angleOffset -= 360; }
             *          if (angleOffset < -180) { angleOffset += 360; }
             *
             *          if (angleOffset > 1) { AngularAcceleration = TurnSpeed * Delta; }
             *          else if (angleOffset < -1) { AngularAcceleration = -TurnSpeed * Delta; }
             *          else
             *          {
             *              AngularAcceleration += TurnSpeed * Math.Pow(angleOffset, 5) * 0.1f;
             *          }
             *      }
             *      if (false)//Behavior == Behaviors.Idle)
             *      {
             *          nudge = Vector2.Normalize(nudge) * 0.01f;
             *      }
             *      else
             *      {
             *          nudge = Vector2.Normalize(nudge) * (Velocity.Length() * 0.1f + 1f) * 0.1f;
             *      }
             *
             *      Velocity += (nudge * 1f) / (float)Mass;
             *  }
             * }*/

            if (shotCooldown > 0)
            {
                shotCooldown -= 1 * Delta * (RNG.NextDouble() + RNG.NextDouble() + RNG.NextDouble());
            }
            if (shotHeat > 0)
            {
                shotHeat -= 0.1 * Delta;
            }

            if (Shield > 0 && Shield < MaxShield)
            {
                Shield += ShieldRegen;
            }

            base.Tick(Delta);
        }
Esempio n. 11
0
        public static SpaceShip FromXml(XmlResource Xml, SpaceShip DstObject)
        {
            if (Xml == null)
            {
                throw new ArgumentNullException("Xml");
            }
            SpaceShip Result = DstObject;

            if (DstObject == null)
            {
                Result = new SpaceShip();
            }
            Result = SpaceObject.FromXml(Xml, Result) as SpaceShip;

            XmlNode obj = Xml.Xml.LastChild;

            string    baseName   = GetXmlText(obj, "Base", string.Empty);
            SpaceShip baseObject = Result;

            if (!string.IsNullOrEmpty(baseName))
            {
                try
                {
                    baseObject = SpaceShip.FromXml(ResourceManager.Get <XmlResource>(baseName), null);
                }
                catch (KeyNotFoundException e)
                {
                    baseObject = Result;
                    Console.WriteLine("XML Error: Failed to locate XML base " + baseName);
                }
            }

            Result.Hull                    = GetXmlValue(obj, "Hull", baseObject.Hull);
            Result.Shield                  = GetXmlValue(obj, "Shield", baseObject.Shield);
            Result.MaxHull                 = GetXmlValue(obj, "MaxHull", baseObject.MaxHull);
            Result.MaxShield               = GetXmlValue(obj, "MaxShield", baseObject.MaxShield);
            Result.ShieldRegen             = GetXmlValue(obj, "ShieldRegen", baseObject.ShieldRegen);
            Result.MaxThrust               = GetXmlValue(obj, "MaxThrust", baseObject.MaxThrust);
            Result.TurnSpeed               = GetXmlValue(obj, "TurnSpeed", baseObject.TurnSpeed);
            Result.RateOfFire              = GetXmlValue(obj, "RateOfFire", baseObject.RateOfFire);
            Result.ShieldRebootProbability = (int)GetXmlValue(obj, "ShieldRebootProbability", baseObject.ShieldRebootProbability);
            Result.Texture                 = ResourceManager.Get <TextureResource>(GetXmlText(obj, "Texture", baseObject.Texture.Name));
            //Result.Hitbox = Hitbox.Automatic(Result.Texture, (int)Math.Max(2, Result.Scale * Result.Texture.Texture.height / 8));

            List <SpaceObject> hardpoints = GetXmlNested(obj, "Hardpoints", null);

            if (hardpoints != null && hardpoints.Count > 0)
            {
                Result.Hardpoints = new List <SpaceShipHardpoint>();
                foreach (SpaceObject o in hardpoints)
                {
                    SpaceShipHardpoint hp = o as SpaceShipHardpoint;
                    if (hp != null)
                    {
                        hp.Parent = Result;
                        hp.Depth += Result.Depth;
                        hp.Scale *= Result.Scale;
                        Result.Hardpoints.Add(hp);
                    }
                }
                Debug.WriteLine("Loaded hardpoints");
            }

            return(Result);
        }