void IntersectionObjectWithSide(Shuriken shur) { Segment[] polySegments; Vector3 intersection = new Vector3(); Vector3 inter = new Vector3(); polySegments = poly.GetSegments(); float distance = polySegments[0].GetLenght(); int index = 0; for (int i = 0; i < polySegments.Length; i++) { intersection = MathG.IntersectionTwoSegments(shur.GetSegment(), polySegments[i]); if (intersection != new Vector3() && shur.GetLastSegment() != polySegments[i]) { if (polySegments[i].GetLenght() <= distance) { distance = polySegments[i].GetLenght(); index = i; inter = intersection; } } } if (inter != new Vector3()) { shur.SetIntersection(inter); shur.SetLastSegment(polySegments[index]); Instantiate(MathG.CreateSphere(inter)); } }
public override bool Shoot() { if (!canShoot) { return(false); } Vector3 pos = gameObject.transform.position; GameObject obj; float newAngleStart = transform.eulerAngles.y + angleStart; float newAngleEnd = transform.eulerAngles.y + angleEnd; for (int i = 0; i < numberBulletsPerBurst; ++i) { if ((Random.RandomRange(0f, 1f) > bulletTwoChance) && randomBullet) { obj = BulletManager2.GetNextBullet(); } else { obj = bulletManager.GetNextBullet(); } if (obj != null) { Vector3 velocity = MathG.DegreeToVector2D(((newAngleEnd - newAngleStart) / numberBulletsPerBurst * i) + newAngleStart, 1); obj.GetComponent <Bullet>().Reset(pos, new Vector3(velocity.x, 0, velocity.y)); } } timer = 0; canShoot = false; return(true); }
public override bool Shoot() { if (!canShoot) { return(false); } if (bulletManager != null) { GameObject bull = bulletManager.GetNextBullet(); if (shootObject != null) { ShootPosition = shootObject.transform.position; } if (bull != null) { Vector3 velocity = MathG.NormalVector(_shootPosition - transform.position); bull.GetComponent <Bullet>().Reset(gameObject.transform.position, velocity); timer = 0; canShoot = false; return(true); } } return(false); }
private void CheckPhotos() { //store players location for easy access in distance calculations var playerLocation = new MapLocation(gpsLocationService.Longitude, gpsLocationService.Latitude); //get the current Epoch time in seconds var now = Epoch.Now; foreach (Photo p in photos) { var d = MathG.Distance(p.location, playerLocation); if (MathG.Distance(p.location, playerLocation) < photoSeeDistance) { p.lastSeenTimestamp = now; if (p.gameObject == null) { print("Monster seen, distance " + d + " started at " + p.spawnTimestamp); SpawnPhoto(p); } else { p.gameObject.SetActive(true); //make sure the monster is visible } continue; } //hide monsters that can't be seen if (p.gameObject != null) { p.gameObject.SetActive(false); } } }
private Vector3 DetectIntersection(Segment segment, Vector3[] vertices) { //This function takes a segment of the space and goes through the Array of Vertex //creating segments and calculates the intersection Vector3 intersection = new Vector3(); Segment side = new Segment(); int index1 = 0, index2 = 0; for (int i = 0; i < vertices.Length; i++) { if (i == vertices.Length - 1)//Checks if it's the last vertex form the array, and assigns to the second index the initial vertex { index1 = i; index2 = 0; } else { index1 = i; index2 = i + 1; } side = new Segment(); side.index1 = index1; side.index2 = index2; //Check if the segmentPlayer is not the same as the sidePlayer if (((segment.index1 != side.index1 && segment.index2 != side.index1) || (segment.index1 != side.index2 && segment.index2 != side.index2))) { side.SetSegment(vertices[index1], vertices[index2]); // Debug.Log(vertices[i] + " "+ vertices[i + 1]); intersection = MathG.IntersectionTwoSegments(segment, side); if (intersection != new Vector3()) { if (!segment.isSelected) //If it's the first intersection it assigns the index of the vertices to index 1 and 2 { segment.SetIndex12(side.index1, side.index2); } else//If it's the second intersection it assigns the index of the vertices to index 3 and 4 { segment.SetIndex34(side.index1, side.index2); } // Debug.Log("Intersection "+intersection+" with segment " + segment.index1 + " and " + segment.index2); return(intersection); /* Visual demostration of the intersection points * GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); * go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); * go.transform.position = intersection; */ } } } return(new Vector3()); }
private void CheckMonsters() { if (Random.value > monsterSpawnRate) { var mlat = gpsLocationService.Latitude + Random.Range(-latitudeSpawnOffset, latitudeSpawnOffset); var mlon = gpsLocationService.Longitude + Random.Range(-longitudeSpawnOffset, longitudeSpawnOffset); var monster = new Monster { location = new MapLocation(mlon, mlat), spawnTimestamp = gpsLocationService.PlayerTimestamp }; monsters.Add(monster); } //store players location for easy access in distance calculations var playerLocation = new MapLocation(gpsLocationService.Longitude, gpsLocationService.Latitude); //get the current Epoch time in seconds var now = Epoch.Now; foreach (Monster m in monsters) { var d = MathG.Distance(m.location, playerLocation); if (MathG.Distance(m.location, playerLocation) < monsterSeeDistance) { m.lastSeenTimestamp = now; m.footstepRange = 4; if (m.gameObject == null) { print("Monster seen, distance " + d + " started at " + m.spawnTimestamp); SpawnMonster(m); } else { m.gameObject.SetActive(true); //make sure the monster is visible } continue; } if (MathG.Distance(m.location, playerLocation) < monsterHearDistance) { m.lastHeardTimestamp = now; var footsteps = CalculateFootstepRange(d); m.footstepRange = footsteps; print("Monster heard, footsteps " + footsteps); } //hide monsters that can't be seen if (m.gameObject != null) { m.gameObject.SetActive(false); } } }
bool IsInsidePolygon(Vector3 point, Polygon poly) { bool isInside = false; int[] indices = poly.GetIndices(); Vector3[] vertices = poly.GetVertices(); for (int i = 0; i < indices.Length && !isInside; i += 3) { //Debug.Log(vertices2D[indices[i]]+" "+indices[i]+" "+ vertices2D[indices[i+1]] + " " + indices[i+1] +" "+ vertices2D[indices[i+2]] + " " + indices[i+2]); isInside = MathG.Trilateration(vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]], point); } return(isInside); }
private void PingPong() { // Debug.Log(" direction: "+shurLine.dir); Vector3 normal = Vector3.Reflect(shurLine.dir, MathG.GetNormal(lastSegment.dir)); // Debug.Log("Normal "+normal); //Debug.Log("Segment dir " + lastSegment.dir + " normal: " + MathG.GetNormal(lastSegment.dir)); shurLine = new Segment(); // Debug.Log("Intersection " + intersection + " second inter " + (normal + intersection)); shurLine.SetSegment(intersection, normal * 20 + intersection); // Debug.Log(normal * 20 + intersection); // Debug.Log(shurLine.GetSegment()); passTime = 0; }
// Update is called once per frame void Update() { if (gpsLocationService != null && gpsLocationService.IsServiceStarted && gpsLocationService.PlayerTimestamp > lastTimestamp) { lastTimestamp = gpsLocationService.PlayerTimestamp; //test code var s = MathG.Distance(gpsLocationService.Longitude, gpsLocationService.Latitude, gpsLocationService.mapCenter.Longitude, gpsLocationService.mapCenter.Latitude); print("Player Distance from map tile center = " + s); //update the photos around the player CheckPhotos(); } }
public void SetSegment(Vector3 v1, Vector3 v2) { Vector3 aux = new Vector3(); dir = v2 - v1; dir.Normalize(); if (v1.x > v2.x) { aux = v2; v2 = v1; v1 = aux; } vertex1 = v1; vertex2 = v2; segment = MathG.GetLineEquation(v2 - v1, v1); }
private Vector3 GetCenter(Vector3[] vertices) // Get the center of a form given a number of vertices { float vertex1x = 0, vertex2x = 0; float vertex1y = 0, vertex2y = 0; float maxLenghtx = 0, maxLenghty = 0; for (int y = 0; y < vertices.Length; y++) { for (int x = 0; x < vertices.Length; x++) { //Debug.Log("v1x: " + vertices[y].x + " v2x: " + vertices[x].x); //Debug.Log(Mathf.Abs(vertices[y].x - vertices[x].x)); if (Mathf.Abs(vertices[y].x - vertices[x].x) > maxLenghtx) //Calculates the furthesh horizontal distance between two vertices { maxLenghtx = Mathf.Abs(vertices[y].x - vertices[x].x); vertex1x = vertices[y].x; vertex2x = vertices[x].x; } //Debug.Log("v1y: " + vertices[y].y + " v2y: " + vertices[x].y); // Debug.Log(Mathf.Abs(vertices[y].y - vertices[x].y)); if (Mathf.Abs(vertices[y].y - vertices[x].y) > maxLenghty)//Calculates the furthesh vertical distance between two vertices { maxLenghty = Mathf.Abs(vertices[y].y - vertices[x].y); vertex1y = vertices[y].y; vertex2y = vertices[x].y; } } } Vector3 middley, middlex; middley = MathG.GetMiddlePoint(new Vector3(vertex1y, 0), new Vector3(vertex2y, 0)); middlex = MathG.GetMiddlePoint(new Vector3(vertex1x, 0, 0), new Vector3(vertex2x, 0, 0)); Vector3 center = middlex + middley; //The center vertex is the result of the Middle Point of the four vertices return(center); }
private void OnPlayerMouseDown() { if (Input.GetMouseButtonUp(0)) { trail.gameObject.SetActive(false); } if (Input.GetMouseButtonDown(0)) { segmentPlayer = new Segment(); isFinished = false; vertexButtonOne = GetMousePositionToWorld(); vertexButtonTwo = vertexButtonOne; trail.transform.position = vertexButtonOne; trail.gameObject.SetActive(true); } if (Input.GetMouseButton(0)) { vertexButtonTwo = GetMousePositionToWorld(); trail.transform.position = vertexButtonTwo - new Vector3(0, 0, +0.1f); if (!isFinished) { segmentPlayer.SetSegment(vertexButtonOne, vertexButtonTwo); Vector3 intersection = DetectIntersection(segmentPlayer, form.GetVertices()); if (intersection != new Vector3() && (Mathf.Infinity != segmentPlayer.Getfx(intersection.x) && Mathf.NegativeInfinity != segmentPlayer.Getfx(intersection.x))) { if (!segmentPlayer.isSelected) { vertexButtonOne = intersection; segmentPlayer.SetVertexOne(intersection); } else { segmentPlayer.SetSegment(segmentPlayer.vertex1, segmentPlayer.vertex2); segmentPlayer.SetVertexTwo(intersection); } // Debug.Log(intersection + " with segments " + segmentPlayer.index1 + " and " + segmentPlayer.index2); // This is the representation of the player segment, use of Debug purpose // Debug.Log("The player segment has touched the segment at " + intersection + " of the " + segmentPlayer.index1 + " " + segmentPlayer.index2 + " segment"); segmentPlayer.isSelected = !segmentPlayer.isSelected; //Mark this segment as selected to exit the loop if (!segmentPlayer.isSelected) { /* * This draw the line of the segment cut. Use for debug purpose * for (float x = -3; x < 3; x += 0.1f) * { * if (segmentPlayer.Getfx(x) != -Mathf.Infinity && segmentPlayer.Getfx(x) != Mathf.Infinity) * { * * * GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); * go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); * * go.transform.position = new Vector3(x, segmentPlayer.Getfx(x), 0); * * } * } */ // Debug.Log("We have cut the segment " + segmentPlayer.index1 + segmentPlayer.index2 + " and the segment " + segmentPlayer.index3 + segmentPlayer.index4); if (IsInsidePolygon(MathG.GetMiddlePoint(segmentPlayer.vec1, segmentPlayer.vec2), form)) //Checks if the segment cuts the figure from inside or outside { List <Vector3[]> v = new List <Vector3[]>(); v = RecalculateVerticesWithSegment(segmentPlayer, form.GetVertices()); Polygon poly1 = new Polygon(v[0]); Polygon poly2 = new Polygon(v[1]); GenerateFallingObject(poly1, poly2); } isFinished = true; } } } } }
private List <Vector3[]> RecalculateVerticesWithSegment(Segment segment, Vector3[] vertices) { int longitud = 0; List <Vector3[]> vertArray = new List <Vector3[]>(); Vector3[] array1 = new Vector3[0], array2 = new Vector3[0]; Vector3 dir = segment.dir; // Debug.Log(dir + " v1: " + segment.vertex1 + " v2: " + segment.vertex2); // Debug.Log("Vert1: " + segment.index1 + segment.index2 + " Vert2: " + segment.index3 + segment.index4); if (MathG.PointNotContainedInLine(segment.GetSegment(), vertices[0]) >= 0) //Check if the vertex[0] is at the left or at the right of the segment { if (dir.x >= 0) //Checks if the director vector is positive or negative { for (int i = segment.index2; i <= segment.index3; i++, longitud++) { } } else { for (int i = segment.index1; i >= segment.index4; i--, longitud++) { } } if (dir.x >= 0) { // Debug.Log(" Pendiente positiva, 0 derecha, vector positivo: "); array1 = new Vector3[longitud + 2]; array1[0] = segment.vec1; array1[array1.Length - 1] = segment.vec2; for (int i = segment.index2, j = 1; j < (array1.Length - 1); i++, j++) { array1[j] = vertices[i]; } array2 = new Vector3[(vertices.Length - longitud) + 2]; array2[0] = segment.vec2; array2[array2.Length - 1] = segment.vec1; int h = 1; for (int i = segment.index4; i < vertices.Length;) { if (i == 0) { i = vertices.Length; } else { array2[h] = vertices[i]; i++; h++; } } for (int i = 0; i <= segment.index1;) { array2[h] = vertices[i]; i++; h++; } } else { //Debug.Log(" Pendiente positiva, 0 derecha, vector negativo: "); array1 = new Vector3[longitud + 2]; array1[0] = segment.vec2; array1[array1.Length - 1] = segment.vec1; for (int i = segment.index4, j = 1; j < (array1.Length - 1); i++, j++) { array1[j] = vertices[i]; } array2 = new Vector3[(vertices.Length - longitud) + 2]; array2[0] = segment.vec1; array2[array2.Length - 1] = segment.vec2; int h = 1; for (int i = segment.index2; i < vertices.Length;) { if (i == 0) { i = vertices.Length; } else { array2[h] = vertices[i]; i++; h++; } } for (int i = 0; i <= segment.index3;) { array2[h] = vertices[i]; i++; h++; } } } else { if (dir.x < 0) { for (int i = segment.index2; i <= segment.index3; i++, longitud++) { } } else { for (int i = segment.index1; i >= segment.index4; i--, longitud++) { } } if (dir.x >= 0) { // Debug.Log(" Pendiente Positiva, 0 izquierda, vector positivo: "); array1 = new Vector3[longitud + 2]; array1[0] = segment.vec2; array1[array1.Length - 1] = segment.vec1; for (int i = segment.index4, j = 1; j < (array1.Length - 1); i++, j++) { array1[j] = vertices[i]; } array2 = new Vector3[(vertices.Length - longitud) + 2]; array2[0] = segment.vec1; array2[array2.Length - 1] = segment.vec2; int h = 1; for (int i = segment.index2; i < vertices.Length;) { if (i == 0) { i = vertices.Length; } else { array2[h] = vertices[i]; i++; h++; } } for (int i = 0; i <= segment.index3;) { array2[h] = vertices[i]; i++; h++; } } else { //Debug.Log(" Pendiente Positiva, 0 izquierda, vector negativo: "); array1 = new Vector3[longitud + 2]; array1[0] = segment.vec1; array1[array1.Length - 1] = segment.vec2; for (int i = segment.index2, j = 1; j < (array1.Length - 1); i++, j++) { array1[j] = vertices[i]; } array2 = new Vector3[(vertices.Length - longitud) + 2]; array2[0] = segment.vec2; array2[array2.Length - 1] = segment.vec1; int h = 1; for (int i = segment.index4; i < vertices.Length;) { if (i == 0) { i = vertices.Length; } else { array2[h] = vertices[i]; i++; h++; } } for (int i = 0; i <= segment.index1;) { array2[h] = vertices[i]; i++; h++; } } } vertArray.Add(array1); vertArray.Add(array2); //Debug.Log("Vertices: " + longitud); return(vertArray); }
void Update() { if (myStats.PV.IsMine) { if (myStats.isGame) { if (myStats.isKind != Kind.Ghost) { GhostUI.instance.HideScreen(); HunterUI.instance.ShowScreen(); Fire(); Reload(); ammoText.text = myStats.myGun.curAmmo + " / " + myStats.myGun.maxAmmo; if (Input.GetKeyDown(KeyCode.Q) && !Main.instance.LightBroken) { if (myStats.myLight.activeSelf) { myStats.myLight.SetActive(false); } else { myStats.myLight.SetActive(true); } } else if (Main.instance.LightBroken) { myStats.myLight.SetActive(false); } gameObject.tag = "Hunter"; goalText.text = "유령을 찾아서 처치하세요 !"; gunText.text = myStats.myGun.name; } else { HunterUI.instance.HideScreen(); GhostUI.instance.ShowScreen(); ggoalText.text = Main.instance.goalCost.ToString() + " / " + (Main.instance.userList.Count - 1).ToString(); hpText.text = "+" + myStats.curHP; hpImage.fillAmount = (myStats.curHP - 0) / (myStats.maxHP - 0); myStats.Rigid.useGravity = false; gameObject.tag = "Ghost"; goalText.text = "?를 전부 찾으세요 !"; if (Input.GetKeyDown(KeyCode.E) && !Main.instance.LightBroken) { Main.instance.PV.RPC("LightB", RpcTarget.All); } if (Input.GetKey(KeyCode.Q) && myStats.isInt) { myStats.getTime += Time.deltaTime; if (myStats.getTime >= 2f) { myStats.isInt = false; myStats.getTime = 0f; //GameObject.FindGameObjectWithTag("Interaction").GetComponent<Interaction>().InteractionObj(); MathG.FindNearTarget("Interaction", gameObject).GetComponent <Interaction>().InteractionObj(); } } else if (Input.GetKeyUp(KeyCode.Q) && myStats.isInt) { myStats.getTime = 0f; } if (interactionText != null) { if (myStats.isInt) { interactionText.gameObject.SetActive(true); } else { interactionText.gameObject.SetActive(false); } } } } if (myStats.curHP <= 0) { myStats.isMove = false; Main.instance.PV.RPC("SetWinner", RpcTarget.AllBuffered, Main.State.HunterWin); Main.instance.winnerObj.SetActive(true); DeadGhost(); } if (myStats.isKind == Kind.Ghost && Main.instance.gameState == Main.State.GhostWin) { Main.instance.winnerObj.SetActive(true); } } else { if (myStats.isKind == Kind.Ghost) { myStats.Rigid.useGravity = false; gameObject.tag = "Ghost"; myStats.myCharacter.SetActive(false); myStats.myCharacter = transform.GetChild(curKind).gameObject; myStats.myCharacter.SetActive(true); if (curKind != 4) { myStats.animtor = myStats.myCharacter.GetComponent <Animator>(); } } else { gameObject.tag = "Hunter"; if (myStats.myGun.isFire) { myStats.myEffect.SetActive(true); } else { myStats.myEffect.SetActive(false); } if (myStats.animtor != null) { myStats.animtor.SetBool("isMove", curMove); myStats.animtor.SetBool("isJump", curJump); myStats.animtor.SetBool("isFire", curFire); myStats.animtor.SetBool("isReload", curReload); } if (myStats.setChar) { myStats.myCharacter.SetActive(false); myStats.myCharacter = transform.GetChild(curKind).gameObject; myStats.myCharacter.SetActive(true); if (curKind != 4) { myStats.animtor = myStats.myCharacter.GetComponent <Animator>(); } myStats.setChar = false; } } } }
void Movement() { //print(charController.isGrounded); velocity.y += -gravity; if (bForward) { SetGFXRotation(); Vector2 norm = MathG.DegreeToVector2D(camPoint.transform.eulerAngles.y, 1); MoveByVelocity(norm); } else if (bBack) { SetGFXRotation(); Vector2 norm = MathG.DegreeToVector2D(camPoint.transform.eulerAngles.y - 180, 1); MoveByVelocity(norm); } else if (bRight) { SetGFXRotation(); Vector2 norm = MathG.DegreeToVector2D(camPoint.transform.eulerAngles.y + 90, 1); MoveByVelocity(norm); } else if (bLeft) { SetGFXRotation(); Vector2 norm = MathG.DegreeToVector2D(camPoint.transform.eulerAngles.y - 90, 1); MoveByVelocity(norm); } else { velocity = new Vector3(0, velocity.y, 0); } bJump = Input.GetKey(playerData.jump); if (bJump && charController.isGrounded) { velocity.y = playerData.jumpForce; } bool bSprint = Input.GetKey(playerData.sprint); if (bSprint) { Vector3 newVel = new Vector3(velocity.x, 0, velocity.z); newVel *= playerData.SprintMultiplier; newVel.y = velocity.y; velocity = newVel; } charController.Move(velocity * Time.fixedDeltaTime); if (charController.isGrounded) { velocity.y = 0; } if (velocity.x != 0 && velocity.z != 0) { animator.SetBool("IsRunning", true); } else { animator.SetBool("IsRunning", false); } }