public virtual void Draw() { if (!Active) { return; } //Raylib.Raylib.DrawTextureEx(Texture.Texture, Location, (float)Angle, 1.0f, Color.WHITE); if (!Initialized) { if (Texture.Loaded == false) { Debug.WriteLine("Loading " + Texture.Name); } TextureOffset = new Vector2(Texture.Texture.width / 2, Texture.Texture.height / 2); Initialized = true; } Vector2 loc = DrawLocation; if (Selected) { DrawCircleLines((int)loc.X, (int)loc.Y, Scale * ((Texture.Texture.width + Texture.Texture.height) / 4f) * GameManager.ViewScale, Color.GREEN); } //Raylib.Raylib.DrawTextureEx(Texture.Texture, RotateAroundPoint(Location - TextureOffset, Location, Angle), (float)Angle, 1.0f, Color.WHITE); DrawTexturePro( Texture.Texture, new Rectangle(0, 0, Texture.Texture.width, Texture.Texture.height), new Rectangle( loc.X, loc.Y, Texture.Texture.width * Scale * GameManager.ViewScale, Texture.Texture.height * Scale * GameManager.ViewScale), TextureOffset * Scale * GameManager.ViewScale, (float)Angle, Color.WHITE); if (drawScript != null) { drawScript.Draw(this); } if (Hitbox != null && Debug.Enabled && !Debug.ConsoleIsOpen && IsKeyDown(KeyboardKey.KEY_F3)) { Hitbox.Draw(loc, (float)(Angle * 0.0174533), GameManager.ViewScale * Scale); } }
public static Hitbox Automatic(TextureResource Texture, int Segments) { Hitbox result = new Hitbox(); float top = -1; float bottom = -1; byte t = 1; using (SixLabors.ImageSharp.Image <SixLabors.ImageSharp.PixelFormats.Rgba32> image = (SixLabors.ImageSharp.Image <SixLabors.ImageSharp.PixelFormats.Rgba32>)SixLabors.ImageSharp.Image.Load(Texture.Path)) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { if (image[x, y].A > t) { top = y; break; } } if (top >= 0) { break; } } for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { if (image[x, image.Height - (y + 1)].A > t) { bottom = image.Height - (y + 1); break; } } if (bottom >= 0) { break; } } float height = bottom - top; float increment = height / Segments; Vector2 offset = new Vector2(image.Width / 2, image.Height / 2) * -1; for (int i = 0; i < Segments; i++) { int y = (int)Math.Round(top + increment * i); float left = -1; float right = -1; float left_next = -1; float right_next = -1; for (int x = 0; x < image.Width; x++) { if (image[x, y].A > t) { left = x; break; } } for (int x = 0; x < image.Width; x++) { if (image[image.Width - (x + 1), y].A > t) { right = image.Width - (x + 1); break; } } for (int x = 0; x < image.Width; x++) { if (image[x, (int)Math.Round(y + increment)].A > t) { left_next = x; break; } } for (int x = 0; x < image.Width; x++) { if (image[image.Width - (x + 1), (int)Math.Round(y + increment)].A > t) { right_next = image.Width - (x + 1); break; } } Vector2[] verts = { new Vector2(left, y) + offset, new Vector2(right, y) + offset, new Vector2(left_next, y + increment) + offset, new Vector2(right, y) + offset, new Vector2(right_next, y + increment) + offset, new Vector2(left_next, y + increment) + offset, }; result.AddVertices(verts); } } return(result); }
public bool CheckCollision(Vector2 Location, float Angle, Hitbox Other, Vector2 OtherLocation, float Scale, float OtherScale, float OtherAngle) { if (Other.Radius > 0) // Other is circle { if (Radius > 0) // Both are circles { return(CheckCollisionCircles(OtherLocation, Other.Radius * OtherScale, Location, Radius * Scale)); } else //I'm a mesh and other is a circle { foreach (Triangle tri in Triangles) { if (CheckCollisionCircleTriangle( OtherLocation, Other.Radius, tri.A.Rotate(Angle) * Scale + Location, tri.B.Rotate(Angle) * Scale + Location, tri.C.Rotate(Angle) * Scale + Location )) { return(true); } } } } else // Other is a mesh { if (Radius > 0) // I'm a circle and other is a mesh { foreach (Triangle tri in Other.Triangles) { if (CheckCollisionCircleTriangle( Location, Radius, tri.A.Rotate(OtherAngle) * OtherScale + OtherLocation, tri.B.Rotate(OtherAngle) * OtherScale + OtherLocation, tri.C.Rotate(OtherAngle) * OtherScale + OtherLocation )) { return(true); } } } else // Both are meshes { foreach (Triangle tri in Triangles) { foreach (Triangle otherTri in Other.Triangles) { if (CheckCollisionTriangleTriangle( tri.A.Rotate(Angle) * Scale + Location, tri.B.Rotate(Angle) * Scale + Location, tri.C.Rotate(Angle) * Scale + Location, otherTri.A.Rotate(OtherAngle) * OtherScale + OtherLocation, otherTri.B.Rotate(OtherAngle) * OtherScale + OtherLocation, otherTri.C.Rotate(OtherAngle) * OtherScale + OtherLocation )) { return(true); } } } } } return(false); }
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(); }