WEB开发网      婵犵數濞€濞佳囧磹婵犳艾鐤炬い鎰堕檮閸嬬喐銇勯弽銊с€掗梻鍕閺岋箑螣娓氼垱笑闂佽姘﹂褔婀佸┑鐘诧工妤犲憡绂嶉崜褏纾奸弶鍫涘妼缁楁岸鏌熷畡鐗堝殗闁诡喒鏅犲畷褰掝敃閵堝棙顔忔繝鐢靛仦閸ㄥ爼骞愰幘顔肩;闁规崘绉ぐ鎺撳亹闁绘垶锕╁Λ鍕⒑閹肩偛濡奸悗娑掓櫇缁顓兼径妯绘櫇闂佹寧绻傞弻濠囨晝閸屾稓鍘甸柣搴㈢⊕閿氶柣蹇ョ稻缁绘繃绻濋崘銊т紝闂佽鍨伴崯鏉戠暦閻旂⒈鏁傞柛鈾€鏅欑槐妯衡攽閻愬樊鍤熷┑顔藉劤铻為柛鏇ㄥ墯閸欏繘鏌嶉崫鍕櫣缂佲偓婢跺绠鹃柟瀛樼箘閿涘秵顨ラ悙顏勭伈闁诡喖缍婂畷鎯邦槻婵℃彃顭烽弻娑㈠Ω閵夈儺鍔夌紓浣稿€哥粔褰掑极閹剧粯鏅搁柨鐕傛嫹 ---闂傚倷鐒︾€笛兠洪埡鍛闁跨噦鎷�
开发学院软件开发C语言 游戏人生Silverlight(5) - 星际竞技场[Silverligh... 阅读

游戏人生Silverlight(5) - 星际竞技场[Silverlight 2.0(c#, Farseer Physics Engine)]

 2009-06-22 08:33:08 来源:WEB开发网 闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹闂傚倷绀侀幉锟犲垂閻㈠灚宕查柟鎵閸庡秵銇勯幒鎴濃偓鐢稿磻閹炬枼妲堟繛鍡楃С濞岊亞绱撻崒姘扁枌闁瑰嚖鎷�婵犵數濮幏鍐川椤撴繄鎹曢梻渚€娼уú銈吤洪妸鈺佺劦妞ゆ帊鑳堕埊鏇㈡煏閸モ晛浠х紒杈╁仱閺佹捇鏁撻敓锟�闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹  闂傚倷鑳舵灙缂佺粯顨呴埢宥夊即閵忕姵鐎梺缁樺姈椤愮厧鈽夊Ο閿嬬€婚梺褰掑亰閸撴稑鈻斿鑸碘拺闁告稑饪村▓鏃€绻涚仦鍌氬闁崇粯鎹囬獮瀣攽閹邦剚顔傛俊鐐€栧濠氬储瑜忛幉鎾晸閿燂拷
核心提示: PlayerSprite.cs(玩家 Sprite 模拟器)using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;usi

PlayerSprite.cs(玩家 Sprite 模拟器)

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Collections.Generic;
using FarseerGames.FarseerPhysics.Mathematics;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;

namespace YYArena.Core
{
    /**//// <summary>
    /// 玩家 Sprite
    /// </summary>
    public class PlayerSprite : Sprite, IFire
    {
        private List<Key> _upKeys { get; set; }
        private List<Key> _downKeys { get; set; }
        private List<Key> _leftKeys { get; set; }
        private List<Key> _rightKeys { get; set; }
        private List<Key> _fireKeys { get; set; }

        private KeyboardHandler _keyHandler;
        private IPhysicsControl _physicsControl;

        /**//// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="physicsSimulator">PhysicsSimulator</param>
        /// <param name="physicsControl">IPhysicsControl</param>
        /// <param name="position">初始位置</param>
        /// <param name="angle">初始转角</param>
        /// <param name="originalVelocity">初始速度</param>
        /// <param name="keyboardHandler">KeyboardHandler</param>
        /// <param name="up">操作玩家向前移动的按键集合</param>
        /// <param name="down">操作玩家向后移动的按键集合</param>
        /// <param name="left">操作玩家向左转动的按键集合</param>
        /// <param name="right">操作玩家向右转动的按键集合</param>
        /// <param name="fire">操作玩家开火的按键集合</param>
        public PlayerSprite(PhysicsSimulator physicsSimulator,
            IPhysicsControl physicsControl, Vector2 position, float angle, float originalVelocity,
            KeyboardHandler keyboardHandler,
            List<Key> up, List<Key> down, List<Key> left, List<Key> right, List<Key> fire)
            : base(physicsSimulator, physicsControl, position, angle, originalVelocity)
        {
            PrevFireDateTime = DateTime.MinValue;
            MinFireInterval = 500d;

            _upKeys = up;
            _downKeys = down;
            _leftKeys = left;
            _rightKeys = right;
            _fireKeys = fire;

            _keyHandler = keyboardHandler;
            _physicsControl = physicsControl;

            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (Enabled)
            {
                // 如果按了开火键,是否可开火
                if (_keyHandler.AnyKeyPressed(_fireKeys) && (DateTime.Now - PrevFireDateTime).TotalMilliseconds > MinFireInterval)
                {
                    PrevFireDateTime = DateTime.Now;
                    if (Fire != null)
                        Fire(this, EventArgs.Empty);
                }
            }
        }

        public DateTime PrevFireDateTime { get; set; }

        public double MinFireInterval { get; set; }

        public event EventHandler<EventArgs> Fire;

        protected override Vector2 GetForce()
        {
            Vector2 force = Vector2.Zero;

            if (_keyHandler.AnyKeyPressed(_upKeys))
                force += Helper.Convert2Vector(_physicsControl.ForceAmount, playerBox.Body.Rotation);
            if (_keyHandler.AnyKeyPressed(_downKeys))
                force += Helper.Convert2Vector(_physicsControl.ForceAmount, playerBox.Body.Rotation - Helper.Angle2Radian(180));

            // 最大线性速度限制
            if (playerBox.Body.LinearVelocity.Length() > _physicsControl.MaxLinearVelocity)
                force = Vector2.Zero;

            return force;
        }

        protected override float GetTorque()
        {
            float torque = 0;

            if (_keyHandler.AnyKeyPressed(_leftKeys))
                torque -= _physicsControl.TorqueAmount;
            if (_keyHandler.AnyKeyPressed(_rightKeys))
                torque += _physicsControl.TorqueAmount;

            // 用于修正 RotationalDragCoefficient (在没有任何 Torque 的情况下,如果转速小于 1.3 则设其为 0)
            // 如果不做此修正的话,转速小于 1.3 后还会转好长时间
            if (!_keyHandler.AnyKeyPressed(_leftKeys) && !_keyHandler.AnyKeyPressed(_rightKeys) && Math.Abs(playerBox.Body.AngularVelocity) < 1.3)
                playerBox.Body.AngularVelocity = 0;

            // 最大转速限制
            if (Math.Abs(playerBox.Body.AngularVelocity) > _physicsControl.MaxAngularVelocity)
                torque = 0;

            return torque;
        }
    }
}

Tags:游戏 人生 Silverlight

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接