#include "Effect.h"
#include "DEFINES.h"
#include "Crawler.h"
#include "utils.h"

INCLUDE_game
INCLUDE_ANIMATION_DATA
INCLUDE_MONSTER_LIST

PulsatingFire::PulsatingFire(vf2d pos, float lifetime, AnimationState animation, bool upperLevel, vf2d size, float fadeout, vf2d spd, Pixel col, float rotation, float rotationSpd, bool additiveBlending)
	:Effect(pos,lifetime,animation,upperLevel,size,fadeout,spd,col,rotation,rotationSpd,additiveBlending){
	for(int i=0;i<8;i++){
		pulsatingFireValues.push_back(util::random(1));
	}
}

bool PulsatingFire::Update(float fElapsedTime){
	lastParticleTimer-=fElapsedTime;
	lastDamageTimer-=fElapsedTime;
	if(lastParticleTimer<=0){
		int particleCount=rand()%5+1;
		for(int i=0;i<particleCount;i++){
			float randomAngle=util::random(2*PI);
			float randomRange=12*size.x*(1-util::random(0.25))*(1-util::random(0.25));
			float randomColorTintG=128-(util::random(64)+util::random(64));
			float randomColorTint=util::random(16);
			game->AddEffect(std::make_unique<Effect>(pos+vf2d{cos(randomAngle),sin(randomAngle)}*randomRange,0,AnimationState::DOT_PARTICLE,OnUpperLevel(),vf2d{util::random(2)+1,1},util::random(4)+2,vf2d{util::random(10)-5,-util::random(15)-5},Pixel{128,uint8_t(randomColorTintG),uint8_t(randomColorTint),uint8_t(util::random(128)+128)}));
		}
		lastParticleTimer=util::random(0.2)+0.025;
	}
	if(lastDamageTimer<=0){
		lastDamageTimer=0.99;
		game->HurtEnemies(pos,4*24,game->GetPlayer()->GetAttack()*1,OnUpperLevel());
	}
	return Effect::Update(fElapsedTime);
}

void PulsatingFire::Draw(){
	for(int i=0;i<8;i++){
		Animate2D::FrameSequence*effectSpr=nullptr;
		switch(int(pulsatingFireValues[i]*5)){
			case 0:{
				effectSpr=&ANIMATION_DATA[AnimationState::FIRE_RING1];
			}break;
			case 1:{
				effectSpr=&ANIMATION_DATA[AnimationState::FIRE_RING2];
			}break;
			case 2:{
				effectSpr=&ANIMATION_DATA[AnimationState::FIRE_RING3];
			}break;
			case 3:{
				effectSpr=&ANIMATION_DATA[AnimationState::FIRE_RING4];
			}break;
			case 4:{
				effectSpr=&ANIMATION_DATA[AnimationState::FIRE_RING5];
			}break;
			default:
				effectSpr=&ANIMATION_DATA[AnimationState::FIRE_RING1];
		}
		const Renderable*img=effectSpr->GetFrame(0).GetSourceImage();
		game->view.DrawPartialDecal(pos-effectSpr->GetFrame(0).GetSourceRect().size/2*size,img->Decal(),effectSpr->GetFrame(0).GetSourceRect().pos,effectSpr->GetFrame(0).GetSourceRect().size,size,{255,uint8_t(pulsatingFireValues[i]*256),0,uint8_t((63*(sin(3*lifetime+PI*pulsatingFireValues[i]))+64)*(fadeout/original_fadeoutTime))});
	}
}