The closest I could get is:
public void OnCollision(CollisionEventArgs collisionInfo)
{
Console.WriteLine(collisionInfo.Other);
}
Example result:
MonogameExtendedCollisionTest.CubeEntity
From this I can also get Position and Bounds, but I’d like to know more.
Would it be possible to access fields of an object we’re colliding with? Is there any other way we can tell whether we’re colliding with a wall or a bullet?
Hello Cube,
you can try to distinguish between the classes, which implement the ICollisionActor or you inherit from that interface
Solution 1:
public class Player : ICollisionActor
{
int health = 100;
public void OnCollision(CollisionEventArgs collisionInfo)
{
if(collisionInfo.Other is Shot)
{
var shot = collisionInfo.Other as Shot;
health -= shot.Damage;
}
}
}
public class Shot : ICollisionActor
{
public int Damage = 7;
bool isAlive = true;
public void OnCollision(CollisionEventArgs collisionInfo)
{
if(collisionInfo.Other is Player)
{
isAlive = false;
}
}
}
Solution 2:
public interface IExtendedCollisionActor : ICollisionActor
{
public int Health {get;set;}
public int Damage {get;set;}
}
public class Player : IExtendedCollisionActor
{
public int Health {get;set;} = 100;
public int Damage {get;set;} = 7;
bool isAlive = true;
public void OnCollision(CollisionEventArgs collisionInfo)
{
if(collisionInfo.Other is IExtendedCollisionActor)
{
var other = collisionInfo.Other as IExtendedCollisionActor;
Health -= other.Damage;
}
}
}
Solution 3:
Same as Solution 2, but instead of an Extended Interface, you can write an abstract class with default properties and default behaviour.
Edit: typo
Hey Peter,
I completely forgot you can do something like that, thanks a lot!
No Problem, i totally know that feeling