This takes the form of either AI (Artificial Intelligence) or player control over a unit .

There is inherit AI base base for an computer-controlled intellect. For example, there is the GameCharacterAI class which is designed for the management of a game character.

Control by a live player (PlayerIntellect) is achieved through the commands of pressed keys or the mouse for control of the unit or turret.

Inheritance: Entity
Esempio n. 1
0
        void ControlledObject_Damage(Dynamic entity, MapObject prejudicial, Vec3 pos, float damage)
        {
            if (generalTaskType != GeneralTaskTypes.Battle && prejudicial != null)
            {
                Unit sourceUnit = null;

                Bullet bullet = prejudicial as Bullet;
                if (bullet != null)
                {
                    sourceUnit = bullet.SourceUnit;
                }
                Explosion explosion = prejudicial as Explosion;
                if (explosion != null)
                {
                    sourceUnit = explosion.SourceUnit;
                }

                if (sourceUnit != null)
                {
                    Intellect unitIntellect = sourceUnit.Intellect as Intellect;
                    if (unitIntellect != null && unitIntellect.Faction != Faction)
                    {
                        //do battle task
                        DoGeneralTask(GeneralTaskTypes.Battle, null);

                        //move to enemy
                        DoMoveTask(sourceUnit.Position);

                        //notify allies
                        NotifyAlliesOnEnemy(sourceUnit.Position);
                    }
                }
            }
        }
Esempio n. 2
0
        public void SetIntellect(Intellect value, bool shouldDeleteAfterDetach)
        {
            Intellect oldIntellect = intellect;
            bool      oldIntellectShouldDeleteAfterDetach = intellectShouldDeleteAfterDetach;

            if (intellect != null)
            {
                RemoveRelationship(intellect);
            }

            intellect = value;
            intellectShouldDeleteAfterDetach = shouldDeleteAfterDetach;

            if (intellect != null)
            {
                AddRelationship(intellect);
            }

            if (oldIntellect != null && oldIntellectShouldDeleteAfterDetach)
            {
                oldIntellect.SetShouldDelete();
            }

            //send update to clients
            if (EntitySystemWorld.Instance.IsServer())
            {
                Server_SendIntellectToClients(EntitySystemWorld.Instance.RemoteEntityWorlds);
            }
        }
Esempio n. 3
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnTick()"/>.</summary>
        protected override void OnTick()
        {
            base.OnTick();

            if (Intellect != null)
            {
                if (Intellect.IsControlKeyPressed(GameControlKeys.Fire1))
                {
                    GunsTryFire(false);
                }
                if (Intellect.IsControlKeyPressed(GameControlKeys.Fire2))
                {
                    GunsTryFire(true);
                }
            }

            //send turn to position to clients
            if (EntitySystemWorld.Instance.IsServer())
            {
                float epsilon = .05f;
                if (!server_shouldSendTurnToPosition.Equals(server_sentTurnToPosition, epsilon))
                {
                    Server_SendTurnToPositionToClients(EntitySystemWorld.Instance.RemoteEntityWorlds,
                                                       server_shouldSendTurnToPosition);
                    server_sentTurnToPosition = server_shouldSendTurnToPosition;
                }
            }
        }
Esempio n. 4
0
        private void ShiftBooster()
        {
            MapObjectAttachedParticle Shift1 = GetAttachedObjectByAlias("Shift1") as MapObjectAttachedParticle;
            MapObjectAttachedParticle Shift2 = GetAttachedObjectByAlias("Shift2") as MapObjectAttachedParticle;

            bool boosted = false;

            if (Intellect.IsControlKeyPressed(GameControlKeys.SHIFT))
            {
                if (ShiftBottel >= 5)
                {
                    HelliBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta,
                                       HelliBody.Rotation * new Vec3(40, 0, 0) * HelliBody.Mass, Vec3.Zero);
                    ShiftBottel -= 0.5f;
                    boosted      = true;
                }
            }
            else
            {
                boosted = false;
                if (ShiftBottel < Type.MaxShiftBottel)
                {
                    ShiftBottel += 0.05f;
                }
            }
            if (Shift1 != null)
            {
                Shift1.Visible = boosted;
                Shift2.Visible = boosted;
            }
        }
        public void Tick(Intellect intellect)
        {
            bool damp = true;
            if (intellect.IsControlKeyPressed(keyMinus))
            {
                damp = false;
                if (val <= 0) val -= increment;
                if (val > 0) val -= decrement;
            }
            if (intellect.IsControlKeyPressed(keyPlus))
            {
                damp = false;
                if (val >= 0) val += increment;
                if (val < 0) val += decrement;
            }

            if (damp)
            {
                if (val < 0)
                {
                    val += damping;
                    if (val > 0) val = 0;
                }
                if (val > 0)
                {
                    val -= damping;
                    if (val < 0) val = 0;
                }
            }

            if (val < -1) val = -1;
            if (val > 1) val = 1;
        }
Esempio n. 6
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnTick()"/>.</summary>
        protected override void OnTick()
        {
            base.OnTick();

            if (Intellect != null)
            {
                if (Intellect.IsControlKeyPressed(GameControlKeys.Fire1))
                {
                    WeaponTryFire(false);
                }

                if (Intellect.IsControlKeyPressed(GameControlKeys.Fire2))
                {
                    WeaponTryFire(true);
                }
            }

            TickContusionTime();

            if (activeWeapon == null || activeWeapon.Ready)
            {
                UpdateTPSArcadeLookDirection();
            }

            if (activeWeapon != null)
            {
                if (EntitySystemWorld.Instance.IsServer())
                {
                    Server_TickSendWeaponVerticalAngleToClients();
                }
            }
        }
Esempio n. 7
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnRelatedEntityDelete(Entity)"/></summary>
        protected override void OnRelatedEntityDelete(Entity entity)
        {
            base.OnRelatedEntityDelete(entity);

            if (intellect == entity)
            {
                intellect = null;
            }
        }
 public void Tick(Intellect intellect)
 {
     if (intellect.IsControlKeyPressed(keyDecel))
         val -= increment;
     if (intellect.IsControlKeyPressed(keyAccel))
         val += increment;
     if (val < 0) val = 0;
     if (val > 1) val = 1;
 }
Esempio n. 9
0
        private void TickSound()
        {
            bool lastMotorOn = HelliOn;

            HelliOn = Intellect != null && Intellect.IsActive();

            //sound on, off
            if (HelliOn != lastMotorOn)
            {
                if (HelliOn)
                {
                    Sound sound = SoundWorld.Instance.SoundCreate(Type.SoundOn, SoundMode.Mode3D);
                    if (sound != null)
                    {
                        soundOnChannel = SoundWorld.Instance.SoundPlay(sound, EngineApp.Instance.DefaultSoundChannelGroup, .7f, true);
                        if (soundOnChannel != null)
                        {
                            soundOnChannel.Position = Position;
                            soundOnChannel.Pause    = false;
                        }
                    }
                    //SoundPlay3D(Type.SoundOn, .7f, true);
                }
                else
                {
                    MohiOff();
                    SoundPlay3D(Type.SoundOff, 0.7f, true);
                }
            }

            string needSoundName = null;

            if (HelliOn)
            {
                needSoundName = Type.SoundIdle;
            }
            if (needSoundName != currentRotorSoundName)
            {
                currentRotorSoundName = needSoundName;

                if (!string.IsNullOrEmpty(needSoundName))
                {
                    Sound sound = SoundWorld.Instance.SoundCreate(needSoundName,
                                                                  SoundMode.Mode3D | SoundMode.Loop);

                    if (sound != null)
                    {
                        rotorSoundChannel = SoundWorld.Instance.SoundPlay(
                            sound, EngineApp.Instance.DefaultSoundChannelGroup, 1, true);
                        rotorSoundChannel.Position = Position;
                        rotorSoundChannel.Pause    = false;
                    }
                }
            }
        }
Esempio n. 10
0
        void CreateInitialAI(bool loaded)
        {
            if (EntitySystemWorld.Instance.IsEditor())
            {
                return;
            }
            if (loaded && EntitySystemWorld.Instance.SerializationMode == SerializationModes.World)
            {
                return;
            }

            if (Intellect != null)
            {
                return;
            }

            AIType initAI = InitialAI;

            if (initAI == null)
            {
                initAI = Type.InitialAI;
            }

            if (initAI == null)
            {
                return;
            }

            if (EntitySystemWorld.Instance.IsDedicatedServer())
            {
                if (initAI.NetworkType == EntityNetworkTypes.ClientOnly)
                {
                    return;
                }
            }
            if (EntitySystemWorld.Instance.IsClientOnly())
            {
                if (initAI.NetworkType == EntityNetworkTypes.ServerOnly)
                {
                    return;
                }
                if (initAI.NetworkType == EntityNetworkTypes.Synchronized)
                {
                    return;
                }
            }

            //create intellect
            Intellect i = (Intellect)Entities.Instance.Create(initAI, World.Instance);

            i.Faction          = InitialFaction;
            i.ControlledObject = this;
            i.PostCreate();
            SetIntellect(i, true);
        }
Esempio n. 11
0
        protected override void OnIntellectCommand( Intellect.Command command )
        {
            base.OnIntellectCommand( command );

            if( command.KeyPressed )
            {
                if( command.Key == GameControlKeys.Fire1 )
                    GunsTryFire( false );
                if( command.Key == GameControlKeys.Fire2 )
                    GunsTryFire( true );
            }
        }
Esempio n. 12
0
		public ServerOrSingle_Player ServerOrSingle_GetPlayer( Intellect intellect )
		{
			if( intellect == null )
				Log.Fatal( "PlayerManager: ServerOrSingle_GetPlayerByIntellect: intellect == null." );

			//it is can be slowly. need to use Dictionary.
			foreach( ServerOrSingle_Player player in serverOrSingle_players )
			{
				if( player.Intellect == intellect )
					return player;
			}
			return null;
		}
Esempio n. 13
0
 //Animation fcharacter
 protected override void OnUpdateBaseAnimation()
 {
     if (Intellect != null)
     {
         if (Intellect.IsControlKeyPressed(GameControlKeys.Right))
         {
             UpdateBaseAnimation(Type.RightAnimationName, false, false, 1);
         }
         if (Intellect.IsControlKeyPressed(GameControlKeys.Left))
         {
             UpdateBaseAnimation(Type.LeftAnimationName, false, false, 1);
         }
     }
 }
Esempio n. 14
0
        protected override void OnIntellectCommand( Intellect.Command command )
        {
            base.OnIntellectCommand( command );

            if( EntitySystemWorld.Instance.IsServer() || EntitySystemWorld.Instance.IsSingle() )
            {
                if( command.KeyPressed )
                {
                    if( command.Key == GameControlKeys.Fire1 )
                        GunsTryFire( false );
                    if( command.Key == GameControlKeys.Fire2 )
                        GunsTryFire( true );
                }
            }
        }
Esempio n. 15
0
        private void Ñar_particle()
        {
            float carspeed = GetWheelsSpeed();

            MapObjectAttachedParticle Carspeed_applyparticle1 = GetAttachedObjectByAlias("Carspeed_applyparticle1") as MapObjectAttachedParticle;
            MapObjectAttachedParticle Carspeed_applyparticle2 = GetAttachedObjectByAlias("Carspeed_applyparticle2") as MapObjectAttachedParticle;

            bool  Hs1      = false;
            bool  Cp1      = false;
            float speed_cp = GetWheelsSpeed();
            bool  on_whell = LRWheel.onGround || RRWheel.onGround;

            if (speed_cp > Type.Carspeed_applyparticle & on_whell)
            {
                Cp1 = true;
            }
            float ang  = chassisBody.AngularVelocity.Z;
            float ang2 = ang * carspeed * 2;

            if (carspeed > 20)
            {
                ang2 = ang * carspeed / 2;
            }
            else if (carspeed > 50)
            {
                ang2 = ang * carspeed / 10;
            }
            if (Intellect.IsControlKeyPressed(GameControlKeys.Jump))
            {
                skidsound(true);
                //EngineApp.Instance.ScreenGuiRenderer.AddText("ang: " + ang2, new Vec2(.6f, .4f));
                if (carspeed > 10 & on_whell)
                {
                    Hs1 = true;
                    chassisBody.AngularDamping = 0;
                }
            }
            else
            {
                //dumpings
                chassisBody.AngularDamping = 3;
            }

            Carspeed_applyparticle1.Visible = Hs1;
            Carspeed_applyparticle2.Visible = Cp1;
        }
Esempio n. 16
0
        void Client_ReceiveIntellect(RemoteEntityWorld sender, ReceiveDataReader reader)
        {
            uint networkUIN = reader.ReadVariableUInt32();
            bool shouldDeleteAfterDetach = reader.ReadBoolean();

            if (!reader.Complete())
            {
                return;
            }

            Intellect i = null;

            if (networkUIN != 0)
            {
                i = (Intellect)Entities.Instance.GetByNetworkUIN(networkUIN);
            }
            SetIntellect(i, shouldDeleteAfterDetach);
        }
Esempio n. 17
0
        void TickFrontWheels()
        {
            Vec3  Velocity  = forkliftBody.LinearVelocity * forkliftBody.Rotation.GetInverse();
            bool  onGround  = leftWheel.onGround || rightWheel.onGround;
            float leftwheel = 0;

            if (Intellect != null)
            {
                float forward = Intellect.GetControlKeyStrength(GameControlKeys.Forward);
                leftwheel += forward;

                float backward = Intellect.GetControlKeyStrength(GameControlKeys.Backward);
                leftwheel -= backward;

                MathFunctions.Clamp(ref leftwheel, -1, 1);
            }
            //return if no throttle and sleeping
            if (forkliftBody.Sleeping && leftwheelBody.Sleeping && rightwheelBody.Sleeping &&
                wheelRearRightBody.Sleeping && wheelRearLeftBody.Sleeping && leftwheel == 0)
            {
                return;
            }

            if (leftWheel.onGround && rightWheel.onGround)
            {
                if (leftwheel > 0 && Velocity.X < Type.FMaximumspeedgear1)
                {
                    float force = Velocity.X > 0 ? Type.Forwardgear1force : Type.Brakeforcegear1;
                    force *= leftwheel;
                    forkliftBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                          new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }



                if (leftwheel < 0 && (-Velocity.X) < Type.BMaximumspeedgear1)
                {
                    float force = -Velocity.X < 0 ? Type.Brakeforcegear1 : Type.Backwardgear1force;
                    force *= leftwheel;
                    forkliftBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                          new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }
            }
        }
Esempio n. 18
0
        void TickIntellect()
        {
            //horizontalMotor
            {
                float throttle = 0;
                throttle += Intellect.GetControlKeyStrength(GameControlKeys.Left);
                throttle -= Intellect.GetControlKeyStrength(GameControlKeys.Right);

                GearedMotor motor = PhysicsModel.GetMotor("horizontalMotor") as GearedMotor;
                if (motor != null)
                {
                    motor.Throttle = throttle;
                }
            }

            //gibbetMotor
            {
                ServoMotor motor = PhysicsModel.GetMotor("gibbetMotor") as ServoMotor;
                if (motor != null)
                {
                    Radian needAngle = motor.DesiredAngle;

                    needAngle += Intellect.GetControlKeyStrength(GameControlKeys.Forward) * .004f;
                    needAngle -= Intellect.GetControlKeyStrength(GameControlKeys.Backward) * .004f;

                    MathFunctions.Clamp(ref needAngle,
                                        new Degree(-20.0f).InRadians(), new Degree(40.0f).InRadians());

                    motor.DesiredAngle = needAngle;
                }
            }

            //Change player LookDirection at rotation
            PlayerIntellect intellect = Intellect as PlayerIntellect;

            if (intellect != null)
            {
                Vec3 lookVector = intellect.LookDirection.GetVector();
                lookVector *= OldRotation.GetInverse();
                lookVector *= Rotation;
                intellect.LookDirection = SphereDir.FromVector(lookVector);
            }
        }
Esempio n. 19
0
        private void TickChassis_hand_brake()
        {
            skidsound(false);
            bool onGround = LRWheel.onGround || RRWheel.onGround;

            float hand_brake_f = 0;
            float hand_brake_b = 0;

            if (Intellect != null)
            {
                Ñar_particle();
                float h_brake = Intellect.GetControlKeyStrength(GameControlKeys.Jump);
                hand_brake_f += h_brake;
                hand_brake_b -= h_brake;
                MathFunctions.Clamp(ref hand_brake_f, -1, 1);
                MathFunctions.Clamp(ref hand_brake_b, -1, 1);
            }

            Vec3  localLinearVelocity = chassisBody.LinearVelocity * chassisBody.Rotation.GetInverse();
            float tm_hb = Type.Hand_brake / 2;

            if (LRWheel.onGround && RRWheel.onGround)
            {
                if (hand_brake_f > 0 && localLinearVelocity.X < Type.Max_forward_speed)
                {
                    float force = localLinearVelocity.X > 0 ? tm_hb : Type.Hand_brake;
                    force *= hand_brake_f;

                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }

                if (hand_brake_b < 0 && (-localLinearVelocity.X) < Type.Max_backward_speed)
                {
                    float force = localLinearVelocity.X > 0 ? Type.Hand_brake : tm_hb;
                    force *= hand_brake_b;

                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }
            }
        }
Esempio n. 20
0
        public FactionType GetRootUnitFaction()
        {
            Unit unit = this;

            while (true)
            {
                Intellect objIntellect = unit.Intellect;
                if (objIntellect != null && objIntellect.Faction != null)
                {
                    return(objIntellect.Faction);
                }

                Unit obj = unit.AttachedMapObjectParent as Unit;
                if (obj == null)
                {
                    return(null);
                }
                unit = obj;
            }
        }
Esempio n. 21
0
        void TickMagnet()
        {
            bool userNeedDetach = Intellect != null &&
                                  (Intellect.IsControlKeyPressed(GameControlKeys.Fire1) ||
                                   Intellect.IsControlKeyPressed(GameControlKeys.Fire2));

            //attach new bodies
            if (!userNeedDetach)
            {
                const int needContacts = 3;

                foreach (KeyValuePair <Body, int> pair in lastMagnetContactsCount)
                {
                    Body mapObjectBody = pair.Key;
                    int  contactsCount = pair.Value;

                    if (contactsCount >= needContacts)
                    {
                        MagnetAttachObject(mapObjectBody);
                    }
                }
            }
            lastMagnetContactsCount.Clear();

            //detach by user
            if (userNeedDetach)
            {
                MagnetDetachAllObjects();
            }

            //detach if joint disposed
again:
            foreach (MagnetObjectItem item in magnetAttachedObjects)
            {
                if (item.fixedJoint.IsDisposed)
                {
                    MagnetDetachObject(item);
                    goto again;
                }
            }
        }
Esempio n. 22
0
        void TickIntellect( Intellect intellect )
        {
            Vec2 forceVec = Vec2.Zero;

            if( forceMoveVectorTimer != 0 )
            {
                forceVec = forceMoveVector;
            }
            else
            {
                Vec2 vec = Vec2.Zero;

                vec.X += intellect.GetControlKeyStrength( GameControlKeys.Forward );
                vec.X -= intellect.GetControlKeyStrength( GameControlKeys.Backward );
                vec.Y += intellect.GetControlKeyStrength( GameControlKeys.Left );
                vec.Y -= intellect.GetControlKeyStrength( GameControlKeys.Right );

                forceVec = ( new Vec3( vec.X, vec.Y, 0 ) * Rotation ).ToVec2();

                //TPS arcade specific (camera observe)
                //set forceVec depending on camera orientation
                if( GameMap.Instance.GameType == GameMap.GameTypes.TPSArcade && forceVec != Vec2.Zero )
                {
                    if( Intellect != null && PlayerIntellect.Instance == Intellect )
                    {
                        //!!!!!!not adapted for networking
                        //using RendererWorld.Instance.DefaultCamera is bad

                        Vec2 diff = Position.ToVec2() - RendererWorld.Instance.DefaultCamera.
                            Position.ToVec2();
                        Degree angle = new Radian( MathFunctions.ATan( diff.Y, diff.X ) );
                        Degree vecAngle = new Radian( MathFunctions.ATan( -vec.Y, vec.X ) );
                        Quat rot = new Angles( 0, 0, vecAngle - angle ).ToQuat();
                        forceVec = ( rot * new Vec3( 1, 0, 0 ) ).ToVec2();
                    }
                }

                if( forceVec != Vec2.Zero )
                {
                    float length = forceVec.Length();
                    if( length > 1 )
                        forceVec /= length;
                }
            }

            if( forceVec != Vec2.Zero )
            {
                float velocityCoefficient = 1;
                if( FastMoveInfluence != null )
                    velocityCoefficient = FastMoveInfluence.Type.Coefficient;

                float maxVelocity;
                float force;

                if( IsOnGround() )
                {
                    maxVelocity = Type.WalkMaxVelocity;
                    force = Type.WalkForce;
                }
                else
                {
                    maxVelocity = Type.FlyControlMaxVelocity;
                    force = Type.FlyControlForce;
                }

                maxVelocity *= forceVec.LengthFast();

                //velocityCoefficient
                maxVelocity *= velocityCoefficient;
                force *= velocityCoefficient;

                if( mainBody.LinearVelocity.LengthFast() < maxVelocity )
                    mainBody.AddForce( ForceType.Global, 0, new Vec3( forceVec.X, forceVec.Y, 0 ) *
                        force * TickDelta, Vec3.Zero );
            }

            lastTickForceVector = forceVec;
        }
Esempio n. 23
0
        protected override void OnIntellectCommand( Intellect.Command command )
        {
            base.OnIntellectCommand( command );

            if( EntitySystemWorld.Instance.IsServer() || EntitySystemWorld.Instance.IsSingle() )
            {
                if( command.KeyPressed )
                {
                    if( command.Key == GameControlKeys.Jump )
                    {
                        //TPS arcade specific (camera observe)
                        //No jump
                        if( GameMap.Instance.GameType == GameMap.GameTypes.TPSArcade )
                        {
                            if( Intellect != null && PlayerIntellect.Instance == Intellect )
                                return;
                        }

                        TryJump();
                    }
                }
            }
        }
Esempio n. 24
0
        public void SetIntellect( Intellect value, bool shouldDeleteAfterDetach )
        {
            Intellect oldIntellect = intellect;
            bool oldIntellectShouldDeleteAfterDetach = intellectShouldDeleteAfterDetach;

            if( intellect != null )
                RemoveRelationship( intellect );

            intellect = value;
            intellectShouldDeleteAfterDetach = shouldDeleteAfterDetach;

            if( intellect != null )
                AddRelationship( intellect );

            if( oldIntellect != null && oldIntellectShouldDeleteAfterDetach )
                oldIntellect.SetShouldDelete();

            //send update to clients
            if( EntitySystemWorld.Instance.IsServer() )
                Server_SendIntellectToClients( EntitySystemWorld.Instance.RemoteEntityWorlds );
        }
Esempio n. 25
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnRelatedEntityDelete(Entity)"/></summary>
        protected override void OnRelatedEntityDelete( Entity entity )
        {
            base.OnRelatedEntityDelete( entity );

            if( intellect == entity )
                intellect = null;
        }
Esempio n. 26
0
 public void DoIntellectCommand( Intellect.Command command )
 {
     OnIntellectCommand( command );
 }
Esempio n. 27
0
        protected override void OnIntellectCommand( Intellect.Command command )
        {
            base.OnIntellectCommand( command );

            if( command.KeyPressed )
            {
                if( command.Key == GameControlKeys.Jump )
                {
                    //TPS arcade specific (camera observe)
                    //No jump
                    if( GameMap.Instance.GameType == GameMap.GameTypes.TPSArcade )
                    {
                        if( PlayerIntellect.Instance != null &&
                            PlayerIntellect.Instance.ControlledObject == this &&
                            !PlayerIntellect.Instance.FPSCamera )
                        {
                            return;
                        }
                    }

                    TryJump();
                }
            }
        }
Esempio n. 28
0
        void TickMotorSound()
        {
            bool lastMotorOn = motorOn;

            motorOn = Intellect != null && Intellect.IsActive();

            //sound on, off
            if (motorOn != lastMotorOn)
            {
                if (!firstTick && Life != 0)
                {
                    if (motorOn)
                    {
                        SoundPlay3D(Type.SoundOn, .7f, true);
                    }
                    else
                    {
                        SoundPlay3D(Type.SoundOff, .7f, true);
                    }
                }
            }

            string needSoundName = null;

            if (motorOn && currentGear != null)
            {
                needSoundName = currentGear.SoundMotor;
            }

            if (needSoundName != currentMotorSoundName)
            {
                //change motor sound

                if (motorSoundChannel != null)
                {
                    motorSoundChannel.Stop();
                    motorSoundChannel = null;
                }

                currentMotorSoundName = needSoundName;

                if (!string.IsNullOrEmpty(needSoundName))
                {
                    Sound sound = SoundWorld.Instance.SoundCreate(needSoundName,
                                                                  SoundMode.Mode3D | SoundMode.Loop);

                    if (sound != null)
                    {
                        motorSoundChannel = SoundWorld.Instance.SoundPlay(
                            sound, EngineApp.Instance.DefaultSoundChannelGroup, .3f, true);
                        motorSoundChannel.Position = Position;
                        motorSoundChannel.Pause    = false;
                    }
                }
            }
            //update motor channel position and pitch
            if (motorSoundChannel != null)
            {
                Range speedRangeAbs = currentGear.SpeedRange;
                if (speedRangeAbs.Minimum < 0 && speedRangeAbs.Maximum < 0)
                {
                    speedRangeAbs = new Range(-speedRangeAbs.Maximum, -speedRangeAbs.Minimum);
                }
                Range pitchRange = currentGear.SoundMotorPitchRange;

                Vec3  Velocity = forkliftBody.LinearVelocity * forkliftBody.Rotation.GetInverse();
                float speedAbs = Velocity.X * 1;

                float speedCoef = 0;
                if (speedRangeAbs.Size() != 0)
                {
                    speedCoef = (speedAbs - speedRangeAbs.Minimum) / speedRangeAbs.Size();
                }
                MathFunctions.Clamp(ref speedCoef, 0, 1);

                //update channel
                motorSoundChannel.Pitch    = pitchRange.Minimum + speedCoef * pitchRange.Size();
                motorSoundChannel.Position = Position;
            }
        }
Esempio n. 29
0
        private void TickRotation()
        {
            bool onGround = LFWheel.onGround || RFWheel.onGround;

            float carspeed        = GetWheelsSpeed();
            float LFWheelThrottle = 0;
            float RFWheelThrottle = 0;

            if (Intellect != null)
            {
                Body WFR = PhysicsModel.GetBody("FR");
                Body WFL = PhysicsModel.GetBody("FL");
                Body WRR = PhysicsModel.GetBody("RR");
                Body WRL = PhysicsModel.GetBody("RL");

                float wheelspeed = GetWheelsSpeed() * 2;
                Vec3  Wspeed     = new Vec3(0, wheelspeed,
                                            0) * chassisBody.Rotation.GetNormalize();

                if (onGround)
                {
                    WFR.AngularVelocity = Wspeed;
                    WFL.AngularVelocity = Wspeed;
                    WRR.AngularVelocity = Wspeed;
                    WRL.AngularVelocity = Wspeed;
                }
                {
                    ServoMotor motor_1 = PhysicsModel.GetMotor("wheel1") as ServoMotor;
                    ServoMotor motor_2 = PhysicsModel.GetMotor("wheel2") as ServoMotor;

                    if (motor_1 != null)
                    {
                        if (motor_2 != null)
                        {
                            float turnspeed = 0.5f;
                            MathFunctions.Clamp(ref turnspeed, 0.5f, 0.5f);
                            Radian needAngle = motor_1.DesiredAngle;
                            if (Intellect.IsControlKeyPressed(GameControlKeys.Left))
                            {
                                if (carspeed > 2)
                                {
                                    LFWheelThrottle -= turnspeed;
                                    RFWheelThrottle += turnspeed;
                                }
                                if (carspeed < -1)
                                {
                                    LFWheelThrottle += turnspeed;
                                    RFWheelThrottle -= turnspeed;
                                }
                                needAngle -= 0.2f;
                            }
                            else if (Intellect.IsControlKeyPressed(GameControlKeys.Right))
                            {
                                if (carspeed > 2)
                                {
                                    LFWheelThrottle += turnspeed;
                                    RFWheelThrottle -= turnspeed;
                                }
                                if (carspeed < -1)
                                {
                                    LFWheelThrottle -= turnspeed;
                                    RFWheelThrottle += turnspeed;
                                }
                                needAngle += 0.2f;
                            }
                            else
                            {
                                needAngle = 0f;
                            }

                            MathFunctions.Clamp(ref needAngle, new Degree(-37.0f).InRadians(),
                                                new Degree(37.0f).InRadians());
                            motor_1.DesiredAngle = needAngle;
                            motor_2.DesiredAngle = needAngle;
                        }
                    }
                }
            }
        }
Esempio n. 30
0
        private void TickMotorSound()
        {
            bool lastMotorOn = motorOn;

            motorOn = Intellect != null && Intellect.IsActive();

            //sound on, off
            if (motorOn != lastMotorOn)
            {
                if (!firstTick && Life != 0)
                {
                    if (motorOn)
                    {
                        Sound sound = SoundWorld.Instance.SoundCreate(Type.SoundOn, SoundMode.Mode3D);
                        if (sound != null)
                        {
                            soundOnChannel = SoundWorld.Instance.SoundPlay(sound, EngineApp.Instance.DefaultSoundChannelGroup, .7f, true);
                            if (soundOnChannel != null)
                            {
                                soundOnChannel.Position = Position;
                                soundOnChannel.Pause    = false;
                            }
                        }
                        //SoundPlay3D(Type.SoundOn, .7f, true);
                    }
                    else
                    {
                        SoundPlay3D(Type.SoundOff, .7f, true);
                    }
                }
            }

            string needSoundName = null;

            if (motorOn && currentGear != null)
            {
                needSoundName = currentGear.SoundMotor;
            }

            if (needSoundName != currentMotorSoundName)
            {
                //change motor sound

                if (motorSoundChannel != null)
                {
                    motorSoundChannel.Stop();
                    motorSoundChannel = null;
                }

                currentMotorSoundName = needSoundName;

                if (!string.IsNullOrEmpty(needSoundName))
                {
                    Sound sound = SoundWorld.Instance.SoundCreate(needSoundName,
                                                                  SoundMode.Mode3D | SoundMode.Loop);

                    if (sound != null)
                    {
                        motorSoundChannel = SoundWorld.Instance.SoundPlay(
                            sound, EngineApp.Instance.DefaultSoundChannelGroup, .3f, true);
                        motorSoundChannel.Position = Position;
                        motorSoundChannel.Pause    = false;
                    }
                }
            }

            //update motor channel position and pitch
            if (motorSoundChannel != null)
            {
                Range speedRangeAbs = currentGear.SpeedRange;
                if (speedRangeAbs.Minimum < 0 && speedRangeAbs.Maximum < 0)
                {
                    speedRangeAbs = new Range(-speedRangeAbs.Maximum, -speedRangeAbs.Minimum);
                }
                Range pitchRange = currentGear.SoundMotorPitchRange;

                float speedAbs = Math.Abs(GetWheelsSpeed());

                float speedCoef = 0;
                if (speedRangeAbs.Size() != 0)
                {
                    speedCoef = (speedAbs - speedRangeAbs.Minimum) / speedRangeAbs.Size();
                }
                MathFunctions.Clamp(ref speedCoef, 0, 1);

                float carpitch;
                //update channel

                /*bool onGround = LFWheel.onGround || RFWheel.onGround;
                 * if (!onGround)
                 * {
                 *  if (Intellect.IsControlKeyPressed(GameControlKeys.ArowUp))
                 *  {
                 *      carpitch = pitchRange.Minimum + pitchRange.Size();
                 *  }
                 *  else
                 *  {
                 *      carpitch = pitchRange.Minimum;
                 *  }
                 * }
                 * else*/
                {
                    carpitch = pitchRange.Minimum + speedCoef * pitchRange.Size();
                }
                motorSoundChannel.Pitch    = carpitch;
                motorSoundChannel.Position = Position;
            }
        }
Esempio n. 31
0
        private void TickChassis_backward_wheel()
        {
            bool onGround = LRWheel.onGround || RRWheel.onGround;

            float LRWheelThrottle = 0;
            float RRWheelThrottle = 0;

            if (Intellect != null)
            {
                float forward = Intellect.GetControlKeyStrength(GameControlKeys.Forward);
                LRWheelThrottle += forward;
                RRWheelThrottle += forward;

                float backward = Intellect.GetControlKeyStrength(GameControlKeys.Backward);
                LRWheelThrottle -= backward;
                RRWheelThrottle -= backward;

                MathFunctions.Clamp(ref LRWheelThrottle, -1, 1);
                MathFunctions.Clamp(ref RRWheelThrottle, -1, 1);
            }

            //return if no throttle and sleeping
            if (chassisBody.Sleeping && LRWheelThrottle == 0 && RRWheelThrottle == 0)
            {
                return;
            }

            Vec3 localLinearVelocity = chassisBody.LinearVelocity * chassisBody.Rotation.GetInverse();

            if (LRWheel.onGround)
            {
                if (LRWheelThrottle > 0 && localLinearVelocity.X < Type.Max_forward_speed)
                {
                    float force = localLinearVelocity.X > 0 ? Type.Drive_forward_force : Type.Brake_force;
                    force *= LRWheelThrottle;

                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }

                if (LRWheelThrottle < 0 && (-localLinearVelocity.X) < Type.Max_backward_speed)
                {
                    float force = localLinearVelocity.X > 0 ? Type.Brake_force : Type.Drive_backward_force;
                    force *= LRWheelThrottle;

                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }
            }

            if (RRWheel.onGround)
            {
                if (RRWheelThrottle > 0 && localLinearVelocity.X < Type.Max_forward_speed)
                {
                    float force = localLinearVelocity.X > 0 ? Type.Drive_forward_force : Type.Brake_force;
                    force *= RRWheelThrottle;

                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }

                if (RRWheelThrottle < 0 && (-localLinearVelocity.X) < Type.Max_backward_speed)
                {
                    float force = localLinearVelocity.X > 0 ? Type.Brake_force : Type.Drive_backward_force;
                    force *= RRWheelThrottle;

                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, 0, 0));
                }
            }

            #endregion Backward wheel

            #region Wheel friction

            float hand_brake_wheelfriction = 0;
            if (Intellect != null)
            {
                float hbwf = Intellect.GetControlKeyStrength(GameControlKeys.Jump);
                hand_brake_wheelfriction += hbwf;
                MathFunctions.Clamp(ref hand_brake_wheelfriction, 0, 1);
            }

            if (hand_brake_wheelfriction == 0 && onGround && localLinearVelocity != Vec3.Zero)
            {
                Vec3  velocity       = localLinearVelocity;
                float wheel_friction = Type.Wheel_friction;
                if (velocity.Y > 0.2f)
                {
                    velocity.Y -= wheel_friction;
                    if (velocity.Y == 0)
                    {
                        wheel_friction = 0;
                    }
                }
                if (-velocity.Y > 0.2f)
                {
                    velocity.Y += wheel_friction;
                    if (velocity.Y == 0)
                    {
                        wheel_friction = 0;
                    }
                }

                chassisBody.LinearVelocity = chassisBody.Rotation * velocity;
            }
            if (hand_brake_wheelfriction > 0 && onGround && localLinearVelocity != Vec3.Zero)
            {
                Vec3  velocity       = localLinearVelocity;
                float wheel_friction = Type.Hand_brake_wheelfriction;
                if (velocity.Y > 0.2f)
                {
                    velocity.Y -= wheel_friction;
                    if (velocity.Y == 0)
                    {
                        wheel_friction = 0;
                    }
                }
                if (-velocity.Y > 0.2f)
                {
                    velocity.Y += wheel_friction;
                    if (velocity.Y == 0)
                    {
                        wheel_friction = 0;
                    }
                }

                chassisBody.LinearVelocity = chassisBody.Rotation * velocity;
            }

            #endregion Wheel friction

            #region Force free stop car

            bool stop = onGround && LRWheelThrottle == 0 && RRWheelThrottle == 0;

            bool noLinearVelocity  = chassisBody.LinearVelocity.Equals(Vec3.Zero, .2f);
            bool noAngularVelocity = chassisBody.AngularVelocity.Equals(Vec3.Zero, .2f);

            //LinearDamping
            float linearDamping;
            if (stop)
            {
                linearDamping = noLinearVelocity ? 5 : Type.Force_freestop_car;
            }
            else
            {
                linearDamping = Type.Force_freestop_car;
            }
            chassisBody.LinearDamping = linearDamping;

            //AngularDamping
            if (onGround)
            {
                if (stop && noAngularVelocity)
                {
                    chassisBody.AngularDamping = 5;
                }
                else
                {
                    chassisBody.AngularDamping = 1;
                }
            }
            else
            {
                chassisBody.AngularDamping = .15f;
            }

            //sleeping
            if (!chassisBody.Sleeping && stop && noLinearVelocity && noAngularVelocity)
            {
                chassisSleepTimer += TickDelta;
                if (chassisSleepTimer > 1)
                {
                    chassisBody.Sleeping = true;
                }
            }
            else
            {
                chassisSleepTimer = 0;
            }
        }
Esempio n. 32
0
        private void TickChassis_forward_wheel()
        {
            bool onGround = LFWheel.onGround || RFWheel.onGround;

            float LFWheelThrottle = 0;
            float RFWheelThrottle = 0;

            if (Intellect != null)
            {
                float left = Intellect.GetControlKeyStrength(GameControlKeys.Left);
                LFWheelThrottle -= left * 2;
                RFWheelThrottle += left * 2;

                float right = Intellect.GetControlKeyStrength(GameControlKeys.Right);
                LFWheelThrottle += right * 2;
                RFWheelThrottle -= right * 2;

                MathFunctions.Clamp(ref LFWheelThrottle, -1, 1);
                MathFunctions.Clamp(ref RFWheelThrottle, -1, 1);
            }

            //return if no throttle and sleeping
            if (chassisBody.Sleeping && LFWheelThrottle == 0 && RFWheelThrottle == 0)
            {
                return;
            }

            //Vec3 localLinearVelocity = chassisBody.LinearVelocity * chassisBody.Rotation.GetInverse();

            float sp = GetWheelsSpeed();
            //add drive force
            float slopeLeftForceCoeffient;
            float slopeRightForceCoeffient;
            float radius = 0;

            {
                Vec3   dir        = chassisBody.Rotation.GetForward();
                Radian slopeAngle = MathFunctions.ATan(dir.Z, dir.GetNormalize().Length());
                Radian maxAngle   = MathFunctions.PI / 4;

                //%_forward_speed==============================================
                float sf1   = (Type.Max_forward_speed * 1) / 100;
                float sf10  = (Type.Max_forward_speed * 10) / 100;
                float sf20  = (Type.Max_forward_speed * 20) / 100;
                float sf30  = (Type.Max_forward_speed * 30) / 100;
                float sf40  = (Type.Max_forward_speed * 40) / 100;
                float sf50  = (Type.Max_forward_speed * 50) / 100;
                float sf60  = (Type.Max_forward_speed * 60) / 100;
                float sf70  = (Type.Max_forward_speed * 70) / 100;
                float sf80  = (Type.Max_forward_speed * 80) / 100;
                float sf90  = (Type.Max_forward_speed * 90) / 100;
                float sf110 = (Type.Max_forward_speed * 110) / 100;
                //%_forward_speed==============================================

                if (sp < 1)
                {
                    radius = Type.Speed10_rotation;
                }
                if (sp >= sf1 & sp < sf10)
                {
                    radius = Type.Speed10_rotation;
                }
                if (sp >= sf10 & sp < sf20)
                {
                    radius = Type.Speed20_rotation;
                }
                if (sp >= sf20 & sp < sf30)
                {
                    radius = Type.Speed30_rotation;
                }
                if (sp >= sf30 & sp < sf40)
                {
                    radius = Type.Speed40_rotation;
                }
                if (sp >= sf40 & sp < sf50)
                {
                    radius = Type.Speed50_rotation;
                }
                if (sp >= sf50 & sp < sf60)
                {
                    radius = Type.Speed60_rotation;
                }
                if (sp >= sf60 & sp < sf70)
                {
                    radius = Type.Speed70_rotation;
                }
                if (sp >= sf70 & sp < sf80)
                {
                    radius = Type.Speed80_rotation;
                }
                if (sp >= sf80 & sp < sf90)
                {
                    radius = Type.Speed90_rotation;
                }
                if (sp >= sf90 & sp < sf110)
                {
                    radius = Type.Speed100_rotation;
                }

                //==================================
                slopeLeftForceCoeffient = radius;
                //zero
                if (slopeAngle > maxAngle)
                {
                    slopeLeftForceCoeffient = 0;
                }
                slopeRightForceCoeffient = radius;
                //zero
                if (slopeAngle < -maxAngle)
                {
                    slopeRightForceCoeffient = 0;
                }
                //==================================

                MathFunctions.Clamp(ref slopeLeftForceCoeffient, -Type.Max_force_angle, Type.Max_force_angle);
                MathFunctions.Clamp(ref slopeRightForceCoeffient, -Type.Max_force_angle, Type.Max_force_angle);
            }

            if (LFWheel.onGround)
            {
                if (LFWheelThrottle > 0)
                {
                    float force = sp;
                    force *= LFWheelThrottle;
                    force *= slopeLeftForceCoeffient;
                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, LFWheelPositionYOffset, 0));
                }

                if (LFWheelThrottle < 0)
                {
                    float force = sp;
                    force *= LFWheelThrottle;
                    force *= slopeLeftForceCoeffient;
                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, LFWheelPositionYOffset, 0));
                }
            }

            if (RFWheel.onGround)
            {
                if (RFWheelThrottle > 0)
                {
                    float force = sp;
                    force *= RFWheelThrottle;
                    force *= slopeRightForceCoeffient;
                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, -RFWheelPositionYOffset, 0));
                }

                if (RFWheelThrottle < 0)
                {
                    float force = sp;
                    force *= RFWheelThrottle;
                    force *= slopeRightForceCoeffient;
                    chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta,
                                         new Vec3(force, 0, 0), new Vec3(0, -RFWheelPositionYOffset, 0));
                }
            }
        }
Esempio n. 33
0
        private void TickIntellect()
        {
            ShiftBooster();

            HelliOn = Intellect != null && Intellect.IsActive();
            // GUItest();

            //finding motors
            GearedMotor main = PhysicsModel.GetMotor("hellimain") as GearedMotor;
            GearedMotor back = PhysicsModel.GetMotor("helliback") as GearedMotor;

            if (HelliOn)
            {
                main.Enabled = true;
                back.Enabled = true;
            }

            //engine force + sound pitch control

            if (Intellect.IsControlKeyPressed(GameControlKeys.Forward))
            {
                force   += forceadd;
                enpitch += 0.02f;
            }
            else if (Intellect.IsControlKeyPressed(GameControlKeys.Backward))
            {
                dec      = true;
                force   -= forceadd;
                enpitch -= 0.02f;
            }
            else
            {
                dec      = false;
                enpitch -= 0.01f;
                if (force > 50)
                {
                    force -= forceadd;
                }
                if (force < 50)
                {
                    force += forceadd;
                }
            }

            MathFunctions.Clamp(ref force, 0.1f, 100 + Type.MaxForce);
            MathFunctions.Clamp(ref enpitch, 0.8f, 1.3f);

            //update helli channel position and pitch
            if (rotorSoundChannel != null)
            {
                //update channel
                rotorSoundChannel.Pitch       = enpitch;
                rotorSoundChannel.Volume      = 1;
                rotorSoundChannel.Position    = Position;
                rotorSoundChannel.MinDistance = 10;
            }

            //end of engine force + sound pitch control

            //Forces
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            //start helli Pitch
            if (Intellect.IsControlKeyPressed(GameControlKeys.ArrowUp) || Intellect.IsControlKeyPressed(GameControlKeys.ArrowDown))
            {
                float AUp   = Intellect.GetControlKeyStrength(GameControlKeys.ArrowUp) / 2;
                float ADown = Intellect.GetControlKeyStrength(GameControlKeys.ArrowDown) / 2;
                Hpitch += (AUp - ADown);
                MathFunctions.Clamp(ref Hpitch, -10, 10);

                HelliBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta,
                                   HelliBody.Rotation * new Vec3(0, 0, Hpitch / 2) * HelliBody.Mass, new Vec3(-2, 0, 0));
            }
            else
            {
                Hpitch = 0;
            }
            //end of helli pitch

            //start helli Z turn
            if (Intellect.IsControlKeyPressed(GameControlKeys.Right) || Intellect.IsControlKeyPressed(GameControlKeys.Left))
            {
                float right = Intellect.GetControlKeyStrength(GameControlKeys.Right) / 2;
                float left  = Intellect.GetControlKeyStrength(GameControlKeys.Left) / 2;
                TrunZ += (left - right);
                MathFunctions.Clamp(ref TrunZ, -10, 10);

                HelliBody.AddForce(ForceType.GlobalTorque, TickDelta,
                                   HelliBody.Rotation * new Vec3(0, 0, TrunZ) * HelliBody.Mass, Vec3.Zero);
            }
            else
            {
                TrunZ = 0;
            }

            //end of helli Z turn

            //start helli X turn
            if (Intellect.IsControlKeyPressed(GameControlKeys.ArowRight) || Intellect.IsControlKeyPressed(GameControlKeys.ArowLeft))
            {
                float rightX = Intellect.GetControlKeyStrength(GameControlKeys.ArowRight) / 2;
                float leftX  = Intellect.GetControlKeyStrength(GameControlKeys.ArowLeft) / 2;
                TrunX += (rightX - leftX);
                MathFunctions.Clamp(ref TrunX, -10, 10);

                HelliBody.AddForce(ForceType.GlobalTorque, TickDelta,
                                   HelliBody.Rotation * new Vec3(TrunX / 5, 0, 0) * HelliBody.Mass, Vec3.Zero);
            }
            else
            {
                TrunX = 0;
            }
            //end of helli X turn

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            //start of adding force

            MathFunctions.Clamp(ref force, 0.1f, 100);

            //anti gravity when helli is not decending
            if (dec == false)
            {
                HelliBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta,
                                   new Vec3(0, 0, 2) * HelliBody.Mass, Vec3.Zero);
            }

            //if Max Alt is not reached add helli motor force
            if (GetRealAlt() < Type.MaxAlt)
            {
                HelliBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta,
                                   HelliBody.Rotation * 2 * new Vec3(0, 0, force / 7) * HelliBody.Mass, Vec3.Zero);
            }

            //dampings
            HelliBody.AngularDamping = 1.5f;
            HelliBody.LinearDamping  = 0.4f;

            //another anti gravity force
            if (HelliBody.LinearVelocity.Z < 0)
            {
                HelliBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta,
                                   HelliBody.Rotation * new Vec3(0, 0, -HelliBody.LinearVelocity.Z) * HelliBody.Mass, Vec3.Zero);
            }
        }
Esempio n. 34
0
        void TickIntellect(Intellect intellect)
        {
            Vec2 forceVec = Vec2.Zero;

            if (forceMoveVectorTimer != 0)
            {
                forceVec = forceMoveVector;
            }
            else
            {
                Vec2 vec = Vec2.Zero;

                vec.X += intellect.GetControlKeyStrength(GameControlKeys.Forward);
                vec.X -= intellect.GetControlKeyStrength(GameControlKeys.Backward);
                vec.Y += intellect.GetControlKeyStrength(GameControlKeys.Left);
                vec.Y -= intellect.GetControlKeyStrength(GameControlKeys.Right);

                forceVec = (new Vec3(vec.X, vec.Y, 0) * Rotation).ToVec2();

                //TPS arcade specific (camera observe)
                //set forceVec depending on camera orientation
                if (GameMap.Instance.GameType == GameMap.GameTypes.TPSArcade && forceVec != Vec2.Zero)
                {
                    if (Intellect != null && PlayerIntellect.Instance == Intellect)
                    {
                        //!!!!!!not adapted for networking
                        //using RendererWorld.Instance.DefaultCamera is bad

                        Vec2 diff = Position.ToVec2() - RendererWorld.Instance.DefaultCamera.
                                    Position.ToVec2();
                        Degree angle    = new Radian(MathFunctions.ATan(diff.Y, diff.X));
                        Degree vecAngle = new Radian(MathFunctions.ATan(-vec.Y, vec.X));
                        Quat   rot      = new Angles(0, 0, vecAngle - angle).ToQuat();
                        forceVec = (rot * new Vec3(1, 0, 0)).ToVec2();
                    }
                }

                if (forceVec != Vec2.Zero)
                {
                    float length = forceVec.Length();
                    if (length > 1)
                    {
                        forceVec /= length;
                    }
                }
            }

            if (forceVec != Vec2.Zero)
            {
                float velocityCoefficient = 1;
                if (FastMoveInfluence != null)
                {
                    velocityCoefficient = FastMoveInfluence.Type.Coefficient;
                }

                float maxVelocity;
                float force;

                if (IsOnGround())
                {
                    maxVelocity = Type.WalkMaxVelocity;
                    force       = Type.WalkForce;
                }
                else
                {
                    maxVelocity = Type.FlyControlMaxVelocity;
                    force       = Type.FlyControlForce;
                }

                maxVelocity *= forceVec.LengthFast();

                //velocityCoefficient
                maxVelocity *= velocityCoefficient;
                force       *= velocityCoefficient;

                if (mainBody.LinearVelocity.LengthFast() < maxVelocity)
                {
                    mainBody.AddForce(ForceType.Global, 0, new Vec3(forceVec.X, forceVec.Y, 0) *
                                      force * TickDelta, Vec3.Zero);
                }
            }

            lastTickForceVector = forceVec;
        }
Esempio n. 35
0
        void TickRearWheels()
        {
            float leftwheelForce  = 0;
            float rightwheelForce = 0;

            if (Intellect != null)
            {
                ServoMotor rightwheel = PhysicsModel.GetMotor("rightwheelMotor") as ServoMotor;
                ServoMotor leftwheel  = PhysicsModel.GetMotor("leftwheelMotor") as ServoMotor;


                if (rightwheel != null)
                {
                    if (leftwheel != null)
                    {
                        float speed = Type.Rotationspeed;
                        MathFunctions.Clamp(ref speed, Type.Rotationspeed, Type.Rotationspeed);
                        Radian needAngle = leftwheel.DesiredAngle;
                        if (Intellect.IsControlKeyPressed(GameControlKeys.Left))
                        {
                            if (3 > 2)
                            {
                                leftwheelForce  += speed;
                                rightwheelForce -= speed;
                            }
                            needAngle += Type.Rotation;
                        }

                        else if (Intellect.IsControlKeyPressed(GameControlKeys.Right))
                        {
                            if (3 > 2)
                            {
                                leftwheelForce  -= speed;
                                rightwheelForce += speed;
                            }

                            needAngle -= Type.Rotation;
                        }

                        else
                        {
                            needAngle = 0f;
                        }

                        MathFunctions.Clamp(ref needAngle, new Degree(-Type.Rotationradius).InRadians(),
                                            new Degree(Type.Rotationradius).InRadians());
                        rightwheel.DesiredAngle = needAngle;
                        leftwheel.DesiredAngle  = needAngle;
                    }
                }
            }
            ServoMotor motor = PhysicsModel.GetMotor("upMotor") as ServoMotor;

            if (motor != null)
            {
                Radian needAngle = motor.DesiredAngle;

                needAngle += Intellect.GetControlKeyStrength(GameControlKeys.Fire1) * Type.Forkupspeed;
                needAngle -= Intellect.GetControlKeyStrength(GameControlKeys.Fire2) * Type.Forkupspeed;

                MathFunctions.Clamp(ref needAngle,
                                    new Degree(-0.0f).InRadians(), new Degree(Type.Forkupmax).InRadians());

                motor.DesiredAngle = needAngle;
            }
        }
Esempio n. 36
0
 protected virtual void OnIntellectCommand( Intellect.Command command )
 {
 }
Esempio n. 37
0
        public ServerOrSingle_Player ServerOrSingle_GetPlayer( Intellect intellect )
        {
            if( intellect == null )
                Log.Fatal( "PlayerManager: ServerOrSingle_GetPlayerByIntellect: intellect == null." );

            //it is can be slowly. need to use Dictionary.
            foreach( ServerOrSingle_Player player in serverOrSingle_players )
            {
                if( player.Intellect == intellect )
                    return player;
            }
            return null;
        }
        protected override void OnIntellectCommand( Intellect.Command command )
        {
            base.OnIntellectCommand( command );

            if( EntitySystemWorld.Instance.IsServer() || EntitySystemWorld.Instance.IsSingle() )
            {
                if( command.KeyPressed )
                {
                    if( command.Key >= GameControlKeys.Weapon1 && command.Key <= GameControlKeys.Weapon9 )
                    {
                        int index = (int)command.Key - (int)GameControlKeys.Weapon1;
                        SetActiveWeapon( index );
                    }

                    if( command.Key == GameControlKeys.PreviousWeapon )
                        SetActivePreviousWeapon();
                    if( command.Key == GameControlKeys.NextWeapon )
                        SetActiveNextWeapon();
                    if( command.Key == GameControlKeys.Fire1 )
                        WeaponTryFire( false );
                    if( command.Key == GameControlKeys.Fire2 )
                        WeaponTryFire( true );
                    if( command.Key == GameControlKeys.Reload )
                        WeaponTryReload();
                }
            }
        }
Esempio n. 39
0
        void TickIntellect( Intellect intellect )
        {
            Vec2 forceVec = Vec2.Zero;

            if( forceMoveVectorTimer != 0 )
            {
                forceVec = forceMoveVector;
            }
            else
            {
                Vec2 vec = Vec2.Zero;

                vec.X += intellect.GetControlKeyStrength( GameControlKeys.Forward );
                vec.X -= intellect.GetControlKeyStrength( GameControlKeys.Backward );
                vec.Y += intellect.GetControlKeyStrength( GameControlKeys.Left );
                vec.Y -= intellect.GetControlKeyStrength( GameControlKeys.Right );

                forceVec = ( new Vec3( vec.X, vec.Y, 0 ) * Rotation ).ToVec2();

                //TPS arcade specific (camera observe)
                //set forceVec depending on camera orientation
                if( GameMap.Instance.GameType == GameMap.GameTypes.TPSArcade && forceVec != Vec2.Zero )
                {
                    if( Intellect != null && PlayerIntellect.Instance == Intellect )
                    {
                        //this is not adapted for networking.
                        //using RendererWorld.Instance.DefaultCamera is bad.

                        Vec2 diff = Position.ToVec2() - RendererWorld.Instance.DefaultCamera.
                            Position.ToVec2();
                        Degree angle = new Radian( MathFunctions.ATan( diff.Y, diff.X ) );
                        Degree vecAngle = new Radian( MathFunctions.ATan( -vec.Y, vec.X ) );
                        Quat rot = new Angles( 0, 0, vecAngle - angle ).ToQuat();
                        forceVec = ( rot * new Vec3( 1, 0, 0 ) ).ToVec2();
                    }
                }

                if( forceVec != Vec2.Zero )
                {
                    float length = forceVec.Length();
                    if( length > 1 )
                        forceVec /= length;
                }
            }

            if( forceVec != Vec2.Zero )
            {
                float speedCoefficient = 1;
                if( FastMoveInfluence != null )
                    speedCoefficient = FastMoveInfluence.Type.Coefficient;

                float maxSpeed;
                float force;

                if( IsOnGround() )
                {
                    //calcualate maxSpeed and force on ground.

                    Vec2 localVec = ( new Vec3( forceVec.X, forceVec.Y, 0 ) * Rotation.GetInverse() ).ToVec2();

                    float absSum = Math.Abs( localVec.X ) + Math.Abs( localVec.Y );
                    if( absSum > 1 )
                        localVec /= absSum;

                    bool running = IsNeedRun();

                    maxSpeed = 0;
                    force = 0;

                    if( Math.Abs( localVec.X ) >= .001f )
                    {
                        //forward and backward
                        float speedX;
                        if( localVec.X > 0 )
                            speedX = running ? Type.RunForwardMaxSpeed : Type.WalkForwardMaxSpeed;
                        else
                            speedX = running ? Type.RunBackwardMaxSpeed : Type.WalkBackwardMaxSpeed;
                        maxSpeed += speedX * Math.Abs( localVec.X );
                        force += ( running ? Type.RunForce : Type.WalkForce ) * Math.Abs( localVec.X );
                    }

                    if( Math.Abs( localVec.Y ) >= .001f )
                    {
                        //left and right
                        maxSpeed += ( running ? Type.RunSideMaxSpeed : Type.WalkSideMaxSpeed ) *
                            Math.Abs( localVec.Y );
                        force += ( running ? Type.RunForce : Type.WalkForce ) * Math.Abs( localVec.Y );
                    }
                }
                else
                {
                    //calcualate maxSpeed and force when flying.
                    maxSpeed = Type.FlyControlMaxSpeed;
                    force = Type.FlyControlForce;
                }

                //speedCoefficient
                maxSpeed *= speedCoefficient;
                force *= speedCoefficient;

                if( mainBody.LinearVelocity.LengthFast() < maxSpeed )
                {
                    mainBody.AddForce( ForceType.Global, 0, new Vec3( forceVec.X, forceVec.Y, 0 ) *
                        force * TickDelta, Vec3.Zero );
                }
            }

            lastTickForceVector = forceVec;
        }