Skip to content

Commit

Permalink
Merge pull request #52 from DaveGreen-Games/contact-ended-system
Browse files Browse the repository at this point in the history
Contact Ended System Overhaul
  • Loading branch information
SoloByte authored Dec 15, 2024
2 parents 86178e2 + 064ffbb commit 1d7d377
Show file tree
Hide file tree
Showing 12 changed files with 675 additions and 849 deletions.
91 changes: 38 additions & 53 deletions Examples/Scenes/ExampleScenes/EndlessSpaceCollision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,23 +483,20 @@ public Bullet(Vector2 pos, Vector2 dir, BulletStats stats, ColorRgba color)

}

protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
foreach (var i in info)
{
if (i.Count <= 0) continue;
if (info.Count <= 0) return;

if (i.Other is AsteroidObstacle asteroid)
if (info.Other is AsteroidObstacle asteroid)
{
foreach (var collision in info)
{
foreach (var collision in i)
{
if(!collision.FirstContact) continue;
asteroid.Damage(Transform.Position, stats.Damage, new Vector2(0f));
effectTimer = effectDuration;
collider.Enabled = false;
Velocity = new(0f);
return;
}
if(!collision.FirstContact) continue;
asteroid.Damage(Transform.Position, stats.Damage, new Vector2(0f));
effectTimer = effectDuration;
collider.Enabled = false;
Velocity = new(0f);
return;
}
}
}
Expand Down Expand Up @@ -617,32 +614,28 @@ public Ship(Vector2 pos, float shipSize)
Health = MaxHp;
}

protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
foreach (var i in info)
{
if(i.Count <= 0 || i.Other is not AsteroidObstacle a) continue;
if(!i.Validate(out CollisionPoint combined)) continue;
if(info.Count <= 0 || info.Other is not AsteroidObstacle a) return;
if(!info.Validate(out CollisionPoint combined)) return;

a.Cut(GetCutShape());
a.Cut(GetCutShape());

if (collisionStunTimer <= 0f)
{
Health--;
if (Health <= 0)
{
collider.Enabled = false;
Kill();
OnKilled?.Invoke();
}
}
if (combined.Valid)
if (collisionStunTimer <= 0f)
{
Health--;
if (Health <= 0)
{
Velocity = combined.Normal * 3500;
collisionStunTimer = CollisionStunTime;
collisionRotationDirection = Rng.Instance.RandDirF();
collider.Enabled = false;
Kill();
OnKilled?.Invoke();
}

}
if (combined.Valid)
{
Velocity = combined.Normal * 3500;
collisionStunTimer = CollisionStunTime;
collisionRotationDirection = Rng.Instance.RandDirF();
}
}

Expand Down Expand Up @@ -926,19 +919,15 @@ public void Damage(Vector2 pos, float amount, Vector2 force)
}
}

protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
foreach (var i in info)
if(info.Count <= 0) return;
if(!info.Validate(out CollisionPoint combined)) return;
foreach (var collision in info)
{
if(i.Count <= 0) continue;
if(!i.Validate(out CollisionPoint combined)) continue;
foreach (var collision in i)
{
if(collision.Points == null || collision.Points.Count <= 0 || !collision.FirstContact)continue;
if(collision.Points == null || collision.Points.Count <= 0 || !collision.FirstContact)continue;

Velocity = Velocity.Reflect(combined.Normal);
}

Velocity = Velocity.Reflect(combined.Normal);
}
}
public void MoveTo(Vector2 newPosition)
Expand Down Expand Up @@ -1864,18 +1853,14 @@ public Destructor(Vector2 position, Vector2 direction, ColorRgba color)
Drag = 0f;
}

protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
if (info.Count <= 0) return;
foreach (var collisionInfo in info)
if (info.Other is AsteroidObstacle asteroid)
{
if(collisionInfo.Count <= 0) continue;
if (collisionInfo.Other is AsteroidObstacle asteroid)
if (info.FirstContact)
{
if (collisionInfo[0].FirstContact)
{
asteroid.Damage(Transform.Position, 10000000, Vector2.Zero);
}
asteroid.Damage(Transform.Position, 10000000, Vector2.Zero);
}
}

Expand Down
160 changes: 77 additions & 83 deletions Examples/Scenes/ExampleScenes/GameObjectHandlerExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ internal class Overlapper : CollisionObject
private CircleCollider circleCollider;

private int overlapCount = 0;
private readonly ColorRgba[] overlapColors = [Colors.Light, Colors.Text, Colors.Cold, Colors.Warm, Colors.Highlight, Colors.Special, Colors.Special2];
private readonly ColorRgba basicColor = Colors.Special;
private readonly ColorRgba overlapColor = Colors.Special2;

private const float contactStartedDuration = 0.5f;
private const float contactEndedDuration = 0.5f;
private float contactStartedTimer = 0f;
private float contactEndedTimer = 0f;

public Overlapper(Vector2 pos) : base(new Transform2D(pos, 0f, new Size(150, 0), 1f))
{
Expand All @@ -129,52 +135,57 @@ internal class Overlapper : CollisionObject
}


protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{

foreach (var colInfo in info)
if (info.Count > 0 && info.FirstContact)
{
if (colInfo.Count > 0)
contactStartedTimer = contactStartedDuration;
overlapCount++;
if (info.Other is BoundaryWall wall)
{
foreach (var collision in colInfo)
{
if (!collision.FirstContact) continue;

if (colInfo.Other is BoundaryWall wall)
{
overlapCount++;
Velocity = -(Transform.Position).Normalize() * Velocity.Length();
}
else
{
overlapCount++;
}
}
Velocity = -(Transform.Position).Normalize() * Velocity.Length();
}
}
}

protected override void CollisionEnded(List<OverlapInformation> info)
protected override void ContactEnded(CollisionObject other)
{
foreach (var overlapInfo in info)
contactEndedTimer = contactEndedDuration;
overlapCount--;
}

public override void Update(GameTime time, ScreenInfo game, ScreenInfo gameUi, ScreenInfo ui)
{
base.Update(time, game, gameUi, ui);
if (contactStartedTimer > 0)
{
foreach (var overlap in overlapInfo)
{
overlapCount--;
}
contactStartedTimer -= time.Delta;
if(contactStartedTimer <= 0) contactStartedTimer = 0;
}
if (contactEndedTimer > 0)
{
contactEndedTimer -= time.Delta;
if(contactEndedTimer <= 0) contactEndedTimer = 0;
}
}

public override void DrawGame(ScreenInfo game)
{
var c = circleCollider.GetCircleShape();
var colorIndex = 0;
if(overlapCount >= overlapColors.Length) colorIndex = overlapColors.Length - 1;
else if(overlapCount < 0) colorIndex = 0;
else colorIndex = overlapCount;

var color = overlapColors[colorIndex];
c.DrawLines(8f, color);

//animated radius
var contactStartedF = contactStartedTimer / contactStartedDuration;
contactStartedF = ShapeTween.CircOut(contactStartedF);
var radius = ShapeMath.LerpFloat(c.Radius, c.Radius * 1.5f, contactStartedF);
c = c.SetRadius(radius);

//animate thickness
var contactEndedF = contactEndedTimer / contactEndedDuration;
contactEndedF = ShapeTween.BounceIn(contactEndedF);
var thickness = ShapeMath.LerpFloat(8, 32, contactEndedF);

var color = overlapCount > 0 ? overlapColor : basicColor;
c.DrawLines(thickness, color);
}

public override bool HasLeftBounds(Rect bounds) => !bounds.OverlapShape(circleCollider.GetCircleShape());
Expand All @@ -184,10 +195,6 @@ public override void DrawGame(ScreenInfo game)
public override void DrawGameUI(ScreenInfo gameUi)
{
}
public override void FixedUpdate(GameTime fixedTime, ScreenInfo game, ScreenInfo gameUi, ScreenInfo ui)
{

}
}

internal class Ball : CollisionObject
Expand All @@ -212,22 +219,18 @@ internal class Ball : CollisionObject
Layer = SpawnAreaLayers.ObjectFlag;
}

protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
CollisionPoint p = new();
foreach (var colInfo in info)
if (info.Count > 0)
{
if (colInfo.Count > 0)
foreach (var collision in info)
{
foreach (var collision in colInfo)
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out CollisionPoint combined))
{
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out CollisionPoint combined))
{
// var cp = collision.Points.GetAverageCollisionPoint();
if (combined.Valid) p = p.Average(combined);
}
if (combined.Valid) p = p.Average(combined);
}
}
}
Expand Down Expand Up @@ -319,24 +322,21 @@ public override void FixedUpdate(GameTime fixedTime, ScreenInfo game, ScreenInfo
}


protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
CollisionPoint p = new();
foreach (var colInfo in info)
if (info.Count > 0)
{
if (colInfo.Count > 0)
foreach (var collision in info)
{
foreach (var collision in colInfo)
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out var combined, out var closest))
{
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out var combined, out var closest))
{
Transform = Transform.SetPosition(closest.Point);
Velocity = new();
Enabled = false;
deadTimer = 2f;
}
Transform = Transform.SetPosition(closest.Point);
Velocity = new();
Enabled = false;
deadTimer = 2f;
}
}
}
Expand Down Expand Up @@ -481,22 +481,19 @@ private void OnEnteredGameArea()
}


protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
CollisionPoint p = new();
foreach (var colInfo in info)
if (info.Count > 0)
{
if (colInfo.Count > 0)
foreach (var collision in info)
{
foreach (var collision in colInfo)
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out var combined, out var closest))
{
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out var combined, out var closest))
{
// var cp = collision.Points.GetAverageCollisionPoint();
if (combined.Valid) p = p.Average(combined);
}
// var cp = collision.Points.GetAverageCollisionPoint();
if (combined.Valid) p = p.Average(combined);
}
}
}
Expand Down Expand Up @@ -573,22 +570,19 @@ internal class Bird : CollisionObject
Layer = SpawnAreaLayers.ObjectFlag;
}

protected override void Collision(List<CollisionInformation> info)
protected override void Collision(CollisionInformation info)
{
CollisionPoint p = new();
foreach (var colInfo in info)
if (info.Count > 0)
{
if (colInfo.Count > 0)
foreach (var collision in info)
{
foreach (var collision in colInfo)
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out CollisionPoint combined))
{
if(!collision.FirstContact) continue;
if(collision.Points == null) continue;
if (collision.Validate(out CollisionPoint combined))
{
// var cp = collision.Points.GetAverageCollisionPoint();
if (combined.Valid) p = p.Average(combined);
}
// var cp = collision.Points.GetAverageCollisionPoint();
if (combined.Valid) p = p.Average(combined);
}
}
}
Expand Down
Loading

0 comments on commit 1d7d377

Please sign in to comment.