public bool CheckRotation(VecInt oldPos, VecInt newPos,float delay) { if(oldPos.x < newPos.x){ if(oldPos.y > newPos.y){ newRotationStateNumber = 5; }else if(oldPos.y < newPos.y){ newRotationStateNumber = 3; }else{ newRotationStateNumber = 4; } }else if(oldPos.x > newPos.x){ if(oldPos.y > newPos.y){ newRotationStateNumber = 7; }else if(oldPos.y < newPos.y){ newRotationStateNumber = 1; }else{ newRotationStateNumber = 8; } }else{ if(oldPos.y > newPos.y){ newRotationStateNumber = 6; }else { newRotationStateNumber = 2; } } UpdateSprite(newRotationStateNumber); bool turning = false; if (newRotationStateNumber != oldRotationStateNumber) { turning = true; } creator.StartCoroutine(Rotate(delay)); return turning; }
public static VecInt[] Area(VecInt a,float width,float height,Rect? rect) { List<VecInt> tempList = new List<VecInt> (); List<VecInt> tempList2 = new List<VecInt> (); int wSteps = (int)(width / tileW); int hSteps = (int)(height / tileH); print ("A (" + a.x + "," + a.y + ") width: "+wSteps+" height: "+hSteps); int up = 1; int right = 1; //wSteps += 1; //hSteps += 1; if (wSteps < 0) { wSteps*=-1; right = -1; } if (hSteps < 0) { hSteps*=-1; up = -1; } //Debug.Log("hSteps: "+hSteps); for (j = 0; j < hSteps; j+=1) { //Debug.Log("========================== j: "+j); for (i = 0; i < wSteps; i+=1) { //Debug.Log("========================== i: "+i); int x = a.x +(i*right)-(j*up); int y = a.y +(i*right)+(j*up); tempList.Add(new VecInt(x,y)); if((i != wSteps-1)&&(j != hSteps-1)){ //Debug.Log("========================== end: "); if(up == -1 && right == -1){ y -=1; }else if(up==-1){ x +=1; }else if(right==-1){ x -=1; }else{ y +=1; } tempList.Add(new VecInt(x,y)); } } } //check if in rect if(rect!=null){ print ("null"); Rect rec = (Rect)rect; for (i =0;i<tempList.Count;i++){ //print ("(" + tempList[i].x + "," + tempList[i].y + ")"); if(tempList[i].x>rec.width-1||tempList[i].x<0){ }else if(tempList[i].y>rec.height-1||tempList[i].y<0){ }else{ tempList2.Add(tempList[i]); } } } return tempList2.ToArray (); }
internal void SetPath(VecInt[] path) { startPos = path[0]; /* Debug.Log ("[path] "+path[0].print); Debug.Log ("[path] "+path[1].print); Debug.Log ("[path] Lenght: "+path.Length); */ oldPos = startPos; endPos = path[path.Length-1]; currentPath = path; pathProgress = 0; loopInt = 0; oldWorldPos = creator.transform.position; Animate(); }
private bool checkNextPosFree(VecInt next,VecInt current) { //check collision array if (LevelData.GetMapObjects (next.x, next.y) !=null) { //set new path //Debug.Log("newPath!!!!!!!!!!!!!!"); SetPath(PathFind.FindPath ( new VecInt(current.x,current.y) , new VecInt(oldPos.x,oldPos.y) , LevelData.CollsionData ,true) ); return false; } else { //update collision array if free //Debug.Log(current.print+" Cur: "+LevelData.GroundVehicles [current.x, current.y]); //Debug.Log(next.print+" Next: "+LevelData.GroundVehicles [next.x, next.y]); /* LevelData.mapObjects[next.x, next.y] = LevelData.mapObjects[current.x, current.y]; LevelData.mapObjects[next.x, next.y].pos = next; LevelData.mapObjects[current.x, current.y] = null; LevelData.objectData[current.x, current.y] = 0; LevelData.objectData[next.x, next.y] = 1; */ LevelData.GetMapObjects(current.x, current.y).pos = next; LevelData.objectData [current.x, current.y] = 0; LevelData.objectData [next.x, next.y] = 1; LevelData.CollsionData[current.x,current.y] = false; LevelData.CollsionData[next.x,next.y] = true; //Debug.Log(next.print+" Next: "+LevelData.GroundVehicles [next.x, next.y]); if(next == endPos){ //currentPath = null; } return true; } }
public void FollowPath(VecInt[] path) { pathFollower.Init (this); pathFollower.SetPath (path); }
private IEnumerator PreBuildImageFollowCursor(){ Vector2 currentMousePos = IsoMath.getMouseWorldPosition(); if (mouseButton1Up && releasingPreBuildImage) { float buildingTileWidthX = 0; float buildingTileWidthY = 0; Vector2 hello = IsoMath.worldToTile(currentMousePos.x - buildingTileWidthX,currentMousePos.y + buildingTileWidthY); VecInt hello2 = new VecInt((int)hello.x,(int)hello.y); LevelData.constructBuilding(hello2.x,hello2.y,buildingId,buildingTileWidth); Color color = preBuildImage.renderer.material.color; color.a = 0; preBuildImage.renderer.material.color = color; releasingPreBuildImage = false; state = InputState.normal; }else{ if(buildingId < 2) { buildingTileWidth = 2; preBuildImage.transform.position = new Vector3(currentMousePos.x-0.50f, currentMousePos.y+0.25f, 0); }else if(buildingId == 3) { buildingTileWidth = 1; preBuildImage.transform.position = new Vector3(currentMousePos.x-0.60f, currentMousePos.y+0.55f, 0); }else if(buildingId == 4 || buildingId == 2) { buildingTileWidth = 3; preBuildImage.transform.position = new Vector3(currentMousePos.x+0.00f, currentMousePos.y+0.1f, 0); } yield return new WaitForEndOfFrame(); StartCoroutine(PreBuildImageFollowCursor()); } yield return 2; }
public void UpdateSelect(){ Vector2? TilePosN = IsoMath.getMouseTilePosition(); if (state == InputState.multiselect) { Vector2 currentMousePos = IsoMath.getMouseWorldPosition(); float areaWidth = currentMousePos.x-startMousePos.x; float areaHeight = currentMousePos.y-startMousePos.y; multiSelectArea.transform.localScale = new Vector3(areaWidth,areaHeight,1); if (Input.GetMouseButtonUp (0)) { VecInt[] selectedArea = IsoMath.Area(startTilePos,areaWidth,areaHeight,new Rect?(new Rect(0,0,LevelData.width,LevelData.height))); List<int> tempSelectedIds = new List<int>(); selected.Clear(); for(int i = 0;i < selectedArea.Length;i++){ //Debug.Log("selected: ("+selectedArea[i].x+","+selectedArea[i].y+")"); //LevelData.LoadedGroundTiles[selectedArea[i].x,selectedArea[i].y].GetComponent<SpriteRenderer>().color = new Color(0,0,1,1); MapObject Tempselected = LevelData.GetMapObjects((int)selectedArea[i].x, (int)selectedArea[i].y);//LevelData.mapObjects [(int)selectedArea[i].x, (int)selectedArea[i].y]; if(Tempselected != null){ tempSelectedIds.Add(Tempselected.gameObject.GetInstanceID()); selected.Add(Tempselected); } } selectedIDs = tempSelectedIds.ToArray(); EventManager.CallOnSelect(selectedIDs); state = InputState.normal; multiSelectArea.SetActive(false); } } else if (state == InputState.normal){ if(TilePosN!= null){ Vector2 TilePos = (Vector2)TilePosN; //print ("tile: " + TilePos + "\n"); //multi select if (Input.GetMouseButtonDown(0) && (Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.JoystickButton2))) { StartMultiSelect(); startTilePos = new VecInt((int)TilePos.x,(int)TilePos.y); Debug.Log("[Main] Multiselect: "+startTilePos);//+selectedIDs.Length); } else if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.JoystickButton4)) { //single select selected.Clear(); //MapObject Tempselected = LevelData.mapObjects [(int)TilePos.x, (int)TilePos.y]; MapObject Tempselected = LevelData.GetMapObjects((int)TilePos.x, (int)TilePos.y); if(Tempselected != null){ selected.Add(Tempselected); selectedIDs = new int[]{selected[0].gameObject.GetInstanceID()}; Debug.Log("[Main] selected: "+selectedIDs.Length); }else if(releasingPreBuildImage){ StartCoroutine(PreBuildImageFollowCursor()); }else{ selectedIDs = new int[]{}; Debug.Log("[Main] not selected: "+selectedIDs.Length); } EventManager.CallOnSelect(selectedIDs); #if UNITY_PSM || UNITY_ANDROID } else if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.JoystickButton4)) { if (selectedIDs.Length > 0) { FindNewPath(); } } #else }else if(Input.GetMouseButtonDown(1)){ if(selectedIDs.Length > 0){ FindNewPath(); } } #endif } }
private void StartMove() { if (pathProgress < currentPath.Length - 1) { if (checkNextPosFree(currentPath[pathProgress + 1], oldPos)) { turnUnit = rotationHandler.CheckRotation(oldPos, currentPath[pathProgress + 1], 0.125f); oldPos = currentPath[pathProgress + 1]; if (turnUnit) { creator.StartCoroutine(RotateDelay()); } else { creator.StartCoroutine(UpdateMove()); } } } }
public static void gaussj(MatDoub a, MatDoub b) { // Linear equation solution by Gauss-Jordan elimination, // equation (2.1.1) above. The input matrix is a[0..n-1][0..n-1]. // b[0..n-1][0..m-1] is input containing the m right-hand side vectors. // On output, a is replaced by its matrix inverse, and b is replaced by // the corresponding set of solution vectors. int i, icol = 0, irow = 0, j, k, l, ll, n = a.nrows(), m = a.ncols(); double big, dum, pivinv; VecInt indxc = new VecInt(n); VecInt indxr = new VecInt(n); VecInt ipiv = new VecInt(n); // These integer arrays are used // for bookkeeping on the pivoting. for (j = 0; j < n; j++) { ipiv[j] = 0; } for (i = 0; i < n; i++) // This is the main loop over the columns to // be { big = 0.0; // reduced. for (j = 0; j < n; j++) { // This is the outer loop of the search for a pivot over the entire matrix! if (ipiv[j] != 1) // element. { for (k = 0; k < n; k++) { if (ipiv[k] == 0) { if (Math.Abs(a[j][k]) >= big) { big = Math.Abs(a[j][k]); irow = j; icol = k; } } } } } ++(ipiv[icol]); // We now have the pivot element, so we interchange rows, if needed, // to put the pivot element on the diagonal. The columns are not // physically interchanged, only relabeled: indxc[i], the column of the .iC1/th // pivot element, is the .iC1/th column that is reduced, while indxr[i] is // the row in which that pivot element was originally located. // If indxr[i] indxc[i], there is an implied column interchange. // With this form of bookkeeping, the solution bs will end up in the // correct order, and the inverse matrix will be scrambled by columns. if (irow != icol) { for (l = 0; l < n; l++) { NR.SWAP(a, irow, l, icol, l); } for (l = 0; l < m; l++) { NR.SWAP(b, irow, l, icol, l); } } indxr[i] = irow; // We are now ready to divide the pivot row by the // pivot element, located at irow and icol. indxc[i] = icol; if (a[icol][icol] == 0.0) { throw new Exception("gaussj: Singular Matrix"); } pivinv = 1.0 / a[icol][icol]; a[icol][icol] = 1.0; for (l = 0; l < n; l++) { a[icol][l] *= pivinv; } for (l = 0; l < m; l++) { b[icol][l] *= pivinv; } for (ll = 0; ll < n; ll++) { // Next, we reduce the rows... if (ll != icol) { // ...except for the pivot one, of course. dum = a[ll][icol]; a[ll][icol] = 0.0; for (l = 0; l < n; l++) { a[ll][l] -= a[icol][l] * dum; } for (l = 0; l < m; l++) { b[ll][l] -= b[icol][l] * dum; } } } } // This is the end of the main loop over columns of the reduction. It // only remains to unscramble the solution in view of the column // interchanges. We do this by interchanging pairs of columns in the // reverse order that the permutation was built up. for (l = n - 1; l >= 0; l--) { if (indxr[l] != indxc[l]) { for (k = 0; k < n; k++) { NR.SWAP(a, k, indxr[l], k, indxc[l]); } } } }
private void Awake() { pos = new VecInt(0,0); size = new VecInt(0,0); }
public void AddSvgStart(VecInt width, VecInt height) { state.Output.AppendLine(FormattableString.Invariant( $"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{width.Value}\" height=\"{height.Value}\">" )); }