コード例 #1
0
ファイル: SpaceShip.cs プロジェクト: StevenGann/spacegame
        public void Shoot()
        {
            double  error   = RNG.Next((int)Math.Sqrt(Math.Ceiling(Math.Clamp(shotHeat, 0, 100)))) - RNG.Next((int)Math.Sqrt(Math.Ceiling(Math.Clamp(shotHeat, 0, 100))));
            double  radians = (Angle - 90 + error) * (Math.PI / 180);
            Vector2 heading = new Vector2((float)Math.Cos(radians), (float)Math.Sin(radians));

            SpaceProjectile obj = new SpaceProjectile()
            {
                Texture  = ResourceManager.Get <TextureResource>(@"images\projectile\proton+"),
                Angle    = this.Angle,
                Velocity = heading * 50.0f,
                Location = this.Location + heading * 100.0f,//Location = RotateAroundPoint(this.Location - (this.TextureOffset), this.Location, this.Angle),
                Scale    = 0.5f,
                Faction  = this.Faction,
                Sender   = this
            };

            obj.Hitbox = new Hitbox((obj.Texture.Texture.width + obj.Texture.Texture.height) / 4);
            //obj.Hitbox = Hitbox.Automatic(obj.Texture, 1);

            GameManager.Add(obj);

            shotCooldown = 60 / RateOfFire;
            shotHeat    += 1;

            if (Hitbox == null && RNG.Next(100) < 10)
            {
                Hitbox = Hitbox.Automatic(Texture, (int)Math.Max(2, Scale * Texture.Texture.height / 8));
            }
        }
コード例 #2
0
        public static SpaceEffect FromXml(XmlResource Xml, SpaceEffect DstObject)
        {
            if (Xml == null)
            {
                throw new ArgumentNullException(nameof(Xml));
            }
            SpaceEffect Result = DstObject;

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

            XmlNode obj = Xml.Xml.LastChild;

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

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

            Result.Lifespan             = GetXmlValue(obj, "Lifespan", baseObject.Lifespan);
            Result.MaxParticles         = (int)GetXmlValue(obj, "MaxParticles", baseObject.MaxParticles);
            Result.MinParticles         = (int)GetXmlValue(obj, "MinParticles", baseObject.MinParticles);
            Result.ParticleSpawnRadius  = GetXmlValue(obj, "ParticleSpawnRadius", baseObject.ParticleSpawnRadius);
            Result.MaxParticleLife      = GetXmlValue(obj, "MaxParticleLife", baseObject.MaxParticleLife);
            Result.MinParticleLife      = GetXmlValue(obj, "MinParticleLife", baseObject.MinParticleLife);
            Result.MaxParticleScale     = GetXmlValue(obj, "MaxParticleScale", baseObject.MaxParticleScale);
            Result.MinParticleScale     = GetXmlValue(obj, "MinParticleScale", baseObject.MinParticleScale);
            Result.MaxParticleRotation  = GetXmlValue(obj, "MaxParticleRotation", baseObject.MaxParticleRotation);
            Result.MinParticleRotation  = GetXmlValue(obj, "MinParticleRotation", baseObject.MinParticleRotation);
            Result.MaxParticleAlpha     = GetXmlValue(obj, "MaxParticleAlpha", baseObject.MaxParticleAlpha);
            Result.MinParticleAlpha     = GetXmlValue(obj, "MinParticleAlpha", baseObject.MinParticleAlpha);
            Result.MaxParticleDirection = GetXmlValue(obj, "MaxParticleDirection", baseObject.MaxParticleDirection);
            Result.MinParticleDirection = GetXmlValue(obj, "MinParticleDirection", baseObject.MinParticleDirection);
            Result.MaxParticleAngle     = GetXmlValue(obj, "MaxParticleAngle", baseObject.MaxParticleAngle);
            Result.MinParticleAngle     = GetXmlValue(obj, "MinParticleAngle", baseObject.MinParticleAngle);
            Result.MaxParticleSpeed     = GetXmlValue(obj, "MaxParticleSpeed", baseObject.MaxParticleSpeed);
            Result.MinParticleSpeed     = GetXmlValue(obj, "MinParticleSpeed", baseObject.MinParticleSpeed);
            Result.ParticleFade         = GetXmlBool(obj, "ParticleFade", baseObject.ParticleFade);
            Result.ParticleDrag         = GetXmlValue(obj, "ParticleDrag", baseObject.ParticleDrag);
            Result.Children             = GetXmlChildrenEffect(obj, "ChildEffect", baseObject.Children);

            return(Result);
        }
コード例 #3
0
        public static SpaceObject FromXml(XmlResource Xml, SpaceObject DstObject)
        {
            if (Xml == null)
            {
                throw new ArgumentNullException(nameof(Xml));
            }
            SpaceObject Result = DstObject;

            if (DstObject == null)
            {
                Result = new SpaceObject();
            }
            Result.XmlSource = Xml.Name;
            XmlNode obj = Xml.Xml.LastChild;

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

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

            Result.Mass        = GetXmlValue(obj, "Mass", baseObject.Mass);
            Result.Depth       = GetXmlValue(obj, "Depth", baseObject.Depth);
            Result.AngularDrag = GetXmlValue(obj, "AngularDrag", baseObject.AngularDrag);
            Result.Drag        = GetXmlValue(obj, "Drag", baseObject.Drag);
            Result.Scale       = (float)GetXmlValue(obj, "Scale", baseObject.Scale);
            try
            {
                Result.Texture = ResourceManager.Get <TextureResource>(GetXmlText(obj, "Texture", @"images\ui\error"));
            }
            catch (KeyNotFoundException e)
            {
                Result.Texture = ResourceManager.Get <TextureResource>(@"images\ui\error");
                Result.Scale   = 1;
            }

            Result.tickScript    = GetXmlScript(obj, "Tick", baseObject.tickScript);
            Result.drawScript    = GetXmlScript(obj, "Draw", baseObject.drawScript);
            Result.spawnScript   = GetXmlScript(obj, "Spawn", baseObject.spawnScript);
            Result.destroyScript = GetXmlScript(obj, "Destroy", baseObject.destroyScript);

            return(Result);
        }
コード例 #4
0
        public static dynamic GetXmlScript(XmlNode Parent, string Name, dynamic Default)
        {
            if (Parent.HasChildNodes)
            {
                XmlNodeList children = Parent.ChildNodes;
                foreach (XmlNode node in children)
                {
                    if (node.Name.ToUpperInvariant() == Name.ToUpperInvariant() && node.Attributes.Count > 0)
                    {
                        XmlAttributeCollection attributes = node.Attributes;
                        foreach (XmlAttribute attribute in attributes)
                        {
                            if (attribute.Name.ToUpperInvariant() == "source".ToUpperInvariant())
                            {
                                ScriptResource script = ResourceManager.Get <ScriptResource>(attribute.Value);
                                script.Loaded = true;
                                return(script.Script);
                            }
                        }
                    }
                }
            }

            string scriptText = GetXmlText(Parent, Name, string.Empty);

            if (scriptText != string.Empty)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();

                if (Debug.GetFlag("DangerousScripts") != 1)
                {
                    List <string> blacklist = GameManager.ScriptBlacklist;
                    foreach (string s in blacklist)
                    {
                        if (scriptText.Contains(s))
                        {
                            watch.Stop();
                            Debug.WriteLine("%WARNING%Did not compile script because it contained \"" + s + "\" and DangerousScripts cvar is disabled.");
                            Debug.WriteLine("%WARNING%If you trust this script, use \"SET DANGEROUSSCRIPTS 1\" in the console or config file.");
                            return(Default);
                        }
                    }
                }

                dynamic result = CSScriptLib.CSScript.Evaluator.LoadMethod(scriptText);
                watch.Stop();
                Debug.WriteLine("%SUCCESS%Compiled script in " + watch.ElapsedMilliseconds + "ms");
                return(result);
            }

            return(Default);
        }
コード例 #5
0
ファイル: SpaceShipUnit.cs プロジェクト: StevenGann/spacegame
        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);
        }
コード例 #6
0
ファイル: SpaceShip.cs プロジェクト: StevenGann/spacegame
        public override void Destroy()
        {
            if (Hardpoints != null)
            {
                foreach (SpaceShipHardpoint hp in Hardpoints)
                {
                    if (hp.Active)
                    {
                        hp.Destroy();
                    }
                }
            }

            var o = SpaceEffect.FromXml(ResourceManager.Get <XmlResource>(@"xml\effect\base_explosion"), null);

            o.Location = Location;
            o.Velocity = Velocity;
            GameManager.Add(o);

            base.Destroy();
        }
コード例 #7
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);
        }
コード例 #8
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);
        }
コード例 #9
0
        private static List <SpaceEffect> GetXmlChildrenEffect(XmlNode Parent, string Name, List <SpaceEffect> Default)
        {
            int count = 0;
            List <SpaceEffect> result = new List <SpaceEffect>();

            if (Default != null)
            {
                foreach (SpaceEffect effect in Default)
                {
                    SpaceEffect newChild = new SpaceEffect();
                    result.Add(FromXml(ResourceManager.Get <XmlResource>(effect.XmlSource), newChild));
                }
                count = Default.Count;
            }

            if (Parent.HasChildNodes)
            {
                XmlNodeList children = Parent.ChildNodes;
                foreach (XmlNode node in children)
                {
                    if (node.Name.ToUpperInvariant() == Name.ToUpperInvariant())
                    {
                        SpaceEffect newChild = new SpaceEffect();
                        result.Add(FromXml(ResourceManager.Get <XmlResource>(node.InnerText), newChild));
                        count++;
                    }
                }
            }

            if (count > 0)
            {
                return(result);
            }

            return(null);
        }
コード例 #10
0
        public static void Tick(double Delta)
        {
            if (Debug.Enabled)
            {
                if (!Debug.ConsoleIsOpen)
                {
                    if (IsKeyPressed(KeyboardKey.KEY_SPACE))
                    {
                        Paused = !Paused;
                    }
                }
            }

            // =============================Camera Controls========================================

            float scrollAmount = GetMouseWheelMove();
            float a            = (float)Math.Abs(scrollAmount) * ViewScale * 100.0f;

            a = 0;
            const float maxZoom = 4;
            const float minZoom = 0.1f;

            if (scrollAmount > 0)
            {
                ViewScale *= 1.1f;
                if (ViewScale > maxZoom)
                {
                    ViewScale = maxZoom;
                }
                else
                {
                    ViewOffset -= new Vector2(a, a);
                }
            }
            if (scrollAmount < 0)
            {
                ViewScale *= 0.9f;
                if (ViewScale < minZoom)
                {
                    ViewScale = minZoom;
                }
                else
                {
                    ViewOffset += new Vector2(a, a);
                }
            }

            if (!Debug.ConsoleIsOpen)
            {
                if (IsKeyDown(KeyboardKey.KEY_W))
                {
                    ViewOffset += Vector2.UnitY * (5f + panBoost);
                }
                if (IsKeyDown(KeyboardKey.KEY_S))
                {
                    ViewOffset += Vector2.UnitY * -(5f + panBoost);
                }
                if (IsKeyDown(KeyboardKey.KEY_A))
                {
                    ViewOffset += Vector2.UnitX * (5f + panBoost);
                }
                if (IsKeyDown(KeyboardKey.KEY_D))
                {
                    ViewOffset += Vector2.UnitX * -(5f + panBoost);
                }
                if (!IsKeyDown(KeyboardKey.KEY_W) &&
                    !IsKeyDown(KeyboardKey.KEY_S) &&
                    !IsKeyDown(KeyboardKey.KEY_A) &&
                    !IsKeyDown(KeyboardKey.KEY_D))
                {
                    panBoost *= 0.90f;
                }
                else
                {
                    panBoost += 0.25f;
                    panBoost  = Math.Clamp(panBoost, 0, 10);
                }
            }

            if (!Paused || ((IsKeyDown(KeyboardKey.KEY_RIGHT) || IsKeyPressed(KeyboardKey.KEY_LEFT)) && !Debug.ConsoleIsOpen))
            {
                // ==================================Collisons=========================================
                while (bufferObjects.Count > 0)
                {
                    Instance.Objects.Add(bufferObjects.Dequeue());
                }

                IEnumerable <SpaceObject> projectiles = Instance.Objects.Where(o =>
                                                                               o is SpaceProjectile &&
                                                                               o.Active
                                                                               );

                IEnumerable <SpaceObject> ships = Instance.Objects.Where(o =>
                                                                         o is SpaceShip &&
                                                                         o.Active
                                                                         );

                foreach (SpaceObject projectile in projectiles)
                {
                    foreach (SpaceObject ship in ships)
                    {
                        if (projectile.Faction != ship.Faction &&
                            //Raylib.Raylib.Vector2Distance(projectile.Location, ship.Location) < ship.Texture.Texture.width / 2 &&
                            ship.CheckCollision(projectile.Hitbox, projectile.Location, projectile.Scale, (float)projectile.Angle) &&
                            (projectile as SpaceProjectile)?.Sender != ship
                            )
                        {
                            (ship as SpaceShip)?.Damage((double)(projectile as SpaceProjectile)?.Damage);

                            var o = SpaceEffect.FromXml(ResourceManager.Get <XmlResource>(@"xml\effect\base_impact"), null);
                            o.Location = projectile.Location;
                            o.Velocity = ship.Velocity;
                            GameManager.Add(o);

                            projectile.Active = false;
                        }
                    }
                }
                // ====================================================================================

                foreach (SpaceShipUnit unit in Instance.Units)
                {
                    unit.Tick(Delta);
                }

                for (int i = 0; i < Instance.Objects.Count; i++)
                {
                    Instance.Objects[i].Tick(Delta);
                }

                if (Instance.Objects.Count > PruneLimit)
                {
                    Prune();
                }
            }
        }
コード例 #11
0
        public static void Main()
        {
            ResourcesPath = System.IO.Directory.GetCurrentDirectory() + @"\..\..\..\..\resources\";

            /*
             * SetTargetFPS(120);
             * InitWindow(800, 600, "Loading");
             * BackgroundWorker loadingWorker = new BackgroundWorker();
             * loadingWorker.DoWork += LoadingWorker_DoWork;
             * loadingWorker.RunWorkerAsync();
             *
             * TextureResource bkg = null;
             * FontResource fnt = null;
             * Color bkgColor = Color.WHITE;
             * Color foreColor = Color.BLACK;
             * while (loadingWorker.IsBusy)
             * {
             *  if (ResourceManager.Instance != null && (bkg == null || fnt == null))
             *  {
             *      try { bkg = ResourceManager.GetTexture("_menu\\loading"); } catch { }
             *      try { fnt = ResourceManager.GetFont("Perfect_DOS_VGA_437_Win"); } catch { }
             *  }
             *
             *  BeginDrawing();
             *  Raylib.Raylib.ClearBackground(bkgColor);
             *  if (bkg != null)
             *  {
             *      Raylib.Raylib.DrawTexturePro(bkg.Texture, new Rectangle(0, 0, bkg.Texture.width, bkg.Texture.height), new Rectangle(0, 0, 800, 600), Vector2.Zero, 0.0f, Color.WHITE);
             *      bkgColor = Color.BLACK;
             *      foreColor = Color.WHITE;
             *  }
             *
             *  if (fnt != null) { Raylib.Raylib.DrawTextEx(fnt.Font, "Loading", new Vector2(20, 20), 16, 4, foreColor); }
             *  else { Raylib.Raylib.DrawText("Loading", 20, 20, 16, foreColor); }
             *  if (ResourceManager.Instance != null)
             *  {
             *      if (ResourceManager.Instance.Xml.Count > 0)
             *      {
             *          string txt = ResourceManager.Instance.Xml.Count + " XML files";
             *          if (fnt != null) { Raylib.Raylib.DrawTextEx(fnt.Font, txt, new Vector2(20, 50), 16, 4, foreColor); }
             *          else { Raylib.Raylib.DrawText(txt, 20, 50, 16, foreColor); }
             *      }
             *      if (ResourceManager.Instance.Fonts.Count > 0)
             *      {
             *          string txt = ResourceManager.Instance.Fonts.Count + " fonts";
             *          if (fnt != null) { Raylib.Raylib.DrawTextEx(fnt.Font, txt, new Vector2(20, 80), 16, 4, foreColor); }
             *          else { Raylib.Raylib.DrawText(txt, 20, 80, 16, foreColor); }
             *      }
             *      if (ResourceManager.Instance.Sounds.Count > 0)
             *      {
             *          string txt = ResourceManager.Instance.Sounds.Count + " sounds";
             *          if (fnt != null) { Raylib.Raylib.DrawTextEx(fnt.Font, txt, new Vector2(20, 110), 16, 4, foreColor); }
             *          else { Raylib.Raylib.DrawText(txt, 20, 110, 16, foreColor); }
             *      }
             *      if (ResourceManager.Instance.Scripts.Count > 0)
             *      {
             *          string txt = ResourceManager.Instance.Scripts.Count + " scripts";
             *          if (fnt != null) { Raylib.Raylib.DrawTextEx(fnt.Font, txt, new Vector2(20, 140), 16, 4, foreColor); }
             *          else { Raylib.Raylib.DrawText(txt, 20, 110, 16, foreColor); }
             *      }
             *      if (ResourceManager.Instance.Textures.Count > 0)
             *      {
             *          string txt = ResourceManager.Instance.Textures.Count + " textures";
             *          if (fnt != null) { Raylib.Raylib.DrawTextEx(fnt.Font, txt, new Vector2(20, 170), 16, 4, foreColor); }
             *          else { Raylib.Raylib.DrawText(txt, 20, 110, 16, foreColor); }
             *      }
             *  }
             *
             *  string line = Debug.ConsoleBuffer;
             *  if (fnt != null) { Raylib.Raylib.DrawTextEx(fnt.Font, line, new Vector2(20, 580), 16, 4, foreColor); }
             *  else { Raylib.Raylib.DrawText(line, 20, 580, 16, foreColor); }
             *  EndDrawing();
             * }
             * CloseWindow();
             */
            ResourceManager.Register(typeof(XmlResource), new string[] { ".xml" });
            ResourceManager.Register(typeof(ScriptResource), new string[] { ".cs" });
            ResourceManager.Load(ResourcesPath);
            GameManager.Instantiate();

            InitWindow((int)Math.Max(1024, Debug.GetFlag("ScreenWidth")), (int)Math.Max(768, Debug.GetFlag("ScreenHeight")), "SpaceGame");
            SetConfigFlags(ConfigFlag.FLAG_VSYNC_HINT | ConfigFlag.FLAG_MSAA_4X_HINT | ((Debug.GetFlag("Fullscreen") == 1) ? (ConfigFlag.FLAG_FULLSCREEN_MODE) : 0));

            for (int j = 0; j < 2; j++)
            {
                SpaceShipUnit unitEnemy = SpaceShipUnit.FromXml(ResourceManager.Get <XmlResource>(@"xml\unit\base_fighter_squadron"), null);
                unitEnemy.Location  = new Vector2(900, 900 - (j * 60));
                unitEnemy.Formation = new Formation();
                GameManager.Add(unitEnemy);
            }

            /*SpaceShip objPlayer = SpaceShip.FromXml(ResourceManager.Get<XmlResource>(@"xml\ship\base_cruiser"), null);
             * objPlayer.Faction = 1;
             * objPlayer.Location = new Vector2(1000, 300);
             * objPlayer.Hitbox = Hitbox.Automatic(objPlayer.Texture, (int)Math.Floor(objPlayer.Texture.Texture.height / 32.0));
             *
             * SpaceShipUnit unitPlayer = new SpaceShipUnit(objPlayer);
             * unitPlayer.UiImage = ResourceManager.Get<TextureResource>(@"images\thumbnail\kar ik vot 349");
             * GameManager.Add(unitPlayer);*/

            SpaceStructure station = new SpaceStructure()
            {
                Texture   = ResourceManager.Get <TextureResource>(@"images\planet\station2"),
                Location  = new Vector2(1500, 500),
                Scale     = 2.0f,
                Hitbox    = Hitbox.Automatic(ResourceManager.Get <TextureResource>(@"images\planet\station2"), 6),
                Faction   = 2,
                MaxHull   = 1000,
                Hull      = 1000,
                MaxShield = 1000,
                Shield    = 1000
            };

            GameManager.Add(station);

            const double TargetFps = 60;

            SetTargetFPS((int)TargetFps);
            while (!WindowShouldClose())
            {
                double fps   = Math.Clamp(GetFPS(), 25, 1000);
                double delta = TargetFps / fps;

                GameManager.Tick(delta);

                UiManager.Tick(delta);

                BeginDrawing();

                GameManager.Draw();

                UiManager.Draw();

                Debug.Draw();

                EndDrawing();

                ResourceManager.Cull();
            }

            CloseWindow();
        }
コード例 #12
0
        /// <summary>
        /// Draws the in-game console, debug overlay, and any text queued by the DrawText() method.
        /// Also manages keyboard handling for enabling or disabling the Debug interface and interacting with the in-game console.
        /// </summary>
        public static void Draw()
        {
            if (instance == null)
            {
                instance = new Debug();
            }

            if (IsKeyPressed(KeyboardKey.KEY_F1))
            {
                if (Enabled)
                {
                    Enabled       = false;
                    ConsoleIsOpen = false;
                }
                else
                {
                    Enabled = true;
                }
            }
            if (IsKeyPressed(KeyboardKey.KEY_GRAVE) && Enabled)
            {
                ConsoleIsOpen = !ConsoleIsOpen;
            }
            if (instance == null)
            {
                return;
            }
            if (!Enabled)
            {
                if (instance.consoleLines.Count > instance.consoleMaxLines * 2)
                {
                    for (int i = 0; i < instance.consoleMaxLines; i++)
                    {
                        instance.consoleLinesBuffer.Push(instance.consoleLines.Pop());
                    }
                    instance.consoleLines.Clear();
                    while (instance.consoleLinesBuffer.Count > 0)
                    {
                        instance.consoleLines.Push(instance.consoleLinesBuffer.Pop());
                    }
                    Debug.WriteLine("Trimmed debug console");
                }
            }
            if (!instance.initialized)
            {
                instance.font        = ResourceManager.Get <FontResource>(@"fonts\Perfect_DOS_VGA_437_Win").Font;
                instance.initialized = true;
            }
            // ------------------------------------------------------------------------------------
            framerate += Math.Max(GetFPS() + 0.5, 0);
            framerate  = Math.Round(framerate / 2);
            Debug.WriteOverlay("FPS: " + Convert.ToString((int)framerate, null));

            Vector2 p = new Vector2();

            lock (instance.shapeJobs)
            {
                while (instance.shapeJobs.Count > 0)
                {
                    DrawShapeJob j = instance.shapeJobs.Dequeue();
                    if (j.ShapeType == Shapes.Line)
                    {
                        Raylib.DrawLine(j.A, j.B, j.C, j.D, j.Color);
                    }
                    else if (j.ShapeType == Shapes.Rectangle)
                    {
                        Raylib.DrawRectangle(j.A, j.B, j.C, j.D, j.Color);
                    }
                }
            }

            if (instance.consoleSize > 0)
            {
                UpdateTerminal();

                DrawRectangle(0, 0, GetScreenWidth(), instance.consoleSize, instance.backgroundColor);
                DrawLineEx(new Vector2(0, instance.consoleSize + 1), new Vector2(GetScreenWidth(), instance.consoleSize + 1), 2, Color.WHITE);
                DrawLineEx(new Vector2(0, instance.consoleSize - instance.consoleFontSize - 1), new Vector2(GetScreenWidth(), instance.consoleSize - instance.consoleFontSize - 1), 1, Color.GRAY);
                DrawTextEx(instance.font, terminalBuffer.Insert(terminalCursor, showCursor ? "_" : " "), new Vector2(4, instance.consoleSize - instance.consoleFontSize - 1), instance.consoleFontSize, 1.0f, instance.foregroundColor);

                lock (instance.consoleLines)
                {
                    p.X = 4;
                    p.Y = instance.consoleSize - (instance.consoleFontSize * 2) - 2;
                    int count = instance.consoleLines.Count;
                    for (int i = 0; i < Math.Min(instance.consoleMaxLines, count); i++)
                    {
                        string line         = instance.consoleLines.Pop();
                        string originalLine = line;
                        if (i >= consoleLinesOffset)
                        {
                            string tag = "";
                            if (line.StartsWith("%", StringComparison.Ordinal))
                            {
                                string[] tokens = line.Split('%');
                                if (tokens.Length > 2)
                                {
                                    line = "";
                                    for (int j = 2; j < tokens.Length; j++)
                                    {
                                        line += ((j > 2) ? "%" : "") + tokens[j];
                                    }

                                    tag = tokens[1].ToUpper(System.Globalization.CultureInfo.InvariantCulture);
                                }
                            }

                            if (p.Y > -instance.consoleFontSize)
                            {
                                if (!string.IsNullOrEmpty(tag))
                                {
                                    Color c = instance.backgroundColorAlt;
                                    if (tag == "SUCCESS")
                                    {
                                        c = new Color(0, 255, 0, 64);
                                    }
                                    else if (tag == "WARNING")
                                    {
                                        c = new Color(255, 255, 0, 64);
                                    }
                                    else if (tag == "ERROR")
                                    {
                                        c = new Color(255, 0, 0, 64);
                                    }
                                    DrawRectangle((int)p.X - 2, (int)p.Y + 1, GetScreenWidth() - 2, instance.consoleFontSize - 1, c);
                                }
                                DrawTextEx(instance.font, line, p, instance.consoleFontSize, 1.0f, instance.foregroundColor);
                            }

                            p.Y -= instance.consoleFontSize;
                        }
                        instance.consoleLinesBuffer.Push(originalLine);
                    }
                    instance.consoleLines.Clear();
                    while (instance.consoleLinesBuffer.Count > 0)
                    {
                        instance.consoleLines.Push(instance.consoleLinesBuffer.Pop());
                    }
                }
            }

            if (ConsoleIsOpen && instance.consoleSize < instance.consoleMaxSize)
            {
                instance.consoleSize += 8;
            }
            if (!ConsoleIsOpen && instance.consoleSize > 0)
            {
                instance.consoleSize -= 8;
            }

            lock (instance.textJobs)
            {
                while (instance.textJobs.Count > 0)
                {
                    DrawTextJob j = instance.textJobs.Dequeue();
                    if (j.Y > instance.consoleSize)
                    {
                        p.X = j.X - 1;
                        p.Y = j.Y - 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);
                        p.X = j.X + 1;
                        p.Y = j.Y + 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);
                        p.X = j.X - 1;
                        p.Y = j.Y + 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);
                        p.X = j.X + 1;
                        p.Y = j.Y - 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);

                        p.X = j.X;
                        p.Y = j.Y;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, Color.BLACK);
                    }
                }
            }

            lock (instance.overlayLines)
            {
                p.Y = instance.consoleSize + (instance.consoleFontSize * 0.25f);
                p.X = 4;
                while (instance.overlayLines.Count > 0)
                {
                    string line = instance.overlayLines.Dequeue();
                    DrawRectangle((int)p.X - 2, (int)p.Y - 1, (int)((line.Length * instance.consoleFontSize * 0.66f) - 2), instance.consoleFontSize - 1, instance.outlineColor);
                    DrawTextEx(instance.font, line, p, instance.consoleFontSize, 1.0f, Color.BLACK);
                    p.Y += instance.consoleFontSize;
                }
            }

            if (instance.logLines.Count > 0)
            {
                using StreamWriter stream = new FileInfo(logfileName.Replace("#", "0")).AppendText();
                while (instance.logLines.Count > 0)
                {
                    stream.WriteLine(instance.logLines.Dequeue());
                }
            }
        }
コード例 #13
0
ファイル: SpaceShip.cs プロジェクト: StevenGann/spacegame
        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);
        }