⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 duckandfireai.cs

📁 说明如何使用托管 Direct3D Mobile 创建一个简单的二维游戏。
💻 CS
字号:
//---------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;

namespace GameApp
{
    /// <summary>
    /// Non-aggressive ground-based AI implementation.  Defines AI type
    /// kDuckAndFire.
    /// </summary>
    class DuckAndFireAI : AI
    {
        /// <summary>
        /// Initialize this instance of AI.
        /// </summary>
        /// <param name="wo">WorldObject controlled by this AI instance
        /// </param>
        /// <param name="lev">Currently active level</param>
        public override void Init(WorldObject wo, Level lev)
        {
            CurFireRate = (FireRateMin + GameMain.Random() *
                (FireRateMax - FireRateMin));
        }

        /// <summary>
        /// Update this AI instance.
        /// </summary>
        /// <param name="wo">WorldObject controlled by this AI instance
        /// </param>
        /// <param name="lev">Currently active level</param>
        public override void Update(WorldObject wo, Level lev)
        {
            // If the player is dead then do not update and stop motion
            if (lev.Player.Dead)
            {
                wo.Stand();
                return;
            }

            // Turn to face the player
            if (wo.WorldX - lev.Player.WorldX < 0)
                wo.FlipX();
            else
                wo.UnflipX();

            // Update the timers
            CurFireTime += GameMain.SecondsPerFrame;
            CurDuckTime += GameMain.SecondsPerFrame;

            // If attacking then let it finish
            if (wo.State == WorldObject.AnimationState.Attack)
                return;

            // Distance to any incoming shots
            float shotDistX = 0.0F;
            float shotDistY = 0.0F;

            if (!CheckDuck(wo, lev, ref shotDistX, ref shotDistY))
            {
                // If not ducking a shot then check if an attack
                // should occur
                if (wo.State != WorldObject.AnimationState.Duck &&
                    Math.Abs(wo.WorldX - lev.Player.WorldX) <= MaxX &&
                    CurFireTime >= CurFireRate)
                {
                    CurFireTime = 0;
                    CurFireRate = (FireRateMin + GameMain.Random() *
                        (FireRateMax - FireRateMin));
                    wo.Attack();
                }
                else if (wo.State != WorldObject.AnimationState.Duck)
                {
                    wo.Stand();
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -