본문 바로가기
GameMaker강좌[GMS2]/전략게임강좌

[게임메이커 강좌-전략][GMS2] RTS 게임 만들기-4 적 유닛

by 타락카얀 2023. 3. 14.
728x90

 

 

GAME MAKER 강좌

 

KAYAN

 

 

 

 

 

 

이번엔 적 유닛의 공격 설정에 대해 알아봅시다.

 

(▲적 유닛의 공격 장면)

 

 

 

 

 

◈적 유닛의 공격 설정(obj_en_unit1)

 

 

적 유닛의 공격 설정은 플레이어와 비슷합니다.

다만 플레이어에선 적유닛을 체크했던것과 달리, 적 유닛에선 플레이어를 체크하는 것이 다를 뿐이지요.

 

저번 강좌에 이어 이번에도 열심히 따라 해봅시다.

적 유닛 오브젝트(obj_en_unit1)로 이동해서 [Step 이벤트]에 아래와 같이 추가합니다.

 


   //● obj_en_unit1 - Step 이벤트
   
   
   //----------------------▼추가
   var aa;
   
   if !(instance_exists(target)){target=noone;
   
   if instance_exists(obj_player){aa=instance_nearest(x,y,obj_player);//목표물 검색
   if instance_exists(aa) && move_on=0{
   if distance_to_object(aa)<=range_max{target=aa;}
   }}
   
   }
   else{
   //==========================================
   
   if !(distance_to_object(target)<range_min){
   mp_potential_step(target.x,target.y,pspeed,0);
   if move_on=1{move_on=2;}//공격 이동
   }
   else{move_on=0;}
   
   //---------- 공격 시작
   if distance_to_object(target)<=range_att{
   direction=point_direction(x,y,target.x,target.y);
   
   if att_sw==0{
   var it;
   it=instance_create_depth(x,y,0,obj_attack_bullet);//공격 오브젝트 발사!
   it.direction=point_direction(x,y,target.x,target.y);
   it.speed=5;
   it.damage_point=damage_point;
   it.flag=flag;
   it.alarm[0]=point_distance(x,y,target.x,target.y)/it.speed;//이동 시간 제한
   
   alarm[0]=att_delay;
   att_sw=1;}
   }
   //---------- 공격 종료
   
   
   //목표물이 이동거리보다 멀때는 target을 초기화
   if !(distance_to_object(target)<=range_max){target=noone;}
   
   //==========================================
   }
   
   //이동 중이었을 때는 목표지점까지 마저 이동함.
   if !(instance_exists(target)){if move_on==2{move_on=1;}}
   
   //----------------------move
   if move_on==1{
   if point_distance(x,y,move_x,move_y)>pspeed{
   mp_potential_step(move_x,move_y,pspeed,0);
   }
   else{move_on=0;}}
   
   //----------------------▲추가
   
   if HP<=0{HP=0;instance_destroy();}
   


 

어이쿠! 추가 분량이 많군요. 흐흐~

그런데 플레이어 설정과 달라보이지 않죠?

네. 거의 동일한 설정입니다.

적 오브젝트를 체크하던 것을 플레이어 오브젝트를 체크하는 것이 바뀌었죠.

이것은 실제 배치할 때나 따로 적 유닛을 이동시키고자 할때 필요한 설정입니다.

 

하지만 이것을 심플하게 이동 거리 안에 들어왔을 때만 이벤트를 발생시키고자 한다면

아래와 같이 이벤트를 축소 시킬 수 있습니다.

 


   //● obj_en_unit1 - Step 이벤트
   
   
   
   //----------------------▼추가
   var aa;
   
   if !(instance_exists(target)){target=noone;
   
   if instance_exists(obj_player){aa=instance_nearest(x,y,obj_player);//목표물 검색
   if instance_exists(aa) && move_on=0{
   if distance_to_object(aa)<=range_max{target=aa;}
   }}
   
   }
   else{
   //==========================================
   
   if !(distance_to_object(target)<range_min){
   mp_potential_step(target.x,target.y,pspeed,0);
   }
   
   //---------- 공격 시작
   if distance_to_object(target)<=range_att{
   direction=point_direction(x,y,target.x,target.y);
   
   if att_sw==0{
   var it;
   it=instance_create_depth(x,y,0,obj_attack_bullet);//공격 오브젝트 발사!
   it.direction=point_direction(x,y,target.x,target.y);
   it.speed=5;
   it.damage_point=damage_point;
   it.flag=flag;
   it.alarm[0]=point_distance(x,y,target.x,target.y)/it.speed;//이동 시간 제한
   
   alarm[0]=att_delay;
   att_sw=1;}
   }
   //---------- 공격 종료
   
   
   //목표물이 이동거리보다 멀때는 target을 초기화
   if !(distance_to_object(target)<=range_max){target=noone;}
   
   //==========================================
   }
   
   //----------------------▲추가
   
   if HP<=0{HP=0;instance_destroy();}
   


 

헉! 축소된것이 별로 없군요. 하하하-

뭐가 줄어들었는지 찾아보세용.

 

보시다시피 이동 부분만 삭제되었습니다.

이동 설정(move_on)은 따로 이벤트 구성시 필요한 것이기 때문에, 심플하게 작동 시키려면 위와 같이 설정하셔도 됩니다.

 

다음은 공격시 재장전이 되는 이벤트를 추가하도록 합시다.

플레이어와 마찬가지로 [Alarm 0 이벤트]를 추가하고, 아래와 같이 작성합니다.

 


   //● obj_en_unit1 - Alarm 0 이벤트
   
   
   att_sw=0;


 

플레이어 유닛과 마찬가지로 이 적 유닛은 움직이는 오브젝트이기 때문에 이동하다보면 적 유닛과 겹치게 됩니다.

네. 그래서 이것을 떨어 뜨려 놓아야 합니다.

 

(▲ 겹치지 않게 떨어뜨려 놓기)

 

현 오브젝트 parent(obj_enemy)와의 [충돌 이벤트]를 추가하고, 아래와 같이 작성합니다.

 


   //● obj_en_unit1 - obj_enemy 와의 충돌 이벤트
   
   
   var spd;
   spd=1;
   if x>other.x{x+=spd;}else{x-=spd;}
   if y>other.y{y+=spd;}else{y-=spd;}


 

이렇게 하면 적 유닛이 겹치더라도 자동으로 서로 떨어지게 됩니다.

 

캬아~ 간단하게 적유닛의 공격을 완성했네요. 굳!

이왕 만든김에 다른 타입의 유닛을 만들어봅시다.

 

 

 

 

 

◈타워형 적 유닛(obj_en_unit2)

 

 

이번에 만들 유닛은 멈춰있고, 플레이어가 공격 범위에 오면 공격하는 유닛입니다.

 

(▲타워형 적 유닛)

 

먼저 이미지를 준비하고, 스프라이트(spr_en_unit2)를 구성합니다.

 

(▲타워형 적 유닛 이미지)

 

회전을 해야하니, 중심점을 가운데로 맞춰주세요.

그리고 적 타워 오브젝트(obj_en_unit2)를 하나 만들고, 스프라이트를 지정해줍니다.

 

(▲타워형 적 유닛 오브젝트)

 

[Create 이벤트]를 추가하고, 이전에 만든 적유닛(obj_en_unit1)의 [Create이벤트]를 복사해 붙여 넣습니다.

 


   //● obj_en_unit2 - Create 이벤트
   
   
   flag=1;
   select=0;
   
   HP=30; //◀체력 변경
   HP_MAX=HP;
   
   pspeed=2;
   
   move_on=0;
   move_x=0;
   move_y=0;
   
   target=noone;
   
   range_min=96;
   range_max=320;
   range_att=256; //◀공격 범위 변경
   
   damage_point=1;//공격력
   att_sw=0;
   att_delay=50; //◀공격 딜레이 변경
   


 

수치들을 적당히 바꿔주면 됩니다.

이 유닛은 멈춰져 있기 때문에, HP ,range_att, att_delay 변수 값만 변경하면 되겠군요.

이벤트 자체는 이전에 만든 이동하는 적 유닛(obj_en_unit1)의 [Step 이벤트]를 제외하곤 다 동일해요.

이미지 표시 방법이 다르다면 [Draw 이벤트]에서 변경하시면 됩니다.

강좌에선 [Destroy 이벤트], [Alarm 0 이벤트], [Draw 이벤트]는 이전에 만든 적 유닛(obj_en_unit1)과 동일하게 설정하도록 하겠습니다.

 

다음은 [Step 이벤트]에서 공격을 설정하도록 하겠습니다.

 


   //● obj_en_unit2 - Step 이벤트
   
   
   var aa;
   
   if !(instance_exists(target)){target=noone;//목표물이 없을 경우
   
   if instance_exists(obj_player){aa=instance_nearest(x,y,obj_player);//가까운 유닛을 검색
   if instance_exists(aa){
   if distance_to_object(aa)<=range_att{target=aa;}//목표물이 공격 범위 안에 들면 목표물로 지정
   }}
   
   }
   else{
   //==========================================
   //---------- 공격 시작
   if distance_to_object(target)<=range_att{//목표물이 공격 범위에 들면 공격함
   direction=point_direction(x,y,target.x,target.y);
   
   if att_sw==0{
   
   var it;
   it=instance_create_depth(x,y,0,obj_attack_bullet);
   it.direction=point_direction(x,y,target.x,target.y);
   it.speed=5;
   it.damage_point=damage_point;
   it.flag=flag;
   it.alarm[0]=point_distance(x,y,target.x,target.y)/it.speed;
   
   alarm[0]=att_delay;
   att_sw=1;
   }

   
   }
   else{target=noone;}
   //---------- 공격 종료
   //==========================================
   }
   
   if HP<=0{HP=0;instance_destroy();}//체력이 0 이면 파기
   


 

이전 설정과 같죠. 네. 이동 부분만 빠졌습니다.

이것은 플레이어 유닛(obj_player)이 공격 범위 안에 들면 목표물(target)로 지정하고,

공격 범위(range_att)를 벗어날때까지 공격하게 됩니다.

만약 공격 범위(range_att)를 벗어나면 목표물에서 해제하고, 플레이어 유닛(obj_player)중

가까이 있는 유닛을 다시 검색하여 목표물로 지정하게 되는 것이에요.

 

와우! 간단하게 유닛을 완성했군요. 굳!

 

이제 테스트 할 시간입니다.

룸에 배치하고 제대로 작동되는지 테스트 해봅시다.

 

(▲테스트 화면)

 

후우~ 잘 되셨는지 모르겟군요.

다음 강좌에선 게임 화면 상단에 미니맵을 한번 만들어 보도록 해요.

 

 

 

- 다음 편에서 계속 -

 

 

strategy_rts_example01a_4.yyz
0.07MB

 

 

 

 

 

300x250

댓글