FPS- Adding handguns, machine guns, and shotguns to AGS 2.7

Started by Dr Lecter, Thu 29/12/2005 18:55:47

Previous topic - Next topic

Dr Lecter

Ponch did one of these for 6, I don't know if he did an updated version or not, but here's the code I'm using in my latest game. I updated the code to make it more easily work with multiple guns, and for the shotgun, work with multiple impacts. The problem with the old code, after updating was that it could only deal with a target being shot once, if you have a shotgun, you sometimes hit the target more than once. If you want a more detailed explanation of what's going on I'll write a follow-up to this.

This script is pretty copied from this game:
Lt. Head Demo

I clicked the wrong forum, can a mod please move this... thank you in advance....

Global scripts:

Code: ags
#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded
SetGlobalInt (1, 2); // Number of clips player has
SetGlobalInt (2, 9); // Gun Ammo Status (7-shots in gun)
SetGlobalInt (4, Random(2)+1); // Random number between 1 and 3 (Sound)
SetGlobalInt (21, 0); // Gun type 0 = hand gun, 1 = machine gun 2 shotgun
SetGlobalInt (22, 2); // Number of machine gun clips
SetGlobalInt (23, 30); // Number of bullets remaining in machine gun
SetGlobalInt (24, 4); // Number of shotgun shells (in groups of 7)
SetGlobalInt (25, 7); // Number of shotgun shells remaining in gun
}
#sectionend game_start  // DO NOT EDIT OR REMOVE THIS LINE


Code: ags
#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
  // put anything you want to happen every game cycle here
  if (IsSoundPlaying()==0) {
    mouse.Visible = true;
}

      	 if (IsInteractionAvailable (mouse.x,  mouse.y, eModeShoot) == 1) {
      	   if (mouse.IsButtonDown(eMouseLeft)) {
      	     if (GetGlobalInt(21) == 1) {
      	       if (GetGlobalInt(23)>0) {
      	         		PlaySound(15);
							 int left = Random(15);
					   	 int right = Random(15);
					  	 int up = Random(15);
					  	 int down = Random(15);
					  	 int somethingelse = mouse.x +left -right;
					  	 int somethingelse2 = mouse.y +up -down;
						   ProcessClick (somethingelse, somethingelse2,  eModeShoot);
				  		 RawDrawImage(somethingelse+84,somethingelse2-1,29);
				  		 SetGlobalInt(23, (GetGlobalInt(23)-1));
					   	 Wait(6);
					   	 } else {
								PlaySound(3);
								}
						  }
					  }
			  	}
			 if (GetGlobalInt(30)==1) {
			   if (GetGlobalInt(31)==1) {
			     if (GetGlobalInt(32)==1) {
								cTarget1.Say("Well done, you hit all the targets!");
								cEgo.ChangeRoom (1, 53, 223);
								SetGlobalInt(30, 0);
								SetGlobalInt(31, 0);
								SetGlobalInt(32, 0);
			     }
			   }
			 }
}
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE


Code: ags
#sectionstart on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE
function on_mouse_click(int button) {
// called when a mouse button is clicked. button is either LEFT or RIGHT
   if (IsGamePaused() == 1) {
   // Game is paused, so do nothing (ie. don't allow mouse click)
	 }
	 else if (GetGlobalInt(18) == 1) { // Combat is enabled, so no clicking
	 }
   else if (mouse.IsButtonDown(eMouseRight)) {
      // look:
         if (IsInteractionAvailable(mouse.x,mouse.y, eModeLookat) == 0) {
	 string buffer;
	 GetLocationName(mouse.x, mouse.y, buffer);
	 cEgo.Say("Nice %s.", buffer);
 }
				 else if (IsInteractionAvailable(mouse.x,mouse.y, eModeLookat) == 1) {
				 ProcessClick (mouse.x, mouse.y, eModeLookat);
   } 
   }
   else if (mouse.IsButtonDown(eMouseLeft)) {
      // use, shoot or walk
      	 if (IsInteractionAvailable (mouse.x,  mouse.y, eModeShoot) == 1) {
      	   if (GetGlobalInt(21) == 0) {
      	     if (GetGlobalInt(2)>0) {
      	       		PlaySound(1);
						 int left = Random(10);
					 	 int right = Random(10);
						 int up = Random(10);
						 int down = Random(10);
						 int somethingelse = mouse.x +left -right;
						 int somethingelse2 = mouse.y +up -down;
						 ProcessClick (somethingelse, somethingelse2,  eModeShoot);
						 RawDrawImage(somethingelse+84,somethingelse2-1,29);
						 SetGlobalInt(2, GetGlobalInt(2)-1);
						 } else {
						 PlaySound(3);
						}
				}
					else if (GetGlobalInt(21) == 2) {
				     if (GetGlobalInt(25)>0) {
				     PlaySound(17);
						 int left = Random(10);
					 	 int right = Random(10);
						 int up = Random(10);
						 int down = Random(10);
						 int somethingelse = mouse.x +left -right;
						 int somethingelse2 = mouse.y +up -down;
						 ProcessClick (somethingelse, somethingelse2,  eModeShoot);
						 RawDrawImage(somethingelse+84,somethingelse2-1,29);
						 left = Random(10);
					 	 right = Random(10);
						 up = Random(10);
						 down = Random(10);
						 somethingelse = mouse.x +left -right;
						 somethingelse2 = mouse.y +up -down;
						 ProcessClick (somethingelse, somethingelse2,  eModeShoot);
						 RawDrawImage(somethingelse+84,somethingelse2-1,29);
						 left = Random(10);
					 	 right = Random(10);
						 up = Random(10);
						 down = Random(10);
						 somethingelse = mouse.x +left -right;
						 somethingelse2 = mouse.y +up -down;
						 ProcessClick (somethingelse, somethingelse2,  eModeShoot);
						 RawDrawImage(somethingelse+84,somethingelse2-1,29);
						 SetGlobalInt(25, GetGlobalInt(25)-1);
						 Wait(12);
					} else {
					  PlaySound(3);
					}
					}
			}
         else if (IsInteractionAvailable(mouse.x,mouse.y, eModeInteract) == 0) {

				 ProcessClick (mouse.x,  mouse.y, eModeWalkto);
         }
				 else {
				 ProcessClick (mouse.x, mouse.y, eModeInteract);	
         }
 }
}
#sectionend on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE


Code: ags
#sectionstart character1_a  // DO NOT EDIT OR REMOVE THIS LINE 2,0, 124

int health1=1;

function character1_a() {
  // script for Character 1 (Target1): Shoot character
  
  // Shoot Target1
			if (GetGlobalInt(4) == 1) {
			PlaySound(9);
			}
			else if (GetGlobalInt(4) == 2) {
			PlaySound(10);
			}
			else {
			PlaySound(11);
			}
			health1=health1-1;
		  cTarget1.StopMoving(); // Move Target off Screen off screen
		  cTarget1.ChangeRoom (2, 0, 124);
			if (health1<1) {
					SetGlobalInt(30, 1);
					}
}
#sectionend character1_a  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart character2_a  // DO NOT EDIT OR REMOVE THIS LINE 2, 523, 176

			int health2;

function character2_a() {
  // script for Character 2 (Target2): Shoot character
  // Shoot Target2
			if (GetGlobalInt(4) == 1) {
			PlaySound(9);
			}
			else if (GetGlobalInt(4) == 2) {
			PlaySound(10);
			}
			else {
			PlaySound(11);
			}
			health2=health2-1;
		  cTarget2.StopMoving(); // Move Target off Screen off screen
		  cTarget2.ChangeRoom (2, 523, 176);
			if (health2<1) {
					SetGlobalInt(31, 1);
					}
}
#sectionend character2_a  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart character3_a  // DO NOT EDIT OR REMOVE THIS LINE 2, 523, 74

int health3=1;

function character3_a() {
  // script for Character 3 (Target3): Shoot character
  // Shoot Target3
			if (GetGlobalInt(4) == 1) {
			PlaySound(9);
			}
			else if (GetGlobalInt(4) == 2) {
			PlaySound(10);
			}
			else {
			PlaySound(11);
			}
			health3=health3-1;
		  cTarget3.StopMoving(); // Move Target off Screen off screen
		  cTarget3.ChangeRoom (2, 523, 74);
			if (health3<1) {
					SetGlobalInt(32, 1);
					}
}
#sectionend character3_a  // DO NOT EDIT OR REMOVE THIS LINE



Reload and weapon change code:

Reload:
Code: ags
  if (GetGlobalInt(21)==0) {
if (GetGlobalInt(1)>=1) { // If the player has a clip left
 SetGlobalInt(1, GetGlobalInt(1)-1);//lose one clip
 PlaySound(2); // Play reload sound
 SetGlobalInt (2, 7);
 //AnimateObject - Reloading Graphic
 mouse.Visible = false; // so the player can't shoot while reloading
  }
} else if (GetGlobalInt(21)==1) {
  if (GetGlobalInt(22)>=1) { // If the player has a clip left
 SetGlobalInt(22, GetGlobalInt(22)-1);//lose one clip
 PlaySound(16); // Play reload sound
 SetGlobalInt (23, 30);
 //AnimateObject - Reloading Graphic
 mouse.Visible = false; // so the player can't shoot while reloading
 }
  } else if (GetGlobalInt(21)==2) {
  if (GetGlobalInt(24)>=1) { // If the player has a clip left
 SetGlobalInt(24, GetGlobalInt(24)-1);//lose one clip
 PlaySound(18); // Play reload sound
 SetGlobalInt (25, 7);
 //AnimateObject - Reloading Graphic
 mouse.Visible = false; // so the player can't shoot while reloading
  }
} else { // the player is out of ammo
 Display ("I'm out of Clips!");
  }  


Change to Handgun, Machine gun, Shotgun:
Code: ags
SetGlobalInt (21, 0); 

Code: ags
SetGlobalInt (21, 1); 

Code: ags
SetGlobalInt (21, 2); 

Ponch

Looks pretty cool.

Nah, I haven't updated to 2.7. After I finish the game I'm working on, I'll upgrade to the new engine. Until then, I'm sticking with the old workhorse I started with. I've put to much time into enemies hit points and spawning code and the guns each character carries to try and port it over to 2.7. I finally got it working under 6. Don't want to jinx anything!

I like your idea of a shotgun. All the Barn Runner guns are ray guns, so it really comes down to the little ray gun, the big ray gun, and the really big ray gun with three barrels. So I never worked with anything more complicated that burst fire and three shots in the air at once.

I'm curious to see where you go with this code. Are you working it into a game?

- Ponch

Dr Lecter

Yeah, I'm planning to make a "Indiana Jones and the Fate of Atlantis" style game, where each section you have the choice to do either with violence, wits, or teamwork. Obviously not EXACTLY like that, but loosely based on that idea. I've written about 25% of the puzzles so far, the coding is underway but I have barely any graphics at all.

- If I had enough graphics I would have posted it in the Game Development section

I did handgun, machine and shotgun for reasons that they pretty much encompass the kinds of weapons most people will be using. Most of the time the only thing that will change will be how accurate, and damage dealt, and with a little moding to the code its not hard. I've just added in the recoil function, which isn't too far from my other code for he location of bullet holes.

Right now I'm thinking of adding bullets in gun and remaining, aswell as health bars for enemies and player... but I think I'll get on the rest of the game before making it more user friendly.

SMF spam blocked by CleanTalk