From 81952747b0ce91fa981635c2fd0be9d30250cf0a Mon Sep 17 00:00:00 2001 From: nolemretaW Date: Sat, 29 Jul 2023 20:29:57 +0200 Subject: [PATCH] added a skipList parameter, release 0.3.1 --- Chroma.Shine.Examples/Raycasting/GameCore.cs | 14 ++++++++++---- Chroma.Shine/Chroma.Shine.csproj | 3 ++- Chroma.Shine/Physics/Raycast.cs | 6 +++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Chroma.Shine.Examples/Raycasting/GameCore.cs b/Chroma.Shine.Examples/Raycasting/GameCore.cs index b1e5b7c..5e33e87 100644 --- a/Chroma.Shine.Examples/Raycasting/GameCore.cs +++ b/Chroma.Shine.Examples/Raycasting/GameCore.cs @@ -11,7 +11,7 @@ internal class GameCore : Game { private Log Log { get; } = LogManager.GetForCurrentAssembly(); - private Entity _e1, _e2, _e3; + private Entity _e1, _e2, _e3, _e4; private Vector2 _mousePos = Vector2.One; private Color _rayOriginColor = Color.Red; @@ -20,26 +20,31 @@ internal GameCore() { _e1 = new Entity { Position = new Vector2(120, 120) }; _e1.AttachCollider(new RectangleCollider(_e1, 32, 32) { Tag = "_e1" }); - + _e2 = new Entity { Position = new Vector2(220, 120) }; _e2.AttachCollider(new RectangleCollider(_e1, 32, 32) { Tag = "_e2" }); - + _e3 = new Entity { Position = new Vector2(320, 120) }; _e3.AttachCollider(new CircleCollider(_e3, 32) { Tag = "_e3" }); + + _e4 = new Entity { Position = new Vector2(420, 120) }; + _e4.AttachCollider(new RectangleCollider(_e1, 32, 32) { Tag = "_e4" }); } + protected override void Update(float delta) { _e1.Update(delta); _e2.Update(delta); _e3.Update(delta); + _e4.Update(delta); } protected override void FixedUpdate(float fixedDelta) { CollisionManager.Update(fixedDelta); RaycastHit hit; - if (Raycast.Cast(new Vector2(300, 350), Vector2.Normalize(_mousePos - new Vector2(300, 350)), out hit, Vector2.Distance(_mousePos, new Vector2(300, 350)))) + if (Raycast.Cast(new Vector2(300, 350), Vector2.Normalize(_mousePos - new Vector2(300, 350)), out hit, Vector2.Distance(_mousePos, new Vector2(300, 350)), new []{"_e4"})) { Log.Info($"Hit {hit.Collider.Tag} at {hit.Posiition.X}, {hit.Posiition.Y}"); _rayOriginColor = Color.Green; @@ -55,6 +60,7 @@ protected override void Draw(RenderContext context) _e1.Draw(context); _e2.Draw(context); _e3.Draw(context); + _e4.Draw(context); context.Rectangle(ShapeMode.Fill, 300, 350, 5, 5, _rayOriginColor); context.Line(new Vector2(300, 350), _mousePos, Color.Red); } diff --git a/Chroma.Shine/Chroma.Shine.csproj b/Chroma.Shine/Chroma.Shine.csproj index 8a318fc..0b632a1 100644 --- a/Chroma.Shine/Chroma.Shine.csproj +++ b/Chroma.Shine/Chroma.Shine.csproj @@ -18,10 +18,11 @@ Various utilities that make life easier but don't quite fit into the core of Chroma Framework. NuGet.md Chroma.Shine - 0.3.0 + 0.3.1 vddCore, nolemretaW git https://github.com/Chroma-2D/Chroma.Shine gamedev;engine;2d;chroma;framework;sdl;opengl;game;xna;netcore + Added a skipTags parameter to Raycast.Cast to specify tags to skip diff --git a/Chroma.Shine/Physics/Raycast.cs b/Chroma.Shine/Physics/Raycast.cs index 506cf90..f11fa72 100644 --- a/Chroma.Shine/Physics/Raycast.cs +++ b/Chroma.Shine/Physics/Raycast.cs @@ -1,18 +1,22 @@ using System; +using System.Linq; using System.Numerics; namespace Chroma.Physics { public static class Raycast { - public static bool Cast(Vector2 origin, Vector2 direction, out RaycastHit raycastHit, float maxDistance = 500f) + public static bool Cast(Vector2 origin, Vector2 direction, out RaycastHit raycastHit, float maxDistance = 500f, string[] skipTags = null) { + if (skipTags == null) + skipTags = new String[] { }; Vector2 currentpos = origin; for (int i = 0; i < maxDistance; i++) { currentpos += direction; foreach (Collider collider in CollisionManager._colliders) { + if (skipTags.Contains(collider.Tag)) continue; if (collider is RectangleCollider rc) { if (currentpos.X >= rc.Position.X &&