public GameBody CreateGameBody(int coordx, int coordy) { GameObject obj = new GameObject(); GameBody body = obj.AddComponent <GameBody> (); body.ChangeCoord(coordx, coordy); body.AddCeil(CreateCeil(), 0, 0); WorldManager.Instance.AddGameBodyToWorld(body); return(body); }
//return 0.没撞到 1.撞到左边 2.撞到上面 3.撞到右边 4.撞到下面 public int MoveGameBody(GameBody body, float x, float y) { ArrayList ceils = body.ceils; float px = (body.gameObject.transform.position.x + x) / 0.16f; float py = (body.gameObject.transform.position.y + y) / 0.16f; int cx = 0; // = Mathf.RoundToInt(px); int cy = 0; // = Mathf.RoundToInt(py); int tx = 0; if (x > 0) { cx = (int)Mathf.Ceil(px); tx = (int)Mathf.Floor(px); } else { cx = (int)Mathf.Floor(px); tx = (int)Mathf.Ceil(px); } if (y > 0) { cx = (int)Mathf.Ceil(py); } else { cy = (int)Mathf.Floor(py); } int result = 0; int ground = -1; foreach (Ceil ceil in ceils) { if (coords.ContainsKey((cx + ceil.ceilx) + "," + (cy + ceil.ceily))) { if (coords[(cx + ceil.ceilx) + "," + (cy + ceil.ceily)].body != body) { if (cy - ceil.coordy == 0) { if (cx - ceil.coordx > 0) { result = 3; body.gameObject.transform.position = new Vector3(body.coordx * 0.16f, body.gameObject.transform.position.y); } else { result = 1; body.gameObject.transform.position = new Vector3(body.coordx * 0.16f, body.gameObject.transform.position.y); } } else { if (cy - ceil.coordy > 0) { result = 2; body.gameObject.transform.position = new Vector3(body.gameObject.transform.position.x, body.coordy * 0.16f); } else { result = 4; body.gameObject.transform.position = new Vector3(body.gameObject.transform.position.x, body.coordy * 0.16f); } } break; } } if (y == 0 && ground == -1) { if (coords.ContainsKey((tx + ceil.ceilx) + "," + (-1 + ceil.coordy))) { if (coords [(tx + ceil.ceilx) + "," + (-1 + ceil.coordy)].body != body) { ground = 0; } } } } if (result == 0) { x = body.gameObject.transform.position.x + x; y = body.gameObject.transform.position.y + y; if (cx != body.coordx || cy != body.coordy) { body.ChangeCoord(cx, cy); } body.gameObject.transform.position = new Vector3(x, y); return(ground); } return(result); }