Skip to content

Commit

Permalink
added a skipList parameter, release 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nolemretaW committed Jul 29, 2023
1 parent 2692eb6 commit 8195274
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
14 changes: 10 additions & 4 deletions Chroma.Shine.Examples/Raycasting/GameCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion Chroma.Shine/Chroma.Shine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<Description>Various utilities that make life easier but don't quite fit into the core of Chroma Framework.</Description>
<PackageReadmeFile>NuGet.md</PackageReadmeFile>
<PackageId>Chroma.Shine</PackageId>
<Version>0.3.0</Version>
<Version>0.3.1</Version>
<Author>vddCore, nolemretaW</Author>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Chroma-2D/Chroma.Shine</RepositoryUrl>
<PackageTags>gamedev;engine;2d;chroma;framework;sdl;opengl;game;xna;netcore</PackageTags>
<releaseNotes>Added a skipTags parameter to Raycast.Cast to specify tags to skip</releaseNotes>
</PropertyGroup>
</Project>
6 changes: 5 additions & 1 deletion Chroma.Shine/Physics/Raycast.cs
Original file line number Diff line number Diff line change
@@ -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 &&
Expand Down

0 comments on commit 8195274

Please sign in to comment.