Exemplo n.º 1
0
        /// <summary>
        /// Updates the state of the enemy
        /// </summary>
        /// <param name="gameTime">Game time</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            float elapsedSeconds = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;   // Elapsed seconds, since the value passed only deals in whole numbers

            // If we don't have a reference already, try to find the player
            if (null == _player)
            {
                _player = (Player)Root.FindInGraph("Player");
            }

            float sightThreshold = 100.0f;  // Distance at which the enemy can see the player
            float soundThreshold = 200.0f;  // Distance at which the enemy can hear the player
            float distanceToPlayer = (_player.Position - this.Position).Length();

            // Update the state of the enemy
            switch (_state)
            {
                case EnemyState.Idle:
                    if (CanSeePlayer(distanceToPlayer, sightThreshold))
                    {
                        State = EnemyState.Pursuing;
                    }
                    else if (CanHearPlayer(distanceToPlayer, soundThreshold))
                    {
                        State = EnemyState.Seeking;
                    }
                    break;
                case EnemyState.Seeking:
                    if (CanSeePlayer(distanceToPlayer, sightThreshold))
                    {
                        State = EnemyState.Pursuing;
                    }
                    else if (!CanHearPlayer(distanceToPlayer, soundThreshold))
                    {
                        State = EnemyState.Idle;
                    }
                    break;
                case EnemyState.Pursuing:
                    if (!CanSeePlayer(distanceToPlayer, sightThreshold))
                    {
                        if (CanHearPlayer(distanceToPlayer, soundThreshold))
                        {
                            State = EnemyState.Seeking;
                        }
                        else
                        {
                            State = EnemyState.Idle;
                        }
                    }
                    break;
                default:
                    break;
            }

            // Update the enemy
            _updateFunction(gameTime, elapsedSeconds);
        }
Exemplo n.º 2
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Load the font
            _renderer.Font = Content.Load<SpriteFont>("Courier New");

            // Load the background
            _background = new Background(new Sprite(Content.Load<Texture2D>("background-1")));

            // Create the scenegraph
            _scene = new SceneNode(null, "_root_");

            // Load the player
            // Load the player animations
            int fps = 6;
            Dictionary<String, FrameSet> playerAnimations = new Dictionary<string, FrameSet>();
            List<Tuple<int, int>> idleFrames = new List<Tuple<int, int>>();
            idleFrames.Add(new Tuple<int, int>(0, 0));
            idleFrames.Add(new Tuple<int, int>(0, 1));
            playerAnimations.Add("idle", new FrameSet(idleFrames, true, fps));

            List<Tuple<int, int>> walkFrames = new List<Tuple<int, int>>();
            walkFrames.Add(new Tuple<int, int>(0, 0));
            playerAnimations.Add("walk", new FrameSet(walkFrames, true, fps));

            List<Tuple<int, int>> crouchFrames = new List<Tuple<int, int>>();
            crouchFrames.Add(new Tuple<int, int>(0, 1));
            playerAnimations.Add("crouch", new FrameSet(crouchFrames, true, fps));

            List<Tuple<int, int>> jumpFrames = new List<Tuple<int, int>>();
            jumpFrames.Add(new Tuple<int, int>(0, 2));
            playerAnimations.Add("jump", new FrameSet(jumpFrames, true, fps));

            List<Tuple<int, int>> interactionFrames = new List<Tuple<int, int>>();
            interactionFrames.Add(new Tuple<int, int>(0, 2));
            playerAnimations.Add("interact", new FrameSet(interactionFrames, true, fps));

            AnimatedSprite playerSprite = new AnimatedSprite(Content.Load<Texture2D>("player-ss"), 32, 64, playerAnimations);

            // Load the player sounds
            Dictionary<String, SoundEffect> playerSoundFX = new Dictionary<string, SoundEffect>();
            playerSoundFX.Add("jump", Content.Load<SoundEffect>("jump"));
            playerSoundFX.Add("run", Content.Load<SoundEffect>("run"));
            playerSoundFX.Add("walk", Content.Load<SoundEffect>("walk"));
            _player = new Player(_scene, "Player", playerSprite, 100.0f, playerSoundFX);

            // Load the enemy
            Sprite enemySprite = new Sprite(Content.Load<Texture2D>("enemy"));
            _enemy = new Enemy(_scene, "Enemy-1", enemySprite, 20.0f);
            _enemy.Position = new Vector2(300.0f, 20.0f);

            // Load the crate
            WorldObject crate = new WorldObject(_scene, "Crate-1", new Sprite(Content.Load<Texture2D>("crate")));
            crate.Position = new Vector2(200.0f, 200.0f);
            crate.IsElevated = true;

            WorldObject crate2 = new WorldObject(_scene, "Crate-2", new Sprite(Content.Load<Texture2D>("crate")));
            crate2.Position = new Vector2(400.0f, 400.0f);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="player">The player owning the state</param>
 public PlayerState(Player player)
 {
     _player = player;
 }