//------------------------------------------------------------------------------------------------------------------------ // Add() //------------------------------------------------------------------------------------------------------------------------ public void Add(GameObject gameObject) { if (collisionLoopActive) { throw new Exception("Cannot call AddChild for gameobjects during OnCollision - use LateAddChild instead."); } if (gameObject.collider != null && !colliderList.Contains(gameObject)) { colliderList.Add(gameObject); } MethodInfo info = gameObject.GetType().GetMethod("OnCollision", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (info != null) { CollisionDelegate onCollision = (CollisionDelegate)Delegate.CreateDelegate(typeof(CollisionDelegate), gameObject, info, false); if (onCollision != null && !_collisionReferences.ContainsKey(gameObject)) { ColliderInfo colliderInfo = new ColliderInfo(gameObject, onCollision); _collisionReferences[gameObject] = colliderInfo; activeColliderList.Add(colliderInfo); } } else { validateCase(gameObject); } }
private void RemoveCollisions(BaseState state) { if (state != null) { collisionEnter -= state.OnCollisionEnter; collisionStay -= state.OnCollisionStay; collisionExit -= state.OnCollisionExit; } }
private void SetCollisions(BaseState state) { if (state != null) { collisionEnter += state.OnCollisionEnter; collisionStay += state.OnCollisionStay; collisionExit += state.OnCollisionExit; } }
public static void RegisterCallback(String Type1, String Type2, CollisionDelegate Callback) { if (!Callbacks.ContainsKey(Type1)) { Callbacks.Add(Type1, new Dictionary <string, LinkedList <CollisionDelegate> >()); } if (!Callbacks[Type1].ContainsKey(Type2)) { Callbacks[Type1].Add(Type2, new LinkedList <CollisionDelegate>()); } Callbacks[Type1][Type2].AddLast(Callback); }
static public bool IsColliding <TShape, TOther>(CollisionDelegate <TShape, TOther> collisionDelegate, ICollider <TShape> shape, ICollider <TOther> other, out Collision collision) where TOther : IShape where TShape : IShape { if (collisionDelegate(shape.Shape, other.Shape, out Vector2 correction)) { collision = new Collision { Sender = shape, OtherCollider = other, Correction = correction }; return(true); } collision = new Collision(); return(false); }
private void OnTriggerEnter2D(Collider2D collision) { try{ CollisionBox cb = collision.GetComponent <CollisionBox>(); if (cb.owner != owner && collision.GetComponentInParent <BaseCharacter>() != null) { if (collision.gameObject.layer == LayerMask.NameToLayer("Hurt") && collided == false) { collided = true; if (del == null) { del = () => { Debug.Log(this.name + " collided with: " + collision.name); BaseCharacter player_hit = collision.gameObject.GetComponentInParent <BaseCharacter>(); player_hit._sm.currentState.FireCustomEvent(new EventList.HitEvent(player_hit, properties, launch_direction)); owner._stamina.Siphon(player_hit, CalculateDamage()); player_hit.sfxPlayer.Play(Services.SFXLibrary.GetSFX(SFXLibrary.SFXTags.Impact)); Camera.main.GetComponent <Cam>().ShakeCamera(.65f, 15); if (player_hit.staminaSystem.GetCurrentMaxStamina() > 2) { owner.staminaSystem.Add(); player_hit.staminaSystem.Remove(); } else { player_hit.staminaSystem.SpendOrbs(1); } }; } del(); } } } catch { } }
//------------------------------------------------------------------------------------------------------------------------ // Add() //------------------------------------------------------------------------------------------------------------------------ public void Add(GameObject gameObject) { if (gameObject.collider != null) { colliderList.Add(gameObject); } MethodInfo info = gameObject.GetType().GetMethod("OnCollision", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (info != null) { CollisionDelegate onCollision = (CollisionDelegate)Delegate.CreateDelegate(typeof(CollisionDelegate), gameObject, info, false); if (onCollision != null) { ColliderInfo colliderInfo = new ColliderInfo(gameObject, onCollision); _collisionReferences[gameObject] = colliderInfo; activeColliderList.Add(colliderInfo); } } else { validateCase(gameObject); } }
// Use this for initialization void Awake() { _eye = GetComponent<SightEye> (); Initialize<GrupperStates>(); //Change to our first state ChangeState(GrupperStates.Searching); agroRange = agroRangeObj.AddComponent <CollisionDelegate>(); agroRange.CollideWithPlayer += () => agro = true; agroRange.ExitPlayerCollision += () => agro = false; _agroCollider = agroRangeObj.GetComponent<SphereCollider> (); _agroColliderRange = new Vector3 (_agroCollider.radius, _agroCollider.radius, _agroCollider.radius); closeRange = closeRangeObj.AddComponent <CollisionDelegate>(); closeRange.CollideWithPlayer += () => close = true; closeRange.ExitPlayerCollision += () => close = false; lightCloseRange = closeRangeObj.AddComponent <CollisionDelegate>(); lightCloseRange.tag = "Player"; lightCloseRange.CollideWithPlayer += () => lightClose = true; lightCloseRange.ExitPlayerCollision += () => lightClose = false; lightAgroRange = agroRangeObj.AddComponent <CollisionDelegate>(); lightAgroRange.tag = "Player"; lightAgroRange.CollideWithPlayer += () => lightAgro = true; lightAgroRange.ExitPlayerCollision += () => lightAgro = false; attackRange = attackAreaObj.AddComponent <CollisionDelegate>(); attackRange.tag = "Player"; attackRange.CollideWithPlayer += () => canAttack = true; attackRange.ExitPlayerCollision += () => canAttack = false; rb = GetComponent<Rigidbody>(); _player = FindObjectOfType<HeroStateMachine> ().gameObject; heroRaycaster.target = _player; heroRaycaster.mask = mask; }
//------------------------------------------------------------------------------------------------------------------------ // ColliderInfo() //------------------------------------------------------------------------------------------------------------------------ public ColliderInfo(GameObject gameObject, CollisionDelegate onCollision) { this.gameObject = gameObject; this.onCollision = onCollision; }
public static bool TryIntersectBoxColliders(PhysicsObject2D_42 first, PhysicsObject2D_42 second, out Vector2 normal) { var fp = first.transform.position; var sp = second.transform.position; var fp2 = new Vector2(fp.x, fp.y); var fp3 = new Vector2(sp.x, sp.y); var a = fp2 - first.size / 2; var b = fp2 + first.size / 2; var c = fp3 - second.size / 2; var d = fp3 + second.size / 2; normal = Vector2.zero; if (!(a.x > d.x || c.x > b.x || a.y > d.y || c.y > b.y)) { if (first.isTrigger || second.isTrigger) { return(true); } var min = float.MaxValue; CollisionDelegate cd = null; if (a.x > c.x && a.x <= d.x && b.x > d.x && d.x - a.x < min) { min = d.x - a.x; cd = OnRightCollision; } if (c.x > a.x && c.x <= b.x && d.x > b.x && b.x - c.x < min) { min = b.x - c.x; cd = OnLeftCollision; } if (a.y > c.y && a.y <= d.y && b.y > d.y && d.y - a.y < min) { min = d.y - a.y; cd = OnTopCollision; } if (c.y > a.y && c.y <= b.y && d.y > b.y && b.y - c.y < min) { min = b.y - c.y; cd = OnBotCollision; } cd?.Invoke(out normal); return(true); } return(false); void OnRightCollision(out Vector2 n) { if (!first.isStatic) { fp.x = sp.x + (first.size.x + second.size.x) / 2 + Threeshold; first.transform.position = fp; } else if (!second.isStatic) { sp.x = fp.x - (first.size.x + second.size.x) / 2 - Threeshold; second.transform.position = sp; } n = Vector2.right; } void OnLeftCollision(out Vector2 n) { if (!first.isStatic) { fp.x = sp.x - (first.size.x + second.size.x) / 2 - Threeshold; first.transform.position = fp; } else if (!second.isStatic) { sp.x = fp.x + (first.size.x + second.size.x) / 2 + Threeshold; second.transform.position = sp; } n = Vector2.left; } void OnTopCollision(out Vector2 n) { if (!first.isStatic) { fp.y = sp.y + (first.size.y + second.size.y) / 2 + Threeshold; first.transform.position = fp; } else if (!second.isStatic) { sp.y = fp.y - (first.size.y + second.size.y) / 2 - Threeshold; second.transform.position = sp; } n = Vector2.up; } void OnBotCollision(out Vector2 n) { if (!first.isStatic) { fp.y = sp.y - (first.size.y + second.size.y) / 2 - Threeshold; first.transform.position = fp; } else if (!second.isStatic) { sp.y = fp.y + (first.size.y + second.size.y) / 2 + Threeshold; second.transform.position = sp; } n = Vector2.down; } }
public static bool UnregisterCallback(String Type1, String Type2, CollisionDelegate Callback) { return Callbacks[Type1][Type2].Remove(Callback); }
public static void RegisterCallback(String Type1, String Type2, CollisionDelegate Callback) { if (!Callbacks.ContainsKey(Type1)) { Callbacks.Add(Type1, new Dictionary<string, LinkedList<CollisionDelegate>>()); } if (!Callbacks[Type1].ContainsKey(Type2)) { Callbacks[Type1].Add(Type2, new LinkedList<CollisionDelegate>()); } Callbacks[Type1][Type2].AddLast(Callback); }
// Use this for initialization void Start() { spawnTime = Time.time; _lightRange = agroRangeObj.AddComponent <CollisionDelegate>(); _lightRange.tag = "light"; _lightRange.CollideWithPlayer += () => lightClose = true; _lightRange.ExitPlayerCollision += () => lightClose = false; _agroRange = agroRangeObj.AddComponent <CollisionDelegate>(); _agroRange.tag = "Player"; _agroRange.CollideWithPlayer += () => agroClose = true; _agroRange.ExitPlayerCollision += () => agroClose = false; _agroCollider = agroRangeObj.GetComponent<SphereCollider> (); _agroColliderRange = new Vector3 (_agroCollider.radius, _agroCollider.radius, _agroCollider.radius); }
public void SetCollisionExitTrigger(CollisionDelegate cDelegate) { mCollisionEnterDelegate += cDelegate; }
static public bool IsShapeCollidingGrid <TShape>(CollisionDelegate <TShape, TopLeftRectangle> rectangleCollisionDelegate, ICollider <TShape> shapeCollider, IGridCollider gridCollider, out Collision collision) where TShape : IShape { IEnumerable <int[]> shapeGridBox = gridCollider.Grid.IndexIntersection( (x, y) => rectangleCollisionDelegate(y, x, out _), shapeCollider.Shape, (i, j) => gridCollider.IsCollidableCase(shapeCollider, i, j)); foreach (int[] indexes in shapeGridBox) { int i = indexes[0]; int j = indexes[1]; var rectangle = new TopLeftRectangle(gridCollider.Grid.ToWorldPoint(i, j), gridCollider.Grid.Delta); bool colliding = false; for (int x = -1; x <= 1 && !colliding; x++) { for (int y = -1; y <= 1 && !colliding; y++) { if ((x + y) % 2 == 0) { continue; } if (!gridCollider.Grid.ContainsPoint(i + y, j + x) || !gridCollider.IsCollidableCase(shapeCollider, i + y, j + x)) { continue; } var otherCase = new TopLeftRectangle(gridCollider.Grid.ToWorldPoint(i + y, j + x), gridCollider.Grid.Delta); if (!rectangleCollisionDelegate(shapeCollider.Shape, otherCase, out _)) { continue; } rectangle = new TopLeftRectangle { Position = (gridCollider.Grid.ToWorldPoint(i, j) + gridCollider.Grid.ToWorldPoint(i + y, j + x)) * 0.5f, Size = gridCollider.Grid.Delta + gridCollider.Grid.Delta.Multiply(System.Math.Abs(x), System.Math.Abs(y)) }; colliding = true; } } if (!rectangleCollisionDelegate(shapeCollider.Shape, rectangle, out Vector2 correction)) { continue; } collision = new Collision { Sender = shapeCollider, OtherCollider = gridCollider, Correction = correction }; return(true); } collision = new Collision(); return(false); }
public static bool UnregisterCallback(String Type1, String Type2, CollisionDelegate Callback) { return(Callbacks[Type1][Type2].Remove(Callback)); }
// Use this for initialization void Awake() { Initialize<GrupperStates>(); //Change to our first state ChangeState(GrupperStates.Searching); agroRange = agroRangeObj.AddComponent <CollisionDelegate>(); agroRange.CollideWithPlayer += () => agro = true; agroRange.ExitPlayerCollision += () => agro = false; _agroCollider = agroRangeObj.GetComponent<SphereCollider> (); _agroColliderRange = new Vector3 (_agroCollider.radius, _agroCollider.radius, _agroCollider.radius); closeRange = closeRangeObj.AddComponent <CollisionDelegate>(); closeRange.CollideWithPlayer += () => close = true; closeRange.ExitPlayerCollision += () => close = false; lightCloseRange = closeRangeObj.AddComponent <CollisionDelegate>(); lightCloseRange.tag = "Player"; lightCloseRange.CollideWithPlayer += () => lightClose = true; lightCloseRange.ExitPlayerCollision += () => lightClose = false; lightAgroRange = agroRangeObj.AddComponent <CollisionDelegate>(); lightAgroRange.tag = "Player"; lightAgroRange.CollideWithPlayer += () => lightAgro = true; lightAgroRange.ExitPlayerCollision += () => lightAgro = false; attackRange = attackAreaObj.AddComponent <CollisionDelegate>(); attackRange.tag = "Player"; attackRange.CollideWithPlayer += () => canAttack = true; attackRange.ExitPlayerCollision += () => canAttack = false; rb = GetComponent<Rigidbody>(); }