Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework; // Required to use XNA features.
- using XNAMachinationisRatio; // Required to use the XNA Machinationis Ratio Engine general features.
- using XNAMachinationisRatio.AI; // Required to use the XNA Machinationis Ratio general AI features.
- using System.Diagnostics;
- using System.Threading;
- /* LERNING PILL: XNAMachinationisRatio Engine
- * XNAMachinationisRatio is an engine that allows implementing
- * simulations and games based on XNA, simplifying the use of XNA
- * and adding features not directly available in XNA.
- * XNAMachinationisRatio is a work in progress.
- * The engine works "under the hood", taking care of many features
- * of an interactive simulation automatically, thus minimizing
- * the amount of code that developers have to write.
- *
- * In order to use the engine, the application main class (Kernel, in the
- * case of FishO'Rama) creates, initializes and stores
- * an instance of class Engine in one of its data members.
- *
- * The classes comprised in the XNA Machinationis Ratio engine and the
- * related functionalities can be accessed from any of your XNA project
- * source code files by adding appropriate 'using' statements at the beginning of
- * the file.
- *
- */
- namespace FishORama
- {
- /* LEARNING PILL: Token behaviors in the XNA Machinationis Ratio engine
- * Some simulation tokens may need to enact specific behaviors in order to
- * participate in the simulation. The XNA Machinationis Ratio engine
- * allows a token to enact a behavior by associating an artificial intelligence
- * mind to it. Mind objects are created from subclasses of the class AIPlayer
- * included in the engine. In order to associate a mind to a token, a new
- * mind object must be created, passing to the constructor of the mind a reference
- * of the object that must be associated with the mind. This must be done in
- * the DefaultProperties method of the token.
- *
- * Hence, every time a new tipe of AI mind is required, a new class derived from
- * AIPlayer must be created, and an instance of it must be associated to the
- * token classes that need it.
- *
- * Mind objects enact behaviors through the method Update (see below for further details).
- */
- class PiranhaMind : AIPlayer
- {
- #region Data Members
- // This mind needs to interact with the token which it possesses,
- // since it needs to know where are the aquarium's boundaries.
- // Hence, the mind needs a "link" to the aquarium, which is why it stores in
- // an instance variable a reference to its aquarium.
- private AquariumToken mAquarium; // Reference to the aquarium in which the creature lives.
- private float mFacingDirection; // Direction the fish is facing (1: right; -1: left).
- private float mSpeed = 5;
- private float hDistance = 400;
- private bool feed = false;
- private double currentTime;
- private double expiryTime;
- private PiranhaToken mToken;
- //defined the acceptable distance of pix
- Boolean RemoveLegX = false;
- Boolean RemoveLegY = false;
- #endregion
- #region Properties
- /// <summary>
- /// Set Aquarium in which the mind's behavior should be enacted.
- /// </summary>
- public AquariumToken Aquarium
- {
- set { mAquarium = value; }
- }
- #endregion
- #region Constructors
- /// <summary>
- /// Default constructor.
- /// </summary>
- /// <param name="pToken">Token to be associated with the mind.</param>
- public PiranhaMind(X2DToken pToken)
- {
- /* LEARNING PILL: associating a mind with a token
- * In order for a mind to control a token, it must be associated with the token.
- * This is done when the mind is constructed, using the method Possess inherited
- * from class AIPlayer.
- */
- mFacingDirection = 1;
- this.Possess(pToken);
- mToken = (PiranhaToken)pToken;
- }
- #endregion
- #region Methods
- /* LEARNING PILL: The AI update method.
- * Mind objects enact behaviors through the method Update. This method is
- * automatically invoked by the engine, periodically, 'under the hood'. This can be
- * be better understood that the engine asks to all the available AI-based tokens:
- * "Would you like to do anything at all?" And this 'asking' is done through invoking
- * the Update method of each mind available in the system. The response is the execution
- * of the Update method of each mind , and all the methods possibly triggered by Update.
- *
- * Although the Update method could invoke other methods if needed, EVERY
- * BEHAVIOR STARTS from Update. If a behavior is not directly coded in Updated, or in
- * a method invoked by Update, then it is IGNORED.
- *
- */
- /// <summary>
- /// AI Update method.
- /// </summary>
- /// <param name="pGameTime">Game time</param>
- #region pHungry
- private Vector3 PHungry(Vector3 tokenPosition)
- {
- tokenPosition.X = tokenPosition.X + mSpeed * mFacingDirection;
- if (tokenPosition.X > hDistance || tokenPosition.X < -hDistance) // hDistance = horizontalDitance
- {
- mFacingDirection = -mFacingDirection;
- mSpeed = 5;
- }
- return tokenPosition;
- }
- #endregion
- #region fullState
- private Vector3 fullState(Vector3 tokenPosition)
- {
- return tokenPosition;
- }
- #endregion
- #region pFeeding
- private Vector3 PFeeding(Vector3 tokenPosition)
- {
- if (tokenPosition.X < mAquarium.ChickenLeg.Position.X)
- {
- tokenPosition.X += mSpeed;
- mFacingDirection = 1;
- }
- else if (tokenPosition.X > mAquarium.ChickenLeg.Position.X)
- {
- tokenPosition.X -= mSpeed;
- mFacingDirection = -1;
- }
- if (tokenPosition.Y < mAquarium.ChickenLeg.Position.Y)
- {
- tokenPosition.Y += mSpeed;
- }
- else if (tokenPosition.Y > mAquarium.ChickenLeg.Position.Y)
- {
- tokenPosition.Y -= mSpeed;
- }
- //DELETION////
- if (tokenPosition.X - mAquarium.ChickenLeg.Position.X <= 10 && tokenPosition.X - mAquarium.ChickenLeg.Position.X >= -10)
- {
- RemoveLegX = true;
- }
- if (tokenPosition.Y - mAquarium.ChickenLeg.Position.Y <= 10 && tokenPosition.Y - mAquarium.ChickenLeg.Position.Y >= -10)
- {
- RemoveLegY = true;
- }
- if (RemoveLegX && RemoveLegY == true)
- {
- mAquarium.RemoveChickenLeg();
- RemoveLegX = false;
- RemoveLegY = false;
- //reduce speed for 5 seconds - full state
- tokenPosition = fullState(tokenPosition); //passess object
- }
- return tokenPosition;
- }
- #endregion
- public override void Update(ref GameTime pGameTime)
- {
- currentTime += pGameTime.ElapsedGameTime.TotalSeconds;
- Vector3 tokenPosition = this.PossessedToken.Position;
- this.PossessedToken.Orientation = new Vector3(mFacingDirection,
- this.PossessedToken.Orientation.Y,
- this.PossessedToken.Orientation.Z);
- currentTime = pGameTime.TotalGameTime.TotalSeconds;
- if (mAquarium.ChickenLeg == null && feed == false)
- {
- tokenPosition = PHungry(tokenPosition); //passess object
- }
- else if (mAquarium.ChickenLeg != null)
- {
- tokenPosition = PFeeding(tokenPosition); //passess object
- }
- this.PossessedToken.Position = tokenPosition; // possesses token */
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement