コード例 #1
0
        // logic to find correct frame of sprite from user input and update movement values
        public void Update(GameTime gameTime)
        {
            if (colliding)
            {
                moving = false;
            }
            else
            {
                moving = true;
            }

            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > millisecondsPerFrame)
            {
                if (moving && !exploded)
                {
                    location.X        += shotDirX;
                    location.Y        += shotDirY;
                    distanceTraveledX += Math.Abs(shotDirX);
                    distanceTraveledY += Math.Abs(shotDirY);
                }
                if (distanceTraveledX > (shotLenX * 1.5) || distanceTraveledY > (shotLenY * 1.5))
                {
                    outOfRange = true;
                }
                timeSinceLastFrame -= millisecondsPerFrame;
            }

            SetBoundingBox();
            SpatialBounding.SetQuad(GetBase());
        }
コード例 #2
0
        public void Update(KeyboardState kstate, GameTime gameTime, Camera camera)
        {
            timeSinceLastShot     += gameTime.ElapsedGameTime.Milliseconds;
            timeSinceLastExpClean += gameTime.ElapsedGameTime.Milliseconds;

            if (showHealthBar)
            {
                timeShowingHealthBar += gameTime.ElapsedGameTime.Milliseconds;
            }
            if (timeShowingHealthBar > millisecondsToShowHealthBar)
            {
                showHealthBar        = false;
                timeShowingHealthBar = 0;
            }

            foreach (var shot in Shots)
            {
                shot.Update(gameTime);
            }

            if (timeSinceLastExpClean > millisecondsExplosionLasts)
            {
                // remove exploded shots
                for (int i = 0; i < Shots.Count; i++)
                {
                    if (Shots[i].exploded || Shots[i].outOfRange)
                    {
                        Shots.RemoveAt(i);
                    }
                }
                timeSinceLastExpClean = 0;
            }

            if (timeSinceLastShot > millisecondsNewShot && Shots.Count < maxShotsMoving && health > 0)
            {
                Vector2?shotDirection = AIUtility.ChooseTargetVector(teamType, range, GetBoundingBox(), inInteriorId);
                if (shotDirection != null)
                {
                    BaseCannonBall cannonShot = new BaseCannonBall(teamType, regionKey, location, _content, _graphics);
                    cannonShot.SetFireAtDirection(shotDirection.Value, RandomEvents.rand.Next(10, 25), RandomEvents.rand.Next(-100, 100)); // 3rd param is aim offset
                    cannonShot.moving = true;
                    Shots.Add(cannonShot);
                }
                timeSinceLastShot = 0;
            }

            SpatialBounding.SetQuad(GetBase());
        }
コード例 #3
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            DrawOrder   = new List <Sprite>();
            Collidable  = new List <Sprite>();
            UpdateOrder = new HashSet <Sprite>();
            this.camera = new Camera(GraphicsDevice);
            collision   = new SpatialBounding(new Rectangle(0, 0, GameOptions.PrefferedBackBufferWidth, GameOptions.PrefferedBackBufferHeight), this.camera);
            map         = new TileGameMap(this.camera);

            dayLight      = new DayLight(Content, GraphicsDevice);
            weather       = new Weather(Content, GraphicsDevice);
            lightsTarget  = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            ambientLight  = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            worldScene    = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            interiorScene = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            base.Initialize();
        }
コード例 #4
0
        /// <summary> ESSENTIALLY THE USER INPUT AND PHYSICS DETAILS
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            var kstate = Keyboard.GetState();
            HashSet <Sprite> groundObjectUpdateOrder = new HashSet <Sprite>();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.F4))
            {
                Exit();
            }
            if (kstate.IsKeyDown(Keys.F12))
            {
                gameState.SaveGameState();
                Exit();
            }

            // game menu before everything
            if (startingMenu.showMenu)
            {
                startingMenu.Update(kstate, gameTime);
                return;
            }
            else if (startingMenu.selected == "new" && !gameState.ready)
            {
                gameState.CreateNewGame();
                return;
            }
            else if (startingMenu.selected == "load" && !gameState.ready)
            {
                gameState.LoadGameState();
                return;
            }


            // weather
            weather.Update(kstate, gameTime);
            // daylight shader
            dayLight.Update(kstate, gameTime);

            // static update (Menus)
            windArrows.Update(kstate, gameTime, null);
            int windDirection = windArrows.getWindDirection();
            int windSpeed     = windArrows.getWindSpeed();

            inventoryMenu.Update(kstate, gameTime, this.camera);
            craftingMenu.Update(kstate, gameTime, this.camera);

            Collidable.Clear();
            DrawOrder.Clear();

            // set any viewport visible(and not visible when in interior) collidable map pieces for collision - update LandTileLocList and GroundObjLocList
            BoundingBoxLocations.LandTileLocationList.Clear();
            BoundingBoxLocations.OceanTileLocationList.Clear();
            BoundingBoxLocations.GroundObjectLocationList.Clear();
            BoundingBoxLocations.TilesInView.Clear();
            Vector2 minCorner = new Vector2(camera.Position.X - (GameOptions.PrefferedBackBufferWidth / 2), camera.Position.Y - (GameOptions.PrefferedBackBufferHeight / 2));
            Vector2 maxCorner = new Vector2(camera.Position.X + (GameOptions.PrefferedBackBufferWidth / 2), camera.Position.Y + (GameOptions.PrefferedBackBufferHeight / 2));

            foreach (var tp in GameMapTiles.map)
            {
                if ((tp.location.X >= (minCorner.X - GameOptions.tileWidth) && tp.location.X <= (maxCorner.X + GameOptions.tileWidth)) &&
                    (tp.location.Y >= (minCorner.Y - GameOptions.tileHeight) && tp.location.Y <= (maxCorner.Y + GameOptions.tileHeight)))
                {
                    BoundingBoxLocations.TilesInView.Add(tp);

                    if (tp.bbKey.Equals("landTile"))
                    {
                        BoundingBoxLocations.LandTileLocationList.Add(tp);
                        SpatialBounding.SetQuad(tp.GetBase());
                    }
                    else
                    {
                        BoundingBoxLocations.OceanTileLocationList.Add(tp);
                    }

                    if (tp.groundObjects != null)
                    {
                        foreach (var groundObject in tp.groundObjects)
                        {
                            if (!groundObject.remove)
                            {
                                BoundingBoxLocations.GroundObjectLocationList.Add(groundObject);
                            }
                            else
                            {
                                if (groundObject is IGroundObject)
                                {
                                    IGroundObject go = (IGroundObject)groundObject;
                                    go.UpdateRespawn(gameTime);
                                }
                            }
                        }
                    }
                }
            }

            BoundingBoxLocations.InteriorTileList.Clear();
            // set interior for collision for the interior that the player is in
            if (gameState.player.playerInInterior != null)
            {
                // interior tiles for collision
                foreach (var tile in gameState.player.playerInInterior.interiorTiles)
                {
                    BoundingBoxLocations.InteriorTileList.Add(tile);
                    SpatialBounding.SetQuad(tile.GetBase());
                }

                // TODO: update the interior objects for collision (only do this when player is in there)
                foreach (var obj in gameState.player.playerInInterior.interiorObjects)
                {
                    SpatialBounding.SetQuad(obj.GetBase());
                    Collidable.Add(obj);
                }
            }

            Vector2 lastCamPos = camera.Position;

            // update any gameObjects that need to track state (will set camera pos to player)
            HashSet <Sprite> GameStateObjectUpdateOrder = gameState.Update(kstate, gameTime, camera);

            // use this to offset water noise
            camMove.X = ((camera.Position.X % GameOptions.PrefferedBackBufferWidth) / (GameOptions.PrefferedBackBufferWidth));
            camMove.Y = ((camera.Position.Y % GameOptions.PrefferedBackBufferHeight) / (GameOptions.PrefferedBackBufferHeight));

            // update ground objects (they do not track their state since they are encoded in the map)
            foreach (var sp in BoundingBoxLocations.GroundObjectLocationList)
            {
                ICanUpdate updateSp = (ICanUpdate)sp;
                updateSp.Update(kstate, gameTime, this.camera);
                groundObjectUpdateOrder.Add(sp);
            }

            // merge update orders
            HashSet <Sprite> fullUpdateOrder = GameStateObjectUpdateOrder;

            fullUpdateOrder.UnionWith(groundObjectUpdateOrder);

            // Set draw order and collision from the full update order list
            foreach (var sp in fullUpdateOrder)
            {
                if (gameState.player.playerInInterior != null)
                {
                    // only add ships and land tiles when to collision when interior is being viewed
                    if (sp is IShip || sp is ITilePiece || sp is IPlayer)
                    {
                        sp.SetBoundingBox();
                        Collidable.Add(sp);
                        SpatialBounding.SetQuad(sp.GetBase());
                    }
                }
                else
                {
                    Collidable.Add(sp);
                    SpatialBounding.SetQuad(sp.GetBase());
                    DrawOrder.Add(sp);
                }
            }

            // handle collision
            collision.Update(this.camera.Position);
            SpatialCollision();

            this.camera.Update(gameTime);
            base.Update(gameTime);
        }
コード例 #5
0
        public void Update(KeyboardState kstate, GameTime gameTime, Camera camera)
        {
            timeSinceLastExpClean += gameTime.ElapsedGameTime.Milliseconds;

            // clean shots
            foreach (var shot in Shots)
            {
                shot.Update(gameTime);
            }
            if (timeSinceLastExpClean > millisecondsExplosionLasts)
            {
                // remove exploded shots
                for (int i = 0; i < Shots.Count; i++)
                {
                    if (Shots[i].exploded || Shots[i].outOfRange)
                    {
                        Shots.RemoveAt(i);
                    }
                }
                timeSinceLastExpClean = 0;
            }

            // lighting items
            if (emittingLight != null)
            {
                // toggle light
                if (kstate.IsKeyDown(Keys.T))
                {
                    msToggleButtonHit += gameTime.ElapsedGameTime.Milliseconds;
                    if (msToggleButtonHit > 500) // toggle time 500ms
                    {
                        emittingLight.lit = !emittingLight.lit;
                        msToggleButtonHit = 0;
                    }
                }

                emittingLight.Update(kstate, gameTime, GetBoundingBox().Center.ToVector2());
            }

            usingItem = false;
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                currColumnFrame = 0;
                // aiming
                if (this is IRanged)
                {
                    currColumnFrame    = 1;
                    timeSinceLastShot += gameTime.ElapsedGameTime.Milliseconds;

                    Vector2?shotDirection = null;
                    int     shotOffsetX   = 0;
                    int     shotOffsetY   = 0;
                    bool    shootHorz     = false;

                    if (currRowFrame == 1 || currRowFrame == 2)
                    {
                        shootHorz = true;
                    }

                    // diagnoal aim
                    Vector2 mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
                    Vector2 clickPos = mousePos - new Vector2(GameOptions.PrefferedBackBufferWidth / 2, GameOptions.PrefferedBackBufferHeight / 2) + camera.Position;
                    int     shootMiddleHoverRangeHalf = 40;
                    bool    shootLeft  = false; // these are not player directional, but used for setting the ammo shot dir with respect to the rotation of gun
                    bool    shootRight = false;
                    bool    shootUp    = false;
                    bool    shootDown  = false;
                    if (shootHorz)
                    {
                        if (clickPos.Y < GetBoundingBox().Center.ToVector2().Y - shootMiddleHoverRangeHalf)
                        {
                            if (currRowFrame == 1) // right
                            {
                                rotation = -0.5f;
                            }
                            else
                            {
                                rotation = 0.5f;
                            }
                            shootUp = true;
                        }
                        else if (clickPos.Y > GetBoundingBox().Center.ToVector2().Y + shootMiddleHoverRangeHalf)
                        {
                            if (currRowFrame == 1)
                            {
                                rotation = 0.5f;
                            }
                            else
                            {
                                rotation = -0.5f;
                            }
                            shootDown = true;
                        }
                        else
                        {
                            rotation = 0;
                        }
                    }
                    else
                    {
                        if (clickPos.X < GetBoundingBox().Center.ToVector2().X - shootMiddleHoverRangeHalf)
                        {
                            if (currRowFrame == 0) // down
                            {
                                rotation = 0.5f;
                            }
                            else
                            {
                                rotation = -0.7f;
                            }
                            shootLeft = true;
                        }
                        else if (clickPos.X > GetBoundingBox().Center.ToVector2().X + shootMiddleHoverRangeHalf)
                        {
                            if (currRowFrame == 0)
                            {
                                rotation = -0.7f;
                            }
                            else
                            {
                                rotation = 0.7f;
                            }
                            shootRight = true;
                        }
                        else
                        {
                            rotation = 0;
                        }
                    }

                    // shooting
                    if (kstate.IsKeyDown(Keys.Space) && timeSinceLastShot > millisecondsNewShot)
                    {
                        if (ammoLoaded != null)
                        {
                            Vector2 shotStart = new Vector2(GetBoundingBox().Center.ToVector2().X + shotOffsetX, GetBoundingBox().Center.ToVector2().Y + shotOffsetY);
                            Ammo    shot      = (Ammo)ItemUtility.CreateItem(ammoTypeKey, TeamType.Player, regionKey, shotStart, _content, _graphics);

                            if (shootHorz)
                            {
                                // aiming straight
                                if (!shootUp && !shootDown)
                                {
                                    if (currRowFrame == 1) // right
                                    {
                                        shot.rotation = (shot is IDirectionalAmmo) ? 1.5f : 0;
                                        shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X + shotRange, (int)shot.GetBoundingBox().Center.ToVector2().Y);
                                    }
                                    else
                                    {
                                        shot.rotation = (shot is IDirectionalAmmo) ? -1.5f : 0;
                                        shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X - shotRange, (int)shot.GetBoundingBox().Center.ToVector2().Y);
                                    }
                                }
                                else
                                {
                                    // angled
                                    if (shootUp)
                                    {
                                        if (currRowFrame == 1)
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? 0.75f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg)));
                                        }

                                        else
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? -0.75f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg)));
                                        }
                                    }
                                    else
                                    {
                                        if (currRowFrame == 1)
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? 2.25f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg)));
                                        }
                                        else
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? -2.25f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg)));
                                        }
                                    }
                                }
                            }
                            else
                            {
                                // aiming straight
                                if (!shootLeft && !shootRight)
                                {
                                    if (currRowFrame == 0) // down
                                    {
                                        shot.rotation = (shot is IDirectionalAmmo) ? 3.1f : 0;
                                        shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X, (int)shot.GetBoundingBox().Center.ToVector2().Y + shotRange);
                                    }
                                    else
                                    {
                                        shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X, (int)shot.GetBoundingBox().Center.ToVector2().Y - shotRange);
                                    }
                                }
                                else
                                {
                                    // angled
                                    if (shootLeft)
                                    {
                                        if (currRowFrame == 0)
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? 3.8f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg)));
                                        }
                                        else
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? -0.75f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg)));
                                        }
                                    }
                                    else
                                    {
                                        if (currRowFrame == 0)
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? 2.25f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg)));
                                        }
                                        else
                                        {
                                            shot.rotation = (shot is IDirectionalAmmo) ? 0.75f : 0;
                                            shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg)));
                                        }
                                    }
                                }
                            }

                            shot.SetFireAtDirection(shotDirection.Value, RandomEvents.rand.Next(10, 25), 0);
                            shot.moving = true;
                            Shots.Add(shot);
                            ammoLoaded.amountStacked -= 1;
                            if (ammoLoaded.amountStacked == 0)
                            {
                                ammoLoaded = null;
                            }
                            timeSinceLastShot = 0;
                        }
                    }
                }

                // lighting on any handheld elements
                if (emittingLight != null)
                {
                    emittingLight.scaleSize = emittingLight.baseSize * 1.4f;
                }
            }
            else if (inCombat)
            {
                if (emittingLight != null)
                {
                    emittingLight.scaleSize = emittingLight.baseSize;
                }

                usingItem = true;

                if (nextFrame)
                {
                    currColumnFrame++;
                    if (currColumnFrame == nColumns)
                    {
                        currColumnFrame = 0;
                    }
                }

                if (this is IRanged)
                {
                    currColumnFrame = 1;
                }
            }

            nextFrame = false;
            SpatialBounding.SetQuad(GetBase());
        }