C#设计模式---策略模式

【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili

工厂模式是一个参数生产一个对象

 

 

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();
        }      
    }    
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值