GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public List birds;
public List pigs;
private bool Win = false,Lose = false;
public Vector3 BirdsBornPos;
private GameObject LoseResult;
private GameObject WinResult;
private GameObject BackAppear;
public GameObject[] Stars;
private GameObject pausePanel;
private GameObject pauseButton;
public static GameManager _instance;
private int CurrentStars;
private int sum;
private GameObject LevelResult;
private Camera UIcamera;
private void Awake()
{
_instance = this;
if (birds.Count > 0) BirdsBornPos = birds[0].gameObject.transform.position;
LevelResult = GameObject.Find("LevelResult");
UIcamera = GameObject.Find("UICamera").GetComponent();
BackAppear = LevelResult.transform.Find("BackAppear").gameObject;
WinResult = LevelResult.transform.Find("Win").gameObject;
LoseResult = LevelResult.transform.Find("Lose").gameObject;
BackAppear.transform.SetSiblingIndex(0);
pauseButton = GameObject.Find("Pause").transform.Find("pauseButton").gameObject;
pausePanel = GameObject.Find("Pause").transform.Find("pausePanel").gameObject;
Stars[0] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("left").gameObject;
Stars[1] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("middle").gameObject;
Stars[2] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("right").gameObject;
Canvas canvas = LevelResult.GetComponent
Bird.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bird : MonoBehaviour {
private bool isClick = false;
protected bool dead = false;
private bool CanControlCamera = true;
private Transform stickMidPos;
public GameObject burst;
public float maxDistance = 1.8f;
private bool CanClick = true;
[HideInInspector]public SpringJoint2D sp;
protected Rigidbody2D rg;
protected SpriteRenderer SR;
public Sprite Wait,Selected,Launched,Skilling,Injured;
private bool lockrotation = true;
private LineRenderer LauchStick_rightPoint_LineRenderer;
private LineRenderer LauchStick_leftPoint_LineRenderer;
private Transform LauchStick_rightPosition;
private Transform LauchStick_leftPosition;
private Trail ItsTrail;
private bool CanUseSkill;
public AudioClip birdClickSound;
public AudioClip birdFlySound;
public float CameraSmooth = 3;
private float BirdPosX;
public static Bird _instance;
private void Awake()
{
_instance = this;
Camera.main.transform.position = new Vector3(1, Camera.main.transform.position.y, Camera.main.transform.position.z);
sp = GetComponent();
rg = GetComponent();
SR = GetComponent();
if(sp.enabled==false)SR.sprite = Wait;
LauchStick_rightPoint_LineRenderer = GameObject.Find("rightPos").GetComponent();
LauchStick_leftPoint_LineRenderer = GameObject.Find("leftPos").GetComponent();
LauchStick_rightPosition = GameObject.Find("rightPos").transform;
LauchStick_leftPosition = GameObject.Find("leftPos").transform;
ItsTrail = GetComponent();
stickMidPos = GameObject.Find("midPos").transform;
}
private void OnMouseDown()//每当按下鼠标
{
if (CanClick)
{
LauchStick_rightPoint_LineRenderer.enabled = true;
LauchStick_leftPoint_LineRenderer.enabled = true;
SR.sprite = Selected;
AudioSource.PlayClipAtPoint(birdClickSound, Camera.main.transform.position);
CanControlCamera = false;
isClick = true;
rg.isKinematic = true;
CanUseSkill = false;
}
}
private void OnMouseUp()//鼠标抬起
{
isClick = false;
LauchStick_rightPoint_LineRenderer.enabled = false;
LauchStick_leftPoint_LineRenderer.enabled = false;
ItsTrail.ShowTrail();
rg.isKinematic = false;
lockrotation = false;
if (CanClick) {
CanUseSkill = true;
AudioSource.PlayClipAtPoint(birdFlySound,Camera.main.transform.position);
SR.sprite = Launched;
Invoke("Fly", 0.1f);
}
CanClick = false;
}
private void Update()
{
if (CanUseSkill)
{
if (Input.GetMouseButtonDown(0))
{
UseSkill();
SR.sprite = Skilling;
}
}
if (lockrotation) transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
if (isClick&&CanClick)
{
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition)+new Vector3(0,0,10);
if (Vector3.Distance(transform.position, stickMidPos.position) > maxDistance)
{
Vector3 e_pos = (transform.position - stickMidPos.position).normalized;
transform.position = stickMidPos.position + maxDistance * e_pos;
}
DrawLauchStickLine();
}
BirdPosX = GameManager._instance.birds[0].transform.position.x;
if(CanControlCamera==false)Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,
new Vector3(Mathf.Clamp(BirdPosX,1,39),
Camera.main.transform.position.y,
Camera.main.transform.position.z),
CameraSmooth);
if (CanControlCamera&&Input.GetKey(KeyCode.LeftArrow)&&Camera.main.transform.position.x>=1.15)
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x-0.15f, Camera.main.transform.position.y,Camera.main.transform.position.z);
}
else if (CanControlCamera&&Input.GetKey(KeyCode.RightArrow)&&Camera.main.transform.position.x<38.85)
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + 0.15f, Camera.main.transform.position.y, Camera.main.transform.position.z);
}
}
void Fly()
{
sp.enabled = false;
Invoke("Dead", 5f);
}
void DrawLauchStickLine()
{
LauchStick_leftPoint_LineRenderer.SetPosition(0,transform.position);
LauchStick_leftPoint_LineRenderer.SetPosition(1, LauchStick_leftPosition.position);
LauchStick_rightPoint_LineRenderer.SetPosition(0, transform.position);
LauchStick_rightPoint_LineRenderer.SetPosition(1, LauchStick_rightPosition.position);
}
public void RemoveBird()
{
dead = true;
GameManager._instance.birds.Remove(this);
Destroy(gameObject);
if (GameManager._instance.birds.Count != 0) GameManager._instance.Initialize();
GameManager._instance.JudgeLevelResult();
lockrotation = true;
CanControlCamera = true;
CanClick = true;
Camera.main.transform.position = new Vector3(1, Camera.main.transform.position.y, Camera.main.transform.position.z);
}
public void Dead()
{
gameObject.SetActive(false);
if (dead == false)
{
Instantiate(burst, transform.position, Quaternion.identity);
}
Invoke("RemoveBird", 1.5f);
}
private void OnCollisionEnter2D(Collision2D collision)
{
ItsTrail.CloseTrail();
if(collision.gameObject.tag=="ground")CanUseSkill = false;
if (collision.relativeVelocity.magnitude > 3.0&&collision.gameObject.tag!="bird")
{
SR.sprite = Injured;
}
}
public virtual void UseSkill()
{
CanUseSkill = false;
}
}
Break.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Break : MonoBehaviour {
public float DeadSpeed;
public float minSpeed;
private SpriteRenderer state_image;
public Sprite hurt;
public GameObject burst;
public AudioClip hurtSound;
public AudioClip destroySound;
public GameObject purplescore3000;
private void Awake()
{
state_image = GetComponent();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.relativeVelocity.magnitude > DeadSpeed)
{
if(gameObject.name == "TNT")
{
gameObject.GetComponent().Explode();
}
DeadPig();
AudioSource.PlayClipAtPoint(destroySound,Camera.main.transform.position);
}
else if (collision.relativeVelocity.magnitude > minSpeed && collision.relativeVelocity.magnitude < DeadSpeed)
{
state_image.sprite = hurt;
AudioSource.PlayClipAtPoint(hurtSound,Camera.main.transform.position);
}
}
public void DeadPig()
{
Destroy(gameObject);
GameObject tempscore = Instantiate(purplescore3000,transform.position+new Vector3(0f,1f,0f),Quaternion.identity);
Destroy(tempscore, 1.2f);
GameManager._instance.pigs.Remove(this);
Instantiate(burst,transform.position,Quaternion.identity);
GameManager._instance.JudgeLevelResult();
}
}
MapSelect.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MapSelect : MonoBehaviour {
public int StarsRequireNum = 0;
private bool CanSelectThisMap = false;
private int sum;
public GameObject locked;
public GameObject starDisplay;
private void Start()
{
switch (gameObject.name)
{
case "map1":
sum = 0;
for (int i = 1; i <= 12; i++)
{
sum += PlayerPrefs.GetInt("T1" + "level" + i);
}
gameObject.transform.Find("star").transform.Find("starscount").GetComponent().text = sum + "/36";
break;
case "map2":
sum = 0;
for (int i = 1; i <= 8; i++)
{
sum += PlayerPrefs.GetInt("T2" + "level" + i);
}
gameObject.transform.Find("star").transform.Find("starscount").GetComponent().text = sum + "/24";
break;
case "map3":
sum = 0;
for (int i = 1; i <= 1; i++)
{
sum += PlayerPrefs.GetInt("T3" + "level" + i);
}
gameObject.transform.Find("star").transform.Find("starscount").GetComponent().text = sum + "/3";
break;
}
if(PlayerPrefs.GetInt("CollectedNum") >= StarsRequireNum)
{
locked.SetActive(false);
starDisplay.SetActive(true);
CanSelectThisMap = true;
}
else
{
locked.SetActive(true);
starDisplay.SetActive(false);
CanSelectThisMap = false;
}
}
public void SelectedMap()
{
if (CanSelectThisMap)
{
if (gameObject.name == "map1")
{
gameObject.transform.parent.parent.transform.Find("LevelsPanel-1").gameObject.SetActive(true);
PlayerPrefs.SetString("CurrentTheme","T1");
}
else if (gameObject.name == "map2") {
gameObject.transform.parent.parent.transform.Find("LevelsPanel-2").gameObject.SetActive(true);
PlayerPrefs.SetString("CurrentTheme","T2");
}
else if (gameObject.name == "map3"){
gameObject.transform.parent.parent.transform.Find("LevelsPanel-3").gameObject.SetActive(true);
PlayerPrefs.SetString("CurrentTheme","T3");
}
else if(gameObject.name == "map_more")
{
Debug.Log("暂未开发");
}
transform.parent.gameObject.SetActive(false);
}
}
}
LevelSelect.cs
using System.Collections
using System.Collections.Generic
using UnityEngine
using UnityEngine.UI
using UnityEngine.SceneManagement
public class LevelSelect : MonoBehaviour {
private bool CanEnter = false
public Sprite LockImage
public Sprite UnlockImage
public Sprite nostar,onestar,twostar,fullstar
private void Start () {
if (int.Parse(gameObject.transform.Find("num").GetComponent().text) == 1)//每个主题的关卡一都为开启状态
{
CanEnter = true
}
else if(PlayerPrefs.GetInt( PlayerPrefs.GetString("CurrentTheme")+"level" + (int.Parse(gameObject.transform.Find("num").GetComponent().text) -1) ) > 0)//如果上一关星星数不为零,此关卡可进入
{
CanEnter = true
}
if (CanEnter)
{
gameObject.GetComponent().sprite = UnlockImage
gameObject.transform.Find("starsGot").gameObject.SetActive(true)
gameObject.transform.Find("num").gameObject.SetActive(true)
switch (PlayerPrefs.GetInt(PlayerPrefs.GetString("CurrentTheme")+gameObject.name))
{
case 0://根据关卡星星数控制小星星图像的选择性显示
gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = nostar
break
case 1:
gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = onestar
break
case 2:
gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = twostar
break
case 3:
gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = fullstar
break
default:
break
}
}
else
{//锁上时的图像变化
gameObject.GetComponent().sprite = LockImage
gameObject.transform.Find("starsGot").gameObject.SetActive(false)
gameObject.transform.Find("num").gameObject.SetActive(false)
}
}
public void BackToMapMenu()
{//返回地图选择界面,使用transform.parent方法调用父对象
gameObject.transform.parent.parent.gameObject.SetActive(false)
gameObject.transform.parent.parent.parent.Find("Map").gameObject.SetActive(true)
}
public void LevelSelected()
{
if (CanEnter)
{
PlayerPrefs.SetString("CurrentLevel",gameObject.name)
SceneManager.LoadSceneAsync("003-game")
}
}
}
Burst.cs
用在烟雾动画上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Burst : MonoBehaviour {
public void Destroying()
{
Destroy(gameObject);
}
}
BlackBird.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Black_bird : Bird {
public List blocks = new List();
public GameObject burst3;
public AudioClip boom;
public override void UseSkill()//继承Bird里的UseSkill方法
{
base.UseSkill();
AudioSource.PlayClipAtPoint(boom,Camera.main.transform.position);
if (blocks != null && blocks.Count > 0)
{
for(int i = 0; i < blocks.Count; i++)
{
blocks[i].DeadPig();
}
}
gameObject.SetActive(false);
if(dead==false)Instantiate(burst3, transform.position, Quaternion.identity);
dead = true;
}
private void OnTriggerEnter2D(Collider2D collision)//通过一个大圆形状的Trigger Collider动态的控制blocks组里的元素
{
if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
{
blocks.Add(collision.gameObject.GetComponent());
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
{
blocks.Remove(collision.gameObject.GetComponent());
}
}
}
GreenBird.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GreenBird : Bird {
public override void UseSkill()
{
base.UseSkill();
rg.velocity = new Vector2(-rg.velocity.x,rg.velocity.y);
SR.flipX=true;
}
}
YellowBird.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class YellowBird : Bird {
public override void UseSkill()
{
base.UseSkill();
rg.velocity *= 2;
}
}
LoadLevel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadLevel : MonoBehaviour
{
private void Awake()
{
Instantiate(Resources.Load(PlayerPrefs.GetString("CurrentTheme") + PlayerPrefs.GetString("CurrentLevel")));
}
}
setting.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class setting : MonoBehaviour {
void Start () {
if(gameObject.name == "BackAppear")
{
gameObject.GetComponent().sizeDelta = new Vector2(1240,600);
}
Screen.SetResolution(1234,530,true);
}
}
TNT.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TNT : MonoBehaviour {
public List blocks = new List();
public GameObject burst3;
public void Explode()
{
if (blocks != null && blocks.Count > 0)
{
for (int i = 0; i < blocks.Count; i++)
{
blocks[i].DeadPig();
}
}
gameObject.SetActive(false);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
{
blocks.Add(collision.gameObject.GetComponent());
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
{
blocks.Remove(collision.gameObject.GetComponent());
}
}
}
Trail.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trail : MonoBehaviour {
public WeaponTrail myTrail;
private float t = 0.033f;
private float tempT = 0;
private float animationIncrement = 0.003f;
void Start()
{
myTrail.SetTime(0.0f, 0.0f, 1.0f);
}
public void ShowTrail()
{
myTrail.SetTime(0.5f, 0.0f, 1.0f);
myTrail.StartTrail(0.5f, 0.4f);
}
public void CloseTrail()
{
myTrail.ClearTrail();
}
void LateUpdate()
{
t = Mathf.Clamp(Time.deltaTime, 0, 0.066f);
if (t > 0)
{
while (tempT < t)
{
tempT += animationIncrement;
if (myTrail.time > 0)
{
myTrail.Itterate(Time.time - t + tempT);
}
else
{
myTrail.ClearTrail();
}
}
tempT -= t;
if (myTrail.time > 0)
{
myTrail.UpdateTrail(Time.time, t);
}
}
}
}