bool TGLobject_directionalcannon_down::cycle(VirtualController *k,class TGLmap *map,GLTManager *GLTM,SFXManager *SFXM,int sfx_volume)
{
	TGLobject *ship;
	m_cycle++;
	bool in_range=false;

	ship=map->object_exists("TGLobject_ship",get_x()-320,get_y()-240,get_x()+320,get_y()+240);
	if (ship!=0) {
		// compute desired angle:
		float dx=ship->get_x()-get_x(),dy=ship->get_y()-get_y();
		float a=float((180*atan2(float(dx),float(dy)))/M_PI);
		a=90-a;
		while (a>360) a-=360;
		while (a<0) a+=360;

		if (a>=190 && a<=350) {
			// ship in valid range:
			if (m_angle<a) m_angle++;
			if (m_angle>a) m_angle--;

			if (fabs(m_angle-a)<2) in_range=true;
		} // if 
	} // if 


	switch(m_state) {
	case 0:	if (in_range) m_state=1;
			break;
	case 1: if (in_range) {
				// fire
				TGLobject *bullet;
				int a=m_angle;
				while(a<0) a+=360;
				while(a>=360) a-=360;
				bullet=new TGLobject_bullet(float(get_x()+(cos_degree(a)*14)),float(get_y()+(sin_degree(a)*14)),0,m_angle+90,2,1,GLTM->get("graphics/objects/bullet-grey.png"),this);
				map->add_object_back(bullet);
				m_state=128;
				Sound_play(SFXM->get(TGL_SFX_FOLDER "/shot"),sfx_volume);

				// Smoke:
				{
					int i;
					for(i=0;i<4;i++) {
						map->add_auxiliary_back_object(new TGLobject_FX_particle(float(get_x()+(cos_degree(a)*14)),float(get_y()+(sin_degree(a)*14)),rand()%60,float((rand()%101)-50)/256.0f,float((rand()%101)-50)/256.0f,1,false,0.25f,0,0.25f,0.75f,50,GLTM->get("graphics/objects/smoke.png")));
					} // for
				}
			} else {
				m_state=0;
			} // if
			break;
	default:
			m_state--;
	} // switch

	if (m_hit) {
		Sound_play(SFXM->get(TGL_SFX_FOLDER "/enemyhit"),sfx_volume);
		m_hit=false;
	} // if 

	if (m_hitpoints<=0) {
		Sound_play(SFXM->get(TGL_SFX_FOLDER "/explosion"),sfx_volume);
		map->add_auxiliary_front_object(new TGLobject_FX_explosion2(get_x(),get_y(),128,200));
		return false;
	} // if 


	return true;
} /* TGLobject_directionalcannon_down::cycle */ 
bool TGLobject_ha_directionalcannon_left::cycle(VirtualController *k,class TGLmap *map,GLTManager *GLTM,SFXManager *SFXM,int sfx_volume)
{
	TGLobject_ship *ship;
	m_cycle++;
	bool in_range=false;

	ship=(TGLobject_ship *)map->object_exists("TGLobject_ship",get_x()-400,get_y()-320,get_x()+400,get_y()+320);
	if (ship!=0) {

		// compute desired angle:
		float dx=ship->get_x()-get_x(),dy=ship->get_y()-get_y();
		float angle=float((180*atan2(float(dx),float(dy)))/M_PI);
		angle=90-angle;
		while (angle>180) angle-=360;
		while (angle<-180) angle+=360;

		{
			int a;
			float best_a=angle;
			float s_sx=ship->get_speedx();
			float s_sy=ship->get_speedy();
			float b_sx,b_sy;
			float d;
			float ls,lb;
			float error,min_error=10000;

			int min=-80,max=80;
			/* Copute the error given an angle "alpha": */ 
			for(a=min;a<max;a+=1) {
				b_sx=float(2*cos_degree(a));
				b_sy=float(2*sin_degree(a));

				d=s_sy*b_sx-s_sx*b_sy;
				if (d!=0) {
					ls=(dx*b_sy-dy*b_sx)/d;
					lb=(s_sy*dx-s_sx*dy)/d;

					if (lb>0) {
						error=float(fabs(ls-lb));
					} else {
						error=10000;
					} /* if */ 
				} else {
					error=10000;
				} /* if */ 

				if (error<min_error) {
					best_a=float(a);
					min_error=error;
				} /* if */ 
			} /* for */ 

			while (best_a>360) best_a-=360;
			while (best_a<0) best_a+=360;

			if (float(fabs(angle-best_a))>60 &&
				float(fabs(angle-best_a))<300) {
				float d=angle-best_a;

				if (d>0 && d<180) best_a=angle-60;
						     else best_a=angle+60;
			} /* if */ 

			angle=best_a;
		}	

		if (angle>=-80 && angle<=80) {
			// ship in valid range:
			if (m_angle<angle) m_angle++;
			if (m_angle>angle) m_angle--;
			if (m_angle<angle) m_angle++;
			if (m_angle>angle) m_angle--;
			if (abs(m_angle-int(angle))<2) in_range=true;
		} // if 
	} // if 


	switch(m_state) {
	case 0:	if (in_range) m_state=1;
			break;
	case 1: if (in_range) {
				// fire
				TGLobject *bullet;
				int a=m_angle;
				while(a<0) a+=360;
				while(a>=360) a-=360;
				bullet=new TGLobject_bullet(float(get_x()+(cos_degree(a)*14)),float(get_y()+(sin_degree(a)*14)),0,m_angle+90,2,1,GLTM->get("objects/bullet-grey"),this);
				map->add_object_back(bullet);
				m_state=128;
				Sound_play(SFXM->get("sfx/shot"),sfx_volume);

				// Smoke:
				{
					int i;
					for(i=0;i<4;i++) {
						map->add_auxiliary_back_object(new TGLobject_FX_particle(float(get_x()+(cos_degree(a)*14)),float(get_y()+(sin_degree(a)*14)),rand()%60,float((rand()%101)-50)/256.0f,float((rand()%101)-50)/256.0f,1,false,0.25f,0,0.25f,0.75f,50,GLTM->get("objects/smoke")));
					} // for
				}
			} else {
				m_state=0;
			} // if
			break;
	default:
			m_state--;
	} // switch

	if (m_hit) {
		Sound_play(SFXM->get("sfx/enemyhit"),sfx_volume);
		m_hit=false;
	} // if 

	if (m_hitpoints<=0) {
		Sound_play(SFXM->get("sfx/explosion"),sfx_volume);
		map->add_auxiliary_front_object(new TGLobject_FX_explosion2(get_x(),get_y(),128,200));
		return false;
	} // if 

	return true;
} /* TGLobject_ha_directionalcannon_left::cycle */ 
Пример #3
0
bool TGLobject_ship_accura::cycle(VirtualController *k,TGLmap *map,GLTManager *GLTM,SFXManager *SFXM,int sfx_volume)
{
	m_cycle++;

	// Check for collision:
	if (map->collision(this,0,0,0)) {
		if (!map->collision_with_foreground(this,0,0,0)) {
			TGLobject *obj = map->collision_with_object(this);
			if (obj->is_a("TGLobject_soft_bullet")) {
				int a=obj->get_angle()-90;
				while(a<0) a+=360;
				while(a>=360) a-=360;
				m_speed_x+=float(cos_degree(a)*48.0)/256.0f;
				m_speed_y+=float(sin_degree(a)*48.0)/256.0f;
			} else {
				Sound_play(SFXM->get("sfx/explosion"),sfx_volume);
				map->add_auxiliary_front_object(new TGLobject_FX_explosion2(get_x(),get_y(),256,200));
				return false;
			}
		} else {
			Sound_play(SFXM->get("sfx/explosion"),sfx_volume);
			map->add_auxiliary_front_object(new TGLobject_FX_explosion2(get_x(),get_y(),256,200));
			return false;
		}
	} // if 

	m_thrusting=false;

	if (m_fuel_recharging_timmer>0) {
		m_fuel_recharging_timmer--;
		if (m_fuel_recharging_timmer==0) {
			if (m_fuel_channel!=-1) {
				Mix_HaltChannel(m_fuel_channel);
				m_fuel_channel=-1;
			} // if 
		} // if 
	} // if 

	if (k->m_button[1] && (fabs(m_speed_x)+fabs(m_speed_y))<1) {
		TGLobject_ball *ball;

		ball=(TGLobject_ball *)map->object_exists("TGLobject_ball",get_x()-16,get_y(),get_x()+16,get_y()+64);
		if (ball!=0 && m_ball==0) {
			if (ball->attractor()) {
				if (ball->get_state()==0) map->action(0);
				ball->capture(5);
				m_ball=ball;
				Sound_play(SFXM->get("sfx/takeball"),sfx_volume);
			} // if 
		} // if 

		// Attractor effect:
		{
			if ((m_cycle%6)==0) {
				map->add_auxiliary_back_object(new TGLobject_FX_particle(get_x(),get_y()+48,0,0,-1,0,false,
												0.5f,0.5f,
												0.5f,0.5f,
												1,1,
												1,0,0.75,0.5f,50,GLTM->get("objects/ripple-back")));
				map->add_auxiliary_front_object(new TGLobject_FX_particle(get_x(),get_y()+48,0,0,-1,0,false,
												0.5f,0.5f,
												0.5f,0.5f,
												1,1,
												1,0,0.75,0.5f,50,GLTM->get("objects/ripple-front")));
			} // if 
		};
	} // if 

	if (k->m_button[0] && !k->m_old_button[0] && m_fuel>40) {
		TGLobject *bullet;
		int a=m_angle-90;
		while(a<0) a+=360;
		while(a>=360) a-=360;

		if (m_missile_side==0) {
			bullet=new TGLobject_bullet_hmissile(float(get_x()+(cos_degree(a)*8)+(sin_degree(a)*7)),float(get_y()+(sin_degree(a)*8)-(cos_degree(a)*7)),0,m_angle,4,1,GLTM->get("objects/bullet-small-grey-missile1"),GLTM->get("objects/bullet-small-grey-missile2"),this);
			m_missile_side=1;
		} else {
			bullet=new TGLobject_bullet_hmissile(float(get_x()+(cos_degree(a)*8)-(sin_degree(a)*7)),float(get_y()+(sin_degree(a)*8)+(cos_degree(a)*7)),0,m_angle,4,1,GLTM->get("objects/bullet-small-grey-missile1"),GLTM->get("objects/bullet-small-grey-missile2"),this);
			m_missile_side=0;
		} // if 
		if (m_ball!=0) bullet->exclude_for_collision(m_ball);
		map->add_object_back(bullet);
		m_fuel-=40;
		Sound_play(SFXM->get("sfx/shipshot2"),sfx_volume);
	} // if 

	if (k->m_joystick[VC_LEFT]) {
		m_angle-=4;
		if (m_angle<0) m_angle+=360;
	} // if 
	if (k->m_joystick[VC_RIGHT]) {
		m_angle+=4;
		if (m_angle>=360) m_angle-=360;
	} // if

	if (k->m_joystick[VC_UP] && m_fuel>0) {
		int a=m_angle-90;
		while(a<0) a+=360;
		while(a>=360) a-=360;
		m_speed_x+=float(cos_degree(a)*11.0)/256.0f;
		m_speed_y+=float(sin_degree(a)*11.0)/256.0f;
		m_thrusting=true;
		m_fuel--;
	} // if 

	if (m_speed_x>0) m_speed_x-=1.0f/256.0f;
	if (m_speed_x<0) m_speed_x+=1.0f/256.0f;
	m_speed_y+=2.0f/256.0f;

	if (m_speed_x>8) m_speed_x=8;
	if (m_speed_x<-8) m_speed_x=-8;
	if (m_speed_y>8) m_speed_y=8;
	if (m_speed_y<-8) m_speed_y=-8;
	m_x+=m_speed_x;
	m_y+=m_speed_y;

	if (m_x<0) {
		m_x=0;
		m_speed_x=0;
	} /* if */ 
	if (m_y<0) {
		m_y=0;
		m_speed_y=0;
	} /* if */ 
	if (m_x>(map->get_dx())) {
		m_x=float(map->get_dx());
		m_speed_x=0;
	} /* if */ 
	if (m_y>(map->get_dy())) {
		m_y=float(map->get_dy());
		m_speed_y=0;
	} /* if */ 


	// Ball attraction:
	if (m_ball!=0) {
		float dx=m_ball->get_x()-get_x();
		float dy=m_ball->get_y()-get_y();
		float d=float(sqrt(dx*dx+dy*dy));

		if (d<2) d=2;
		{
			float inc=float(28/sqrt(d));
			float inc_x=((dx/d)*inc)/256.0f;
			float inc_y=((dy/d)*inc)/256.0f;
			m_ball->set_speed_x(m_ball->get_speed_x()-inc_x);
			m_ball->set_speed_y(m_ball->get_speed_y()-inc_y);
		}

	} // if 

	if (m_thrusting) {
		if ((m_cycle%4)==0) map->add_auxiliary_back_object(new TGLobject_FX_particle(get_x(),get_y(),rand()%60,0,0,1,false,0.125f,0,0.5f,1.5f,50,GLTM->get("objects/smoke")));

		if (m_thrust_channel==-1) {
			m_thrust_channel=Sound_play_continuous(SFXM->get("sfx/thrust"),sfx_volume);
		} // if 
	} else {
		if (m_thrust_channel!=-1) {
			Mix_HaltChannel(m_thrust_channel);
			m_thrust_channel=-1;
		} // if 
	} // if

	return true;
} /* TGLobject::cycle */ 
Пример #4
0
bool TGLobject_FX_explosion2::cycle(VirtualController *k,class TGLmap *map,GLTManager *GLTM,SFXManager *SFXM,int sfx_volume)
{
	// Wave
	if (m_cycle==0) {
		map->insert_auxiliary_front_object(new TGLobject_FX_particle(get_x(),get_y(),0,0,0,0,false,
										0.5f,0.5f,
										0.5f,0.5f,
										1,1,
										1,0,m_maxsize/512.0f,m_maxsize/32.0f,m_duration,GLTM->get("objects/ripple-back")));
		map->add_auxiliary_front_object(new TGLobject_FX_particle(get_x(),get_y(),0,0,0,0,false,
										0.5f,0.5f,
										0.5f,0.5f,
										1,1,
										1,0,m_maxsize/512.0f,m_maxsize/32.0f,m_duration,GLTM->get("objects/ripple-front")));
	} // if 

	// fire:
	{
		int i;
		int range=100;
		range=int(m_maxsize/16);
		if (range<1) range=1;
		for(i=0;i<(m_duration-m_cycle*4);i+=8) {
			int r=rand()%range;
			int a=rand()%360;
			if ((rand()%128)==0) {
				map->insert_auxiliary_front_object(new TGLobject_FX_particle(get_x(),get_y(),rand()%360,float(r*cos_degree(a))/16,float(r*sin_degree(a))/16,4,true,
																		 0.5f,0.5f,
																		 0.5f,0.5f,
																		 0.5f,0.5f,
																		 1,0,0.25f,0.35f,m_duration/2,GLTM->get("objects/explosion")));
			} else {
				if (r<range/2) {
					map->add_auxiliary_front_object(new TGLobject_FX_particle(get_x(),get_y(),rand()%360,float(r*cos_degree(a))/32,float(r*sin_degree(a))/32,4,false,
																			 1,1,
																			 1,1,
																			 1,1,
																			 1,0,0.25f,0.35f,m_duration/4,GLTM->get("objects/explosion")));
				} else {
					map->insert_auxiliary_front_object(new TGLobject_FX_particle(get_x(),get_y(),rand()%360,float(r*cos_degree(a))/32,float(r*sin_degree(a))/32,4,false,
																			 0.5f,0.5f,
																			 0.5f,0.5f,
																			 1,1,
																			 1,0,0.25f,0.35f,m_duration/4,GLTM->get("objects/explosion")));
				} // if 
			} // if 
		} // for
	}

	// smoke:
	// ...

	// particles:
	if (m_cycle==0) {
		int i;
		int range=100;
		range=int(m_maxsize);
		if (range<1) range=1;
		for(i=0;i<m_duration/2;i++) map->add_auxiliary_back_object(new TGLobject_FX_particle(get_x(),get_y(),rand()%60,float((rand()%(range*2+1))-range),float((rand()%(range*2+1))-range),4,true,0.5f,0.5f,0.5f,0.5f,1,1,1.0f,0,0.25f,0.25f,m_duration,GLTM->get("objects/particle1")));
	} // if

	m_cycle++;
	if (m_cycle>m_duration) return false;
	return true;
} /* TGLobject_FX_explosion2::cycle */