//------------------------------------------------------------------------- /// <summary> /// GameObject constructor. /// </summary> /// <param name="node">GameNode controlled by this GameObject.</param> public GameObject(String name, GameNode node) { _name = name; _node = node; _entityObj = node.Entity; Active = true; }
//------------------------------------------------------------------------- /// <summary> /// Default GameCharacter constructor. /// </summary> /// <param name="gameLevelMgr">This Character's GameLevelManager</param> /// <param name="name">Character's name</param> /// <param name="drawable">Character's DrawableAnimated</param> public GameCharacter(String name, GameNode node) : base(name, node) { _moveSpeedSlow = 26.0f; _moveSpeedFast = 60.0f; _jumpSpeed = 60.0f; setState(AnimationState.ANIMSTATE_IDLE); }
//------------------------------------------------------------------------- /// <summary> /// GameNode constructor. /// </summary> /// <param name="gameLevelMgr">GameLevelManager containing this GameNode.</param> /// <param name="name">Name of this GameNode.</param> /// <param name="active">Initial active status.</param> public GameNode(GameLevelManager gameLevelMgr, String name) : base(gameLevelMgr) { _name = name; _parent = null; _children = new Dictionary<string, GameNode>(); _positionIsometric = Vector3.Zero; _entity = null; }
//------------------------------------------------------------------------- /// <summary> /// GameObjectMovable constructor. /// </summary> /// <param name="name">This GameObject's name.</param> /// <param name="node">This GameObject's GameNode.</param> public GameObjectMovable(String name, GameNode node) : base(name, node) { _velocity = new Vector3(0.0f, 0.0f, 0.0f); _grounded = false; // When the node changes parent tile, it should start falling node.ParentChange += delegate(NodeEventArgs e) { _grounded = false; }; }
//------------------------------------------------------------------------- /// <summary> /// Check whether or not this GameObject is allowed to move where it's trying /// to move. Modify velocity accordingly to make this move possible. /// </summary> protected void checkForValidMove() { // Grab what will be our parent GameNode nextParent = _node.GameLevelMgr.tileAtIsoCoords( _node.PositionIsometric + _velocity).Node; // If there won't be a new parent, do nothing if (nextParent == _node.Parent) { return; } // If the next parent doesn't exist, the GameObject is trying // to move outside the bounds of the GameLevel if (nextParent == null) { _velocity.X = 0.0f; _velocity.Z = 0.0f; return; } // If the next parent is inactive, then this indicates an empty // hole in the GameLevel. Should we fall through it? if (!nextParent.Entity.Active) { return; } // If the new parent is too high to reach, disallow a movement // to that tile float dy = _node.PositionIsometric.Y - nextParent.PositionIsometric.Y; if (dy > STEP_HEIGHT) { _velocity.X = 0.0f; _velocity.Z = 0.0f; return; } }
//------------------------------------------------------------------------- /// <summary> /// Creates a new GameObject and inserts it into our GameLevel. /// </summary> /// <typeparam name="T">Type of GameObject.</typeparam> /// <param name="name">Name of GameObject.</param> /// <param name="drawable">GameObject's drawable.</param> /// <returns>Newly created GameObject.</returns> public T createGameObject <T>(String name, String drawable) where T : GameObject { GameNode gobjNode = new GameNode(this, name); Drawable gobjDrawable = null; if (typeof(T) == typeof(GameObject) || typeof(T) == typeof(GameObjectMovable)) { gobjDrawable = _gameContentMgr.loadDrawable(drawable); } else if (typeof(T) == typeof(GameCharacter)) { gobjDrawable = _gameContentMgr.loadDrawableAnimated(drawable); } gobjNode.attachEntity(new Entity(gobjDrawable)); T gobj = (T)Activator.CreateInstance(typeof(T), new object[] { name, gobjNode }); _gameObjs.Add(gobj.Name, gobj); return(gobj); }
/// <summary> /// Verify that this GameNode's parent tile is the tile directly underneath it. /// This functionality is essential for corrent draw order of objects. /// </summary> private void checkForNewParentTile() { // Node must already have a parent if (_parent == null) { return; } GameNode newParent = _gameLevelMgr.tileAtIsoCoords(_positionIsometric).Node; // If no node was found, we can't do anything if (newParent == null) { return; } // If we do have a parent, make sure we didn't grab the same one if (_parent == newParent) { return; } // We have a new parent, make the switch _parent.Entity.Color = Color.White; _parent.detachChildNode(_name); newParent.attachChildNode(this); _parent.Entity.Color = Color.Gold; // Fire parent changed event NodeEventArgs e = new NodeEventArgs(this); if (ParentChange != null) { ParentChange(e); } }
//------------------------------------------------------------------------- /// <summary> /// GameTile constructor. /// </summary> /// <param name="node">This GameTile's node.</param> public GameTile(String name, GameNode node) : base(name, node) { }
//------------------------------------------------------------------------- /// <summary> /// NodeEventArgs primary constructor. This constructor should populate /// all event variables. /// </summary> /// <param name="sender">Sender GameNode</param> /// <param name="child">Sender's child which may have triggered the event</param> public NodeEventArgs( GameNode sender, GameNode child = null) { _sender = sender; _child = child; }
/// <summary> /// Unsubscribe from a node's attached event handler /// </summary> /// <param name="node">Node to unsubscribe from</param> private void unsubscribe(GameNode node) { node.ChildAttached -= parentAttachedChild; node.ChildDetached -= parentDetachedChild; }
//------------------------------------------------------------------------- /// <summary> /// Subscribe to a node to receive events when that node attaches a new child. /// </summary> /// <param name="node">Node to receive events from</param> private void subscribe(GameNode node) { node.ChildAttached += new GameNode.NodeEventHandler(parentAttachedChild); node.ChildDetached += new GameNode.NodeEventHandler(parentDetachedChild); }
/// <summary> /// Attach a preexisting GameNode to this GameNode. /// </summary> /// <param name="node">GameNode to attach.</param> public void attachChildNode(GameNode node) { // Form the parent-child relationship node._parent = this; _children.Add(node.Name, node); // Tell current children about the new child NodeEventArgs e = new NodeEventArgs(this, node); if (ChildAttached != null) ChildAttached(e); // Tell the new child about future children node.subscribe(this); }
//------------------------------------------------------------------------- /// <summary> /// Respawn this GameObject at the tile specified. /// </summary> /// <param name="parentTile">Tile where this GameObject will respawn.</param> public void respawn(GameNode parentTile) { _velocity = Vector3.Zero; _node.translateTo(parentTile.PositionIsometric - new Vector3(0.0f, 120.0f, 0.0f)); }
//------------------------------------------------------------------------- /// <summary> /// Give this controller a GameNode to follow /// </summary> /// <param name="target">Target for this controller to follow</param> /// <param name="follow">Whether or not we are initially following the target</param> public void setCharacterTarget(GameNode target, bool follow = true) { _nodeTarget = target; setFollowTarget(follow); }