Advertisement
Guest User

Mc Gimper - some code

a guest
May 30th, 2020
1,680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.94 KB | None | 0 0
  1. //Hi, it's Pawel. That's the basic code you need for Point and Click game like Mc Gimper
  2. //It could be written better but there's no need to fix a game that works fine at 1500-2400 FPS
  3. //https://www.youtube.com/channel/UCPcmUOVswVvxW39S8nrh7Rw?sub_confirmation=1
  4. //Have fun!
  5.  
  6. ////////////////////////////////////////////////////
  7.  
  8. using System.Collections;
  9. using UnityEngine;
  10.  
  11. public class clickableObject : MonoBehaviour
  12. {
  13.     public enum clickableAction
  14.     {
  15.         equipAsHat,
  16.         kick,
  17.         equipAsItem,
  18.         touchObject
  19.     }
  20.     public bool blockCanvas = false;
  21.     public Sprite[] requiredItem;
  22.     public clickableAction[] action;
  23.     public Transform playerStandingPosT = null;
  24.     SimpleAnimations playerAnimation = null;
  25.     IEnumerator chosenActionCoroutine;
  26.  
  27.     public void StartAction()
  28.     {
  29.         //every item from array has it's own action with the same index
  30.         int requiredItems = requiredItem.Length, actions = action.Length;
  31.         if (requiredItems != actions)
  32.             Debug.Log("Broken clickable object!");
  33.         //grab player
  34.         playerAnimation = GameObject.FindGameObjectWithTag("Player").GetComponent<SimpleAnimations>();
  35.         Sprite item = playerAnimation.itemSR.sprite;
  36.         clickableAction chosenAction = clickableAction.kick;
  37.         for (int i = 0; i < requiredItems; i++) //find player's item in array
  38.         {
  39.             if (requiredItem[i] == item)
  40.                 chosenAction = action[i]; //choose action
  41.         }
  42.  
  43.         if (chosenAction == clickableAction.kick) //choose coroutine baced on enum action
  44.             chosenActionCoroutine = KickObject();
  45.         else if (chosenAction == clickableAction.equipAsHat)
  46.             chosenActionCoroutine = EquipAsHat();
  47.         else if (chosenAction == clickableAction.equipAsItem)
  48.             chosenActionCoroutine = EquipAsItem();
  49.         else if (chosenAction == clickableAction.touchObject)
  50.             chosenActionCoroutine = TouchObject();
  51.         else
  52.         {
  53.             Debug.Log("Couldn't find action");
  54.             return;
  55.         }
  56.         StartCoroutine(GoToClickableCall());
  57.     }
  58.  
  59.     public IEnumerator GoToClickableCall()
  60.     {
  61.         FindObjectOfType<Canvas>().enabled = false; //turn off canvas
  62.         Walking walking = playerAnimation.GetComponent<Walking>();
  63.         walking.GoToClickable(this); //go to clicked obj
  64.         yield return new WaitForSeconds(0.5f);
  65.         while (!walking.reachedTarget)
  66.             yield return new WaitForSeconds(0.1f);
  67.         yield return new WaitForSeconds(0.5f);
  68.         StartCoroutine(chosenActionCoroutine);
  69.     }
  70.  
  71.     public IEnumerator EquipAsHat() //not necessary
  72.     {
  73.         playerAnimation.bodySR.sprite = playerAnimation.sprites_3[0];
  74.         ChangePlayerItemTransform(playerAnimation.touchItemT);
  75.         yield return new WaitForSeconds(0.5f);
  76.         playerAnimation.bodySR.sprite = playerAnimation.defaultSprite;
  77.         ChangePlayerItemTransform(playerAnimation.idleItemT);
  78.         SpriteRenderer mysR = GetComponentInChildren<SpriteRenderer>(),
  79.             hatSR = playerAnimation.hatSR;
  80.         hatSR.sprite = mysR.sprite;
  81.         mysR.sprite = null;
  82.         FindObjectOfType<Canvas>().enabled = true; //turn on canvas
  83.     }
  84.     public IEnumerator EquipAsItem()//not necessary, you can have only one item
  85.     {
  86.         playerAnimation.bodySR.sprite = playerAnimation.sprites_3[0];
  87.         yield return new WaitForSeconds(0.5f);
  88.         playerAnimation.bodySR.sprite = playerAnimation.defaultSprite;
  89.         SpriteRenderer mySR = GetComponentInChildren<SpriteRenderer>(),
  90.             itemSR = playerAnimation.itemSR;
  91.         itemSR.sprite = mySR.sprite;
  92.         mySR.gameObject.SetActive(false);
  93.         FindObjectOfType<Canvas>().enabled = true; //turn on canvas
  94.     }
  95.  
  96.     public IEnumerator KickObject()//not necessary
  97.     {
  98.         playerAnimation.playAnimation = true;
  99.         playerAnimation.StartCoroutine(playerAnimation.PlayAnimation(1));
  100.         yield return new WaitForSeconds(0.25f);
  101.         if(GetComponent<ObjectReaction>())
  102.             GetComponent<ObjectReaction>().Reaction();
  103.         yield return new WaitForSeconds(0.5f);
  104.         playerAnimation.playAnimation = false;
  105.         FindObjectOfType<Canvas>().enabled = !blockCanvas; //turn on/off canvas
  106.     }
  107.     public IEnumerator TouchObject()
  108.     {
  109.         ChangePlayerItemTransform(playerAnimation.touchItemT);
  110.         playerAnimation.bodySR.sprite = playerAnimation.sprites_3[0];
  111.         yield return new WaitForSeconds(0.5f);
  112.         ChangePlayerItemTransform(playerAnimation.idleItemT);
  113.         playerAnimation.bodySR.sprite = playerAnimation.defaultSprite;
  114.         if (GetComponent<ObjectReaction>())
  115.             GetComponent<ObjectReaction>().Reaction();
  116.         FindObjectOfType<Canvas>().enabled = !blockCanvas; //turn on/off canvas
  117.     }
  118.  
  119.     private void ChangePlayerItemTransform(Transform t)
  120.     {
  121.         playerAnimation.itemSR.transform.position = t.position;
  122.         playerAnimation.itemSR.transform.rotation = t.rotation;
  123.     }
  124. }
  125.  
  126. ///////////////////////////////////////////////////////////////////////
  127.  
  128. using System.Collections;
  129. using UnityEngine;
  130.  
  131. public class Walking : MonoBehaviour
  132. {
  133.     public float speed = 12,
  134.         accuracy = 0.02f;
  135.     public bool reachedTarget = true,
  136.         animated = true,
  137.         flipObj = true;
  138.     SimpleAnimations simpleAnim = null;
  139.     //[Header("clickable")]
  140.     bool calledbyClickable = false;
  141.     Vector2 clickablePos = Vector2.zero;
  142.  
  143.     private void Awake()//you can skip this if want risk
  144.     {
  145.         simpleAnim = GetComponent<SimpleAnimations>();
  146.         if (accuracy < speed * 0.025f)
  147.             accuracy = speed * 0.025f;
  148.     }
  149.  
  150.     public IEnumerator GoToTarget(Vector3 target)
  151.     {
  152.         reachedTarget = false;
  153.         target = SnapVector2ToGrid(target);
  154.         if (flipObj)
  155.         {
  156.             if (target.x < transform.position.x)
  157.                 transform.localScale = new Vector2(-1, 1);
  158.             else if (target.x > transform.position.x)
  159.                 transform.localScale = new Vector2(1, 1);
  160.         }
  161.         if (animated)
  162.         {
  163.             simpleAnim.playAnimation = true;
  164.             simpleAnim.StartCoroutine(simpleAnim.PlayAnimation(0)); //walking
  165.         }
  166.         reachedTarget = false;
  167.         float dist = (transform.position - target).magnitude; //calculate distance
  168.         Vector2 direction = (target - transform.position).normalized;
  169.         while (dist > accuracy) //note: if the accuracy is too little, you may overshoot the target
  170.         {
  171.             transform.position += (Vector3)direction * speed * Time.deltaTime;//move towards target
  172.             dist = (transform.position - target).magnitude; //calculate distance
  173.             yield return new WaitForEndOfFrame();
  174.         }
  175.         EndWalking(target); //face target, snap to grid
  176.     }
  177.  
  178.     private void EndWalking(Vector3 target)
  179.     {
  180.         transform.position = target;
  181.         if (calledbyClickable) // face clickable
  182.         {
  183.             if (clickablePos.x < transform.position.x)
  184.                 transform.localScale = new Vector2(-1, 1);
  185.             else
  186.                 transform.localScale = new Vector2(1, 1);
  187.         }
  188.         calledbyClickable = false;
  189.         reachedTarget = true;
  190.         if (animated)
  191.             simpleAnim.playAnimation = false;
  192.     }
  193.  
  194.     public void GoToPoint(Vector2 pos)
  195.     {
  196.         StopAllCoroutines();
  197.         StartCoroutine(GoToTarget(pos));
  198.     }
  199.     public void GoToClickable(clickableObject c)
  200.     {
  201.         clickablePos = c.transform.position;
  202.         calledbyClickable = true;
  203.         GoToPoint(c.playerStandingPosT.position);
  204.     }
  205.  
  206.     public Vector2 SnapVector2ToGrid(Vector2 v)
  207.     {
  208.         return new Vector2(Mathf.Round(v.x * 100) / 100, Mathf.Round(v.y * 100) / 100);
  209.     }
  210. }
  211.  
  212. /////////////////////////////////////////////////////
  213.  
  214. using System.Collections;
  215. using UnityEngine;
  216.  
  217. public class ObjectReaction : MonoBehaviour
  218. {
  219.     public enum kickReaction
  220.     {
  221.         none,
  222.         main_menu_start
  223.     }
  224.     public GameObject[] relatedObjects = null;
  225.     public kickReaction myReaction;
  226.  
  227.     public void Reaction() //put there all reactions
  228.     {
  229.         switch (myReaction)
  230.         {
  231.             case kickReaction.none:
  232.                 Debug.Log("No reaction");
  233.                 break;
  234.             case kickReaction.main_menu_start:
  235.                 StartCoroutine(Main_menu_start());
  236.                 break;
  237.         }
  238.     }
  239.     public IEnumerator Main_menu_start()
  240.     {
  241.         relatedObjects[0].SetActive(false);
  242.         yield return new WaitForSeconds(0.25f);
  243.         Debug.Log("Game Started");
  244.     }
  245. }
  246.  
  247. /////////////////////////////////////////////
  248.  
  249. using System.Collections;
  250. using UnityEngine;
  251.  
  252. public class SimpleAnimations : MonoBehaviour
  253. {
  254.     static float frameWait = 0.016667f;
  255.     public bool playAnimation = false, playOneShotAnim = false,
  256.         anim1OnStart = false, animateHat = false;
  257.     public SpriteRenderer bodySR = null,
  258.         hatSR = null,
  259.         itemSR = null;
  260.     Transform hatTransform = null,
  261.         itemTransform = null;
  262.     Vector2 originalHatPos = Vector2.zero;
  263.     public Transform idleItemT = null,
  264.         walkItemT = null,
  265.         kickItemT = null,
  266.         touchItemT = null;
  267.  
  268.     public int framesOfGap = 20;
  269.     public Sprite defaultSprite = null;
  270.     [Header("Animation 1")]
  271.     public Sprite[] sprites_1 = null; //default walk
  272.  
  273.     [Header("Animation 2")]
  274.     public Sprite[] sprites_2 = null;//default kick
  275.  
  276.     [Header("Animation 3")]
  277.     public Sprite[] sprites_3 = null;//default touch
  278.  
  279.     private void Awake()
  280.     {
  281.         AutoSetVariables();
  282.         if (anim1OnStart)
  283.         {
  284.             playAnimation = true;
  285.             StartCoroutine(PlayAnimation(0));
  286.         }
  287.     }
  288.  
  289.     private void AutoSetVariables()
  290.     {
  291.         if (!bodySR)
  292.             Debug.Log("Set animated SR! " + name);
  293.         if (hatSR)
  294.         {
  295.             hatTransform = hatSR.transform;
  296.             originalHatPos = hatTransform.localPosition;
  297.         }
  298.         if (itemSR)
  299.             itemTransform = itemSR.transform;
  300.     }
  301.  
  302.     public IEnumerator PlayAnimation(int i)
  303.     {
  304.         Sprite[] sprites = ChooseSpritesForAnim(i);
  305.         int spritesAmount = sprites.Length - 1,
  306.             spritesNum = 0;
  307.         while (playAnimation)
  308.         {
  309.             bodySR.sprite = sprites[spritesNum++]; //animate body
  310.             if (spritesNum > spritesAmount)
  311.             {
  312.                 spritesNum = 0;
  313.                 if (playOneShotAnim)
  314.                     playAnimation = false;
  315.             }
  316.  
  317.             if(hatSR && animateHat)
  318.                 AnimateHat(spritesNum);
  319.             if(itemSR)
  320.                 AnimateItem(spritesNum, i);
  321.              yield return new WaitForSeconds(frameWait * framesOfGap);
  322.         }
  323.         ReturnToDefault();
  324.     }
  325.  
  326.     private void ReturnToDefault()
  327.     {
  328.         bodySR.sprite = defaultSprite;
  329.         if (hatTransform)
  330.             hatTransform.localPosition = originalHatPos;
  331.         if (itemTransform)
  332.             AnimateItem(0,0);
  333.     }
  334.  
  335.     private Sprite[] ChooseSpritesForAnim(int i)
  336.     {
  337.         Sprite[] sprites;
  338.         if (i == 0)
  339.             sprites = sprites_1;
  340.         else if (i == 1)
  341.             sprites = sprites_2;
  342.         else
  343.             sprites = sprites_3;
  344.         return sprites;
  345.     }
  346.  
  347.     private void AnimateItem(int i, int anim)
  348.     {
  349.         Transform target = walkItemT;
  350.         if (anim == 1 && i != 0)
  351.             target = kickItemT;
  352.         else if (anim == 2)
  353.             target = touchItemT;
  354.         else if(i == 0)
  355.             target = idleItemT;
  356.         itemTransform.position = target.position;
  357.         itemTransform.localRotation = target.localRotation;
  358.     }
  359.  
  360.     private void AnimateHat(int i)
  361.     {
  362.         if (i % 2 == 0)
  363.             hatTransform.position += new Vector3(0, 0.05f);
  364.         else
  365.             hatTransform.localPosition = originalHatPos;
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement