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 System.Threading.Tasks;
- using System.Threading;
- using PlayerIOClient;
- using System.IO;
- /*
- *
- * v1.1
- *
- * Introducing EEObject!
- * EEObject allows you to create fun games, such as a pellet shooter game, or something wild and crazy!
- * With EEObject, you can tap into an easy way to create a fun game.
- *
- * By SirJosh3917 - ninjasupeatsninja
- * Documentation at josh0n.weebly.com
- */
- namespace EEObjects
- {
- /// <summary>
- /// An EEObject. This is an object that lives in an EEObjectMap.
- /// 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.
- /// </summary>
- class EEObject
- {
- public Connection con;
- public EEObjectMap map;
- public int objOldX_, objOldY_, x, y, blockId, replaceBlockId, blockPlacementTime, blockLayer;
- public bool objectIsAlive = false;
- public bool objectIsMoving = false;
- public bool useThreading = false;
- public object[] blockPlaceParameters;
- public object[] replaceBlockPlaceParameters;
- /// <summary>
- /// Initilize an object at x, y (and give it the correct connection to use)
- /// </summary>
- /// <param name="x_">The X position of the object</param>
- /// <param name="y_">The Y position of the object</param>
- /// <param name="blockId_">The BlockID of the object</param>
- /// <param name="replaceBlockId_">The BlockID to replace the object when it moves somewhere else.</param>
- /// <param name="blockPlacementTime_">The amount of time to wait before each placement</param>
- /// <param name="placeBlockLayer_">The layer of the block to replace and place</param>
- /// <param name="useThreading_">If you would like to use threading. If this is true, expect a lot of lag.</param>
- /// <param name="con_">The connection to use for placing the block</param>
- /// <param name="life">The EEObjectMap for the EEObject to live on.</param>
- /// <param name="blockPlaceParams_">The parameters of the block that needs to be placed. Leave blank if there are none.</param>
- /// <param name="replaceBlockPlaceParams_">The parameters of the replacing block that needs to be placed. Leave blank if there are none.</param>
- 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)
- {
- //variables
- objectIsAlive = true;
- x = x_;
- y = y_;
- objOldX_ = x;
- objOldY_ = y;
- blockId = blockId_;
- replaceBlockId = replaceBlockId_;
- con = con_;
- blockPlacementTime = blockPlacementTime_;
- map = life;
- blockPlaceParameters = blockPlaceParams_;
- replaceBlockPlaceParameters = replaceBlockPlaceParams_;
- objectIsAlive = true;
- objectIsMoving = false;
- useThreading = useThreading_;
- blockLayer = placeBlockLayer_;
- if (useThreading)
- {
- Thread objLifetime = new Thread(delegate() { ObjectThread(); });
- objLifetime.Start();
- }
- }
- /// <summary>
- /// Terminate the object fully.
- /// </summary>
- public void TerminateObject()
- {
- //variables
- objectIsMoving = false;
- objectIsAlive = false;
- }
- /// <summary>
- /// Moves the object by moverX in x, and moverY in y.
- /// </summary>
- /// <param name="moverX">The amount of spaces to move along the X axis</param>
- /// <param name="moverY">The amount of spaces to move along the Y axis</param>
- public void MoveObject(int moverX, int moverY)
- {
- while (objectIsMoving) { /* Do Nothing while the object is moving */ }
- x = x + moverX;
- y = y + moverY;
- if (map.Overlap)
- {
- x = map.CorrectX(x);
- y = map.CorrectY(y);
- }
- objectIsMoving = true;
- if (!useThreading)
- if (objectIsMoving)
- {
- if (map.OnMap(x, y))
- {
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, x, y, blockId);
- else
- con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
- Thread.Sleep(blockPlacementTime);
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId);
- else
- con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId, replaceBlockPlaceParameters);
- objOldX_ = x;
- objOldY_ = y;
- objectIsMoving = false;
- }
- else
- {
- if (map.Kill)
- {
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, x, y, blockId);
- else
- con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
- Thread.Sleep(blockPlacementTime);
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId);
- else
- con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId, replaceBlockPlaceParameters);
- this.TerminateObject();
- }
- if (map.Overlap)
- {
- x = map.CorrectX(x);
- y = map.CorrectY(y);
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, x, y, blockId);
- else
- con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
- Thread.Sleep(blockPlacementTime);
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId);
- else
- con.Send("b", blockLayer, objOldX_, objOldY_, replaceBlockId, replaceBlockPlaceParameters);
- objOldX_ = x;
- objOldY_ = y;
- objectIsMoving = false;
- }
- }
- }
- if (!useThreading)
- objectIsMoving = false;
- }
- /// <summary>
- /// Never call in your code. This code will automatically be ran when the object is initialized, and determines the outcome of the object.
- /// </summary>
- public void ObjectThread()
- {
- int objOldX = x;
- int objOldY = y;
- while (objectIsAlive)
- {
- if (objectIsMoving)
- {
- if (map.OnMap(x, y))
- {
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, x, y, blockId);
- else
- con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
- Thread.Sleep(blockPlacementTime);
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId);
- else
- con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId, replaceBlockPlaceParameters);
- objOldX = x;
- objOldY = y;
- objectIsMoving = false;
- }
- else
- {
- if (map.Kill)
- {
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, x, y, blockId);
- else
- con.Send("b", blockLayer, x, y, blockId, blockPlaceParameters);
- Thread.Sleep(blockPlacementTime);
- if (replaceBlockPlaceParameters == null)
- con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId);
- else
- con.Send("b", blockLayer, objOldX, objOldY, replaceBlockId, replaceBlockPlaceParameters);
- this.TerminateObject();
- }
- if (map.Overlap)
- {
- x = map.CorrectX(x);
- y = map.CorrectY(y);
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// The map for the EE Object to live on. It will always live as long as it's on the map.
- /// If it's not on the map and overlap is enabled, then it'll just go to the other side.
- /// It'll also kill the object when its off the map, if kill is enabled.
- /// </summary>
- class EEObjectMap
- {
- public int x1;
- public int x2;
- public int y1;
- public int y2;
- public bool Overlap;
- public bool Kill;
- /// <summary>
- /// Initialize the map for the object to live on.
- /// </summary>
- /// <param name="x1_">The left-wall of the map. Objects are not allowed to go on the border.</param>
- /// <param name="x2_">The right-wall of the map. Objects are not allowed to go on the border.</param>
- /// <param name="y1_">The upper wall of the map. Objects are not allowed to go on the border.</param>
- /// <param name="y2_">The lower wall of the map. Objects are not allowed to go on the border.</param>
- /// <param name="Overlap_">If you would like an object to overlap when it is off the map.</param>
- /// <param name="Kill_">If you would like to kill the object when it is off the map.</param>
- public void InitializeMap(int x1_, int x2_, int y1_, int y2_, bool Overlap_, bool Kill_)
- {
- x1 = x1_;
- x2 = x2_;
- y1 = y1_;
- y2 = y2_;
- Overlap = Overlap_;
- Kill = Kill_;
- }
- /// <summary>
- /// Returns if an object is off the map at a certain X and Y position.
- /// </summary>
- /// <param name="x">The X position of the object.</param>
- /// <param name="y">The Y position of the object.</param>
- /// <returns>Returns true if the object is on the map, else, it returns false.</returns>
- public bool OnMap(int x, int y)
- {
- if (x > x1 && x < x2 && y > y1 && y < y2)
- return true;
- return false;
- }
- /// <summary>
- /// Corrects an object's X position if needs to overlap.
- /// </summary>
- /// <param name="x">The X position</param>
- /// <returns>Returns the correct over-lapped X position.</returns>
- public int CorrectX(int x)
- {
- if (x <= x1)
- x = x2 - 1;
- if (x >= x2)
- x = x1 + 1;
- return x;
- }
- /// <summary>
- /// Corrects an object's Y position if needs to overlap.
- /// </summary>
- /// <param name="y">The Y position</param>
- /// <returns>Returns the correct over-lapped Y position.</returns>
- public int CorrectY(int y)
- {
- if (y <= y1)
- y = y2 - 1;
- if (y >= y2)
- y = y1 + 1;
- return y;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement