Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » DirectX » Games » Downloads
 
Add your own
alternative version

Game Engine

, 20 Nov 2009
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MTV3D65;

namespace gefx
{
   public  class UserInput
    {
       TVInputEngine input;

       float sngPositionX;
       float sngPositionY;
       float sngPositionZ = -20;
       float sngLookAtX;
       float sngLookAtY;
       float sngLookAtZ;

       float sngLookatX;
       float sngLookatY;
       float sngLookatZ;

       float sngWalk;
       float sngStrafe;
       float sngAngleX, sngAngleY;

      public  int tmpMouseX, tmpMouseY;
      public  bool tmpMouseB1, tmpMouseB2, tmpMouseB3, tmpMouseB4;
      public  int tmpMouseScrollOld, tmpMouseScrollNew;

       public float  WalkIntDef = 0.1f;

       public bool PressA = false;
       public bool PressS = false;
       public bool PressD = false;


       float cX = 0;
       float cY = 0;
       float cZ = 0;

       float cdX = 0;
       float cdY = 0;
       float cdZ = 0;

       public UserInput()
       {
           input = new TVInputEngine();
           //input.Initialize();



       }

       public void SetCameraFPS(ref Camera Cam, ref TVEngine tv)
       {


           if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_UP))
           {
               cX += 0.1f;
           }

           if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWN))
           {
               cX -= 0.1f;
           }


           if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFT))
           {
               cZ += 0.1f;
           }

           if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHT))
           {

               cZ -= 0.1f;

           }

           
           tmpMouseScrollOld = tmpMouseScrollNew;


           Cam.SetUpCameraPosition(new TV_3DVECTOR(cX,cY,cZ ));


       }

       public  void CheckMovement(ref Camera Cam, ref TVEngine tv)
       {
           bool Enable = false;

           if(input.IsKeyPressed(CONST_TV_KEY.TV_KEY_SPACE))
           {
               Enable = true;

           }

           if (Enable)
           {

               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_UP))
               {
                   sngWalk = WalkIntDef;
               }

               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWN))
               {
                   sngWalk = -WalkIntDef;
               }


               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFT))
               {
                   sngStrafe = WalkIntDef;
               }

               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHT))
               {

                   sngStrafe = -WalkIntDef;

               }

               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_A))
               {

                   PressA = true;
                   PressD = false;
                   PressS = false;
              
               }
               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_S))
               {

                   PressS = true;
                   PressA = false;
                   PressD = false;

                   
               }
               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_D))
               {
                   
                   PressD = true;
                   PressA = false;
                   PressS = false;
               

                   
               }

               tmpMouseScrollOld = tmpMouseScrollNew;

               input.GetMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2, ref tmpMouseB3, ref tmpMouseB4, ref tmpMouseScrollNew);
               //input.GetAbsMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2);

               sngAngleX = sngAngleX - ((float)tmpMouseY / 100);
               sngAngleY = sngAngleY - ((float)tmpMouseX / 100);



              


               if (sngAngleX > 1.3)
               {
                   sngAngleX = 1.3f;
               }
               if (sngAngleX < -1.3)
               {
                   sngAngleX = -1.3f;
               }


               if (sngWalk > 0)
               {
                   sngWalk = sngWalk - 0.05f;
                   if (sngWalk < 0)
                   {
                       sngWalk = 0.0f;
                   }
               }

               if (sngWalk < 0)
               {
                   sngWalk = sngWalk + 0.05f;
                   if (sngWalk > 0)
                   {
                       sngWalk = 0;
                   }
               }

               if (sngStrafe > 0)
               {
                   sngStrafe = sngStrafe - 0.05f;
                   if (sngStrafe < 0)
                   {
                       sngStrafe = 0;
                   }
               }

               if (sngStrafe < 0)
               {
                   sngStrafe = sngStrafe + 0.05f;
                   if (sngStrafe > 0)
                   {
                       sngStrafe = 0;
                   }
               }

               sngPositionX = sngPositionX + (float)(System.Math.Cos((double)sngAngleY) * sngWalk * tv.TimeElapsed()) + (float)(System.Math.Cos((double)sngAngleY + 3.141596 / 2) * sngStrafe * tv.TimeElapsed());
               sngPositionZ = sngPositionZ + (float)(System.Math.Sin((double)sngAngleY) * sngWalk * tv.TimeElapsed()) + (float)(System.Math.Cos((double)sngAngleY + 3.141596 / 2) * sngStrafe * tv.TimeElapsed());
               sngPositionY = sngPositionY + (float)(System.Math.Sin((double)sngAngleY) * sngWalk * tv.TimeElapsed()) + (float)(System.Math.Sin((double)sngAngleY + 3.141596 / 2) * sngStrafe * tv.TimeElapsed());
               sngLookAtX = sngPositionX + (float)System.Math.Cos((double)sngAngleY);
               sngLookAtY = sngPositionY + (float)System.Math.Tan((double)sngAngleX);
               sngLookAtZ = sngPositionZ + (float)System.Math.Sin((double)sngAngleY);
               TV_3DVECTOR v;
               v.x = sngPositionX;
               v.y = sngPositionY;
               v.z = sngPositionZ;

               Cam.SetUpCameraPosition(v);
               Cam.SetUpCameraDirection(sngLookAtX, sngLookAtY, sngLookAtZ);
           }

       }

       public void CheckMovement(ref Camera Cam, ref TVEngine TV, TVLandscape Land)
       {
           bool Enable = false;

           if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_SPACE))
           {
               Enable = true;

           }

           if (Enable)
           {
               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_UP))
                   sngWalk = 1;

                               // If we are not walking forward, maybe we are walking backward
               // by using the DOWN arrow? If so, set walk speed to negative.
               else if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWN))
                   sngWalk = -1;

               // Check if we pressed the LEFT arrow key, if so, then strafe
               // on the left.
               if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFT))
                   sngStrafe = 1;

               // If we are not strafing left, maybe we want to strafe to the
               // right, using the RIGHT arrow? If so, set strafe to negative.
               else if (input.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHT))
                   sngStrafe = -1;

               // Actual value to old mouse scroller value.
               tmpMouseScrollOld = tmpMouseScrollNew;

               // Get the movement of the mouse.
               input.GetMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2, ref tmpMouseB3, ref tmpMouseB4, ref tmpMouseScrollNew);

               // Update the camera angles.
               sngAngleX = sngAngleX - ((float)tmpMouseY / 100);
               sngAngleY = sngAngleY - ((float)tmpMouseX / 100);

               if (sngAngleX > 1.3)
                   sngAngleX = 1.3F;
               if (sngAngleX < -1.3)
                   sngAngleX = -1.3F;
               // Okay, now for the smothing of the movement... Update
               // the forward and backward (walk) movement.
               if (sngWalk > 0)
               {
                   sngWalk = sngWalk - 0.005F * (float)TV.TimeElapsed();
                   if (sngWalk < 0)
                       sngWalk = 0;
               }
               else
               {
                   sngWalk = sngWalk + 0.005F * (float)TV.TimeElapsed();
                   if (sngWalk > 0)
                       sngWalk = 0;
               }

               // Now, we update the left and right (strafe) movement.
               if (sngStrafe > 0)
               {
                   sngStrafe = sngStrafe - 0.005F * (float)TV.TimeElapsed();
                   if (sngStrafe < 0)
                       sngStrafe = 0;
               }
               else
               {
                   sngStrafe = sngStrafe + 0.005F * (float)TV.TimeElapsed();
                   if (sngStrafe > 0)
                       sngStrafe = 0;
               }

               // Update the vectors using the angles and positions.
               sngPositionX = sngPositionX + (float)(System.Math.Cos((double)sngAngleY) * sngWalk / 5 * TV.TimeElapsed()) + (float)(System.Math.Cos((double)sngAngleY + 3.141596 / 2) * sngStrafe / 5 * TV.TimeElapsed());
               sngPositionZ = sngPositionZ + (float)(System.Math.Sin((double)sngAngleY) * sngWalk / 5 * TV.TimeElapsed()) + (float)(System.Math.Sin((double)sngAngleY + 3.141596 / 2) * sngStrafe / 5 * TV.TimeElapsed());

               // New : because we are using a landscape with up and down, we
               // can't let the camera at the same height. We want the camera to
               // follow the height of the map, so we use the "get height". Also,
               // because we want to have the effect that we are not a mouse,
               // we will add some height to the height returned...
               sngPositionY = Land.GetHeight(sngPositionX, sngPositionZ) + 10;

               // We update the look at position.
               sngLookatX = sngPositionX + (float)System.Math.Cos((double)sngAngleY);
               sngLookatY = sngPositionY + (float)System.Math.Tan((double)sngAngleX);
               sngLookatZ = sngPositionZ + (float)System.Math.Sin((double)sngAngleY);


               // With the new values of the camera vectors (position and
               // look at), we update the scene's camera.
               TV_3DVECTOR v;
               v.x = sngPositionX;
               v.y = sngPositionY;
               v.z = sngPositionZ;

               Cam.SetUpCameraPosition(v);
               Cam.SetUpCameraDirection(sngLookatX, sngLookatY, sngLookatZ);
               
               input.GetAbsMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2);
               
           }

       }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)

About the Author

Member 3873633
Software Developer
Croatia Croatia
Programer

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Member 3873633
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid