Advertisement
Guest User

working piranha

a guest
May 3rd, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;              // Required to use XNA features.
  6. using XNAMachinationisRatio;                // Required to use the XNA Machinationis Ratio Engine general features.
  7. using XNAMachinationisRatio.AI;             // Required to use the XNA Machinationis Ratio general AI features.
  8. using System.Diagnostics;
  9. using System.Threading;
  10.  
  11. /* LERNING PILL: XNAMachinationisRatio Engine
  12.  * XNAMachinationisRatio is an engine that allows implementing
  13.  * simulations and games based on XNA, simplifying the use of XNA
  14.  * and adding features not directly available in XNA.
  15.  * XNAMachinationisRatio is a work in progress.
  16.  * The engine works "under the hood", taking care of many features
  17.  * of an interactive simulation automatically, thus minimizing
  18.  * the amount of code that developers have to write.
  19.  *
  20.  * In order to use the engine, the application main class (Kernel, in the
  21.  * case of FishO'Rama) creates, initializes and stores
  22.  * an instance of class Engine in one of its data members.
  23.  *
  24.  * The classes comprised in the  XNA Machinationis Ratio engine and the
  25.  * related functionalities can be accessed from any of your XNA project
  26.  * source code files by adding appropriate 'using' statements at the beginning of
  27.  * the file.
  28.  *
  29.  */
  30.  
  31. namespace FishORama
  32. {
  33.     /* LEARNING PILL: Token behaviors in the XNA Machinationis Ratio engine
  34.      * Some simulation tokens may need to enact specific behaviors in order to
  35.      * participate in the simulation. The XNA Machinationis Ratio engine
  36.      * allows a token to enact a behavior by associating an artificial intelligence
  37.      * mind to it. Mind objects are created from subclasses of the class AIPlayer
  38.      * included in the engine. In order to associate a mind to a token, a new
  39.      * mind object must be created, passing to the constructor of the mind a reference
  40.      * of the object that must be associated with the mind. This must be done in
  41.      * the DefaultProperties method of the token.
  42.      *
  43.      * Hence, every time a new tipe of AI mind is required, a new class derived from
  44.      * AIPlayer must be created, and an instance of it must be associated to the
  45.      * token classes that need it.
  46.      *
  47.      * Mind objects enact behaviors through the method Update (see below for further details).
  48.      */
  49.     class PiranhaMind : AIPlayer
  50.     {
  51.  
  52.         #region Data Members
  53.  
  54.         // This mind needs to interact with the token which it possesses,
  55.         // since it needs to know where are the aquarium's boundaries.
  56.         // Hence, the mind needs a "link" to the aquarium, which is why it stores in
  57.         // an instance variable a reference to its aquarium.
  58.         private AquariumToken mAquarium;        // Reference to the aquarium in which the creature lives.
  59.  
  60.         private float mFacingDirection;         // Direction the fish is facing (1: right; -1: left).
  61.         private float mSpeed = 5;
  62.         private float hDistance = 400;
  63.  
  64.         private bool feed = false;
  65.         private double currentTime;
  66.         private double expiryTime;
  67.         private PiranhaToken mToken;
  68.         //defined the acceptable distance of pix
  69.         Boolean RemoveLegX = false;
  70.         Boolean RemoveLegY = false;
  71.    
  72.  
  73.         #endregion
  74.  
  75.         #region Properties
  76.  
  77.         /// <summary>
  78.         /// Set Aquarium in which the mind's behavior should be enacted.
  79.         /// </summary>
  80.         public AquariumToken Aquarium
  81.         {
  82.             set { mAquarium = value; }
  83.         }
  84.  
  85.         #endregion
  86.  
  87.         #region Constructors
  88.  
  89.         /// <summary>
  90.         /// Default constructor.
  91.         /// </summary>
  92.         /// <param name="pToken">Token to be associated with the mind.</param>
  93.         public PiranhaMind(X2DToken pToken)
  94.         {
  95.             /* LEARNING PILL: associating a mind with a token
  96.              * In order for a mind to control a token, it must be associated with the token.
  97.              * This is done when the mind is constructed, using the method Possess inherited
  98.              * from class AIPlayer.
  99.              */
  100.             mFacingDirection = 1;
  101.             this.Possess(pToken);
  102.  
  103.             mToken = (PiranhaToken)pToken;
  104.         }
  105.  
  106.         #endregion
  107.  
  108.         #region Methods
  109.  
  110.         /* LEARNING PILL: The AI update method.
  111.          * Mind objects enact behaviors through the method Update. This method is
  112.          * automatically invoked by the engine, periodically, 'under the hood'. This can be
  113.          * be better understood that the engine asks to all the available AI-based tokens:
  114.          * "Would you like to do anything at all?" And this 'asking' is done through invoking
  115.          * the Update method of each mind available in the system. The response is the execution
  116.          * of the Update method of each mind , and all the methods possibly triggered by Update.
  117.          *
  118.          * Although the Update method could invoke other methods if needed, EVERY
  119.          * BEHAVIOR STARTS from Update. If a behavior is not directly coded in Updated, or in
  120.          * a method invoked by Update, then it is IGNORED.
  121.          *
  122.          */
  123.  
  124.         /// <summary>
  125.         /// AI Update method.
  126.         /// </summary>
  127.         /// <param name="pGameTime">Game time</param>
  128.       #region pHungry
  129.         private Vector3 PHungry(Vector3 tokenPosition)
  130.         {
  131.             tokenPosition.X = tokenPosition.X + mSpeed * mFacingDirection;
  132.  
  133.             if (tokenPosition.X > hDistance || tokenPosition.X < -hDistance) // hDistance = horizontalDitance
  134.             {
  135.                 mFacingDirection = -mFacingDirection;
  136.                 mSpeed = 5;
  137.             }
  138.  
  139.             return tokenPosition;
  140.         }
  141.        #endregion
  142.  
  143.        #region fullState
  144.  
  145.         private Vector3 fullState(Vector3 tokenPosition)
  146.  
  147.         {
  148.  
  149.             return tokenPosition;  
  150.         }
  151.        #endregion
  152.  
  153.        #region pFeeding
  154.         private Vector3 PFeeding(Vector3 tokenPosition)
  155.         {
  156.  
  157.             if (tokenPosition.X < mAquarium.ChickenLeg.Position.X)
  158.             {
  159.                 tokenPosition.X += mSpeed;
  160.                 mFacingDirection = 1;
  161.             }
  162.             else if (tokenPosition.X > mAquarium.ChickenLeg.Position.X)
  163.             {
  164.                 tokenPosition.X -= mSpeed;
  165.                 mFacingDirection = -1;
  166.             }
  167.  
  168.  
  169.             if (tokenPosition.Y < mAquarium.ChickenLeg.Position.Y)
  170.             {
  171.                 tokenPosition.Y += mSpeed;
  172.             }
  173.             else if (tokenPosition.Y > mAquarium.ChickenLeg.Position.Y)
  174.             {
  175.                 tokenPosition.Y -= mSpeed;
  176.             }
  177.  
  178.             //DELETION////
  179.  
  180.             if (tokenPosition.X - mAquarium.ChickenLeg.Position.X <= 10 && tokenPosition.X - mAquarium.ChickenLeg.Position.X >= -10)
  181.             {
  182.                 RemoveLegX = true;
  183.             }
  184.  
  185.             if (tokenPosition.Y - mAquarium.ChickenLeg.Position.Y <= 10 && tokenPosition.Y - mAquarium.ChickenLeg.Position.Y >= -10)
  186.             {
  187.                 RemoveLegY = true;
  188.             }
  189.  
  190.  
  191.             if (RemoveLegX && RemoveLegY == true)
  192.             {
  193.                 mAquarium.RemoveChickenLeg();
  194.                 RemoveLegX = false;
  195.                 RemoveLegY = false;
  196.                 //reduce speed for 5 seconds - full state
  197.                 tokenPosition = fullState(tokenPosition); //passess object
  198.             }
  199.            
  200.  
  201.  
  202.             return tokenPosition;
  203.         }
  204.  
  205.        #endregion
  206.  
  207.         public override void Update(ref GameTime pGameTime)
  208.         {
  209.  
  210.           currentTime += pGameTime.ElapsedGameTime.TotalSeconds;
  211.  
  212.             Vector3 tokenPosition = this.PossessedToken.Position;
  213.             this.PossessedToken.Orientation = new Vector3(mFacingDirection,
  214.                                                 this.PossessedToken.Orientation.Y,
  215.                                                 this.PossessedToken.Orientation.Z);
  216.  
  217.             currentTime = pGameTime.TotalGameTime.TotalSeconds;
  218.             if (mAquarium.ChickenLeg == null && feed == false)
  219.             {
  220.                 tokenPosition = PHungry(tokenPosition); //passess object
  221.                
  222.             }
  223.             else if (mAquarium.ChickenLeg != null)
  224.             {
  225.                 tokenPosition = PFeeding(tokenPosition); //passess object
  226.             }
  227.             this.PossessedToken.Position = tokenPosition; // possesses token */
  228.  
  229.  
  230.         }
  231.  
  232.        
  233.         #endregion
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement