工厂模式是一个参数生产一个对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public abstract class Skill {
public GameHero[] AttackTargets { get; set; }
public GameHero SkillFireHero { get; set; }
public float[] SkillParameters { get; set; }
public float Damage { get; set; }
public abstract void TakeDamage();
}
/// <summary>
/// 圆形攻击技能
/// </summary>
public class CubeSkill : Skill
{
public override void TakeDamage()
{
//拿到玩家坐标
Vector3 playerPos = SkillFireHero.HeroTra.position;
//计算技能中心---playerPos+forward*length
Vector3 skillCenter = playerPos + SkillFireHero.HeroTra.forward * SkillParameters[0];
for (int i=0;i<AttackTargets.Length;i++) {
//计算该目标与技能中心的距离
Vector3 dir=AttackTargets[i].HeroTra.position-skillCenter;
//计算技能方向向量在横向Right和纵向Forword的投影向量
Vector3 rightDir=Vector3.Project(dir, SkillFireHero.HeroTra.right);
Vector3 forwardDir = Vector3.Project(dir, SkillFireHero.HeroTra.forward);
//如果在矩形范围内
if (rightDir.magnitude < SkillParameters[1]/2 && forwardDir.magnitude < SkillParameters[2]/2) {
//受到伤害
AttackTargets[i].TakeDamage(Damage);
}}
}
}
/// <summary>
/// 矩形攻击技能
/// </summary>
public class CircleSkill : Skill
{
public override void TakeDamage()
{
//拿到玩家坐标
Vector3 playerPos = SkillFireHero.HeroTra.position;
//计算技能中心---playerPos+forward*length
Vector3 skillCenter = playerPos + SkillFireHero.HeroTra.forward * SkillParameters[0];
for (int i = 0; i < AttackTargets.Length; i++)
{
//计算该目标与技能中心的距离
float dis = Vector3.Distance(skillCenter, AttackTargets[i].HeroTra.position);
//如果距离小于技能半径
if (dis <= SkillParameters[1])
{
AttackTargets[i].TakeDamage(Damage);
}
}
}
}
/// <summary>
/// 扇形攻击技能
/// </summary>
public class SectorSkill : Skill
{
public override void TakeDamage()
{
}
}public class GameHero {
private string name;
public float HeroHp { get; set; }
public Transform HeroTra { get; set; }
public Skill Skill { get; set; }
public GameHero(string name) {
this.name = name;
}
public void FireSkill()
{
Skill.SkillFireHero = this;
Skill.TakeDamage();
}
public void TakeDamage(float damage) {
HeroHp -= damage;
Debug.Log(name+"受到伤害"+damage);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class StrategyConsole : MonoBehaviour {
public Transform libaiTra;
public Transform AngelTra;
// Use this for initialization
private IEnumerator Start () {
Skill skill01 = new CubeSkill();
skill01.Damage = 100;
skill01.SkillParameters = new float[] { 10,5,3};
GameHero angel = new GameHero("安琪拉");
angel.HeroHp = 1000;
angel.HeroTra = AngelTra;
GameHero libai = new GameHero("李白");
libai.HeroHp = 1200;
libai.HeroTra = libaiTra;
GameHero[] allHeros = new GameHero[] { angel,libai};
skill01.AttackTargets = allHeros;Skill skill02 = new CircleSkill();
skill02.Damage = 100;
skill02.SkillParameters = new float[] { 10, 5, 3 };
skill02.AttackTargets = allHeros;angel.Skill = skill01;
libai.Skill = skill02;
while (true) {
yield return 0;
libai.FireSkill();
}
}
}