public Game1() { graphics = new GraphicsDeviceManager(this); scenegraph = new Scenegraph(this); Registry.Register(graphics); Content.RootDirectory = "Content"; }
protected override void UpdateMe(GameTime gameTime, Scenegraph graph) { base.UpdateMe(gameTime, graph); ai.UpdateAI((float)gameTime.ElapsedGameTime.TotalSeconds); }
/// <summary> /// Scene object type specific update /// Must be overrriden to handle object update. implement as empty if there is no update logic /// </summary> /// <param name="gameTime">elapsed time since last call</param> /// <param name="graph">the scenegraph doing the updating</param> protected abstract void UpdateMe(GameTime gameTime, Scenegraph graph);
/// <summary> /// Calls the implementation specific UpdateMe and then dispacthes /// updates to all children /// </summary> /// <param name="gameTime">elapsed time since last call</param> /// <param name="graph">the scenegraph rendering this object</param> public void Update(GameTime gameTime, Scenegraph graph) { UpdateMe(gameTime, graph); foreach (SceneObject child in children) { child.Update(gameTime, graph); } }
/// <summary> /// The scenegraph itself gets an update epr frame. It does some internal things /// and then descends the tree calling each node's UpdateMe /// </summary> /// <param name="gameTime"> The elapsed time since the last update</param> /// <param name="graph">The scenegraph that is calling this UpdateMe</param> protected override void UpdateMe(GameTime gameTime, Scenegraph graph) { base.UpdateMe(gameTime, graph); // let any base classes do their updating Vector2 cellPos = GetLocalCellPosition(); // get our current position for movement calculation // get input keys and update based on them KeyboardState state = Keyboard.GetState(); if (!moving) // if we are moving then we dont want to repsond to keys until we get where we are going { #region Input code if (state.IsKeyDown(Keys.W)) { cellPos.Y--; // set the destination one up from where we are tankBody.SetLocalRotation((float)-Math.PI / 2); // point the body the right way moving = true; // tell future updates we are moving } if (state.IsKeyDown(Keys.S)) { cellPos.Y++; // set the destination one down from where we are tankBody.SetLocalRotation((float)Math.PI / 2); // point the body the right way moving = true;// tell future updates we are moving } if (state.IsKeyDown(Keys.A)) { cellPos.X--; // set the destination one left from where we are tankBody.SetLocalRotation((float)Math.PI); // point the body the right way moving = true;// tell future updates we are moving } if (state.IsKeyDown(Keys.D)) { cellPos.X++; // set the destination one right from where we are tankBody.SetLocalRotation(0); // point the body the right way moving = true;// tell future updates we are moving } //check tilemap blocking, if the destination tile is blocked, then abort the move if (tileMap.GetTileIndex("blocking", cellPos) > 0) { // blocking tile at this square moving = false; } // If were still moving then we werent blocked so go ahead and set the movement if (moving) { // not blocked MoveToCell(cellPos); } #endregion } // turn turret based on Q and E double deltaT = gameTime.ElapsedGameTime.TotalSeconds; // get time delta to use below if (state.IsKeyDown(Keys.Q)) { tankTurret.SetLocalRotation(tankTurret.GetLocalRotation() + // turn clockwise (float)(deltaT* turretTurnSpeed)); } if (state.IsKeyDown(Keys.E)) { tankTurret.SetLocalRotation(tankTurret.GetLocalRotation() - // turn counter clockwise (float)(deltaT * turretTurnSpeed)); } // now do motion if (moving) { // check to see if we overshot and end motion // calculate distance to goal from where we are // We use a trick to avoid the square root in the distance claculation // movement speed is always >=0 // if X& Y are both >=0 then IFF (X*X)>(Y*Y) THEN X>Y // so the comaprison is the same whether we take the quare root or not // as long as we comapre against another squred value Vector2 pos = GetLocalPosition(); float dx = destinationPixel.X - pos.X; float dy = destinationPixel.Y - pos.Y; // Note this is a distance claculation without the final SQRT float distanceToGoalSqd = (dx * dx) + (dy * dy); // find the distance traveled since last update float distanceTraveled = (float)(deltaT * pixelPerSecSpeed); // calculate the distance squared for comparesion (much cheaper then SQRT) float distanceTraveledSqd = distanceTraveled * distanceTraveled; // compare. If we moved a distance equal to or greater then the distance to the goal // for our last position, then we have reached our goal so just set us there and //end the move if (distanceToGoalSqd <= distanceTraveledSqd) { SetLocalPosition(destinationPixel); moving = false; } else { // otehrwise move closer SetLocalPosition(new Vector2( pos.X + (float)(velocityPixPerSec.X * deltaT), pos.Y + (float)(velocityPixPerSec.Y * deltaT))); } } }
protected override void UpdateMe(GameTime gameTime, Scenegraph scenegraph) { double deltaT = gameTime.ElapsedGameTime.TotalSeconds; KeyboardState state = Keyboard.GetState(); // Get local position and modify in keyed direction by speed Vector2 newPos = GetLocalPosition(); // relative to tile map parent bool moving = false; if (state.IsKeyDown(Keys.A)) { newPos.X -= (float)(GetPPSSpeed() * deltaT); SetCurrentImage(1); moving = true; } if (state.IsKeyDown(Keys.D)) { newPos.X += (float)(GetPPSSpeed() * deltaT); SetCurrentImage(2); moving = true; } if (!moving) { SetCurrentImage(0); } // Get the width and height of the entire tile map Vector2 mapSize = GetTileMap().GetPixelSize(); // get the width and height of this image Vector2 imageSz = GetSpriteImage().GetCurrentImageSize(); // Check to see if new position is still on map if ((newPos.X - (imageSz.X / 2) > 0) && (newPos.X + (imageSz.X / 2) < mapSize.X) && (newPos.Y - (imageSz.Y / 2) > 0) && (newPos.Y + (imageSz.Y / 2) < mapSize.Y)) { // check to see if blocked if (!PositionCollidesWithBlocking(newPos)) { // not blocked SetLocalPosition(newPos); } // check for chaarcter within scroll borders of screen and if so move tilemap Vector2 screenPosition = GetGlobalPosition(); // our pixel position on screen Vector2 tilemapLocalPos = GetTileMap().GetLocalPosition(); // current psition of tile map Rectangle viewport = gmgr.GraphicsDevice.Viewport.Bounds; // size of screen (window) Vector2 scaledPushBorder = pushBorder * GetGlobalScale(); if (screenPosition.X < scaledPushBorder.X) { tilemapLocalPos.X += (scaledPushBorder.X - screenPosition.X); } if (screenPosition.X > viewport.Width - scaledPushBorder.X) { tilemapLocalPos.X -= screenPosition.X - (viewport.Width - scaledPushBorder.X); } if (screenPosition.Y < scaledPushBorder.Y) { tilemapLocalPos.Y += (scaledPushBorder.Y - screenPosition.Y); } if (screenPosition.Y > viewport.Height - scaledPushBorder.Y) { tilemapLocalPos.Y -= screenPosition.Y - (viewport.Height - scaledPushBorder.Y); } if (SCROLLBORDERS) { // tilemapLocalPos.X = Math.Min(tilemapLocalPos.X, 0); // tilemapLocalPos.Y = Math.Min(tilemapLocalPos.Y, 0); GetTileMap().SetLocalPosition(tilemapLocalPos); } } base.UpdateMe(gameTime, scenegraph); // do sprite targeted movement update }
protected override void UpdateMe(GameTime gameTime, Scenegraph graph) { base.UpdateMe(gameTime, graph); // get input Vector2 currentPos = GetLocalPosition(); KeyboardState state = Keyboard.GetState(); float time = (float)gameTime.ElapsedGameTime.TotalSeconds; #region Input code if (state.IsKeyDown(Keys.W)) { currentPos.Y -= pixelPerSecSpeed*time; this.SetLocalRotation((float)-Math.PI / 2); moving = true; } if (state.IsKeyDown(Keys.S)) { currentPos.Y += pixelPerSecSpeed * time; this.SetLocalRotation((float)Math.PI / 2); moving = true; } if (state.IsKeyDown(Keys.A)) { currentPos.X -= pixelPerSecSpeed * time; this.SetLocalRotation((float)Math.PI); moving = true; } if (state.IsKeyDown(Keys.D)) { currentPos.X += pixelPerSecSpeed * time; this.SetLocalRotation(0); moving = true; } //check tilemap blocking Vector2 size = GetSize(); Vector2 position = GetLocalPosition(); #endregion // move turret #region Move Turret double deltaT = gameTime.ElapsedGameTime.TotalSeconds; if (state.IsKeyDown(Keys.Q)) { tankTurret.SetLocalRotation(tankTurret.GetLocalRotation() + (float)(deltaT* turretTurnSpeed)); } if (state.IsKeyDown(Keys.E)) { tankTurret.SetLocalRotation(tankTurret.GetLocalRotation() - (float)(deltaT * turretTurnSpeed)); } #endregion Vector2 cellPos = PixelToCell(currentPos); if (tileMap.GetTileIndex("blocking",cellPos)==0){ SetLocalPosition(currentPos); } // do pickup if (state.IsKeyDown(Keys.Space) && (currentCollisionObject != null)) { PickupCurrentCollisionObject(); } }