2,538
社区成员
发帖
与我相关
我的任务
分享
public GameObject cellteil;
public GameObject character_walker;
public GameObject cellcube;
public GameObject character_solder;
public GameObject character_solder_impl;
public GameObject treep01;//场景中 带碰撞的树的模型
public int map_w, map_h;
// Use this for initialization
void Start () {
map_w = 60;
map_h = 80;
InitMap ();
//放置人物
MakeCharacter ();
}
// Update is called once per frame
void Update () {
doCharacterOnGround ();
}
void InitMap(){
TileCell[,] tc = new TileCell[map_w,map_h];
//首先生成数据模型,第一步全部填充为木质地板。
for (int x = 0; x < map_w; x++) {
for (int y = 0; y < map_h; y++) {
tc [x, y].tiletype = 0;
//增加下修改,边缘地区全部换为3类型, 3类型表示带有1x1碰撞的树,作为地图边界
if (x == 0 || x == map_w - 1 || y == 0 || y == map_h - 1) {
tc [x, y].tiletype = 0;
}
//CreateCell (x, y);
}
}
//在某个位置放置 小花园
MakeGarden(tc);
//根据模型数据生成实际显示对象
for (int x = 0; x < map_w; x++) {
for (int y = 0; y < map_h; y++) {
//tc [x, y].tiletype = 0;
//if (tc [x, y].tiletype == 0) {
CreateCell (x, y,tc[x,y].tiletype);
//}
}
}
}
//创建随机花园
void MakeGarden(TileCell[,] tc){
//随机坐标位置,花园默认大小 8*8 所以坐标必须在1-13 之间
//int i = num1.Next(12);
int gx =Random.Range(1, 40);
int gy=Random.Range(1, 40);
tc [gx, gy].tiletype = 1;
tc [gx+1, gy].tiletype = 1;
tc [gx+2, gy].tiletype = 1;
tc [gx+5, gy].tiletype = 1;
tc [gx+6, gy].tiletype = 1;
tc [gx+7, gy].tiletype = 1;
tc [gx, gy+1].tiletype = 1;
tc [gx, gy+2].tiletype = 1;
tc [gx+7, gy+1].tiletype = 1;
tc [gx+7, gy+2].tiletype = 1;
tc [gx, gy+5].tiletype = 1;
tc [gx, gy+6].tiletype = 1;
tc [gx+7, gy+5].tiletype = 1;
tc [gx+7, gy+6].tiletype = 1;
tc [gx, gy+7].tiletype = 1;
tc [gx+1, gy+7].tiletype = 1;
tc [gx+2, gy+7].tiletype = 1;
tc [gx+5, gy+7].tiletype = 1;
tc [gx+6, gy+7].tiletype = 1;
tc [gx+7, gy+7].tiletype = 1;
}
//将数组中的地块实现化
private void CreateCell (int x, int z,int type) {
if (type == 0) {
GameObject go01 = Instantiate (cellteil);
//go01.transform.parent = transform;
go01.transform.position = new Vector3 (x * 1.0f, 2.0f, z * 1.0f);
}
if (type == 1) {
GameObject go01 = Instantiate (cellcube);
//go01.transform.parent = transform;
go01.transform.position = new Vector3 (x * 1.0f, 3.0f, z * 1.0f);
}
if (type == 3) {
GameObject go01 = Instantiate (treep01);
//go01.transform.parent = transform;
go01.transform.position = new Vector3 (x * 1.0f, 3.0f, z * 1.0f);
}
//go01.transform.localPosition = new Vector3(x - 3 * 0.5f + 0.5f, 0f, z - 3 * 0.5f + 0.5f);
//prefab_gress newCell = Instantiate(cellteil) as prefab_gress;
//cells[x, z] = newCell;
//newCell.name = "Maze Cell " + x + ", " + z;
//newCell.transform.parent = transform;
//newCell.transform.localPosition = new Vector3(x - sizeX * 0.5f + 0.5f, 0f, z - sizeZ * 0.5f + 0.5f);
}
//放置人物
private void MakeCharacter(){
//主要角色
character_solder_impl = Instantiate (character_solder);
character_solder_impl.name = "solder01";
character_solder_impl.transform.position=new Vector3 (10 * 1.0f, 6.0f, 10 * 1.0f);
GameObject.Find ("Main Camera").GetComponent<follow> ().doSetFollower ();
//敌人,也就是僵尸
character_walker= Instantiate (character_walker);
character_walker.name = "character_walker01";
character_walker.transform.position=new Vector3 (20 * 1.0f, 6.0f, 30 * 1.0f);
}
private void doCharacterOnGround(){
if (character_solder_impl==null) {
//移动到位置就停止了
return ;
}
Vector3 moveDirection = Vector3.zero;
float gravity = 10f; //这个是下落速度
// doCharacterOnGround = GetComponent<CharacterController> ();
if (character_solder_impl.GetComponent<CharacterController>().isGrounded) {
//Debug.Log ("已经落地");
//character_solder_impl.GetComponent<CharacterController>().SimpleMove( new Vector3(0,0,-30) * Time.deltaTime);
} else {
//没有落地
moveDirection.y -= gravity ;
//Debug.Log ("没有没有没有没有落地"+moveDirection.y);
character_solder_impl.GetComponent<CharacterController>().SimpleMove(moveDirection * Time.deltaTime);
}
}