Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hi, it's Pawel. That's the basic code you need for Point and Click game like Mc Gimper
- //It could be written better but there's no need to fix a game that works fine at 1500-2400 FPS
- //https://www.youtube.com/channel/UCPcmUOVswVvxW39S8nrh7Rw?sub_confirmation=1
- //Have fun!
- ////////////////////////////////////////////////////
- using System.Collections;
- using UnityEngine;
- public class clickableObject : MonoBehaviour
- {
- public enum clickableAction
- {
- equipAsHat,
- kick,
- equipAsItem,
- touchObject
- }
- public bool blockCanvas = false;
- public Sprite[] requiredItem;
- public clickableAction[] action;
- public Transform playerStandingPosT = null;
- SimpleAnimations playerAnimation = null;
- IEnumerator chosenActionCoroutine;
- public void StartAction()
- {
- //every item from array has it's own action with the same index
- int requiredItems = requiredItem.Length, actions = action.Length;
- if (requiredItems != actions)
- Debug.Log("Broken clickable object!");
- //grab player
- playerAnimation = GameObject.FindGameObjectWithTag("Player").GetComponent<SimpleAnimations>();
- Sprite item = playerAnimation.itemSR.sprite;
- clickableAction chosenAction = clickableAction.kick;
- for (int i = 0; i < requiredItems; i++) //find player's item in array
- {
- if (requiredItem[i] == item)
- chosenAction = action[i]; //choose action
- }
- if (chosenAction == clickableAction.kick) //choose coroutine baced on enum action
- chosenActionCoroutine = KickObject();
- else if (chosenAction == clickableAction.equipAsHat)
- chosenActionCoroutine = EquipAsHat();
- else if (chosenAction == clickableAction.equipAsItem)
- chosenActionCoroutine = EquipAsItem();
- else if (chosenAction == clickableAction.touchObject)
- chosenActionCoroutine = TouchObject();
- else
- {
- Debug.Log("Couldn't find action");
- return;
- }
- StartCoroutine(GoToClickableCall());
- }
- public IEnumerator GoToClickableCall()
- {
- FindObjectOfType<Canvas>().enabled = false; //turn off canvas
- Walking walking = playerAnimation.GetComponent<Walking>();
- walking.GoToClickable(this); //go to clicked obj
- yield return new WaitForSeconds(0.5f);
- while (!walking.reachedTarget)
- yield return new WaitForSeconds(0.1f);
- yield return new WaitForSeconds(0.5f);
- StartCoroutine(chosenActionCoroutine);
- }
- public IEnumerator EquipAsHat() //not necessary
- {
- playerAnimation.bodySR.sprite = playerAnimation.sprites_3[0];
- ChangePlayerItemTransform(playerAnimation.touchItemT);
- yield return new WaitForSeconds(0.5f);
- playerAnimation.bodySR.sprite = playerAnimation.defaultSprite;
- ChangePlayerItemTransform(playerAnimation.idleItemT);
- SpriteRenderer mysR = GetComponentInChildren<SpriteRenderer>(),
- hatSR = playerAnimation.hatSR;
- hatSR.sprite = mysR.sprite;
- mysR.sprite = null;
- FindObjectOfType<Canvas>().enabled = true; //turn on canvas
- }
- public IEnumerator EquipAsItem()//not necessary, you can have only one item
- {
- playerAnimation.bodySR.sprite = playerAnimation.sprites_3[0];
- yield return new WaitForSeconds(0.5f);
- playerAnimation.bodySR.sprite = playerAnimation.defaultSprite;
- SpriteRenderer mySR = GetComponentInChildren<SpriteRenderer>(),
- itemSR = playerAnimation.itemSR;
- itemSR.sprite = mySR.sprite;
- mySR.gameObject.SetActive(false);
- FindObjectOfType<Canvas>().enabled = true; //turn on canvas
- }
- public IEnumerator KickObject()//not necessary
- {
- playerAnimation.playAnimation = true;
- playerAnimation.StartCoroutine(playerAnimation.PlayAnimation(1));
- yield return new WaitForSeconds(0.25f);
- if(GetComponent<ObjectReaction>())
- GetComponent<ObjectReaction>().Reaction();
- yield return new WaitForSeconds(0.5f);
- playerAnimation.playAnimation = false;
- FindObjectOfType<Canvas>().enabled = !blockCanvas; //turn on/off canvas
- }
- public IEnumerator TouchObject()
- {
- ChangePlayerItemTransform(playerAnimation.touchItemT);
- playerAnimation.bodySR.sprite = playerAnimation.sprites_3[0];
- yield return new WaitForSeconds(0.5f);
- ChangePlayerItemTransform(playerAnimation.idleItemT);
- playerAnimation.bodySR.sprite = playerAnimation.defaultSprite;
- if (GetComponent<ObjectReaction>())
- GetComponent<ObjectReaction>().Reaction();
- FindObjectOfType<Canvas>().enabled = !blockCanvas; //turn on/off canvas
- }
- private void ChangePlayerItemTransform(Transform t)
- {
- playerAnimation.itemSR.transform.position = t.position;
- playerAnimation.itemSR.transform.rotation = t.rotation;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- using System.Collections;
- using UnityEngine;
- public class Walking : MonoBehaviour
- {
- public float speed = 12,
- accuracy = 0.02f;
- public bool reachedTarget = true,
- animated = true,
- flipObj = true;
- SimpleAnimations simpleAnim = null;
- //[Header("clickable")]
- bool calledbyClickable = false;
- Vector2 clickablePos = Vector2.zero;
- private void Awake()//you can skip this if want risk
- {
- simpleAnim = GetComponent<SimpleAnimations>();
- if (accuracy < speed * 0.025f)
- accuracy = speed * 0.025f;
- }
- public IEnumerator GoToTarget(Vector3 target)
- {
- reachedTarget = false;
- target = SnapVector2ToGrid(target);
- if (flipObj)
- {
- if (target.x < transform.position.x)
- transform.localScale = new Vector2(-1, 1);
- else if (target.x > transform.position.x)
- transform.localScale = new Vector2(1, 1);
- }
- if (animated)
- {
- simpleAnim.playAnimation = true;
- simpleAnim.StartCoroutine(simpleAnim.PlayAnimation(0)); //walking
- }
- reachedTarget = false;
- float dist = (transform.position - target).magnitude; //calculate distance
- Vector2 direction = (target - transform.position).normalized;
- while (dist > accuracy) //note: if the accuracy is too little, you may overshoot the target
- {
- transform.position += (Vector3)direction * speed * Time.deltaTime;//move towards target
- dist = (transform.position - target).magnitude; //calculate distance
- yield return new WaitForEndOfFrame();
- }
- EndWalking(target); //face target, snap to grid
- }
- private void EndWalking(Vector3 target)
- {
- transform.position = target;
- if (calledbyClickable) // face clickable
- {
- if (clickablePos.x < transform.position.x)
- transform.localScale = new Vector2(-1, 1);
- else
- transform.localScale = new Vector2(1, 1);
- }
- calledbyClickable = false;
- reachedTarget = true;
- if (animated)
- simpleAnim.playAnimation = false;
- }
- public void GoToPoint(Vector2 pos)
- {
- StopAllCoroutines();
- StartCoroutine(GoToTarget(pos));
- }
- public void GoToClickable(clickableObject c)
- {
- clickablePos = c.transform.position;
- calledbyClickable = true;
- GoToPoint(c.playerStandingPosT.position);
- }
- public Vector2 SnapVector2ToGrid(Vector2 v)
- {
- return new Vector2(Mathf.Round(v.x * 100) / 100, Mathf.Round(v.y * 100) / 100);
- }
- }
- /////////////////////////////////////////////////////
- using System.Collections;
- using UnityEngine;
- public class ObjectReaction : MonoBehaviour
- {
- public enum kickReaction
- {
- none,
- main_menu_start
- }
- public GameObject[] relatedObjects = null;
- public kickReaction myReaction;
- public void Reaction() //put there all reactions
- {
- switch (myReaction)
- {
- case kickReaction.none:
- Debug.Log("No reaction");
- break;
- case kickReaction.main_menu_start:
- StartCoroutine(Main_menu_start());
- break;
- }
- }
- public IEnumerator Main_menu_start()
- {
- relatedObjects[0].SetActive(false);
- yield return new WaitForSeconds(0.25f);
- Debug.Log("Game Started");
- }
- }
- /////////////////////////////////////////////
- using System.Collections;
- using UnityEngine;
- public class SimpleAnimations : MonoBehaviour
- {
- static float frameWait = 0.016667f;
- public bool playAnimation = false, playOneShotAnim = false,
- anim1OnStart = false, animateHat = false;
- public SpriteRenderer bodySR = null,
- hatSR = null,
- itemSR = null;
- Transform hatTransform = null,
- itemTransform = null;
- Vector2 originalHatPos = Vector2.zero;
- public Transform idleItemT = null,
- walkItemT = null,
- kickItemT = null,
- touchItemT = null;
- public int framesOfGap = 20;
- public Sprite defaultSprite = null;
- [Header("Animation 1")]
- public Sprite[] sprites_1 = null; //default walk
- [Header("Animation 2")]
- public Sprite[] sprites_2 = null;//default kick
- [Header("Animation 3")]
- public Sprite[] sprites_3 = null;//default touch
- private void Awake()
- {
- AutoSetVariables();
- if (anim1OnStart)
- {
- playAnimation = true;
- StartCoroutine(PlayAnimation(0));
- }
- }
- private void AutoSetVariables()
- {
- if (!bodySR)
- Debug.Log("Set animated SR! " + name);
- if (hatSR)
- {
- hatTransform = hatSR.transform;
- originalHatPos = hatTransform.localPosition;
- }
- if (itemSR)
- itemTransform = itemSR.transform;
- }
- public IEnumerator PlayAnimation(int i)
- {
- Sprite[] sprites = ChooseSpritesForAnim(i);
- int spritesAmount = sprites.Length - 1,
- spritesNum = 0;
- while (playAnimation)
- {
- bodySR.sprite = sprites[spritesNum++]; //animate body
- if (spritesNum > spritesAmount)
- {
- spritesNum = 0;
- if (playOneShotAnim)
- playAnimation = false;
- }
- if(hatSR && animateHat)
- AnimateHat(spritesNum);
- if(itemSR)
- AnimateItem(spritesNum, i);
- yield return new WaitForSeconds(frameWait * framesOfGap);
- }
- ReturnToDefault();
- }
- private void ReturnToDefault()
- {
- bodySR.sprite = defaultSprite;
- if (hatTransform)
- hatTransform.localPosition = originalHatPos;
- if (itemTransform)
- AnimateItem(0,0);
- }
- private Sprite[] ChooseSpritesForAnim(int i)
- {
- Sprite[] sprites;
- if (i == 0)
- sprites = sprites_1;
- else if (i == 1)
- sprites = sprites_2;
- else
- sprites = sprites_3;
- return sprites;
- }
- private void AnimateItem(int i, int anim)
- {
- Transform target = walkItemT;
- if (anim == 1 && i != 0)
- target = kickItemT;
- else if (anim == 2)
- target = touchItemT;
- else if(i == 0)
- target = idleItemT;
- itemTransform.position = target.position;
- itemTransform.localRotation = target.localRotation;
- }
- private void AnimateHat(int i)
- {
- if (i % 2 == 0)
- hatTransform.position += new Vector3(0, 0.05f);
- else
- hatTransform.localPosition = originalHatPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement