NPC 击退.拉近.简单方法

//create by crazys_popcorn@126.com

#include "iostream"
#include "stdlib.h"
#include "math.h"
using namespace std;
//坐标点
struct xFpos
{
    float  x;
    float  y;
    xFpos(float _x, float _y) :x(_x), y(_y)
    {

    }

};
//算出2点的距离
float getDistance( xFpos pos1,xFpos pos2)  
{
    return sqrt((pos2.x - pos1.x) *(pos2.x - pos1.x) + (pos2.y - pos1.y) * (pos2.y - pos1.y));
}



xFpos checkRepelBlock(xFpos xfpos, float distance, float arc)
{
#if 0
/
///直接返回距离 
    ///里面不带 阻挡判断
    float fy = sin(arc) *distance + xfpos.y;
    float fx = cos(arc) * distance + xfpos.x;

    return xFpos(fx, fy);
#endif 
/
//==============经过的点判断=========================//
    //中间 每多少个点判断
    float endy = xfpos.y;
    float endx = xfpos.x;

    int  atomDis = 10;  //10个像素点监测一次
    //s算出多少步骤
    int count = int(distance / atomDis);


    float remain = distance - (int)(distance / atomDis)*atomDis;



    int   end = remain > 0 ? count + 1 : count;
    for (int i =0;i < end; i++)
    {
        float dis = atomDis;

        if (remain && i == end - 1)
            dis = remain;

        float fy = sin(arc) *dis + endy;
        float fx = cos(arc) *dis + endx;

        xFpos newPos(fx, fy);
        //  判断new pOS  这个坐标的 有没有阻挡
        //if (scene->checkBlock(newPos))
        //{
        //}

        endy = fy;
        endx = fx;
    }
    return xFpos(endx, endy);

}


bool Back(xFpos& atkPos, xFpos &defPos)
{


    //弧度
    float arc;
    //算出atkPos  在defPos 的什么角度   用π 3.14  
    arc = atan2(atkPos.y - defPos.y, atkPos.x - defPos.x);


    printf("弧度  %f \n",arc);


    float distance = 0;
    //目前的距离 
    //算出2着的距离
    distance = getDistance(atkPos, defPos);
    printf("距离  %f", distance);


    //加入拉近百分之  80

    distance = distance * 10 / 100;


    //根据距离算移动到那个点了

    xFpos fend = checkRepelBlock(defPos, distance, arc);

    defPos = fend;
    //根据距离算重点坐标
    return true;
}




void main()
{

    xFpos  atk(2, 2);  //攻击者坐标

    xFpos  def(2, 1);  // 防御者坐标


    Back(atk,def);

    printf("x:%d  y:%d", def.x, def.y);

    system("pause");
}

//解释下意思//就是例如 玩家 从A点 把B点的怪。直接拉近百分之90的距离..大概就是这么个意思了。。。
2017年1月16日 14:54:22

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值