# untiy 一个逗比并且有无聊的游戏制作

2016-07-18

CircleBuilder，

```public class CircleBuilder
{
}```

```    private Material _shareMaterial;
private PlayerHandler _playerHandler;
private GameObject _obstacle;
private GameObject _circle;

public void Intilized(Material mat,PlayerHandler player,GameObject obstacle,GameObject circle)
{
_shareMaterial = mat;
_playerHandler = player;
_obstacle = obstacle;

_circle = circle;
}```

```  public GameObject CreateCirlceMesh(float circle,bool isaddColider,ref CircleData circleData)
{
GameObject ob = new GameObject("Child");
retun ob;
}```

```   GameObject ob = new GameObject("Child");

List inList = new List(150);
List outList = new List(150);

Vector3 p1, p2, p3, p4;
int nodeCount = (int)((circle * 2 * Mathf.PI) / 0.2f);```
nodecount表示圆环的节点个数，接着我们开始对2内环数组和外环数组对其赋值。
```        int nodeCount = (int)((circle * 2 * Mathf.PI) / 0.2f);
for (int i = 0; i < nodeCount; i++)
{
p1=(GetPosByIndex(circle, nodeCount, i));
p2=(GetPosByIndex(circle, nodeCount, (i + 1) % nodeCount));
p3=(GetPosByIndex(circle - 0.1f, nodeCount, (i + 1) % nodeCount));
p4=(GetPosByIndex(circle - 0.1f, nodeCount, i));
if (!outList.Contains(p1))
if (!outList.Contains(p2))
if (!inList.Contains(p4))
if (!inList.Contains(p3))
}```

```       if (!inList.Contains(p3))
}
int q = inList.Count;
int p = q / 2;
List lefList=new List(80);
List rightList = new List(80);
for (int i = 0; i <= p; i++)
for (int i = p; i >=0 ; i--)

for (int i = p; i < q; i++)
for (int i = q - 1; i >= p; i--)

circleData.Intilized(q, ob.transform, _playerHandler.gameObject, circle);```

```    private void CreateChildGameObject(Transform parent, List pointlist, bool isAdd)
{

GameObject tem = new GameObject("sb");
tem.tag = "Obstacle";

meshRenderer.sharedMaterial = _shareMaterial;
tem.transform.SetParent(parent);

{
colider.points = V3ToV2(pointlist).ToArray();
}

} ```
```    private Vector3 GetPosByIndex(float circle, int nodecount, int index)
{
float angle = (360f / nodecount * index - 90) * Mathf.Deg2Rad;
float x = Mathf.Cos(angle) * circle;
float y = Mathf.Sin(angle) * circle;
return new Vector3(x, y, 0);
}
private List V3ToV2(List points)
{
List point = new List();
for (int i = 0; i < points.Count; i++)
{
}
return point;
}```

```public static class ViewTool
{
public static void ChangeMatColor(Material meshMaterial)
{
float r = Random.Range(0, 255);
float g = Random.Range(0, 255);
float b = Random.Range(0, 255);
float a = Random.Range(125, 255);
Color tempColor=new Color(r/255f,g/255f,b/255f,a/255f);
meshMaterial.DOColor(tempColor, 5.0f).OnComplete(delegate
{
meshMaterial.DOColor(new Color(0,0,0,1), 5.0f);
});
}
public static void RemoveView(GameObject ob)
{
for (int i = 0; i < ob.transform.childCount; i++)
{
var child = ob.transform.GetChild(i);
if (child.transform.CompareTag("Spike"))
Pools.RecyclePoolObj(child.gameObject);
}
Object.Destroy(ob);
}
public static float GetColorLength(Material shareMaterial)
{
Vector3 temp=new Vector3(1,0,0);
Color p = shareMaterial.color;
Vector3 endvalue=new Vector3(p.r,p.g,p.b);
return Vector3.Dot(endvalue, temp);
}

public static Mesh GetObjMesh(List pointsList)
{
Mesh curmesh=new Mesh();
int onepertwo = pointsList.Count/2;
List indictInts=new List();
for (int i = 0; i < onepertwo; i++)
{
}
curmesh.vertices = pointsList.ToArray();
curmesh.triangles = indictInts.ToArray();
return curmesh;
}
}
```

```using System.Collections.Generic;
using UnityEngine;

public class CircleData
{

public Transform Parent { set; get; }
public ListType CurType { set; get; }
public int Count { set; get; }
public float Radius { set; get; }

public Vector3 NextVector3 { set; get; }
public Vector3 PreVector3 { set; get; }

private GameObject _player;
public Vector3 GetTargetPos(int index,ListType curType)
{
float scale = _player.transform.localScale.x;
float r = 0;
if (curType == ListType.In)
r = Radius - scale * offset-0.1f;
if (curType == ListType.Out)
r = Radius + scale * offset;

return GetTargetPos(r,index);
}

private Vector3 GetTargetPos(float r,int index)
{
float x = Mathf.Cos(index * Mathf.PI / Count * 2 - Mathf.PI / 2) * r;
float y = Mathf.Sin(index * Mathf.PI / Count * 2 - Mathf.PI / 2) * r;
Vector3 temp = new Vector3(x, y, 0);

return temp;
}
public int GetTargetIndex(Vector3 targetVector3,ListType listType)
{
float scale = _player.transform.localScale.x;
int min = 0, max = 0;
float r=0;
if (listType == ListType.In)
r = Radius - scale*offset - 0.1f;
if (listType == ListType.Out)
if (targetVector3.x >= 0 && targetVector3.y >= 0)
{
min = Count / 4 - 1;
max = Count / 4 + 1;
}
if (targetVector3.x >= 0 && targetVector3.y <= 0)
{
min = 0;
max = Count / 4 + 1;
}
if (targetVector3.x <= 0 && targetVector3.y >= 0)
{
min = Count / 2 + 1;
max = Count / 4 + 1;
}
if (targetVector3.x <= 0 && targetVector3.y <= 0)
{
min = Count / 4 * 3 + 1;
max = Count / 4 + 1;
}
for (int i = 0; i < max; i++)
{
Vector3 temp = GetTargetPos(r, (i + min) % Count);
float distance = Vector3.SqrMagnitude(temp - targetVector3);
if (distance < 0.05)
return i + min;
}
return 200;
}
public void  Intilized(int count,Transform parent,GameObject obj,float radius)
{

CurType=ListType.In;

_player = obj;
Parent = parent;
Count = count;
}
}
```

```public enum GameState
{
None,
Playing,
Pause,
FailOver,
}
public enum ListType
{
None,
In,
Out
}
public enum LevelState
{
None,
OneState,
TwoState,
ThreeState,
FourState,
FiveState,
}

public enum ColorType
{
None,
Green,
Blue,
Yellow,
Red
}```

```public class CirclesManager
{
private List _circleDatas;
private List _circleObjects;
private CircleBuilder _circleBuilder;

public int Curindex;
private PlayerHandler _player;
public CircleData CurCircleData
{
get
{
return _circleDatas[Curindex];
}
}

public void Intilized(Material mat, PlayerHandler player, GameObject obstacle,GameObject circle)
{
Curindex = 0;
_circleDatas=new List();
_circleObjects=new List(4);
for (int i = 0; i < 4; i++)
{
var temp=new CircleData();
}
_player = player;
_circleBuilder=new CircleBuilder();
_circleBuilder.Intilized(mat, player, obstacle, circle);
}
public void InitlizedCircleObj(int totalNum,List circleList)
{
for (int i = 0; i < totalNum; i++)
{
var circleData = _circleDatas[i];
GameObject ob = _circleBuilder.CreateCirlceMesh(circleList[i], false,ref circleData);
if (i==0)
ob.transform.position = new Vector3(0, 0, 0);
else
{

ob.transform.position = VectorHelpr.GetVector3ByDis(radius, _circleDatas[i - 1].Parent.position);

Vector3 dir = (ob.transform.position - _circleDatas[i - 1].Parent.position).normalized;
_circleDatas[i - 1].NextVector3 = dir * (_circleDatas[i - 1].Radius - 0.1f - _player.Skin)+
_circleDatas[i - 1].Parent.position;
_circleDatas[i].PreVector3 = -dir * (_circleDatas[i].Radius - _player.Skin - 0.1f) + _circleDatas[i].Parent.position;
}
}
}
{
int count = _circleDatas.Count;
var preCircleData = _circleDatas[Curindex];
GameObject ob1 = _circleBuilder.CreateCirlceMesh(UnityEngine.Random.Range(1.5f, 2.7f), false, ref preCircleData);

int temp = Curindex;
Curindex++;
Curindex %= 4;
var curCircleData = _circleDatas[(Curindex + 2) % count];

Vector3 dir = (preCircleData.Parent.position - curCircleData.Parent.position).normalized;
curCircleData.NextVector3 = dir * (curCircleData.Radius - 0.1f - _player.Skin) +
curCircleData.Parent.position;
preCircleData.PreVector3 = -dir * (preCircleData.Radius - _player.Skin - 0.1f) + preCircleData.Parent.position;

_circleObjects[temp] = ob1;
_circleBuilder.AddObsToCircle(_circleDatas[(temp + 2) % count], num);
}
public void UpdateList(int num)
{
ViewTool.RemoveView(_circleObjects[Curindex]);
_circleObjects[Curindex]=null;

}
public void ClearCircleData()
{
_circleDatas.Clear();
for (int i = 0; i < _circleObjects.Count; i++)
{
Object.Destroy(_circleObjects[i]);
}
}
public void Reintilized(Action callAction)
{
bool isComplete = false;
for (int i = 0; i < _circleObjects.Count; i++)
{
_circleObjects[i].transform.localScale = Vector3.zero;
_circleObjects[i].transform.DOScale(new Vector3(1, 1, 0), 1.0f).OnComplete(delegate
{
if (!isComplete)
{
isComplete = true;
callAction.Invoke();
}
});
}
}

}```

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DG.Tweening;
using UnityEngine;

public static class VectorHelpr
{
public static Vector3 GetVector3ByDis(float radius,Vector3 pos)
{

if (pos.x > 0)
x = -x;
return  pos+ new Vector3(x,y,0);
}
public static bool JudgeGameOver(CircleData curCircleData,Transform player)
{
int cur = curCircleData.GetTargetIndex(curCircleData.NextVector3-curCircleData.Parent.position,ListType.In);
int nextindex = curCircleData.GetTargetIndex(player.position - curCircleData.Parent.position,ListType.Out);
ListType curType = curCircleData.CurType;
if (curType == ListType.Out)
{

bool b1 = Mathf.Abs(nextindex - cur) <=3;
return  b1;
}
return false;
}
public static void CamerFloow(Camera mainCamera,Vector3 targetVector3)
{
targetVector3 = new Vector3(targetVector3.x, targetVector3.y, mainCamera.transform.position.z);
mainCamera.transform.position = targetVector3;
}
public static Vector3 GetNormalDir(Vector3 dir, Vector3 center)
{
Vector3 endvalue = Quaternion.AngleAxis(90, new Vector3(0, 0, 1))*dir;
return endvalue;
}
public static float GetSpeedByLevel(SpeedLevel curLevel)
{
switch (curLevel)
{
case SpeedLevel.One:
return 2f;
case SpeedLevel.Two:
return 2.5f;
case SpeedLevel.Three:
return 3f;
case SpeedLevel.Four:
return 4;
case SpeedLevel.Five:
return 4.5f;
}
return 0;
}
private static LevelState GetLevelStateByScore(int score)
{
if (score >= 0 && score <= 50)
return LevelState.OneState;
if(score>50&&score<=150)
return LevelState.TwoState;
if(score>150&&score<=300)
return LevelState.ThreeState;
if(score>300&&score<=500)
return LevelState.FourState;
if(score>500&&score<=700)
return LevelState.FiveState;
return LevelState.None;
}
public static Vector2 GetSpeedAndObsByScore(int score)
{
LevelState curLevelState = GetLevelStateByScore(score);
float speed=0, num=0;
switch (curLevelState)
{
case LevelState.OneState:
speed = GetSpeedByLevel(SpeedLevel.One);
num = UnityEngine.Random.Range(0, 10)%6 == 0 ? 0 : UnityEngine.Random.Range(2,5);
break;
case LevelState.TwoState:
speed = GetSpeedByLevel(SpeedLevel.One);
int min = UnityEngine.Random.Range(2, 4);
num = UnityEngine.Random.Range(0, 10) % 6 == 0 ? min : min + 2;
break;
case LevelState.ThreeState:
speed = GetSpeedByLevel(SpeedLevel.Two);
num = UnityEngine.Random.Range(4, 6);
break;
case LevelState.FourState:
speed = GetSpeedByLevel(SpeedLevel.Three);
num = UnityEngine.Random.Range(4, 6)+1;
break;
case LevelState.FiveState:
speed = GetSpeedByLevel(SpeedLevel.Four);
num = UnityEngine.Random.Range(4, 7)+2;
break;

}
return new Vector2(speed, num);
}
public static bool IsBoundIndex(int minindex,int maxindex,int index)
{
bool b1 = Mathf.Abs(index - minindex) > 4;
bool b2 = Mathf.Abs(index - maxindex) > 4;
return (b1 && b2);
}
public static List GetRandomInts(int min,int max,int num)
{
List endvalue=new List();
while (num>=1)
{
int temp = UnityEngine.Random.Range(min, max);
if (!endvalue.Contains(temp))
{
num--;
}
}
return endvalue;
}

{
Vector3 dir = (end - start).normalized;
Vector3 dir1 = (temp - start).normalized;

float cos = Vector3.Dot(dir, dir1);
float sin = Mathf.Sqrt(1 - cos * cos);

bool b = (dir.x*dir1.y - dir.y*dir1.x)>0;
Vector3 normal = (Quaternion.Euler(0, 0, 90) * dir).normalized;
Vector3 endvalue = -dir*hdistance + (b?1:-1)*normal*vdistance;

return endvalue+end;
}
}```

```using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class GameLayer : MonoBehaviour, IGetCurGameState
{
[SerializeField] private  Material _meshMaterial;
[SerializeField] private GameObject _obstacle;
[SerializeField] private GameObject _explostion;
[SerializeField] private Camera _mainCamera;
[SerializeField] private GameObject _playerprefab;

[SerializeField] private EventSystem eventSystem;
[SerializeField] private GraphicRaycaster graphicRaycaster;
[SerializeField] private PlayerHandler _playerHandler;
[SerializeField] private UIManager _uiManager;
[SerializeField] private GameObject _circle;

private float _speed;
private GameState _curGameState;

private int _score = 0;
private CirclesManager _manager;
private bool _isReverse=true;

#region MonoBehavior Function

private PointerEventData _eventData=null;
private void Awake()
{
_eventData = new PointerEventData(eventSystem);
}
private void Start()
{
_curGameState = GameState.Playing;
_manager=new CirclesManager();
_manager.Intilized(_meshMaterial, _playerHandler, _obstacle, _circle);
_manager.InitlizedCircleObj(4, new List() { 2, 1.5f, 2.5f, 1.0f });
_uiManager.Intilized(this);

MessagingSystem.Instance.AttachListener(typeof(GamstateMessage), this.HandleGameStateMsg);
_mainCamera.transform.DOMove(new Vector3(0, 0, -10), 2.0f);
_speed = (int)VectorHelpr.GetSpeedAndObsByScore(_score).x;

Pools.ManageRecyclePool(new ObjectPool() { Prefab = _obstacle, InstancesToPreallocate = 15});
}

private Vector3 _offsetVector3;
private bool _isMove = false;
private CircleData _curCircleData;
private float _tempr;
private bool _isDown = false;
private void Update()
{
if (_curGameState == GameState.Playing)
{
_curCircleData = _manager.CurCircleData;
if (Input.GetMouseButtonDown(0)&&_isDown)
{
if (CheckGuiRaycastObjects()) return;

var temp = _curCircleData.GetTargetIndex(_curCircleData.NextVector3 - _curCircleData.Parent.position, ListType.In);
var temp1 = _curCircleData.GetTargetIndex(_playerHandler.transform.position - _curCircleData.Parent.position, ListType.In);

if (Mathf.Abs(temp - temp1) <= 1)
{

Vector3 prePos = _curCircleData.Parent.position;

_speed = (int) VectorHelpr.GetSpeedAndObsByScore(_score).x;
_manager.UpdateList((int)VectorHelpr.GetSpeedAndObsByScore(_score).y);
_isReverse = !_isReverse;
_curCircleData = _manager.CurCircleData;

var endvalue = VectorHelpr.GetPosByNormal(prePos, _curCircleData.Parent.position,

_index = _curCircleData.GetTargetIndex(endvalue - _curCircleData.Parent.position, ListType.In);
if (_isReverse)
{
_index++;
_index %= _curCircleData.Count;
}
else
{
_index--;
if (_index < 0)
_index += _curCircleData.Count;
}

_playerHandler.transform.position = endvalue;
_isMove = false;

_curCircleData.CurType = ListType.In;

VectorHelpr.CamerFloow(_mainCamera, _curCircleData.Parent.position);
_uiManager.SetCurScoreText(++_score);
}
else
{

if (_curCircleData.CurType == ListType.In)
{
_curCircleData.CurType = ListType.Out;
_offsetVector3 = _playerHandler.transform.position - _curCircleData.Parent.position;
_playerHandler.transform.position = _curCircleData.Parent.position + _offsetVector3.normalized * _tempr;
}
else
{
_offsetVector3 = _playerHandler.transform.position - _curCircleData.Parent.position;
_tempr = _curCircleData.Radius - _playerHandler.Skin - 0.1f;
_playerHandler.transform.position = _curCircleData.Parent.position + _offsetVector3.normalized * _tempr;
_curCircleData.CurType = ListType.In;
}
}
}
if (_index > 0 && !_isDown)
_isDown = true;
if (_isMove)
MoveThisPlayer(_curCircleData);
_isMove = true;

if (VectorHelpr.JudgeGameOver(_curCircleData, _playerHandler.transform))
_curGameState = GameState.FailOver;
}
if (_curGameState == GameState.FailOver)
{
Instantiate(_explostion, _playerHandler.transform.position, Quaternion.identity);
Destroy(_playerHandler.gameObject);
_uiManager.PopUpFailItem(0, null);

_manager.ClearCircleData();
_curGameState = GameState.None;
}
}

#endregion
#region Private Function

private int _index = 0;
private void MoveThisPlayer(CircleData curCircleData)
{
Vector3 targetPos = curCircleData.GetTargetPos(_index, curCircleData.CurType) + curCircleData.Parent.position;
Vector3 playerPos = _playerHandler.transform.position;
float delta = _speed*Time.smoothDeltaTime;
bool b = (Vector3.SqrMagnitude(targetPos - playerPos) < delta*delta);
if (b)
{
if (_isReverse)
{
_index++;
_index %= curCircleData.Count;
}
else
{
_index--;
if (_index < 0)
_index += curCircleData.Count;
}

}
_playerHandler.transform.position += delta * (targetPos - playerPos).normalized;
}
private bool HandleGameStateMsg(BaseMessage curMessage)
{
GamstateMessage gamstateMessage = curMessage as GamstateMessage;
if (gamstateMessage != null)
{
_curGameState = gamstateMessage.CurGameState;
}
return true;
}
private bool CheckGuiRaycastObjects()
{
_eventData.pressPosition = Input.mousePosition;
_eventData.position = Input.mousePosition;
List list = new List();
graphicRaycaster.Raycast(_eventData, list);
for (int i = 0; i < list.Count; i++)
{
if (list[i].gameObject.CompareTag("Button"))
return true;
}
return false;
}

#endregion
public GameState GetGameState()
{
return _curGameState;
}
public void ReIntilized()
{
GameObject ob = GameObject.Instantiate(_playerprefab, new Vector3(0,0.5f,0), Quaternion.identity)as GameObject;
if (ob != null) _playerHandler = ob.GetComponent();

_speed =VectorHelpr.GetSpeedByLevel(SpeedLevel.One);
_manager.Intilized(_meshMaterial, _playerHandler, _obstacle, _circle);
_manager.InitlizedCircleObj(4, new List() { 2, 1.5f, 2.5f, 1.0f });
_manager.Reintilized(delegate {_curGameState=GameState.Playing;});

_isReverse = true;
_score = 0;
_index = 0;
_mainCamera.transform.position=new Vector3(0,0,-10);
VectorHelpr.CamerFloow(_mainCamera, _manager.CurCircleData.Parent.position);
}

}
```

http://pan.baidu.com/s/1i5uHuXf