public void Reset(int[] lengths, float xStart, float yStart, float xDelta, float yDelta) { if (lengths == null || lengths.Length == 0) { throw new Exception("Invalid lengths"); } // init Lengths = lengths; IsAngry = false; Row = 0; Column = 0; IsDone = false; XStart = xStart; YStart = yStart; XDelta = xDelta; YDelta = yDelta; // reset indexs TailIndex.Reset(); FaceIndex.Reset(); for (int i = 0; i < LegIndexes.Length; i++) { LegIndexes[i].Reset(); } }
public CubePos CheckHead() { // change snake to target state //moving = false; //rotating = false; willGrow = false; // whether snake head need rotate over edge nextFaceIndex = FaceIndex.none; if (cubePos.x + deltaCubePos.x < 0) { //Debug.Log ("x neg"); nextFaceIndex = FaceIndex.x_neg; } else if (cubePos.x + deltaCubePos.x > moveDim) { //Debug.Log ("x pos"); nextFaceIndex = FaceIndex.x_pos; } else if (cubePos.y + deltaCubePos.y < 0) { //Debug.Log ("y neg"); nextFaceIndex = FaceIndex.y_neg; } else if (cubePos.y + deltaCubePos.y > moveDim) { //Debug.Log ("y pos"); nextFaceIndex = FaceIndex.y_pos; } else if (cubePos.z + deltaCubePos.z < 0) { //Debug.Log ("z neg"); nextFaceIndex = FaceIndex.z_neg; } else if (cubePos.z + deltaCubePos.z > moveDim) { //Debug.Log ("z pos"); nextFaceIndex = FaceIndex.z_pos; } if (nextFaceIndex != FaceIndex.none) { return(HandleEdge()); } // whether there is a operation to handle if (snakeChangeDirection != SnakeChangeDirection.none) { return(HandleOperation()); } nextCubePos = nextCubePos + deltaCubePos; CheckFood(); return(nextCubePos); }
public void SetColor(FaceIndex face) { if (colors == null) { colors = Resources.LoadAll <Material>("Colors"); } faces[(int)face].GetComponent <Renderer>().material = colors[(int)face]; }
public void Draw(IGraphics graphics) { // nothing to draw if (Lengths == null || Lengths.Length == 0) { return; } // draw paw prints (the tracks we have already been through for (int i = 0; i < Lengths.Length && i <= Row; i++) { var tx = XStart; var ty = YStart + (YDelta * i); var maxx = (i < Row) ? XStart + (Lengths[i] * XDelta) : XStart + (Column * XDelta) - Width; while (tx < maxx) { // display paw-prints graphics.Image(PawPrintImage.Image, tx, ty, Width, Height); tx += Width; } } // the cat reached the end, this should not be possible in a normal game if (IsDone) { return; } // set the cat x,y based on the current track var x = XStart + (XDelta * Column) - (Width * 0.9f); var y = YStart + (Row * YDelta); var index = 0; // add the body graphics.Image(BodyImage.Image, x, y, Width, Height); // draw legs for (int i = 0; i < LegIndexes.Length; i++) { index = LegIndexes[i].Increment(); graphics.Image(LegImages[i][index].Image, x, y, Width, Height); } // draw tail graphics.Image(TailImages[TailIndex.Increment()].Image, x, y, Width, Height); // draw face index = FaceIndex.Increment(); graphics.Image(FaceImages[index].Image, x, y, Width, Height); if (index < 7) { // add a mood graphics.Image(IsAngry ? AngryImage.Image : HappyImage.Image, x, y, Width, Height); } }
// meathods /* * Given a face string this function will return the integer value of * the face (order it is in the array ). * It is very important to remember that 0-ace and 1-two 2-three and so on. */ public static FaceIndex FindFace(string face_) { for (FaceIndex i = FaceIndex.ACE; (int)i < FaceList.Length; ++i) { if (FaceList[(int)i] == face_.ToLower()) { return(i); } } return(FaceIndex.NOT_FOUND); }
public void Init(CubePos cp, CubePos dcp, float cd, float mt, int md, FaceIndex cfi) { cubePos = cp; deltaCubePos = dcp; cubeDistance = cd; moveTime = mt; moveDim = md; currentFaceIndex = cfi; nextCubePos = cubePos; targetPos = nextCubePos.ToVec3(); }
public static Vector3 GetVector3ByFaceIndex(FaceIndex fi) { switch(fi){ case FaceIndex.x_neg: return new Vector3 (-1,0,0); case FaceIndex.x_pos: return new Vector3 (1,0,0); case FaceIndex.y_neg: return new Vector3 (0,-1,0); case FaceIndex.y_pos: return new Vector3 (0,1,0); case FaceIndex.z_neg: return new Vector3 (0,0,-1); case FaceIndex.z_pos: return new Vector3 (0,0,1); } return Vector3.zero; }
/// <summary> /// Reads an OBJ mesh from a file. /// Please note that this method only supports extremely simple OBJ meshes. /// </summary> /// <param name="path">The file path.</param> public void ReadFile(string path) { var materialLibraryList = new List <string>(); var readVertexList = new List <Vector3d>(VertexInitialCapacity); List <Vector3> readNormalList = null; List <Vector3> readTexCoordList = null; var vertexList = new List <Vector3d>(VertexInitialCapacity); List <Vector3> normalList = null; List <Vector3> texCoordList = null; var triangleIndexList = new List <int>(IndexInitialCapacity); var subMeshIndicesList = new List <int[]>(); var subMeshMaterialList = new List <string>(); var faceTable = new Dictionary <FaceIndex, int>(IndexInitialCapacity); var tempFaceList = new List <int>(6); bool texCoordsAre3D = false; string currentGroup = null; string currentObject = null; string currentMaterial = null; int newFaceIndex = 0; using (StreamReader reader = File.OpenText(path)) { string line; while ((line = reader.ReadLine()) != null) { if (line.Length == 0 || line[0] == '#') { continue; } string[] lineSplit = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string firstPart = lineSplit[0]; if (string.Equals(firstPart, "v")) { if (lineSplit.Length < 4) { throw new InvalidDataException("Vertices needs at least 3 components."); } double f0, f1, f2; double.TryParse(lineSplit[1], NumberStyles.Float, CultureInfo.InvariantCulture, out f0); double.TryParse(lineSplit[2], NumberStyles.Float, CultureInfo.InvariantCulture, out f1); double.TryParse(lineSplit[3], NumberStyles.Float, CultureInfo.InvariantCulture, out f2); readVertexList.Add(new Vector3d(f0, f1, f2)); } else if (string.Equals(firstPart, "vn")) { if (lineSplit.Length != 4) { throw new InvalidDataException("Normals must be 3 components."); } if (readNormalList == null) { readNormalList = new List <Vector3>(VertexInitialCapacity); } float f0, f1, f2; float.TryParse(lineSplit[1], NumberStyles.Float, CultureInfo.InvariantCulture, out f0); float.TryParse(lineSplit[2], NumberStyles.Float, CultureInfo.InvariantCulture, out f1); float.TryParse(lineSplit[3], NumberStyles.Float, CultureInfo.InvariantCulture, out f2); readNormalList.Add(new Vector3(f0, f1, f2)); } else if (string.Equals(firstPart, "vt")) { if (lineSplit.Length < 3) { throw new InvalidDataException("Texture coordinates needs at least 2 components."); } if (readTexCoordList == null) { readTexCoordList = new List <Vector3>(VertexInitialCapacity); } float f0, f1, f2; float.TryParse(lineSplit[1], NumberStyles.Float, CultureInfo.InvariantCulture, out f0); float.TryParse(lineSplit[2], NumberStyles.Float, CultureInfo.InvariantCulture, out f1); if (lineSplit.Length > 3) { float.TryParse(lineSplit[3], NumberStyles.Float, CultureInfo.InvariantCulture, out f2); if (!texCoordsAre3D && f2 != 0f) { texCoordsAre3D = true; } } else { f2 = 0f; } readTexCoordList.Add(new Vector3(f0, f1, f2)); } else if (string.Equals(firstPart, "f")) { if (lineSplit.Length < 4) { throw new InvalidDataException("Faces must have at least three indices."); } int vertexIndex, texIndex, normalIndex; tempFaceList.Clear(); for (int i = 1; i < lineSplit.Length; i++) { string word = lineSplit[i]; int slashCount = CountOccurrences(word, '/'); if (slashCount == 0) { int.TryParse(word, out vertexIndex); vertexIndex = ShiftIndex(vertexIndex, readVertexList.Count); texIndex = -1; normalIndex = -1; } else if (slashCount == 1) { int splitIndex = word.IndexOf('/'); string word1 = word.Substring(0, splitIndex); string word2 = word.Substring(splitIndex); int.TryParse(word1, out vertexIndex); int.TryParse(word2, out texIndex); vertexIndex = ShiftIndex(vertexIndex, readVertexList.Count); texIndex = ShiftIndex(texIndex, readTexCoordList.Count); normalIndex = -1; } else if (slashCount == 2) { int splitIndex1 = word.IndexOf('/'); int splitIndex2 = word.IndexOf('/', splitIndex1 + 1); string word1 = word.Substring(0, splitIndex1); string word2 = word.Substring(splitIndex1 + 1, splitIndex2 - splitIndex1 - 1); string word3 = word.Substring(splitIndex2 + 1); int.TryParse(word1, out vertexIndex); bool hasTexCoord = int.TryParse(word2, out texIndex); int.TryParse(word3, out normalIndex); vertexIndex = ShiftIndex(vertexIndex, readVertexList.Count); if (hasTexCoord) { texIndex = ShiftIndex(texIndex, readTexCoordList.Count); } else { texIndex = -1; } normalIndex = ShiftIndex(normalIndex, readNormalList.Count); } else { throw new InvalidDataException(string.Format("Invalid face data are supported (expected a maximum of two slashes, but found {0}.", slashCount)); } int faceIndex; var face = new FaceIndex(vertexIndex, texIndex, normalIndex); if (faceTable.TryGetValue(face, out faceIndex)) { tempFaceList.Add(faceIndex); } else { faceTable[face] = newFaceIndex; tempFaceList.Add(newFaceIndex); ++newFaceIndex; vertexList.Add(readVertexList[vertexIndex]); if (readNormalList != null) { if (normalList == null) { normalList = new List <Vector3>(VertexInitialCapacity); } if (normalIndex >= 0 && normalIndex < readNormalList.Count) { normalList.Add(readNormalList[normalIndex]); } else { normalList.Add(Vector3.zero); } } if (readTexCoordList != null) { if (texCoordList == null) { texCoordList = new List <Vector3>(VertexInitialCapacity); } if (texIndex >= 0 && texIndex < readTexCoordList.Count) { texCoordList.Add(readTexCoordList[texIndex]); } else { texCoordList.Add(Vector3.zero); } } } } // Convert into triangles (currently we only support triangles and quads) int faceIndexCount = tempFaceList.Count; if (faceIndexCount >= 3 && faceIndexCount < 5) { triangleIndexList.Add(tempFaceList[0]); triangleIndexList.Add(tempFaceList[1]); triangleIndexList.Add(tempFaceList[2]); if (faceIndexCount > 3) { triangleIndexList.Add(tempFaceList[2]); triangleIndexList.Add(tempFaceList[3]); triangleIndexList.Add(tempFaceList[0]); } } } else if (string.Equals(firstPart, "g")) { string groupName = string.Join(" ", lineSplit, 1, lineSplit.Length - 1); currentGroup = groupName; } else if (string.Equals(firstPart, "o")) { string objectName = string.Join(" ", lineSplit, 1, lineSplit.Length - 1); currentObject = objectName; } else if (string.Equals(firstPart, "mtllib")) { string materialLibraryPath = string.Join(" ", lineSplit, 1, lineSplit.Length - 1); materialLibraryList.Add(materialLibraryPath); } else if (string.Equals(firstPart, "usemtl")) { string materialName = string.Join(" ", lineSplit, 1, lineSplit.Length - 1); currentMaterial = materialName; if (triangleIndexList.Count > 0) { subMeshIndicesList.Add(triangleIndexList.ToArray()); triangleIndexList.Clear(); if (subMeshMaterialList.Count != subMeshIndicesList.Count) { subMeshMaterialList.Add("none"); } } subMeshMaterialList.Add(materialName); } } } if (triangleIndexList.Count > 0) { subMeshIndicesList.Add(triangleIndexList.ToArray()); triangleIndexList.Clear(); if (currentMaterial == null) { subMeshMaterialList.Add("none"); } } int subMeshCount = subMeshIndicesList.Count; bool hasNormals = (readNormalList != null); bool hasTexCoords = (readTexCoordList != null); int vertexCount = vertexList.Count; var processedVertexList = new List <Vector3d>(vertexCount); var processedNormalList = (hasNormals ? new List <Vector3>(vertexCount) : null); var processedTexCoordList = (hasTexCoords ? new List <Vector3>(vertexCount) : null); var processedIndices = new List <int[]>(subMeshCount); var indexMappings = new Dictionary <int, int>(IndexInitialCapacity); for (int subMeshIndex = 0; subMeshIndex < subMeshCount; subMeshIndex++) { var indices = subMeshIndicesList[subMeshIndex]; for (int i = 0; i < indices.Length; i++) { int index = indices[i]; int mappedIndex; if (indexMappings.TryGetValue(index, out mappedIndex)) { indices[i] = mappedIndex; } else { processedVertexList.Add(vertexList[index]); if (hasNormals) { processedNormalList.Add(normalList[index]); } if (hasTexCoords) { processedTexCoordList.Add(texCoordList[index]); } mappedIndex = processedVertexList.Count - 1; indexMappings[index] = mappedIndex; indices[i] = mappedIndex; } } processedIndices.Add(indices); } vertices = processedVertexList.ToArray(); normals = (processedNormalList != null ? processedNormalList.ToArray() : null); if (processedTexCoordList != null) { if (texCoordsAre3D) { texCoords3D = processedTexCoordList.ToArray(); } else { int texCoordCount = processedTexCoordList.Count; texCoords2D = new Vector2[texCoordCount]; for (int i = 0; i < texCoordCount; i++) { var texCoord = processedTexCoordList[i]; texCoords2D[i] = new Vector2(texCoord.x, texCoord.y); } } } else { texCoords2D = null; texCoords3D = null; } /* * vertices = new Vector3d[faceCount]; * for (int i = 0; i < faceCount; i++) * { * int vertexIndex = faceList[i].x; * vertices[i] = vertexList[vertexIndex]; * } * * if (normalList != null) * { * int normalCount = normalList.Count; * normals = new Vector3[faceCount]; * for (int i = 0; i < faceCount; i++) * { * int normalIndex = faceList[i].z; * if (normalIndex >= 0 && normalIndex < normalCount) * { * normals[i] = normalList[normalIndex]; * } * } * } * else * { * normals = null; * } * * if (texCoordList != null) * { * int texCoordCount = texCoordList.Count; * if (texCoordsAre3D) * { * texCoords3D = new Vector3[faceCount]; * for (int i = 0; i < faceCount; i++) * { * int texCoordIndex = faceList[i].y; * if (texCoordIndex >= 0 && texCoordIndex < texCoordCount) * { * texCoords3D[i] = texCoordList[texCoordIndex]; * } * } * } * else * { * texCoords2D = new Vector2[faceCount]; * for (int i = 0; i < faceCount; i++) * { * int texCoordIndex = faceList[i].y; * if (texCoordIndex >= 0 && texCoordIndex < texCoordCount) * { * var texCoord = texCoordList[texCoordIndex]; * texCoords2D[i] = new Vector2(texCoord.x, texCoord.y); * } * } * } * } * else * { * texCoords2D = null; * texCoords3D = null; * } */ subMeshIndices = processedIndices.ToArray(); subMeshMaterials = subMeshMaterialList.ToArray(); materialLibraries = materialLibraryList.ToArray(); }
CubePos HandleEdge() { //Vector3 rotateOffset = new Vector3 (0, 0, 0); switch (nextFaceIndex) { case FaceIndex.x_neg: deltaCubePos.x = 0; //rotateOffset.x = 0.5f; break; case FaceIndex.x_pos: deltaCubePos.x = 0; //rotateOffset.x = -0.5f; break; case FaceIndex.y_neg: deltaCubePos.y = 0; //rotateOffset.y = 0.5f; break; case FaceIndex.y_pos: deltaCubePos.y = 0; //rotateOffset.y = -0.5f; break; case FaceIndex.z_neg: deltaCubePos.z = 0; //rotateOffset.z = 0.5f; break; case FaceIndex.z_pos: deltaCubePos.z = 0; //rotateOffset.z = -0.5f; break; } switch (currentFaceIndex) { case FaceIndex.x_neg: deltaCubePos.x = 1; //rotateOffset.x = 0.5f; break; case FaceIndex.x_pos: deltaCubePos.x = -1; //rotateOffset.x = -0.5f; break; case FaceIndex.y_neg: deltaCubePos.y = 1; //rotateOffset.y = 0.5f; break; case FaceIndex.y_pos: deltaCubePos.y = -1; //rotateOffset.y = -0.5f; break; case FaceIndex.z_neg: deltaCubePos.z = 1; //rotateOffset.z = 0.5f; break; case FaceIndex.z_pos: deltaCubePos.z = -1; //rotateOffset.z = -0.5f; break; } willRotate = true; willRotateCamera = true; Vector3 vecCurrent = Utils.GetVector3ByFaceIndex(currentFaceIndex); Vector3 vecNext = Utils.GetVector3ByFaceIndex(nextFaceIndex); rotateAxis = Vector3.Cross(vecCurrent, vecNext); rotateBase = 0.5f * (right_bottom.position + left_bottom.position); rotateAngle = 90; transform.RotateAround(rotateBase, rotateAxis, rotateAngle); targetRot = transform.eulerAngles; transform.RotateAround(rotateBase, rotateAxis, -rotateAngle); //rotateBase = transform.position + rotateOffset * cubeDistance; currentFaceIndex = nextFaceIndex; nextCubePos = nextCubePos + deltaCubePos; CheckFood(); return(nextCubePos); }
private static bool HashVertexesAndFaces_Helper(LinkedList <M3D.Model.Utils.Vector3> verticies, ref ProgressHelper progressHelper, out List <M3D.Model.Utils.Vector3> newVerticesList, out List <ModelData.FaceIndex> newFaceList) { newVerticesList = null; newFaceList = null; var num1 = 0; var num2 = 0; var source = new Dictionary <M3D.Model.Utils.Vector3, int>(); var faceIndexSet = new HashSet <ModelData.FaceIndex>(); var flag = ModelData.VerticiesFlipped(verticies); while (verticies.First != null) { M3D.Model.Utils.Vector3 vector3_1 = verticies.First.Value; ModelData.AssertIfNANOrNULL(vector3_1); verticies.RemoveFirst(); M3D.Model.Utils.Vector3 vector3_2 = verticies.First.Value; ModelData.AssertIfNANOrNULL(vector3_2); verticies.RemoveFirst(); M3D.Model.Utils.Vector3 vector3_3 = verticies.First.Value; ModelData.AssertIfNANOrNULL(vector3_3); verticies.RemoveFirst(); if (flag) { M3D.Model.Utils.Vector3 vector3_4 = vector3_1; vector3_1 = vector3_3; vector3_3 = vector3_4; } M3D.Model.Utils.Vector3 normal = ModelData.CalcNormal(vector3_1, vector3_2, vector3_3); if (!double.IsNaN(normal.X) && !double.IsNaN(normal.Y) && !double.IsNaN(normal.Z)) { int[] faceIndicies = new int[3]; M3D.Model.Utils.Vector3[] vector3Array = new M3D.Model.Utils.Vector3[3] { vector3_1, vector3_2, vector3_3 }; for (var index = 0; index < 3; ++index) { M3D.Model.Utils.Vector3 key = vector3Array[index]; int num3; if (source.ContainsKey(key)) { num3 = source[key]; } else { source.Add(key, num1); num3 = num1++; } faceIndicies[index] = num3; } for (var index1 = 0; index1 < 3; ++index1) { for (var index2 = index1 + 1; index2 < 3; ++index2) { if (faceIndicies[index1] == faceIndicies[index2]) { return(false); } } } var faceIndex = new FaceIndex(faceIndicies, normal); if (!faceIndexSet.Contains(faceIndex)) { faceIndexSet.Add(faceIndex); } progressHelper.Process(num2 += 3); } } IEnumerable <M3D.Model.Utils.Vector3> collection = source.OrderBy <KeyValuePair <M3D.Model.Utils.Vector3, int>, int>(pair => pair.Value).Select <KeyValuePair <M3D.Model.Utils.Vector3, int>, M3D.Model.Utils.Vector3>(pair => pair.Key); newVerticesList = new List <M3D.Model.Utils.Vector3>(collection); source.Clear(); newFaceList = new List <ModelData.FaceIndex>(faceIndexSet); faceIndexSet.Clear(); return(true); }
private FaceIndex IndexCells(Triangles triangles) { var zero = 0; var fals = false; var index = new FaceIndex(WorkBufferPool); triangles.Fill(index.Cells); //First get cells and canonicalize var cells = index.Cells; var nc = cells.UsedSize; var cellsData = cells.Data; for (var i = 0; i < nc; ++i) { var c = cellsData[i]; var x = c.x; var y = c.y; var z = c.z; if (y < z) { if (y < x) { c.x = y; c.y = z; c.z = x; cellsData[i] = c; } } else if (z < x) { c.x = z; c.y = x; c.z = y; cellsData[i] = c; } } WorkBuffer <Int3> .Sort(cells); //Initialize flag array var flags = index.Flags; flags.Fill(ref zero, nc); //Build neighbor index, initialize queues var active = index.Active; var next = index.Next; var neighbor = index.Neighbor; var constraint = index.Constraint; var boundary = index.Boundary; neighbor.Fill(ref zero, nc * 3); constraint.Fill(ref fals, nc * 3); var flagsData = flags.Data; var neighborData = neighbor.Data; var constraintData = constraint.Data; for (var i = 0; i < nc; ++i) { var c = cellsData[i]; for (var j = 0; j < 3; ++j) { var x = 0; var y = 0; switch (j) { case 0: x = c.x; y = c.y; break; case 1: x = c.y; y = c.z; break; case 2: x = c.z; y = c.x; break; } var a = neighborData[3 * i + j] = Locate(cells, y, x, triangles.Opposite(y, x)); var b = constraintData[3 * i + j] = triangles.IsConstraint(x, y); if (a < 0) { if (b) { next.Push(ref i); } else { active.Push(ref i); flagsData[i] = 1; } if (Infinity) { var v = new Int3(y, x, -1); boundary.Push(ref v); } } } } return(index); }