void Awake() { GameDirector director = GameDirector.getInstance(); director.currentSceneController = this; mF = Singleton <myFactory> .Instance; player = mF.getPlayer(); }
/// <summary> /// 简单工厂模式 /// </summary> /// <returns></returns> public static string Ins88_SimpleFactory() { string strVal = ""; myFactory f = new myFactory(); Tool xidao = f.Produce("EndMill"); strVal += "\n" + xidao.Create(); Tool qiudao = f.Produce("BallMill"); strVal += "\n" + qiudao.Create(); return(strVal); }
IEnumerator shoot() {//协程实现npc坦克每隔1s进行射击 while (!gameover) { for (float i = 1; i > 0; i -= Time.deltaTime) { yield return(0); } if (Vector3.Distance(transform.position, target) < 20) { //和玩家坦克距离小于20,则射击 myFactory mF = Singleton <myFactory> .Instance; GameObject bullet = mF.getBullet(tankType.Enemy); //获取子弹,传入的参数表示发射子弹的坦克类型 bullet.transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z) + transform.forward * 1.5f; //设置子弹 bullet.transform.forward = transform.forward; //设置子弹方向 Rigidbody rb = bullet.GetComponent <Rigidbody>(); rb.AddForce(bullet.transform.forward * 20, ForceMode.Impulse); //发射子弹 } } }
IEnumerator shoot() { while (!gameover) { for (float i = 1; i > 0; i -= Time.deltaTime) { yield return(0); } if (Vector3.Distance(transform.position, target) < 20) { myFactory mF = Singleton <myFactory> .Instance; GameObject bullet = mF.getBullet(tankType.Enemy); bullet.transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z) + transform.forward * 1.5f; bullet.transform.forward = transform.forward; Rigidbody rb = bullet.GetComponent <Rigidbody>(); rb.AddForce(bullet.transform.forward * 20, ForceMode.Impulse); } } }
void OnCollisionEnter(Collision other) { myFactory mF = Singleton <myFactory> .Instance; ParticleSystem explosion = mF.getPs(); explosion.transform.position = transform.position; Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].tag == "tankPlayer" && this.type == tankType.Enemy || colliders[i].tag == "tankEnemy" && this.type == tankType.Player) { float distance = Vector3.Distance(colliders[i].transform.position, transform.position); float hurt = 100f / distance; float current = colliders[i].GetComponent <Tank>().getHp(); colliders[i].GetComponent <Tank>().setHp(current - hurt); } } explosion.Play(); if (this.gameObject.activeSelf) { mF.recycleBullet(this.gameObject); } }
private tankType type; //发射子弹的坦克类型 void OnCollisionEnter(Collision other) { myFactory mF = Singleton <myFactory> .Instance; ParticleSystem explosion = mF.getPs(); //获取爆炸的粒子系统 explosion.transform.position = transform.position; //设置粒子系统位置 Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius); //获取爆炸范围内的所有碰撞体 for (int i = 0; i < colliders.Length; i++) { if (colliders[i].tag == "tankPlayer" && this.type == tankType.Enemy || colliders[i].tag == "tankEnemy" && this.type == tankType.Player) //发射子弹的坦克类型和被击中的坦克类型不同,才造成伤害 { float distance = Vector3.Distance(colliders[i].transform.position, transform.position); //被击中坦克与爆炸中心的距离 float hurt = 100f / distance; //受到的伤害 float current = colliders[i].GetComponent <Tank>().getHp(); //当前的生命值 colliders[i].GetComponent <Tank>().setHp(current - hurt); //设置受伤后生命值 } } explosion.Play(); //播放爆炸的粒子系统 if (this.gameObject.activeSelf) { mF.recycleBullet(this.gameObject); //回收子弹 } }