using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using static UnityEditor.Progress;
public class BrickCoinController : MonoBehaviour
{
private Animator anim;
[SerializeField]
private GameObject coinPrefab;
private bool isCrashed = false;
void Start()
{
this.anim = this.GetComponent<Animator>();
}
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "mario")
{
this.anim.SetBool("isCrashed", true);
if (isCrashed == false)
{
Instantiate(coinPrefab, new Vector3(transform.position.x, transform.position.y + 1.2f, transform.position.z), transform.rotation);
isCrashed = true;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using static UnityEditor.Progress;
public class BrickMushroomController : MonoBehaviour
{
private AudioSource audioSource;
[SerializeField]
private AudioClip mushroomProduceSfx;
private Animator anim;
[SerializeField]
private GameObject mushroomPrefab;
private bool isCrashed = false;
void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
this.anim = this.GetComponent<Animator>();
}
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "mario")
{
this.anim.SetBool("isCrashed", true);
if (isCrashed == false)
{
Instantiate(mushroomPrefab, new Vector3(transform.position.x, transform.position.y + 0.9f, transform.position.z), transform.rotation);
this.audioSource.PlayOneShot(this.mushroomProduceSfx);
isCrashed = true;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
private Transform playerTransform;
[SerializeField]
Vector3 cameraPosition;
[SerializeField]
Vector2 center;
[SerializeField]
Vector2 mapSize;
[SerializeField]
float cameraMoveSpeed;
float height;
float width;
public void Init()
{
playerTransform = GameObject.Find("Mario").GetComponent<Transform>();
height = Camera.main.orthographicSize;
width = height * Screen.width / Screen.height;
}
void FixedUpdate()
{
LimitCameraArea();
}
void LimitCameraArea()
{
if (playerTransform == null) return;
transform.position = Vector3.Lerp(transform.position,
playerTransform.position + cameraPosition,
Time.deltaTime * cameraMoveSpeed);
float lx = mapSize.x - width;
float clampX = Mathf.Clamp(transform.position.x, -lx + center.x, lx + center.x);
float ly = mapSize.y - height;
float clampY = Mathf.Clamp(transform.position.y, -ly + center.y, ly + center.y);
transform.position = new Vector3(clampX, clampY, -10f);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(center, mapSize * 2);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;
public class DataManager : MonoBehaviour
{
public static readonly DataManager instance = new DataManager();
private Dictionary<int, MarioData> dicMarioDatas = new Dictionary<int, MarioData>();
public void LoadMarioData()
{
TextAsset asset= Resources.Load<TextAsset>("mario_data");
string json = asset.text;
Debug.Log(json);
var marioDatas = JsonConvert.DeserializeObject<MarioData[]>(json);
Debug.LogFormat("arrMarioDatas.length:{0}", marioDatas.Length);
foreach(var marioData in marioDatas)
{
this.dicMarioDatas.Add(marioData.id, marioData);
}
Debug.LogFormat("mario_data 에셋을 로드 했습니다. {0}", this.dicMarioDatas.Count);
}
public List<MarioData> GetMarioDatas()
{
return this.dicMarioDatas.Values.ToList();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FlagController : MonoBehaviour
{
private AudioSource audioSource;
[SerializeField]
private AudioClip gameOverSfx;
[SerializeField]
private GameObject flagGo;
[SerializeField]
private float moveSpeed = 0.5f;
[SerializeField]
private Transform handle;
private MarioController marioController;
private bool isInFlag = false;
private bool canKey = true;
void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
isInFlag = false;
}
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log(collision.tag);
if (collision.CompareTag("mario"))
{
MarioController mario = GameObject.Find("Mario").GetComponent<MarioController>();
mario.IgnoreInput(true);
Rigidbody2D mRigid = mario.GetComponent<Rigidbody2D>();
mRigid.velocity = Vector3.zero;
mRigid.angularVelocity = 0;
mario.transform.SetParent(this.handle);
mRigid.isKinematic = true;
handle.transform.position = new Vector3(handle.transform.position.x, mario.transform.position.y, handle.transform.position.z);
mario.transform.position = handle.transform.position;
this.StartCoroutine(this.CoMoveHandle());
this.StartCoroutine(this.CoMoveFlag());
this.StartCoroutine(this.CoLoadGameOver());
}
}
private IEnumerator CoMoveFlag()
{
while (true)
{
flagGo.transform.Translate(this.moveSpeed * Vector3.down * Time.deltaTime);
if (flagGo.transform.localPosition.y <= 0.3f)
{
this.moveSpeed = 0f;
this.audioSource.PlayOneShot(this.gameOverSfx);
break;
}
yield return null;
}
Debug.LogFormat("깃발이 이동을 완료 했습니다.");
}
private IEnumerator CoLoadGameOver()
{
yield return new WaitForSeconds(3f);
SceneManager.LoadScene("GameOverScene");
}
private IEnumerator CoMoveHandle()
{
while (true)
{
handle.transform.Translate(this.moveSpeed * Vector3.down * Time.deltaTime);
if (handle.transform.localPosition.y <= 0.63f)
{
this.moveSpeed = 0f;
break;
}
yield return null;
}
Debug.LogFormat("handle이 이동을 완료 했습니다.");
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
public class Map1Main : MonoBehaviour
{
private AudioSource audioSource;
[SerializeField]
private AudioClip backgoundSfx;
[SerializeField]
private UIDirector uiDirector;
[SerializeField]
private GameObject uiPrefab;
private Transform parent;
[SerializeField]
private GameObject marioPrefab;
[SerializeField]
private Transform marioTrans;
[SerializeField]
private CameraController cameraController;
private MarioController marioController;
void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
this.audioSource.PlayOneShot(this.backgoundSfx);
DataManager.instance.LoadMarioData();
GameObject uiGo = Instantiate(this.uiPrefab);
uiGo.transform.SetParent(parent, false);
this.uiDirector = uiGo.GetComponent<UIDirector>();
this.uiDirector.UpdateUI();
uiDirector.txtMapName.text = string.Format("WORLD\n1-1");
GameObject marioGo = Instantiate(marioPrefab);
marioGo.transform.position = this.marioTrans.position;
marioGo.name = "Mario";
cameraController.Init();
this.marioController = marioGo.GetComponent<MarioController>();
this.marioController.onGetMushroom = (scale) => {
this.marioController.Bigger(scale);
Debug.LogFormat("버섯획득 : isBig => {0}", this.marioController.IsBig);
};
this.marioController.onAttackedEnemy = (scale) =>
{
this.marioController.Smaller(scale);
Debug.Log("적과 부딪혔습니다.");
};
this.marioController.onChangeScene = (sceneName) => {
this.ChangeScene(sceneName);
};
this.marioController.onCoinEat = () =>
{
this.EatCoin();
Debug.Log("코인획득");
};
}
public void ChangeScene(string sceneName)
{
bool isBig = this.marioController.IsBig;
bool isSmall = this.marioController.IsSmall;
Debug.LogFormat("isBig: {0}", isBig);
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
asyncOperation.completed += (obj) => {
switch (sceneName)
{
case "Map2Scene":
Map2main map2Main = GameObject.FindObjectOfType<Map2main>();
if (isBig)
{
map2Main.InitBig(isBig);
}
else if (isSmall)
{
map2Main.InitSmall(isSmall);
}
else
map2Main.InitSmall(isSmall);
break;
}
};
}
private void Save()
{
}
public void EatCoin()
{
Debug.Log("eatCoin 호출");
uiDirector.IncreaseScore(50);
uiDirector.IncreaseCoin();
uiDirector.UpdateUI();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro.Examples;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;
public class Map2main : MonoBehaviour
{
private AudioSource audioSource;
[SerializeField]
private AudioClip backgoundSfx;
private UIDirector uiDirector;
[SerializeField]
private GameObject uiPrefab;
private Transform parent;
[SerializeField]
private GameObject marioPrefab;
[SerializeField]
private Transform marioTrans;
private MarioController marioController;
void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
this.audioSource.PlayOneShot(this.backgoundSfx);
uiDirector.UpdateUI();
this.marioController.onChangeScene = (sceneName) => {
this.ChangeScene(sceneName);
};
uiDirector.txtMapName.text = string.Format("WORLD\n1-2");
this.marioController.onCoinEat = () =>
{
this.EatCoin();
Debug.Log("코인획득");
};
}
public void ChangeScene(string sceneName)
{
bool isBig = this.marioController.IsBig;
Debug.LogFormat("isBig: {0}", isBig);
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
asyncOperation.completed += (obj) =>
{
switch (sceneName)
{
case "Map3Scene":
Map3main map3Main = GameObject.FindObjectOfType<Map3main>();
map3Main.Init(isBig);
break;
}
};
}
public void EatCoin()
{
Debug.Log("eatCoin 호출");
uiDirector.IncreaseScore(50);
uiDirector.IncreaseCoin();
uiDirector.UpdateUI();
}
public void InitBig(bool isBig)
{
this.CreateMarioBig(isBig);
this.CreateUI(); ;
}
public void InitSmall(bool isSmall)
{
this.CreateMarioSmall(isSmall);
this.CreateUI();
}
private void CreateMarioBig(bool isBig)
{
var go = Instantiate(this.marioPrefab);
go.transform.position = this.marioTrans.position;
go.name = "Mario";
this.marioController = go.GetComponent<MarioController>();
if (isBig)
this.marioController.Bigger(0.6f);
}
private void CreateMarioSmall(bool isSmall)
{
var go = Instantiate(this.marioPrefab);
Debug.Log(go);
go.transform.position = this.marioTrans.position;
go.name = "Mario";
this.marioController = go.GetComponent<MarioController>();
if (isSmall)
this.marioController.Smaller(0.4f);
}
private void CreateUI()
{
GameObject uiGo = Instantiate(this.uiPrefab);
uiGo.transform.SetParent(parent, false);
this.uiDirector = uiGo.GetComponent<UIDirector>();
this.uiDirector.LoadData();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro.Examples;
using UnityEngine;
using UnityEngine.UI;
public class Map3main : MonoBehaviour
{
private AudioSource audioSource;
[SerializeField]
private AudioClip backgoundSfx;
[SerializeField]
private GameObject uiPrefab;
private Transform parent;
private UIDirector uiDirector;
[SerializeField]
private GameObject marioPrefab;
[SerializeField]
private Transform marioTrans;
private MarioController marioController;
[SerializeField]
private CameraController cameraController;
private void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
this.audioSource.PlayOneShot(this.backgoundSfx);
this.marioController.onCoinEat = () =>
{
this.EatCoin();
Debug.Log("코인획득");
};
uiDirector.txtMapName.text = string.Format("WORLD\n2-1");
}
public void EatCoin()
{
Debug.Log("eatCoin 호출");
uiDirector.IncreaseScore(50);
uiDirector.IncreaseCoin();
uiDirector.UpdateUI();
}
public void Init(bool isBig)
{
this.CreateMario(isBig);
cameraController.Init();
this.CreateUI();
}
private void CreateMario(bool isBig)
{
var go = Instantiate(marioPrefab);
go.transform.position = this.marioTrans.position;
go.name = "Mario";
this.marioController = go.GetComponent<MarioController>();
if (isBig)
this.marioController.Bigger(0.6f);
}
private void CreateUI()
{
GameObject uiGo = Instantiate(this.uiPrefab);
uiGo.transform.SetParent(parent, false);
this.uiDirector = uiGo.GetComponent<UIDirector>();
this.uiDirector.LoadData();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MarioController : MonoBehaviour
{
private AudioSource audioSource;
[SerializeField]
private AudioClip eatCoinSfx;
[SerializeField]
private AudioClip powerUpSfx;
[SerializeField]
private AudioClip portalSfx;
[SerializeField]
private AudioClip jumpSfx;
private Rigidbody2D rBody2D;
public float jumpForce = 500f;
public float moveForce = 60f;
public Animator anim;
private bool isJump = false;
private bool isLongJump = false;
[SerializeField]
private Transform rayOrigin;
public System.Action<float> onGetMushroom;
public System.Action<float> onAttackedEnemy;
public System.Action<string> onChangeScene;
public System.Action onCoinEat;
void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
this.rBody2D = this.GetComponent<Rigidbody2D>();
this.anim = this.GetComponent<Animator>();
}
void Update()
{
if (this.ignoreInput) return;
if (Input.GetKeyDown(KeyCode.Space))
{
if (this.rBody2D.velocity.y == 0)
{
this.rBody2D.AddForce(Vector2.up * this.jumpForce);
if (this.isJump == false)
{
this.isJump = true;
}
this.audioSource.PlayOneShot(this.jumpSfx);
}
}
if (this.isJump)
{
if (this.rBody2D.velocity.y > 0)
{
this.anim.SetBool("isJump", true);
}
else if (this.rBody2D.velocity.y <= 0)
{
this.anim.SetBool("isJump", false);
}
}
int dirX = 0;
if (Input.GetKey(KeyCode.LeftArrow))
{
dirX = -1;
}
if (Input.GetKey(KeyCode.RightArrow))
{
dirX = 1;
}
float speedX = Mathf.Abs(this.rBody2D.velocity.x);
if (speedX < 5f)
{
this.rBody2D.AddForce(new Vector2(dirX, 0) * this.moveForce);
}
if (dirX != 0)
{
this.anim.SetInteger("State", 1);
this.anim.SetBool("isJump", false);
this.transform.localScale = new Vector3(dirX *Mathf.Abs(this.transform.localScale.x), this.transform.localScale.y, 1);
this.transform.position = new Vector3(Mathf.Clamp(this.transform.position.x, -8.1f, 25.9f),this.transform.position.y, 0);
}
else
{
if (this.isJump==false)
{
this.anim.SetInteger("State", 0);
this.anim.SetBool("isJump", false);
}
}
this.anim.speed = speedX / 2.0f;
if (Input.GetKey(KeyCode.Space))
{
isLongJump = true;
}
else if (Input.GetKeyUp(KeyCode.Space))
{
isLongJump = false;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("아래 들어갈때가 있는지 확인한다");
this.CheckPortalBelow();
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
this.CheckPortalRight();
}
}
private Coroutine routineCheckPortalBelow;
private Coroutine routineCheckPortalRight;
private void CheckPortalBelow()
{
if (this.routineCheckPortalBelow != null) StopCoroutine(this.routineCheckPortalBelow);
this.routineCheckPortalBelow = this.StartCoroutine(this.CoCheckPortalBelow());
}
private void CheckPortalRight()
{
if (this.routineCheckPortalRight != null) StopCoroutine(this.routineCheckPortalRight);
this.routineCheckPortalRight = this.StartCoroutine(this.CoCheckPortalRight());
}
private IEnumerator CoCheckPortalBelow()
{
yield return null;
Ray ray = new Ray(this.rayOrigin.transform.position, this.transform.up * -1);
Debug.DrawRay(ray.origin, ray.direction, Color.red, 10f);
LayerMask mask = 1 << LayerMask.NameToLayer("Mario");
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 1, ~mask);
if (hit)
{
Debug.Log(hit.collider.gameObject.name);
if (hit.collider.gameObject.name == "portal2")
{
Debug.Log("씬2로 전환");
this.onChangeScene("Map2Scene");
this.audioSource.PlayOneShot(this.portalSfx);
}
}
}
private IEnumerator CoCheckPortalRight()
{
yield return null;
Ray ray2 = new Ray(this.rayOrigin.transform.position, this.transform.right * -1);
Debug.DrawRay(ray2.origin, ray2.direction, Color.green, 12f);
LayerMask mask = 1 << LayerMask.NameToLayer("Mario");
RaycastHit2D hit2 = Physics2D.Raycast(ray2.origin, ray2.direction, 1, ~mask);
if (hit2)
{
Debug.Log(hit2.collider.gameObject.name);
if (hit2.collider.gameObject.name == "portal3")
{
Debug.Log("씬3으로전환");
this.onChangeScene("Map3Scene");
this.audioSource.PlayOneShot(this.portalSfx);
}
}
}
private void FixedUpdate()
{
if (this.ignoreInput) return;
if (isLongJump && this.rBody2D.velocity.y > 0)
{
this.rBody2D.gravityScale = 1.2f;
}
else
{
this.rBody2D.gravityScale = 2.5f;
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.tag == "flag")
{
this.anim.SetInteger("State", 3);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "coin")
{
Destroy(other.gameObject);
this.onCoinEat();
this.audioSource.PlayOneShot(this.eatCoinSfx);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "mushroom")
{
Destroy(collision.gameObject);
this.audioSource.PlayOneShot(this.powerUpSfx);
this.onGetMushroom(0.6f);
}
if (collision.gameObject.tag == "enemy")
{
}
}
public void Bigger(float scale)
{
if (this.IsBig)
{
Debug.Log("이미 커진 상태 입니다.");
}
else
{
this.transform.localScale = new Vector3(0.6f, 0.6f, 0.6f);
this.IsBig = true;
}
}
public void Smaller(float scale)
{
if (this.IsSmall)
{
Debug.Log("이미 작아진 상태 입니다.");
}
else
{
this.transform.localScale = new Vector3(0.4f, 0.4f, 0.4f);
this.IsSmall = true;
this.IsBig = false;
}
}
private bool ignoreInput = false;
public void IgnoreInput(bool ignore)
{
ignoreInput = ignore;
Debug.LogFormat("IgnoreInput: <color=lime>{0}</color>", this.ignoreInput);
}
public bool IsBig
{
get; set;
}
public bool IsSmall
{
get;set;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MarioData
{
public int id;
public int score;
public int time;
public int coin_amount;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MushroomController : MonoBehaviour
{
public float moveSpeed = 0.05f;
private Animator anim;
void Start()
{
this.anim = this.GetComponent<Animator>();
}
void Update()
{
this.transform.Translate(this.moveSpeed, 0, 0);
if (this.transform.position.y < -3.5f)
{
Destroy(this.gameObject);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "portal")
{
moveSpeed = moveSpeed * -1;
}
if (collision.gameObject.tag == "wall")
{
moveSpeed = moveSpeed * -1;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "mario")
{
this.anim.SetBool("isAttacked", true);
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y - 0.3f);
this.moveSpeed = 0f;
Invoke("Die", 0.15f);
}
}
private void Die()
{
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class UIDirector : MonoBehaviour
{
[SerializeField]
private Text txtScore;
[SerializeField]
private Text txtCoin;
[SerializeField]
private Text txtTime;
public Text txtMapName;
public float time =400f;
private int score=0;
private int coin=0;
void Start()
{
}
void Update()
{
this.time -= Time.deltaTime;
this.txtTime.text = this.time.ToString("F0");
this.txtTime.text = string.Format("TIME\n{0}", this.txtTime.text);
}
public void UpdateUI()
{
List<MarioData> marioDatas = DataManager.instance.GetMarioDatas();
foreach (MarioData data in marioDatas) {
Debug.LogFormat("<color=green>id:{0},score{1}</color>",data.id,data.score);
}
this.txtScore.text = string.Format("MARIO\n{0:D6}", score);
this.txtCoin.text = string.Format("X {0}", this.coin);
this.SaveData();
}
public void IncreaseScore(int score)
{
Debug.Log("50점 획득");
this.score += score;
}
public void IncreaseCoin()
{
this.coin += 1;
}
public void SaveData()
{
PlayerPrefs.SetInt("Score",score);
PlayerPrefs.SetFloat("Time", time);
PlayerPrefs.SetInt("Coin", coin);
}
public void LoadData()
{
int score = PlayerPrefs.GetInt("Score");
this.txtScore.text = score.ToString();
this.txtScore.text = string.Format("MARIO\n{0:D6}", score);
this.txtCoin.text = PlayerPrefs.GetInt("Coin").ToString();
this.txtCoin.text = string.Format("X {0}", this.txtCoin.text);
}
}