void SetupLifecycle(Shape shape, Vector3 durations) { if (durations.x > 0f) { if (durations.y > 0f || durations.z > 0f) { shape.AddBehavior <LifecycleShapeBehavior>().Initialize( shape, durations.x, durations.y, durations.z ); } else { shape.AddBehavior <GrowingShapeBehavior>().Initialize( shape, durations.x ); } } else if (durations.y > 0f) { shape.AddBehavior <LifecycleShapeBehavior>().Initialize( shape, durations.x, durations.y, durations.z ); } else if (durations.z > 0f) { shape.AddBehavior <DyingShapeBehavior>().Initialize( shape, durations.z ); } }
void SetupLifecycle(Shape shape, Vector3 durations) { if (durations.x > 0f) //growing이 있고, { if (durations.y > 0f || durations.z > 0f) //adult와 dying 둘 중 하나라도 있으면 { shape.AddBehavior <LifecycleShapeBehavior>().Initialize( //1)모두 다 커버하는 lifecycle 추가 shape, durations.x, durations.y, durations.z ); } else //아니면 growing만 필요 { shape.AddBehavior <GrowingShapeBehavior>().Initialize( shape, durations.x ); } } else if (durations.y > 0f) { shape.AddBehavior <LifecycleShapeBehavior>().Initialize( shape, durations.x, durations.y, durations.z ); } else if (durations.z > 0f) { shape.AddBehavior <DyingShapeBehavior>().Initialize( shape, durations.z ); } }
public virtual void SpawnShape() { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); Transform t = shape.transform; t.localPosition = SpawnPoint; t.localRotation = Random.rotation; t.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange; SetupColor(shape); float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; if (angularSpeed != 0f) { var rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; } float speed = spawnConfig.speed.RandomValueInRange; if (speed != 0f) { var movement = shape.AddBehavior <MovementShapeBehavior>(); movement.Velocity = GetDirectionVector(spawnConfig.movementDirection, t) * speed; } SetupOscillation(shape); int satelliteCount = spawnConfig.satellite.amount.RandomValueInRange; for (int i = 0; i < satelliteCount; i++) { CreateSatelliteFor(shape); } }
public virtual void SpawnShapes() { //随机场景 int factoryIndex = Random.Range(0, spawnConfig.factories.Length); //生成prefab在指定的场景下 Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); shape.gameObject.layer = gameObject.layer; Transform t = shape.transform; t.localPosition = SpawnPoint; //随机旋转值 t.localRotation = Random.rotation; t.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange; SetupColor(shape); //改变颜色HSV //https://blog.csdn.net/zgjllf1011/article/details/79391241 //shape.SetColor(Random.ColorHSV( // hueMin: 0f, hueMax: 1f, // saturationMin: 0.5f, saturationMax: 1f, // valueMin: 0.25f, valueMax: 1f, // alphaMin: 1f, alphaMax: 1f //)); //添加旋转 float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; if (angularSpeed != 0f) { var rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; } //根据方向计算速度 添加速度 float speed = spawnConfig.speed.RandomValueInRange; if (speed != 0f) { var movment = shape.AddBehavior <MovementShapeBehavior>(); movment.Velocity = GetDirectionVector(spawnConfig.movementDirection, t) * speed; } Vector3 lifecycleConfigurations = spawnConfig.lifecycle.RandomDurations; int satelliteCount = spawnConfig.satellite.amount.RandomValueInRange; for (int i = 0; i < satelliteCount; i++) { CreatSatelliterFor(shape, spawnConfig.satellite.uniformLifecycles ? lifecycleConfigurations : spawnConfig.lifecycle.RandomDurations); } SetupOscillation(shape); SetupLifecycle(shape, lifecycleConfigurations); }
public virtual void SpawnShapes() { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); Transform t = shape.transform; t.localPosition = SpawnPoint; t.localRotation = Random.rotation; t.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange; SetupColor(shape); float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; if (angularSpeed != 0f) { var rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; } //50 = 1/time step of fixedUpdate float speed = spawnConfig.speed.RandomValueInRange; if (speed != 0f) { var movement = shape.AddBehavior <MovementShapeBehavior>(); movement.Velocity = GetDirectionVector(spawnConfig.movementDirection, t) * speed; } SetupOscillation(shape); Vector3 lifecycleDurations = spawnConfig.lifecycle.RandomDurations; int satelliteCount = spawnConfig.satellite.satelliteAmount.RandomValueInRange; for (int i = 0; i < satelliteCount; i++) { if (spawnConfig.satellite.uniformLifecycles) { CreateSatelliteFor(shape, lifecycleDurations); } else { Vector3 lifecycleDurationsSatellites = spawnConfig.lifecycle.RandomDurations; CreateSatelliteFor(shape, lifecycleDurationsSatellites); } } SetupLifeCycle(shape, lifecycleDurations); }
public virtual Shape SpawnShape() { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); Transform t = shape.transform; t.localPosition = SpawnPoint; t.localRotation = Random.rotation; t.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange; if (spawnConfig.uniformColor) { Color setColor = spawnConfig.color.RandomInRange; for (int i = 0; i < shape.ColorCount; i++) { shape.SetColor(setColor, i); } } else { for (int i = 0; i < shape.ColorCount; i++) { shape.SetColor(spawnConfig.color.RandomInRange, i); } } float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; if (angularSpeed != 0f) { var rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; } //50 = 1/time step of fixedUpdate float speed = spawnConfig.speed.RandomValueInRange; if (speed != 0f) { var movement = shape.AddBehavior <MovementShapeBehavior>(); movement.Velocity = GetDirectionVector(spawnConfig.movementDirection, t) * speed; } SetupOscillation(shape); return(shape); }
public virtual void SpawnShapes() { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); // Randomly selects a shape factory to spawn shapes from, then ramdomly selects a shape from said factory. Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); shape.gameObject.layer = gameObject.layer; // Changes shape's layer to match this gameobject's layer Transform t = shape.transform; // Updates transform data of shape to be at the desired spawn point, with a random rotation and scale (within a certain range) t.localPosition = SpawnPoint; t.localRotation = Random.rotation; t.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange; SetupColor(shape); float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; // Randomly assigns rotational speed value if (angularSpeed != 0f) // If rotational speed value is not zero, it means the shape will rotate, so an appropriate behaviour must be added { var rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; // Assigns speed value } float speed = spawnConfig.speed.RandomValueInRange; // Randomly assigns speed value if (angularSpeed != 0f) // If speed value is not zero, it means the shape will move, so an appropriate behaviour must be added { if (speed != 0f) { var movement = shape.AddBehavior <MovementShapeBehavior>(); movement.Velocity = GetDirectionVector(spawnConfig.movementDirection, t) * speed; // Assigns speed value } } SetupOscillation(shape); // Assigns oscillation values Vector3 lifecycleDurations = spawnConfig.lifecycle.RandomDurations; int satelliteCount = spawnConfig.satellite.amount.RandomValueInRange; for (int i = 0; i < satelliteCount; i++) { CreateSatelliteFor( shape, spawnConfig.satellite.uniformLifecycles ? lifecycleDurations : spawnConfig.lifecycle.RandomDurations ); } SetupLifecycle(shape, lifecycleDurations); }
public virtual Shape SpawnShape() { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); Transform t = shape.transform; // Random Position t.localPosition = SpawnPoint; // Random Rotation t.transform.localRotation = Random.rotation; // Random Scale t.transform.localScale = spawnConfig.speed.RandomValueInRange * Vector3.one * spawnConfig.scale.RandomValueInRange;; if (spawnConfig.uniformColor) { // Random Color shape.SetColor(spawnConfig.color.RandomInRange); } else { for (int i = 0; i < shape.ColorCount; i++) { shape.SetColor(spawnConfig.color.RandomInRange, i); } } // Constant Rotation float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; if (angularSpeed != 0f) { var rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; } float speed = spawnConfig.speed.RandomValueInRange; if (speed != 0f) { // Constant Movement and get Differents Movements var movement = shape.AddBehavior <MovementShapeBehavior>(); movement.Velocity = GetDirectionVector(spawnConfig.movementDirection, t) * speed; } SetupOscillation(shape); return(shape); }
//이제 shape의 spawn을 game에서 zone의 역할로 가져옴 public virtual void SpawnShape() { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); shape.gameObject.layer = gameObject.layer; //특정 레이어의 존에서 생성된 shape은 그 레이어를 가짐. 다른 레이어는 충돌검사 하지 않도록 editor에서 설정함 Transform t = shape.transform; t.localPosition = SpawnPoint; t.localRotation = Random.rotation; t.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange; SetupColor(shape); float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; if (angularSpeed != 0f) //필요한 경우에만 컴포넌트로 추가 { //var rotation = shape.gameObject.AddComponent<RotationShapeBehavior>(); var rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; } float speed = spawnConfig.speed.RandomValueInRange; if (speed != 0f) { var movement = shape.AddBehavior <MovementShapeBehavior>(); movement.Velocity = GetDirectionVector(spawnConfig.movementDirection, t) * speed; } SetupOscillation(shape); Vector3 lifecycleDuration = spawnConfig.lifecycle.RandomDurations; int satelliteCount = spawnConfig.satellite.amount.RandomValueInRange; for (int i = 0; i < satelliteCount; i++) { CreateSatelliteFor(shape, spawnConfig.satellite.uniformLifecycles ? lifecycleDuration : spawnConfig.lifecycle.RandomDurations); } SetupLifecycle(shape, lifecycleDuration); }
public void Initialize(Shape shape, float growingDuration, float adultDuration, float dyingDuration) { this.adultDuration = adultDuration; this.dyingDuration = dyingDuration; dyingAge = growingDuration + adultDuration; if (growingDuration > 0) { shape.AddBehavior <GrowingShapeBehavior>().Initialize(shape, growingDuration); } }
public virtual void SpawnShape() { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); shape.gameObject.layer = gameObject.layer; Transform trans = shape.transform; trans.localPosition = SpawnPoint; trans.localRotation = Random.rotation; trans.localScale = Vector3.one * spawnConfig.scale.RandomValueInRange; SetupColor(shape); float angularSpeed = spawnConfig.angularSpeed.RandomValueInRange; if (angularSpeed != 0f) { RotationShapeBehavior rotation = shape.AddBehavior <RotationShapeBehavior>(); rotation.AngularVelocity = Random.onUnitSphere * angularSpeed; } float speed = spawnConfig.speed.RandomValueInRange; if (speed != 0f) { MovementShapeBehavior movement = shape.AddBehavior <MovementShapeBehavior>(); movement.Velocity = GetDirectionVector(spawnConfig.movementDirection, trans) * speed; } SetupOscillation(shape); Vector3 lifecycleDurations = spawnConfig.lifecycle.RandomDurations; int satelliteCount = spawnConfig.satellite.amount.RandomValueInRange; for (int i = 0; i != satelliteCount; ++i) { CreateSatelliteFor(shape, spawnConfig.satellite.uniformLifecycles ? lifecycleDurations : spawnConfig.lifecycle.RandomDurations); } SetupLifecycle(shape, lifecycleDurations); }
//특정 shape에 딸려 생기는 위성 shape 생성 void CreateSatelliteFor(Shape focalShape, Vector3 lifecycleDuration) { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); shape.gameObject.layer = gameObject.layer; Transform t = shape.transform; t.localRotation = Random.rotation; t.localScale = focalShape.transform.localScale * spawnConfig.satellite.relativeScale.RandomValueInRange; t.localPosition = focalShape.transform.localPosition + Vector3.up; shape.AddBehavior <MovementShapeBehavior>().Velocity = Vector3.up; SetupColor(shape); //return을 해야 game에 가서 update loop를 돌게 되는데, 아직 없음. 확장 필요. Game 에 instance 생성 shape.AddBehavior <SatelliteShapeBehavior>().Initialize(shape, focalShape, spawnConfig.satellite.orbitRadius.RandomValueInRange, spawnConfig.satellite.orbitFrequency.RandomValueInRange); SetupLifecycle(shape, lifecycleDuration); }
private void CreateSatelliteFor(Shape focalShape) { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape satelliteShape = spawnConfig.factories[factoryIndex].GetRandom(); Transform satelliteTrans = satelliteShape.transform; satelliteTrans.localRotation = Random.rotation; satelliteTrans.localScale = focalShape.transform.localScale * spawnConfig.satellite.relativeScale.RandomValueInRange; SetupColor(satelliteShape); satelliteShape.AddBehavior <SatelliteShapeBehavior>().Initialize(satelliteShape, focalShape, spawnConfig.satellite.orbitRadius.RandomValueInRange, spawnConfig.satellite.oribitFrequency.RandomValueInRange); }
public override bool GameUpdate(Shape shape) { if (focalShape.IsValid) { float t = 2f * Mathf.PI * frequency * shape.Age; previousPosition = shape.transform.localPosition; shape.transform.localPosition = focalShape.Shape.transform.localPosition + cosOffset * Mathf.Cos(t) + sinOffset * Mathf.Sin(t); return(true); } shape.AddBehavior <MovementShapeBehavior>().Velocity = (shape.transform.localPosition - previousPosition) / Time.deltaTime; return(false); }
public override bool GameUpdate(Shape shape) { if (_focalShape.IsValid) { var t = 2f * Mathf.PI * _frequency * shape.Age; _previousPosition = shape.transform.localPosition; shape.transform.localPosition = _focalShape.Shape.transform.localPosition + _cosOffset * Mathf.Cos(t) + _sinOffset * Mathf.Sin(t); return(true); } shape.AddBehavior <MovementBehaviour>().Velocity = (shape.transform.localPosition - _previousPosition) / Time.deltaTime; return(false); }
public override bool GameUpdate(Shape shape) { //若当前的已达到规定的存活总时长 if (shape.Age >= dyingAge) { //死亡时间为0是,直接死亡 if (dyingDuration <= 0f) { shape.Die(); return(true); } if (!shape.IsMarkAsDying) { shape.AddBehavior <DyingShapeBehavior>().Initialize(shape, dyingDuration + dyingAge - shape.Age); } shape.AddBehavior <DyingShapeBehavior>().Initialize(shape, dyingDuration + dyingAge - shape.Age); return(false); } return(true); }
void SetupOscillation(Shape shape) { float amplitude = spawnConfig.oscillationAmplitude.RandomValueInRange; float frequency = spawnConfig.oscillationFrequency.RandomValueInRange; if (amplitude == 0f || frequency == 0f) { return; } var oscillation = shape.AddBehavior <OscillationShapeBehavior>(); oscillation.Offset = GetDirectionVector(spawnConfig.oscillationDirection, shape.transform) * amplitude; oscillation.Frequency = frequency; }
void SetupLifeCycle(Shape shape, Vector3 durations) { if (durations.x > 0f) { //if grow, adult, die all configured if (durations.y > 0f || durations.z > 0f) { shape.AddBehavior <LifecycleShapeBehavior>().Initialize( shape, durations.x, durations.y, durations.z ); } else //if grow configured { shape.AddBehavior <GrowingShapeBehavior>().Initialize( shape, durations.x ); } } else if (durations.y > 0f) //if grow <= 0, adult configured { shape.AddBehavior <LifecycleShapeBehavior>().Initialize( shape, durations.x, durations.y, durations.z ); } else if (durations.z > 0f) //if grow <= 0, adult<=0, die configured { shape.AddBehavior <DyingShapeBehavior>().Initialize( shape, durations.z ); } else { Debug.LogError("Parameters of lifecycle must at least have one positive"); } }
void DestroyShape() { if (shapes.Count - dyingShapeCount > 0) // Checks if shapes exist and are not already dying { Shape shape = shapes[Random.Range(dyingShapeCount, shapes.Count)]; // Randomly selects shape if (destroyDuration <= 0f) // If duration has expired, destroy shape immediately { KillImmediately(shape); } else { shape.AddBehavior <DyingShapeBehavior>().Initialize(shape, destroyDuration); // Adds behaviour so shape goes through dying process } } }
public override bool GameUpdate(Shape shape) { if (shape.Age >= dyingAge) { if (dyingDuration <= 0f) { shape.Die(); return(true); } shape.AddBehavior <DyingShapeBehavior>().Initialize( shape, dyingDuration + dyingAge * shape.Age); return(false); } return(true); }
private void DestroyShape() { if (shapes.Count - dyingShapeCount > 0) { Shape shape = shapes[Random.Range(dyingShapeCount, shapes.Count)]; if (destroyDuration <= 0f) { KillImmediately(shape); } else { shape.AddBehavior <DyingShapeBehavior>().Initialize(shape, destroyDuration); } } }
/// <summary> /// 绕目标旋转 /// </summary> /// <param name="focalShape"> 目标obj </param> /// <param name="lifecycleConfigurations"> </param> void CreatSatelliterFor(Shape focalShape, Vector3 lifecycleConfigurations) { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); shape.gameObject.layer = gameObject.layer; Transform t = shape.transform; t.localRotation = Random.rotation; t.localScale = focalShape.transform.localScale * 0.5f; SetupColor(shape); shape.AddBehavior <SatelliteShapeBehavior>().Initialize(shape, focalShape, spawnConfig.satellite.orbitRadius.RandomValueInRange, spawnConfig.satellite.orbitFrequency.RandomValueInRange); SetupLifecycle(shape, lifecycleConfigurations); }
public override bool GameUpdate(Shape shape) { if (shape.Age >= _dyingAge) { if (_dyingDuration <= 0f) { shape.Die(); return(true); } if (!shape.IsMarkedAsDying) { shape.AddBehavior <DyingShapeBehavior>().Initialize(shape, _dyingDuration + _dyingAge - shape.Age); } } return(false); }
private void OnTriggerExit(Collider other) { Shape shape = other.GetComponent <Shape>(); if (shape) { if (dyingDuration <= 0f) { shape.Die(); } else if (!shape.IsMarkedAsDying) { shape.AddBehavior <DyingShapeBehavior>().Initialize(shape, dyingDuration); } } }
void CreateSatelliteFor(Shape focalShape) { int factoryIndex = Random.Range(0, spawnConfig.factories.Length); Shape shape = spawnConfig.factories[factoryIndex].GetRandom(); Transform t = shape.transform; t.localRotation = Random.rotation; t.localScale = focalShape.transform.localScale * spawnConfig.satellite.relativeScale.RandomValueInRange; //t.localPosition = focalShape.transform.localPosition + Vector3.up; //shape.AddBehavior<MovementShapeBehavior>().Velocity = Vector3.up; SetupColor(shape); shape.AddBehavior <SatelliteShapeBehavior>().Initialize( shape, focalShape, spawnConfig.satellite.orbitRadius.RandomValueInRange, spawnConfig.satellite.orbitFrequency.RandomValueInRange ); }
public void Initialize( Shape shape, float growingDuration, float adultDuration, float dyingDuration ) { //originalScale = shape.transform.localScale; //this.duration = duration; this.adultDuration = adultDuration; this.dyingDuration = dyingDuration; dyingAge = growingDuration + adultDuration; if (growingDuration > 0f) { shape.AddBehavior <GrowingShapeBehavior>().Initialize( shape, growingDuration ); } }
public void Initialize(Shape shape, Shape focalShape, float radius, float frequency) { this.focalShape = focalShape; this.frequency = frequency; Vector3 orbitAxis = Random.onUnitSphere; do { cosOffset = Vector3.Cross(orbitAxis, Random.onUnitSphere).normalized; } while (cosOffset.sqrMagnitude < 0.1f); sinOffset = Vector3.Cross(cosOffset, orbitAxis); cosOffset *= radius; sinOffset *= radius; shape.AddBehavior <RotationShapeBehavior>().AngularVelocity = -360f * frequency * shape.transform.InverseTransformDirection(orbitAxis); GameUpdate(shape); previousPosition = shape.transform.localPosition; }
public override bool GameUpdate(Shape shape) { if (shape.Age >= dyingAge) { if (dyingDuration <= 0f) { shape.Die(); return(true); } if (!shape.IsMarkedAsDying) //already added dying behavior { shape.AddBehavior <DyingShapeBehavior>().Initialize( shape, dyingDuration + dyingAge - shape.Age); } return(false); } return(true); }
//spawn zone에서 motion을 생성해 저정의하는 것은 적절하지 않으므로 여기에 initialize method 정의 public void Initialize(Shape shape, Shape focalShape, float radius, float frequency) { this.focalShape = focalShape; this.frequency = frequency; Vector3 orbitAxis = Random.onUnitSphere; do { cosOffset = Vector3.Cross(orbitAxis, Random.onUnitSphere).normalized; } while (cosOffset.sqrMagnitude < 0.1f); sinOffset = Vector3.Cross(cosOffset, orbitAxis); cosOffset *= radius; sinOffset *= radius; //focal shape을 항상 바라보도록 rotation shape.AddBehavior <RotationShapeBehavior>().AngularVelocity = -360f * frequency * shape.transform.InverseTransformDirection(orbitAxis); GameUpdate(shape); //초기 위치를 맞추기 위해 GameUpdate 한번 호출 previousPosition = shape.transform.localPosition; //focus가 없어지는 것이 첫 프레임일수도 있고, 이전 데이터가 남아있을 수도 있으므로, 초기화 }