/// <summary> /// Sets up this entitys event listener and collision polygon when called. /// </summary> public void InitializeCollision() { _collisionPolygon = new CollisionRectangle(new Transformation(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f), 0.0f, 0.0f); _collisionPolygon.MetaData = this; _collisionPolygon.PreProcessingDelegate += new CollisionProcessingNotificationDelegate(PreCollisionProcessing); _collisionPolygon.PostProcessingDelegate += new CollisionProcessingNotificationDelegate(PostCollisionProcessing); CollisionManager.AttachPolygon(_collisionPolygon); }
/// <summary> /// Called by this entitys collision polygon when this entity touches another. /// </summary> /// <param name="aPolygon">Entity that touched.</param> /// <param name="bPolygon">Entity that was touched.</param> protected void OnTouch(CollisionPolygon aPolygon, CollisionPolygon bPolygon) { if (_process == null || _process.Process == null || aPolygon.MetaData == null || bPolygon.MetaData == null || _process.Process == null || _enabled == false) return; _process.Process[0].PassParameter(new SceneNodeScriptObject((SceneNode)aPolygon.MetaData)); _process.Process[0].PassParameter(new SceneNodeScriptObject((SceneNode)bPolygon.MetaData)); _process.Process[0].InvokeFunction("OnTouch", true, false); //base.OnTouch(aPolygon, bPolygon); }
/// <summary> /// Resets this entity to the state it was in when it was created. /// </summary> public override void Reset() { base.Reset(); if (_collisionPolygon != null) DeinitializeCollision(); _forceVisibility = false; _forceBoundingBoxVisibility = false; _forceCollisionBoxVisibility = false; _visible = true; _enabled = true; _solid = false; _event = ""; _renderEventLines = false; _eventNodes.Clear(); _eventLineColor = unchecked((int)0xFFFF0000); _image = null; _mesh = null; _frame = 0; _renderMode = EntityRenderMode.Rectangle; _boundingRectangle = new Rectangle(0, 0, 16, 16); _collisionRectangle = new Rectangle(0, 0, 16, 16); _color = unchecked((int)0xFFFFFFFF); _blendMode = BlendMode.Alpha; _text = ""; _bitmapFont = null; _renderBoundingBox = false; _renderCollisionBox = false; _renderSizingPoints = false; _boundingBoxColor = unchecked((int)0xFF0000FF); _collisionBoxColor = unchecked((int)0xFF666666); _sizingPointsColor = unchecked((int)0xFFFFFFFF); _sizingPointsSize = 5; _collisionPolygon = null; _depthLayer = 0; _depthMode = EntityDepthMode.SubtractCollisionBoxBottom; _triggered = false; _shader = null; }
/// <summary> /// Removes this entitys event listener and collision polygon when called. /// </summary> public void DeinitializeCollision() { if (_collisionPolygon == null) return; CollisionManager.DetachPolygon(_collisionPolygon); _collisionPolygon.PreProcessingDelegate = null; _collisionPolygon.PostProcessingDelegate = null; _collisionPolygon.OnEnterDelegate = null; _collisionPolygon.OnLeaveDelegate = null; _collisionPolygon.OnTouchDelegate = null; _collisionPolygon.OnCollidingDelegate = null; _collisionPolygon.MetaData = null; _collisionPolygon = null; }
/// <summary> /// Checks if this polygons is is colliding with a given polygon. /// </summary> /// <param name="polygon">Polygon to check collision against.</param> /// <returns>True if polygons are colliding, else false.</returns> public abstract bool HitTest(CollisionPolygon polygon);
/// <summary> /// Attempts to respond to a collision by moving this polygon out /// of the given polygon. /// </summary> /// <param name="polygon">Polygon that this polygon is currently penetrating.</param> public abstract void RespondToCollision(CollisionPolygon polygon);
/// <summary> /// Copys all the data contained in this collision polygon to another collision polygon. /// </summary> /// <param name="polygon">Collision polygon to copy data into.</param> public virtual void CopyTo(CollisionPolygon polygon) { polygon._onEnterDelegate = _onEnterDelegate; polygon._onLeaveDelegate = _onLeaveDelegate; polygon._onTouchDelegate = _onTouchDelegate; polygon._onCollidingDelegate = _onCollidingDelegate; foreach (CollisionPolygon subPolygon in _enteredList) polygon._enteredList.Add(subPolygon); // foreach (CollisionPolygon subPolygon in _touchedList) // polygon._touchedList.Add(subPolygon); polygon._solid = _solid; polygon._transformation = _transformation; polygon._layers = new int[_layers.Length]; for (int i = 0; i < _layers.Length; i++) polygon._layers[i] = _layers[i]; }
/// <summary> /// Attachs a given collision polygon to this manager. /// </summary> /// <param name="polygon">Collision polygon to attach.</param> public static void AttachPolygon(CollisionPolygon polygon) { if (_polygonList.Contains(polygon)) return; _polygonList.Add(polygon); #if USE_OCTTREE AddToOctTree(polygon); #endif }
/// <summary> /// Adds the polygon to the oct-tree. /// </summary> /// <param name="polygon">Collision polygon to attach.</param> private static void AddToOctTree(CollisionPolygon polygon) { _octTreeAdditions.Add(polygon); }
/// <summary> /// Adds the polygon to the oct-tree. /// </summary> /// <param name="polygon">Collision polygon to detach.</param> private static void RemoveFromOctTree(CollisionPolygon polygon) { _octTreeRemovals.Add(polygon); }
/// <summary> /// Checks for collision against a given polygon and all the other polygons /// attached to this collision manager. /// </summary> /// <param name="aPolygon">Polygon to check collision against.</param> /// <returns>True if a collision exists, else false.</returns> public static bool HitTest(CollisionPolygon aPolygon) { if (aPolygon.Solid == false) return false; foreach (CollisionPolygon polygon in _polygonList) if (aPolygon != polygon && polygon.HitTest(aPolygon) == true && polygon.Solid == true) return true; return false; }
/// <summary> /// Detachs a given collision polygon to this manager. /// </summary> /// <param name="polygon">Collision polygon to detach.</param> public static void DetachPolygon(CollisionPolygon polygon) { _polygonList.Remove(polygon); #if USE_OCTTREE RemoveFromOctTree(polygon); #endif }
/// <summary> /// Attempts to respond to a collision by moving this rectangle out /// of the given polygon. /// </summary> /// <param name="polygon">Polygon that this rectangle is currently penetrating.</param> public override void RespondToCollision(CollisionPolygon polygon) { if (polygon is CollisionRectangle) RespondToRectangleCollision(polygon as CollisionRectangle); else return; }
/// <summary> /// Checks if this polygons is is colliding with a given polygon. /// </summary> /// <param name="polygon">Polygon to check collision against.</param> /// <returns>True if polygons are colliding, else false.</returns> public override bool HitTest(CollisionPolygon polygon) { if (polygon is CollisionRectangle) { CollisionRectangle collisionRectangle = polygon as CollisionRectangle; if (collisionRectangle._width == 0 || collisionRectangle._height == 0 || _width == 0 || _height == 0) return false; int right1 = (int)(collisionRectangle.Transformation.X) + (int)(collisionRectangle.BoundingWidth * Math.Abs(collisionRectangle.Transformation.ScaleX)); int bottom1 = (int)(collisionRectangle.Transformation.Y) + (int)(collisionRectangle.BoundingHeight * Math.Abs(collisionRectangle.Transformation.ScaleY)); int right2 = (int)(_transformation.X) + (int)(_width * Math.Abs(_transformation.ScaleX)); int bottom2 = (int)(_transformation.Y) + (int)(_height * Math.Abs(_transformation.ScaleY)); return (!((bottom1 <= (int)(_transformation.Y)) || ((int)(collisionRectangle.Transformation.Y) >= bottom2) || (right1 <= (int)(_transformation.X)) || ((int)(collisionRectangle.Transformation.X) >= right2))); } return false; }
/// <summary> /// Copys all the data contained in this collision polygon to another collision polygon. /// </summary> /// <param name="polygon">Collision polygon to copy data into.</param> public override void CopyTo(CollisionPolygon polygon) { CollisionRectangle rectanglePolygon = polygon as CollisionRectangle; if (rectanglePolygon == null) return; base.CopyTo(polygon); rectanglePolygon._width = _width; rectanglePolygon._height = _height; }