Advertisement
JoshON

EEObject.cs

Mar 22nd, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using PlayerIOClient;
  8. using System.IO;
  9.  
  10. /*
  11.  *
  12.  * v1.1
  13.  *
  14.  * Introducing EEObject!
  15.  * EEObject allows you to create fun games, such as a pellet shooter game, or something wild and crazy!
  16.  * With EEObject, you can tap into an easy way to create a fun game.
  17.  *
  18.  * By SirJosh3917 - ninjasupeatsninja
  19.  * Documentation at josh0n.weebly.com
  20.  */
  21. namespace EEObjects
  22. {
  23.     /// <summary>
  24.     /// An EEObject. This is an object that lives in an EEObjectMap.
  25.     /// It is useful for making some sort of pellet-shooter thing, or any kind of program that requires an object to be on a map.
  26.     /// </summary>
  27.     class EEObject
  28.     {
  29.         public Connection con;
  30.         public EEObjectMap map;
  31.         public int objOldX_, objOldY_, x, y, blockId, replaceBlockId, blockPlacementTime, blockLayer;
  32.         public bool objectIsAlive = false;
  33.         public bool objectIsMoving = false;
  34.         public bool useThreading = false;
  35.         public object[] blockPlaceParameters;
  36.         public object[] replaceBlockPlaceParameters;
  37.  
  38.         /// <summary>
  39.         /// Initilize an object at x, y (and give it the correct connection to use)
  40.         /// </summary>
  41.         /// <param name="x_">The X position of the object</param>
  42.         /// <param name="y_">The Y position of the object</param>
  43.         /// <param name="blockId_">The BlockID of the object</param>
  44.         /// <param name="replaceBlockId_">The BlockID to replace the object when it moves somewhere else.</param>
  45.         /// <param name="blockPlacementTime_">The amount of time to wait before each placement</param>
  46.         /// <param name="placeBlockLayer_">The layer of the block to replace and place</param>
  47.         /// <param name="useThreading_">If you would like to use threading. If this is true, expect a lot of lag.</param>
  48.         /// <param name="con_">The connection to use for placing the block</param>
  49.         /// <param name="life">The EEObjectMap for the EEObject to live on.</param>
  50.         /// <param name="blockPlaceParams_">The parameters of the block that needs to be placed. Leave blank if there are none.</param>
  51.         /// <param name="replaceBlockPlaceParams_">The parameters of the replacing block that needs to be placed. Leave blank if there are none.</param>
  52.         public void InitializeObject(int x_, int y_, int blockId_, int replaceBlockId_, int blockPlacementTime_, int placeBlockLayer_, bool useThreading_, Connection con_, EEObjectMap life, object[] blockPlaceParams_ = null, object[] replaceBlockPlaceParams_ = null)
  53.         {
  54.             //variables
  55.             objectIsAlive = true;
  56.             x = x_;
  57.             y = y_;
  58.             objOldX_ = x;
  59.             objOldY_ = y;
  60.             blockId = blockId_;
  61.             replaceBlockId = replaceBlockId_;
  62.             con = con_;
  63.             blockPlacementTime = blockPlacementTime_;
  64.             map = life;
  65.             blockPlaceParameters = blockPlaceParams_;
  66.             replaceBlockPlaceParameters = replaceBlockPlaceParams_;
  67.             objectIsAlive = true;
  68.             objectIsMoving = false;
  69.             useThreading = useThreading_;
  70.             blockLayer = placeBlockLayer_;
  71.             if (useThreading)
  72.             {
  73.                 Thread objLifetime = new Thread(delegate() { ObjectThread(); });
  74.                 objLifetime.Start();
  75.             }
  76.         }
  77.  
  78.         /// <summary>
  79.         /// Terminate the object fully.
  80.         /// </summary>
  81.         public void TerminateObject()
  82.         {
  83.             //variables
  84.             objectIsMoving = false;
  85.             objectIsAlive = false;
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Moves the object by moverX in x, and moverY in y.
  90.         /// </summary>
  91.         /// <param name="moverX">The amount of spaces to move along the X axis</param>
  92.         /// <param name="moverY">The amount of spaces to move along the Y axis</param>
  93.         public void MoveObject(int moverX, int moverY)
  94.         {
  95.             while (objectIsMoving) { /* Do Nothing while the object is moving */ }
  96.             x = x + moverX;
  97.             y = y + moverY;
  98.             if (map.Overlap)
  99.             {
  100.                 x = map.CorrectX(x);
  101.                 y = map.CorrectY(y);
  102.             }
  103.             objectIsMoving = true;
  104.             if (!useThreading)
  105.                 if (objectIsMoving)
  106.                 {
  107.                     if (map.OnMap(x, y))
  108.                     {
  109.                         if (replaceBlockPlaceParameters == null)
  110.                             con.Send("b", blockLayer, x, y, blockId);
  111.                         else
  112.                             con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
  113.                         Thread.Sleep(blockPlacementTime);
  114.                         if (replaceBlockPlaceParameters == null)
  115.                             con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId);
  116.                         else
  117.                             con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId, replaceBlockPlaceParameters);
  118.                         objOldX_ = x;
  119.                         objOldY_ = y;
  120.                         objectIsMoving = false;
  121.                     }
  122.                     else
  123.                     {
  124.                         if (map.Kill)
  125.                         {
  126.                             if (replaceBlockPlaceParameters == null)
  127.                                 con.Send("b", blockLayer, x, y, blockId);
  128.                             else
  129.                                 con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
  130.                             Thread.Sleep(blockPlacementTime);
  131.                             if (replaceBlockPlaceParameters == null)
  132.                                 con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId);
  133.                             else
  134.                                 con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId, replaceBlockPlaceParameters);
  135.                             this.TerminateObject();
  136.                         }
  137.                         if (map.Overlap)
  138.                         {
  139.                             x = map.CorrectX(x);
  140.                             y = map.CorrectY(y);
  141.                             if (replaceBlockPlaceParameters == null)
  142.                                 con.Send("b", blockLayer, x, y, blockId);
  143.                             else
  144.                                 con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
  145.                             Thread.Sleep(blockPlacementTime);
  146.                             if (replaceBlockPlaceParameters == null)
  147.                                 con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId);
  148.                             else
  149.                                 con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId, replaceBlockPlaceParameters);
  150.                             objOldX_ = x;
  151.                             objOldY_ = y;
  152.                             objectIsMoving = false;
  153.                         }
  154.                     }
  155.                 }
  156.             if (!useThreading)
  157.                 objectIsMoving = false;
  158.         }
  159.  
  160.         /// <summary>
  161.         /// Never call in your code. This code will automatically be ran when the object is initialized, and determines the outcome of the object.
  162.         /// </summary>
  163.         public void ObjectThread()
  164.         {
  165.             int objOldX = x;
  166.             int objOldY = y;
  167.             while (objectIsAlive)
  168.             {
  169.                 if (objectIsMoving)
  170.                 {
  171.                     if (map.OnMap(x, y))
  172.                     {
  173.                         if (replaceBlockPlaceParameters == null)
  174.                             con.Send("b", blockLayer, x, y, blockId);
  175.                         else
  176.                             con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
  177.                         Thread.Sleep(blockPlacementTime);
  178.                         if (replaceBlockPlaceParameters == null)
  179.                             con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId);
  180.                         else
  181.                             con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId, replaceBlockPlaceParameters);
  182.                         objOldX = x;
  183.                         objOldY = y;
  184.                         objectIsMoving = false;
  185.                     }
  186.                     else
  187.                     {
  188.                         if (map.Kill)
  189.                         {
  190.                             if (replaceBlockPlaceParameters == null)
  191.                                 con.Send("b", blockLayer, x, y, blockId);
  192.                             else
  193.                                 con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
  194.                             Thread.Sleep(blockPlacementTime);
  195.                             if (replaceBlockPlaceParameters == null)
  196.                                 con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId);
  197.                             else
  198.                                 con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId, replaceBlockPlaceParameters);
  199.                             this.TerminateObject();
  200.                         }
  201.                         if (map.Overlap)
  202.                         {
  203.                             x = map.CorrectX(x);
  204.                             y = map.CorrectY(y);
  205.                         }
  206.                     }
  207.                 }
  208.             }
  209.         }
  210.     }
  211.     /// <summary>
  212.     /// The map for the EE Object to live on. It will always live as long as it's on the map.
  213.     /// If it's not on the map and overlap is enabled, then it'll just go to the other side.
  214.     /// It'll also kill the object when its off the map, if kill is enabled.
  215.     /// </summary>
  216.     class EEObjectMap
  217.     {
  218.         public int x1;
  219.         public int x2;
  220.         public int y1;
  221.         public int y2;
  222.         public bool Overlap;
  223.         public bool Kill;
  224.  
  225.         /// <summary>
  226.         /// Initialize the map for the object to live on.
  227.         /// </summary>
  228.         /// <param name="x1_">The left-wall of the map. Objects are not allowed to go on the border.</param>
  229.         /// <param name="x2_">The right-wall of the map. Objects are not allowed to go on the border.</param>
  230.         /// <param name="y1_">The upper wall of the map. Objects are not allowed to go on the border.</param>
  231.         /// <param name="y2_">The lower wall of the map. Objects are not allowed to go on the border.</param>
  232.         /// <param name="Overlap_">If you would like an object to overlap when it is off the map.</param>
  233.         /// <param name="Kill_">If you would like to kill the object when it is off the map.</param>
  234.         public void InitializeMap(int x1_, int x2_, int y1_, int y2_, bool Overlap_, bool Kill_)
  235.         {
  236.             x1 = x1_;
  237.             x2 = x2_;
  238.             y1 = y1_;
  239.             y2 = y2_;
  240.             Overlap = Overlap_;
  241.             Kill = Kill_;
  242.         }
  243.  
  244.         /// <summary>
  245.         /// Returns if an object is off the map at a certain X and Y position.
  246.         /// </summary>
  247.         /// <param name="x">The X position of the object.</param>
  248.         /// <param name="y">The Y position of the object.</param>
  249.         /// <returns>Returns true if the object is on the map, else, it returns false.</returns>
  250.         public bool OnMap(int x, int y)
  251.         {
  252.             if (x > x1 && x < x2 && y > y1 && y < y2)
  253.                 return true;
  254.             return false;
  255.         }
  256.  
  257.         /// <summary>
  258.         /// Corrects an object's X position if needs to overlap.
  259.         /// </summary>
  260.         /// <param name="x">The X position</param>
  261.         /// <returns>Returns the correct over-lapped X position.</returns>
  262.         public int CorrectX(int x)
  263.         {
  264.             if (x <= x1)
  265.                 x = x2 - 1;
  266.             if (x >= x2)
  267.                 x = x1 + 1;
  268.             return x;
  269.         }
  270.  
  271.         /// <summary>
  272.         /// Corrects an object's Y position if needs to overlap.
  273.         /// </summary>
  274.         /// <param name="y">The Y position</param>
  275.         /// <returns>Returns the correct over-lapped Y position.</returns>
  276.         public int CorrectY(int y)
  277.         {
  278.             if (y <= y1)
  279.                 y = y2 - 1;
  280.             if (y >= y2)
  281.                 y = y1 + 1;
  282.             return y;
  283.         }
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement