public static void Flurry(int splashDamage, Character target) { // Flurry 효과는 몬스터의 무기에 적용할 수 없음 (플레이어가 한 명이므로) Vector3 pos = VectorUtility.PositionToInt(target.GetComponent <Transform>().position); Vector3[] direction = new Vector3[4] { new Vector3(1f, 0f, 0f), new Vector3(-1f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(0f, -1f, 0f) }; foreach (Vector3 d in direction) { GameObject g = Instantiate(GameManager.gm.player.GetComponent <PlayerMover>().distanceSprite, new Vector3(pos.x + d.x, pos.y + d.y, -0.25f), Quaternion.identity); g.GetComponent <SpriteRenderer>().color = new Color(0f, 0.8f, 0.8f, 0.8f); g.GetComponent <DistanceSprite>().Disappear(60); Entity e = GameManager.gm.map.GetEntityOnTile(pos + d); if (e != null && e.GetType().Equals(typeof(Character)) && ((Character)e).type == Character.Type.Enemy) { // 진행 방향에 플레이어가 있을 경우 Character enemy = (Character)e; enemy.gustDamage += splashDamage; enemy.gustDirection = d; } } }
/// <summary> /// 이동하지 않고 상하좌우로 붙어 있는 플레이어를 공격합니다. /// 공격에 성공하면 턴을 넘기고 true를 반환합니다. /// 플레이어가 주변에 없으면 턴을 넘기지 않고 false를 반환합니다. /// </summary> /// <returns></returns> private bool AttackWithoutMove() { bool isAttacked = false; List <Vector3> melee = new List <Vector3>(); for (int i = 0; i <= c.EquippedWeapon.Range; i++) { melee.Add(new Vector3(-1f * i, 0f, 0f)); melee.Add(new Vector3(1f * i, 0f, 0f)); melee.Add(new Vector3(0f, -1f * i, 0f)); melee.Add(new Vector3(0f, 1f * i, 0f)); } foreach (Vector3 v in melee) { Vector3 destination = GetCurrentPosition() + v; if (gm.map.GetEntityOnTile(destination) != null && gm.map.GetEntityOnTile(destination).GetType().Equals(typeof(Character)) && ((Character)gm.map.GetEntityOnTile(destination)).type == Character.Type.Player) { if (destination.x < GetCurrentPosition().x) { GetComponent <SpriteRenderer>().flipX = false; } else if (destination.x > GetCurrentPosition().x) { GetComponent <SpriteRenderer>().flipX = true; } Attack(VectorUtility.PositionToInt((destination - GetCurrentPosition()).normalized), false); isAttacked = true; break; } } return(isAttacked); }
public void Seek(Vector2 target) { Vector2 desired = target - (Vector2)transform.position; desired = desired.normalized; desired = desired * maxSpeed; Vector2 steer = desired - rb.velocity; VectorUtility.Limit(ref steer, maxForce); lr.SetPosition(0, transform.position); Vector3 secondPosition = rb.velocity.normalized; lr.SetPosition(1, transform.position + secondPosition * avoidanceDistance); lr.SetColors(Color.blue, Color.blue); if (!CanAvoid()) { rb.velocity += steer * Time.fixedDeltaTime; //rb.AddForce(steer * Time.fixedDeltaTime); } else { Avoid(); } transform.up = rb.velocity; }
public void XZTest() { Vector3 v1 = new Vector3(3, 4, 5); Vector2 expected = new Vector2(3, 5); Assert.AreEqual(expected, VectorUtility.XZ(v1)); }
public void HeightVector2dTest() { Vector2 v1 = VectorUtility.HeightVector2d(3); Vector2 v2 = new Vector2(0, 3); Assert.AreEqual(v2, v1); }
private Vector3 GetWindVector() { var xyRandomRot = Random.Range(0f, this.WindVectorRandomRadius) * MiscUtils.RandomSign(); var windUnitVect = VectorUtility.GetPointingVector3(this.transform, this.TargetBody.transform).RotateXZPlanar(xyRandomRot) * this.WindSpeedImpulse; return(windUnitVect); }
public void HeightVector3dTest() { Vector3 v1 = VectorUtility.HeightVector3d(3); Vector3 v2 = new Vector3(0, 3, 0); Assert.AreEqual(v2, v1); }
public void Seek(Vector2 target, float maxForce) { Vector2 desired = target - (Vector2)transform.position; desired = desired.normalized; desired = desired * maxSpeed; Vector2 steer = desired - rb.velocity; VectorUtility.Limit(ref steer, minForce, maxForce); lr.SetPosition(0, transform.position); Vector3 secondPosition = rb.velocity.normalized; lr.SetPosition(1, transform.position + secondPosition * avoidanceDistance); lr.SetColors(Color.blue, Color.blue); Debug.DrawRay(transform.position, transform.up * avoidanceDistance, Color.green); rb.velocity += steer * Time.fixedDeltaTime; //VectorUtility.Limit(ref rb.velocity, minForce, maxForce); //if (!CanAvoid() || avoiding) //{ // rb.velocity = steer * Time.fixedDeltaTime; //} //else if (!avoiding) //{ // StartCoroutine(avoid(1.25f)); //} transform.up = rb.velocity; }
private IEnumerator spawnAcc() { //(transform.position + " " + planet.getRadPln()); Vector3 pos = VectorUtility.getRandPoint(transform.position, planet.getRadPln()); GameObject spawned = Instantiate(GameObject.Find("BmainBase"), pos, Quaternion.identity); spawned.transform.parent = gameObject.transform; Rigidbody rgb = spawned.GetComponent <Rigidbody>(); spawned.transform.localScale = new Vector3(0.5f / transform.localScale.x, 0.5f / transform.localScale.y, 0.5f / transform.localScale.z); spawned.transform.LookAt(transform.position); spawned.transform.Rotate(new Vector3(-90f, 0f, 0f), Space.Self); rgb.constraints = RigidbodyConstraints.FreezeAll; float timer = 0.0f; while (timer < timeBetween) { timer += Time.deltaTime; yield return(null); } if (planet.isBlocked()) { Destroy(spawned); } else if (spawned != null) { Building building = spawned.AddComponent <Building>(); planet.addBuilding(building); planet.CratersAdded++; } }
protected virtual void InSightState() { pf.FollowPath(); timer -= Time.deltaTime; if (timer < 0.0) { timer = updatePathTime; entity = (pf.CurrentPath() != Pathfollowing.PathState.CIRCLE) ? vc.GetObjectInVisionCone(vc.locateRadius) : vc.GetObjectInVisionCone(0f); if (entity == null) { currentState = SharkState.LOSS_SIGHT; LossSightStateStart(); } else { float distance = VectorUtility.distance(transform.position, entity.transform.position); if (distance < circlePathDistance && (pf.CurrentPath() != Pathfollowing.PathState.CIRCLE)) { pf.GenerateCirclePath(entity.transform.position); } else if (distance > circlePathDistance) { pf.GenerateHeadToPath(entity.transform.position); } } } }
public static void Reflect(int percentDamage, Character target) { if (target.trueOldHealth > target.currentHealth) { float damage = (target.trueOldHealth - target.currentHealth) * (percentDamage / 100f); Vector3 pos = VectorUtility.PositionToInt(target.GetComponent <Transform>().position); Vector3[] direction = new Vector3[4] { new Vector3(1f, 0f, 0f), new Vector3(-1f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(0f, -1f, 0f) }; foreach (Vector3 d in direction) { GameObject g = Instantiate(GameManager.gm.player.GetComponent <PlayerMover>().distanceSprite, new Vector3(pos.x + d.x, pos.y + d.y, -0.25f), Quaternion.identity); g.GetComponent <SpriteRenderer>().color = new Color(0.7f, 0.7f, 0.7f, 0.8f); g.GetComponent <DistanceSprite>().Disappear(60); Entity e = GameManager.gm.map.GetEntityOnTile(pos + d); if (e != null && e.GetType().Equals(typeof(Character)) && ((Character)e).type != target.type) { // 진행 방향에 적이 있을 경우 Character opponent = (Character)e; opponent.hasReflected = true; opponent.reflectDamage += damage; opponent.reflectDirection = d; } } } }
private void Update() { //foreach(Bullet bullet in toMove) { for (int i = 0; i < toMove.Count; i++) { Bullet bullet = toMove[i]; if (bullet.to == null) { toMove.Remove(bullet); Destroy(bullet.obj); i--; continue; } Vector3 to = bullet.to.transform.position; GameObject obj = bullet.obj; obj.transform.position = Vector3.MoveTowards(obj.transform.position, to, bulletSpeed); obj.transform.LookAt(to); if (VectorUtility.vecLength(obj.transform.position, to) < 2) { toMove.Remove(bullet); Destroy(obj); check(bullet.to); i--; } } }
public static void GetFrustumPlanes(ref OrthoCam ortho, float4 *planes) { planes[0] = VectorUtility.GetPlane(ortho.up, ortho.position + ortho.up * ortho.size); planes[1] = VectorUtility.GetPlane(-ortho.up, ortho.position - ortho.up * ortho.size); planes[2] = VectorUtility.GetPlane(ortho.right, ortho.position + ortho.right * ortho.size); planes[3] = VectorUtility.GetPlane(-ortho.right, ortho.position - ortho.right * ortho.size); planes[4] = VectorUtility.GetPlane(ortho.forward, ortho.position + ortho.forward * ortho.farClipPlane); planes[5] = VectorUtility.GetPlane(-ortho.forward, ortho.position + ortho.forward * ortho.nearClipPlane); }
/// <summary> /// Drive mechanism by velocity. /// </summary> /// <param name="velocity">Velocity of drive.</param> /// <param name="mode">Mode of drive.</param> /// <returns>Drive is unrestricted?</returns> protected override bool OnDrive(float velocity = 0, DriveMode mode = DriveMode.Ignore) { joint.position = transform.position; var angle = -VectorUtility.ProjectAngle(joint.forward, ZeroAxis, Axis); var euler = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, angle); transform.localRotation = Quaternion.Euler(euler); return(true); }
Vector2 GetNormalPoint(Vector2 predictedLoc, Vector2 pointA, Vector2 pointB) { Vector2 ap = predictedLoc - pointA; Vector2 ab = pointB - pointA; ab = ab.normalized; ab = ab * (VectorUtility.DotProduct(ap, ab)); Vector2 normalPoint = pointA + ab; return(normalPoint); }
public static void GetFrustumPlanes(ref PerspCam perspCam, float4 *planes) { float3 *corners = stackalloc float3[4]; GetFrustumCorner(ref perspCam, perspCam.farClipPlane, corners); planes[0] = VectorUtility.GetPlane(corners[1], corners[0], perspCam.position); planes[1] = VectorUtility.GetPlane(corners[2], corners[3], perspCam.position); planes[2] = VectorUtility.GetPlane(corners[0], corners[2], perspCam.position); planes[3] = VectorUtility.GetPlane(corners[3], corners[1], perspCam.position); planes[4] = VectorUtility.GetPlane(perspCam.forward, perspCam.position + perspCam.forward * perspCam.farClipPlane); planes[5] = VectorUtility.GetPlane(-perspCam.forward, perspCam.position + perspCam.forward * perspCam.nearClipPlane); }
private Vector3 Move(Vector3 destination) { if (VectorUtility.IsSamePosition(destination, GetCurrentPosition())) { // 제자리에 머물러 있는 경우 움직이는 애니메이션 없이 턴 넘김 // 제자리에서 인접한 네 칸 안에 플레이어가 있으면 공격함 if (!AttackWithoutMove()) { isMoved = true; } return(destination); } else if (gm.map.CanMoveToTile(destination)) { // TODO 한 턴에 2칸 이상을 이동하는 경우, 지나는 모든 타일에 대한 고려가 필요함 if (destination.x < GetCurrentPosition().x) { GetComponent <SpriteRenderer>().flipX = false; } else if (destination.x > GetCurrentPosition().x) { GetComponent <SpriteRenderer>().flipX = true; } StartCoroutine(MoveAnimation(destination)); return(destination); } else if (gm.map.GetTypeOfTile(destination) == 0 && gm.map.GetEntityOnTile(destination) != null && gm.map.GetEntityOnTile(destination).GetType().Equals(typeof(Character)) && ((Character)gm.map.GetEntityOnTile(destination)).type == Character.Type.Player) { // 진행 방향으로 한 칸 앞에 플레이어가 있는 경우 if (destination.x < GetCurrentPosition().x) { GetComponent <SpriteRenderer>().flipX = false; } else if (destination.x > GetCurrentPosition().x) { GetComponent <SpriteRenderer>().flipX = true; } Attack(VectorUtility.PositionToInt((destination - GetCurrentPosition()).normalized), false); // TODO 택시 거리 1칸이 보장되지 않음 return(GetCurrentPosition()); } else { // TODO 일단은 가려고 하는 곳이 갈 수 없는 지형이면 움직이지 않고 턴 넘김 // 제자리에서 인접한 네 칸 안에 플레이어가 있으면 공격함 if (!AttackWithoutMove()) { isMoved = true; } return(GetCurrentPosition()); } }
private void Update() { Vector2 position2D = VectorUtility.FromV3ToV2(transform.position); Vector2 direction = new Vector2(Input.GetAxis("Horizontal"), 0f); direction.Normalize(); Vector2 velocity = direction.normalized * speed * Time.deltaTime; rb.MovePosition(position2D + velocity); //transform.Translate(velocity, Space.Self); }
public bool InterpolateToPoint(Vector3 targetPoint, float moveScalar, float tolerance) { var moveVect = VectorUtility.GetPointingVector3(this.transform.position, targetPoint); var dist = Vector3.Distance(this.transform.position, targetPoint); if (dist <= tolerance) { return(true); } Move(moveVect * moveScalar); return(false); }
/// <summary> /// 点燃接近的可燃物 /// </summary> private void TryLightOnOther() { VectorUtility.GetColliderObjectAround(transform.position, lightDistence, ref sphereCasts, tag); for (int i = 0; i < sphereCasts.Count; i++) { Debug.Log(sphereCasts[i].name); Fire other = sphereCasts[i].GetComponent <Fire>(); if (other != null) { other.IsOn = true; } } }
public void TwoDimensional3dTest() { // test if no value given for y works Vector3 v1 = new Vector2(2, 3); Vector3 v2 = VectorUtility.twoDimensional3d(v1); Assert.AreEqual(0, v2.y); // test if setting y works Vector2 v3 = VectorUtility.twoDimensional3d(v1, 3); Assert.AreEqual(3, v3.y); }
private bool hasFreePoints(Vector3 center, float radius, Ship ship, float planetDist) { for (int i = 1; i <= 10; i++) { Vector3 randomPoint = VectorUtility.getRandPoint(center, radius); if (!isBlocked(randomPoint, ship, planetDist)) { point = randomPoint; return(true); } } return(false); }
public void CalculateUpdates(Random rng) { Vector2 variation = Vector2.Zero; foreach (SimpleLink link in Neighbors) { Tile other = (Tile)link.Target; variation += other.Wind; } variation /= Neighbors.Count; variation += VectorUtility.CreateVector(rng.NextDouble() * MathHelper.TwoPi, (float)(rng.NextDouble() * 0.5)); WindUpdate = PrevailingWind + variation; WindUpdate *= 0.5f; }
public void TestData2D_3_closest_point_is_edge() { var anchor = new Vector2D(1, 3); var a = new Vector2D(3, 2); var b = new Vector2D(5, 4); var h = VectorUtility.FindClosestPointOnSegment(anchor, a, b); var expectedX = a.X; var expectedY = a.Y; Assert.IsTrue(RoughlyEquals(h.X, expectedX, 0.01)); Assert.IsTrue(RoughlyEquals(h.Y, expectedY, 0.01)); }
public void TestData2D_2() { var anchor = new Vector2D(-1, 2); var a = new Vector2D(1, 3); var b = new Vector2D(-2, -2); var h = VectorUtility.FindClosestPointOnSegment(anchor, a, b); var expectedX = 0; var expectedY = 1.4; Assert.IsTrue(RoughlyEquals(h.X, expectedX, 0.1)); Assert.IsTrue(RoughlyEquals(h.Y, expectedY, 0.1)); }
/// <summary> /// 순찰 경로를 초기화하는 함수입니다. /// </summary> /// <param name="checkpoints">들러야 하는 지점의 목록(순서대로)</param> private void InitializeCheckpoints(List <Vector3> checkpoints) { headCheckpoints = new Stack <Vector3>(); passedCheckpoints = new Stack <Vector3>(); passedCheckpoints.Push(GetCurrentPosition()); if (checkpoints != null) { for (int i = checkpoints.Count - 1; i >= 0; i--) { headCheckpoints.Push(VectorUtility.PositionToInt(checkpoints[i])); } } }
/// <summary> /// Gets the vertexs for regular polygon. /// </summary> /// <returns>The vertexs for regular polygon.</returns> /// <param name="previousCenter">Previous regular polygon center.</param> /// <param name="previousFirstVertex">Previous regular polygon first vertex.</param> /// <param name="currentCenter">Current regular polygon center.</param> /// <param name="nextCenter">Next regular polygon center.</param> /// <param name="radius">Radius.</param> /// <param name="numberOfSide">Number of side.</param> static List <Vector3> GetVertexsForRegularPolygon(Vector3 previousCenter, Vector3 previousFirstVertex, Vector3 currentCenter, Vector3 nextCenter, float radius, int numberOfSide) { List <Vector3> listCurrentVertex = new List <Vector3>(); float angleDelta = -360f / numberOfSide; Vector3 currentNormal = Vector3.one; //当前正多边形的法向量 Vector3 firstVertexDir = Vector3.one; //当前正多边形的中心点指向第一个顶点的方向 //当previousCenter是Vector3.positiveInfinity时,表示currentCenter是第一个正多边形的中心点 //当nextCenter是Vector3.positiveInfinity时,表示currentCenter是最后一个正多边形的中心点 //每个正多边形的法向量为自己的中心点指向下一个正多边形的中心点 //最后一个正多边形的法向量和倒数第二个一样 if (nextCenter.Equals(Vector3.positiveInfinity)) { currentNormal = currentCenter - previousCenter; } else { currentNormal = nextCenter - currentCenter; } if (previousCenter.Equals(Vector3.positiveInfinity)) { /* * 当前正多边形是第一个时, * 用世界坐标的上方和右方旋转分别对应currentNormal和firstVertexDir,求得firstVertexDir */ Quaternion fromToRotation = Quaternion.FromToRotation(Vector3.up, currentNormal); firstVertexDir = fromToRotation * Vector3.right; } else { /* * 当前正多边形不是第一个正多边形时, * 由上一个正多边形的第一个顶点发出射线(方向和上一个正多边形的法向量一样) * 和当前正多边形相交,得到的点减去当前正多边形的中心点, * 就得到当前正多边形中心点指向第一个顶点的方向向量 */ Vector3 interacPoint = VectorUtility.GetInteracPointRay2Plane(previousFirstVertex, currentCenter - previousCenter, currentCenter, currentNormal); firstVertexDir = (interacPoint - currentCenter).normalized; } for (int i = 0; i < numberOfSide; i++) { Vector3 vertex = currentCenter + Quaternion.AngleAxis(angleDelta * i, currentNormal) * (firstVertexDir * radius); listCurrentVertex.Add(vertex); } listCurrentVertex.Add(listCurrentVertex[0]);//最后一个顶点和第一个顶点重合 return(listCurrentVertex); }
public void GetAngleTest() { // check for 0 angle Vector2 v1 = new Vector2(0, 2); Vector2 v2 = new Vector2(0, 5); Assert.AreEqual(0, VectorUtility.GetAngle(v1, v2)); // check for regular angle Vector2 v3 = new Vector2(1, 2); Vector2 v4 = new Vector2(3, 4); Assert.AreEqual(0.785398185f, VectorUtility.GetAngle(v3, v4)); }
public void TestData3D_1() { var anchor = new Vector3D(0, 0, 0); var a = new Vector3D(-1, 4, 2); var b = new Vector3D(2, -2, 3); var h = VectorUtility.FindClosestPointOnSegment(anchor, a, b); var expectedX = 0.63; var expectedY = 0.739; var expectedZ = 2.543; Assert.IsTrue(RoughlyEquals(h.X, expectedX, 0.001)); Assert.IsTrue(RoughlyEquals(h.Y, expectedY, 0.001)); Assert.IsTrue(RoughlyEquals(h.Z, expectedZ, 0.001)); }
private void FireParticle() { //Debug.Log("Fire!"); Vector2 velocityVector = VectorUtility.GetRandomAngledVector(this.MinAngle, this.MaxAngle) * UnityEngine.Random.Range(this.MinVelocity, this.MaxVelocity); GameObject g = GameObject.Instantiate(Resources.Load <GameObject>(this.ParticleName)); g.GetComponent <Rigidbody2D>().velocity = velocityVector; g.transform.position = this.gameObject.transform.position; if (this.ApplyScaleFromHost) { g.transform.localScale = this.transform.localScale; } // Set the TTL. All particles must support the TimeDeathHandler behavior g.GetComponent <TimedDeathHandler>().DeathDuration = this.ParticleTTL; }