#include "PeSpray.h"

PeSpray::PeSpray()
{
	type = 3;
	//随机初始速度
	vy = Random(-200, 0) / 100.0f;
	vx = Random(-30, 30) / 100.0f;
	//设置贴图
	ani.png = L"water";
	if (Random(0, 1) == 0)
		ani.sprite_index = L"size0";
	else
		ani.sprite_index = L"size1";
	//启用碰撞
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	//设置实体框
	cbRect.top = 1;
	cbRect.bottom = 8;
	cbRect.left = cbRect.right = 1;
	action_timecounter = action_timecounter_max = 16;
}

void PeSpray::Step()
{
	//重力
	vy = vy + gravity;
	//限制下落速度
	if (vy > max_fall_speed)
		vy = max_fall_speed;
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
	if (action_timecounter > 0)
		action_timecounter--;
	//在碰到地面后播放浪花动画
	if (action_timecounter == 0 && (cbRect.cbBottom || cbRect.inWater))
	{
		vx = 0;
		vy = 0;
		ani.sprite_index = L"spray";
	}
	//消失
	if (ani.sprite_index == L"spray" && ani.image_index == 3)
		Destroy();
}

std::shared_ptr<Entity> CreatePeSpray()
{
	return std::make_shared<PeSpray>();
}
