实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹

void CreateObstacleCopy::flyAction()
{
	int currentPositionX = getPositionX();//当前坐标
	int currentPositionY = getPositionY();
	speed = CCRANDOM_0_1() *500+200;//随机速度

	
	Point destinationPosition;
	int tempY = directionY;
	
	if (directionY >0)//向上运动
	{
			float areaHeight = visibleSize.height-currentPositionY;//获取飞行区域高度
			float getRandom = CCRANDOM_0_1() *(visibleSize.width+areaHeight*2);//随机左上右的一个位置
			
			if(getRandom >areaHeight+visibleSize.width)//飞行最后触碰到左边的位置
			{
				destinationPosition = Point( 0 , visibleSize.height-(getRandom - visibleSize.width-areaHeight));
				directionY=1;
			}
			else if(getRandom>areaHeight)//飞行最后触碰到上边的位置
			{
				destinationPosition = Point( getRandom-areaHeight , visibleSize.height);
				directionY = -1;
			}
			else//飞行最后触碰到右边的位置
			{
				destinationPosition = Point(visibleSize.width,  visibleSize.height-getRandom);
				directionY = 1;
			}
			//log("up");

	}
	else//向下运动
	{
		float areaHeight = currentPositionY;
		float getRandom = CCRANDOM_0_1() *(visibleSize.width+areaHeight*2);//随机左下右的一个位置

		if(getRandom >areaHeight+visibleSize.width)//飞行最后触碰到左边的位置
		{
			destinationPosition = Point( 0 , getRandom - visibleSize.width-areaHeight);
			directionY= -1;
		}
		else if(getRandom>areaHeight)//飞行最后触碰到下边的位置
		{
			destinationPosition = Point( getRandom-areaHeight , 0);
			directionY = 1;
		}
		else//飞行最后触碰到右边的位置
		{
			destinationPosition = Point(visibleSize.width, getRandom);
			directionY =-1;
		}
		//log("down");
	}

	float width = destinationPosition.x- currentPositionX;
	float heigth = destinationPosition.y - currentPositionY;//得到当前对象到目的地的矢量高
	
	float lenght = sqrt(width*width+heigth*heigth);//长度
	float radian = atan(width/ heigth);//h弧度
	float angle = (radian*360)/(2*3.141592);//化成角度了
	if(tempY<0) angle=180+angle;
	float during = lenght / speed;//飞行时间
	FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);//移动动画
	CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
	FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,angle);//旋转动画
	FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
	runAction(task);
}

void CreateObstacleCopy::animationDone()//动画结束后调用该函数
{
	//isCreate = false;
	flyAction();
}

void CreateObstacleCopy::firstLaunch()
{
	
	Point destinationPosition;
	
	//左上角到对象的角度
	float radian = atan(-getPositionX()/ (visibleSize.height-getPositionY()));//h弧度
	float angleUpLeft = (radian*360)/(2*3.141592);//化成角度了

	radian = atan((visibleSize.width-getPositionX())/ (visibleSize.height-getPositionY()));//h弧度
	float angleUpRight = (radian*360)/(2*3.141592);//化成角度了

	radian = atan( (getPositionY())/(visibleSize.width-getPositionX()));//h弧度
	float angleDownRight = (radian*360)/(2*3.141592)+90;//化成角度了

	 radian = atan(float(getPositionY())/ float(getPositionX()));//h弧度
	float angleDownLeft = -(radian*360)/(2*3.141592)-90;//化成角度了
	
	int angle = launchAngle;//传进来的方向
	if (angle>360+angleDownLeft)//角度转换,因为这的角度有负数,传进来的角度是正数
	{
		angle = angle - 360;
	}
	
	if (angleDownLeft<=angle &&angle<angleUpLeft)
	{

		float width =  getPositionX();
		float heigth = width/tan((launchAngle) *2*3.1415/360);//长不变, 算高

		destinationPosition.x = 0;
		destinationPosition.y = getPositionY()-heigth;//得到目的地位置
	

		float lenght = sqrt(width*width+heigth*heigth);

		speed = CCRANDOM_0_1() *200+500;
		float during = lenght / speed;//动画时间

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);//创建移动动画
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);//旋转动画
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);
	}
  	else if(angleUpLeft<=angle &&angle<angleUpRight)
	{
		float heigth = visibleSize.height - getPositionY();
		float width = heigth*tan(launchAngle *2*3.1415/360);

		destinationPosition.x = getPositionX()+width;
		destinationPosition.y = visibleSize.height;
		directionY = -1;

		float lenght = sqrt(width*width+heigth*heigth);

		speed = CCRANDOM_0_1() *200+500;
		float during = lenght / speed;

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);

	}

	else if(angleUpRight<=angle &&angle<angleDownRight)
	{

		float width = visibleSize.width - getPositionX();
		float heigth = width*tan((launchAngle-90) *2*3.1415/360);

		destinationPosition.x = visibleSize.width;
		destinationPosition.y = getPositionY()-heigth;


		float lenght = sqrt(width*width+heigth*heigth);

		speed = CCRANDOM_0_1() *200+500;
		float during = lenght / speed;

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);
	}
	else
	{
		float heigth = getPositionY();
		float width = heigth*tan((launchAngle-180) *2*3.1415/360);

		destinationPosition.x = getPositionX()-width;
		destinationPosition.y = 0;
	

		float lenght = sqrt(width*width+heigth*heigth);
			directionY = 1;
		speed = 500;
		float during = lenght / speed;

		FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);
		CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
		FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,launchAngle);
		FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
		runAction(task);
	}
	
}

猜你喜欢

转载自blog.csdn.net/sun___shine/article/details/89489501
今日推荐