Beispiel #1
0
 public Miner(MinerGame game)
     : base(game)
 {
     this.Suit = new Nanosuit();
     this.ExpToNextLevel = 100;
     this.Width -= 10.0f;
     this.Height -= 10.0f;
     this.sprite = game.Content.Load<Texture2D>(ASSET_NAME);
     this.JumpImpulse = JUMP_SPEED;
     LastMoveDirection = ShootDirection.right;
     this.currentBonus = new Laser(this.game);
 }
Beispiel #2
0
        /// <summary>
        /// Metoda odpowiadająca za uaktualnienie stanu związanego z obiektem.
        /// </summary>
        /// <param name="gameTime">Czas, jaki upłynął od ostatniego wywołania tej metody.</param>
        public override void Update(GameTime gameTime)
        {
            float time = (float)gameTime.ElapsedGameTime.TotalSeconds;

            KeyboardState keyState = Keyboard.GetState();

            if (state == State.Digging)
            {
                Position += Velocity * time;
                if (Position.Y > (digStartPosition.Y + Field.FieldHeight))
                {
                    Velocity.Y = 0.0f;
                    state = State.OnGround;
                }
                return;
            }
            // PrintState();

            if (state != State.OnGround && state != State.Digging)
            {
                foreach (var firstDim in game.GameState.Map.Fields)
                {
                    foreach (var item in firstDim)
                    {
                        if (!item.IsEmpty && item.Position.Y - this.Position.Y - Field.FieldHeight <= EPS &&
                            this.CollidesWith(item))
                        {
                            state = State.OnGround;
                            //this.Position = new Vector2(this.Position.X, item.Position.Y - this.Height);
                            Velocity.Y = 0.0f;
                            DoubleJump dJump = currentBonus as DoubleJump;
                            if (dJump != null)
                            {
                                dJump.Used = false;
                            }
                            break;
                        }
                    }
                }

                if (state != State.OnGround)
                {
                    Velocity += gravity * time;
                }
            }

            if (keyState.IsKeyDown(this.game.Settings.Controls.Up) && !OldKeyState.IsKeyDown(this.game.Settings.Controls.Up) &&
                (state == State.OnGround || (state == State.Jumping && currentBonus is DoubleJump && !((DoubleJump)currentBonus).Used)))
            {
                Jump();
            }

            if (keyState.IsKeyDown(this.game.Settings.Controls.Left))
            {
                Move(Direction.left);
            }
            else if (keyState.IsKeyDown(this.game.Settings.Controls.Right))
            {
                Move(Direction.right);
            }
            else if (keyState.IsKeyDown(this.game.Settings.Controls.Down) && !OldKeyState.IsKeyDown(this.game.Settings.Controls.Down) && state == State.OnGround)
            {
                Dig();
            }
            else if (state != State.Jumping)
            {
                Velocity.X = 0.0f;
            }

            Vector2 oldPosition = this.Position;
            Position += Velocity * time;

            distanceTraveled += Math.Abs(Position.X - oldPosition.X);
            if (distanceTraveled >= Field.FieldWidth)
            {
                distanceTraveled = 0.0f;
                this.Suit.Fuel -= FUEL_PER_MOVE_EXPENDITURE;
                this.game.GameState.User.Score += FUEL_PER_MOVE_EXPENDITURE;

                if (Suit.Fuel <= 0)
                {
                    this.game.ExitGame();
                }
            }

            int topLeftX = (int)Position.X / Field.FieldWidth;
            int topLeftY = (int)Position.Y / Field.FieldHeight;
            int topRightX = (int)(Position.X + this.Width) / Field.FieldWidth;
            int topRightY = (int)Position.Y / Field.FieldHeight;
            int bottomLeftX = (int)Position.X / Field.FieldWidth;
            int bottomLeftY = (int)(Position.Y + this.Height - 10f) / Field.FieldHeight;
            int bottomRightX = (int)(Position.X + this.Width) / Field.FieldWidth;
            int bottomRightY = (int)(Position.Y + this.Height - 10f) / Field.FieldHeight;

            Field topLeft = (topLeftX >= 0 && topLeftY >= 0 && topLeftX < game.GameState.Map.Fields.Length && topLeftY < game.GameState.Map.Fields[0].Length) ?
                game.GameState.Map.Fields[topLeftX][topLeftY] : null;
            Field topRight = (topRightX >= 0 && topRightY >= 0 && topRightX < game.GameState.Map.Fields.GetLength(0) && topRightY < game.GameState.Map.Fields[0].Length) ?
                game.GameState.Map.Fields[topRightX][topRightY] : null;
            Field bottomRight = (bottomRightX >= 0 && bottomRightY >= 0 && bottomRightX < game.GameState.Map.Fields.GetLength(0) && bottomRightY < game.GameState.Map.Fields[0].Length) ?
                game.GameState.Map.Fields[bottomRightX][bottomRightY] : null;
            Field bottomLeft = (bottomLeftX >= 0 && bottomLeftY >= 0 && bottomLeftX < game.GameState.Map.Fields.GetLength(0) && bottomLeftY < game.GameState.Map.Fields[0].Length) ?
                game.GameState.Map.Fields[bottomLeftX][bottomLeftY] : null;

            if (((topLeft == null || topRight == null || bottomLeft == null || bottomRight == null) ||
                (!topLeft.IsEmpty || !topRight.IsEmpty)) ||
                (state != State.OnGround && (!bottomLeft.IsEmpty || !bottomRight.IsEmpty)))
            {
                if (oldPosition.X < 0)
                {
                    oldPosition.X = 0;
                }
                if (oldPosition.X >= Field.FieldWidth * GameMap.DEF_WIDTH)
                {
                    oldPosition.X = Field.FieldWidth * GameMap.DEF_WIDTH - this.Width;
                }
                Position = new Vector2(oldPosition.X, Position.Y);
            }

            bool shouldFall = true;
            if (state != State.Digging && state != State.Jumping)
            {
                foreach (var firstDim in this.game.GameState.Map.Fields)
                {
                    foreach (var field in firstDim)
                    {

                        if (!field.IsEmpty && this.CollidesWith(field))
                        {
                            shouldFall = false;
                            break;
                        }
                    }

                }
                if (shouldFall)
                {
                    this.state = State.Falling;
                }
            }

            foreach (var firstDim in game.GameState.Map.Fields)
            {
                foreach (var item in firstDim)
                {

                    if (item.IsEmpty && item.Bonus != null)
                    {
                        if (this.CollidesWith(item))
                        {
                            Fuel f = item.Bonus as Fuel;
                            if (f != null)
                            {
                                Suit.AddFuel(f);
                            }
                            else if (item.Bonus is Key)
                            {
                                game.GameState.Keys.Add(item.Bonus as Key);
                                // TODO
                                if (game.GameState.Keys.Count == game.GameState.CurrentLevel + 2)
                                {
                                    game.GameState.GatesOpen = true;
                                }
                            }
                            else
                            {
                                currentBonus = item.Bonus;
                                //Console.WriteLine("CurrentBonus: {0}", currentBonus.ToString());
                            }
                            item.Bonus = null;
                            break;
                        }
                    }
                }
            }

            if (keyState.IsKeyDown(this.game.Settings.Controls.Action) && !OldKeyState.IsKeyDown(this.game.Settings.Controls.Action))
            {
                IActionUsable actionBonus = currentBonus as IActionUsable;
                if (actionBonus != null)
                {
                    actionBonus.UseAction();
                }
            }

            if (currentBonus != null)
            {
                currentBonus.Update(gameTime);
                currentBonus.RemainingUsageTime -= gameTime.ElapsedGameTime.Milliseconds;
                if (currentBonus.RemainingUsageTime <= 0)
                {
                    //Console.WriteLine("CurrentBonus destroyed");
                    currentBonus = null;
                }
            }

            // Check whether Miner has enough experience to level up
            if (this.Experience >= this.ExpToNextLevel)
            {
                // Excessed experience points pass to the next level.
                this.Experience = this.Experience - this.ExpToNextLevel;
                this.ExpToNextLevel *= 2;
                this.Suit.IncreaseNanosuitLevel();
            }

            OldKeyState = keyState;
        }