/// <summary> /// Create a new MouseHandler for the given target. /// </summary> /// <param name="target">Target.</param> public MouseHandler (GameObject target) { _target = target; _game = _target.game; _game.OnAfterStep += HandleOnStep; _lastX = Input.mouseX; _lastY = Input.mouseY; }
//------------------------------------------------------------------------------------------------------------------------ // Remove() //------------------------------------------------------------------------------------------------------------------------ public void Remove(GameObject gameObject) { colliderList.Remove (gameObject); if (_collisionReferences.ContainsKey (gameObject)) { ColliderInfo colliderInfo = _collisionReferences [gameObject]; activeColliderList.Remove (colliderInfo); _collisionReferences.Remove (gameObject); } }
//------------------------------------------------------------------------------------------------------------------------ // GetCurrentCollisions() //------------------------------------------------------------------------------------------------------------------------ public GameObject[] GetCurrentCollisions (GameObject gameObject) { List<GameObject> list = new List<GameObject>(); for (int j=colliderList.Count-1; j>=0; j--) { if (j >= colliderList.Count) continue; //fix for removal in loop GameObject other = colliderList[j]; if (gameObject != other) { if (gameObject.HitTest(other)) { list.Add(other); } } } return list.ToArray(); }
public DebugMouseEvent(GameObject gameObject) { _gameObject = gameObject; _ballHandler = new MouseHandler(_gameObject); _ballHandler.OnMouseClick += OnMouseEvent; _ballHandler.OnMouseDown += OnMouseEvent; _ballHandler.OnMouseDownOnTarget += OnMouseEvent; //_ballHandler.OnMouseMove += OnMouseEvent; //_ballHandler.OnMouseMoveOnTarget += OnMouseEvent; _ballHandler.OnMouseOffTarget += OnMouseEvent; _ballHandler.OnMouseOverTarget += OnMouseEvent; _ballHandler.OnMouseUp += OnMouseEvent; _ballHandler.OnMouseUpOnTarget += OnMouseEvent; }
/// <summary> /// responds to a collision when it's encountered /// </summary> /// <returns><c>true</c>, If player should be allowed through, <c>false</c> otherwise.</returns> /// <param name="other">Other.</param> /// <param name="moveX">Move x.</param> /// <param name="moveY">Move y.</param> bool handleCollision(GameObject other, float moveX, float moveY) { if (other is Tile) { Tile tile = other as Tile; if (tile.currentFrame == 2) { //Slightly darken ground tiles you touch tile.SetColor (0.9f,0.9f,0.9f); //Slightly force the tile downward if(moveY >= 16) tile.y += 3; return resolveCollision (other as Sprite, moveX, moveY); } } return true; }
/// <summary> /// This draws the highscore and adds it as a child of the gameobject it is called from /// </summary> /// <param name="pSize"> /// The pSize represents the size of the drawn highscorelist, /// With a value between 1 and 20. /// </param> public void DrawHighScore(float pSize, GameObject pParent) { if (pSize < 1) { pSize = 1; } if (pSize > 20) { pSize = 20; } Canvas highscorecv = new Canvas(1028, (int)pSize * 30); pParent.AddChild(highscorecv); Font font = new Font("Arial", pSize, FontStyle.Regular); Brush brush = new SolidBrush(Color.White); int placementValue = 0; for (int i = 9; i >= 0; i--) { placementValue++; PointF posI = new PointF(0, placementValue * pSize * 2); string message = _highScoreNames[i] + " " + _highScores[i]; highscorecv.graphics.DrawString(message, font, brush, posI); } }
//------------------------------------------------------------------------------------------------------------------------ // Add() //------------------------------------------------------------------------------------------------------------------------ public void Add(GameObject gameObject) { 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); } }
//------------------------------------------------------------------------------------------------------------------------ // GetGameObjectCollisions() //------------------------------------------------------------------------------------------------------------------------ internal GameObject[] GetGameObjectCollisions (GameObject gameObject) { return _collisionManager.GetCurrentCollisions(gameObject); }
//------------------------------------------------------------------------------------------------------------------------ // Contains() //------------------------------------------------------------------------------------------------------------------------ public Boolean Contains(GameObject gameObject) { return _gameObjectsContained.Contains(gameObject); }
//------------------------------------------------------------------------------------------------------------------------ // Remove() //------------------------------------------------------------------------------------------------------------------------ internal void Remove (GameObject gameObject) { if (_gameObjectsContained.Contains (gameObject)) { _updateManager.Remove (gameObject); _collisionManager.Remove (gameObject); _gameObjectsContained.Remove (gameObject); } }
private void onMouseUpHandler (GameObject target , MouseEventType type) { OnMouseUp -= onMouseUpHandler; OnMouseMove -= onMouseMoveHandler; }
//------------------------------------------------------------------------------------------------------------------------ // addChild() //------------------------------------------------------------------------------------------------------------------------ private void addChild(GameObject child) { if (child.HasChild(this)) return; //no recursive adding _children.Add(child); return; }
//------------------------------------------------------------------------------------------------------------------------ // RemoveChild() //------------------------------------------------------------------------------------------------------------------------ /// <summary> /// Removes the specified child GameObject from this object. /// </summary> /// <param name='child'> /// Child object to remove. /// </param> public void RemoveChild (GameObject child) { if (child.parent == this) { child.parent = null; } }
//------------------------------------------------------------------------------------------------------------------------ // validateCase() //------------------------------------------------------------------------------------------------------------------------ private void validateCase(GameObject gameObject) { MethodInfo info = gameObject.GetType ().GetMethod ("OnCollision", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (info != null) { throw new Exception ("'OnCollision' function was not binded. Please check it's correct case (capital O?)"); } }
private void onTargetMouseDownHandler (GameObject target, MouseEventType type) { OnMouseMove += onMouseMoveHandler; OnMouseUp += onMouseUpHandler; }
void OnCollision(GameObject other) { if (other.y - 32 < y) { _position.y = other.y - 32; _velocity.y = 0; onGround = true; } }
public DragHandler (GameObject target):base (target) { OnMouseDownOnTarget+= onTargetMouseDownHandler; }
private void OnMouseEvent(GameObject target,MouseEventType type) { Console.WriteLine("Event: " + type + " triggered on " + target); }
//------------------------------------------------------------------------------------------------------------------------ // AddChild() //------------------------------------------------------------------------------------------------------------------------ /// <summary> /// Adds the specified GameObject as a child to this one. /// </summary> /// <param name='child'> /// Child object to add. /// </param> public void AddChild(GameObject child) { child.parent = this; }
//------------------------------------------------------------------------------------------------------------------------ // ColliderInfo() //------------------------------------------------------------------------------------------------------------------------ public ColliderInfo(GameObject gameObject, CollisionDelegate onCollision) { this.gameObject = gameObject; this.onCollision = onCollision; }
//------------------------------------------------------------------------------------------------------------------------ // removeChild() //------------------------------------------------------------------------------------------------------------------------ private void removeChild(GameObject child) { _children.Remove(child); }
//------------------------------------------------------------------------------------------------------------------------ // SetChildIndex() //------------------------------------------------------------------------------------------------------------------------ /// <summary> /// Inserts the specified object in this object's child list at given location. /// This will alter the position of other objects as well. /// You can use this to determine the layer order (z-order) of child objects. /// </summary> /// <param name='child'> /// Child. /// </param> /// <param name='index'> /// Index. /// </param> public void SetChildIndex(GameObject child, int index) { if (child.parent != this) AddChild(child); if (index < 0) index = 0; if (index >= _children.Count) index = _children.Count - 1; _children.Remove(child); _children.Insert(index, child); }
//------------------------------------------------------------------------------------------------------------------------ // HasChild() //------------------------------------------------------------------------------------------------------------------------ /// <summary> /// Returns 'true' if the specified object is a child of this object. /// </summary> /// <param name='gameObject'> /// The GameObject that should be tested. /// </param> public bool HasChild(GameObject gameObject) { GameObject par = gameObject; while (par != null) { if (par == this) return true; par = par.parent; } return false; }
private void onMouseMoveHandler (GameObject target, MouseEventType type) { target.x = Input.mouseX + offsetToTarget.x; target.y = Input.mouseY + offsetToTarget.y; }
//------------------------------------------------------------------------------------------------------------------------ // HitTest() //------------------------------------------------------------------------------------------------------------------------ /// <summary> /// Tests if this object overlaps the one specified. /// </summary> /// <returns> /// <c>true</c>, if test was hit, <c>false</c> otherwise. /// </returns> /// <param name='other'> /// Other. /// </param> virtual public bool HitTest(GameObject other) { return _collider != null && other._collider != null && _collider.HitTest (other._collider); }
//------------------------------------------------------------------------------------------------------------------------ // Add() //------------------------------------------------------------------------------------------------------------------------ internal void Add (GameObject gameObject) { if (!_gameObjectsContained.Contains (gameObject)) { _updateManager.Add (gameObject); _collisionManager.Add (gameObject); _gameObjectsContained.Add (gameObject); } }