[4.1] Adding Ammo system (charging weapon) for your projectiles in the Platformer module

dale_coop

Moderator
Staff member
Based on what I've already done in previous versions of NESMaker...
Here's how to add an Ammo system for your projectiles (or laser gun charge system... or whatever you want it to be) in your Platformer game.

[media]https://youtu.be/VHCnJ0-diys[/media]


1/ Add a new "User Constant" in the "Project settings", name it "PLAYER1_MAX_AMMO" with a value of "5".

2019-01-08-23-42-46-Param-tres-du-projet.png



2/ In the "HUD & Boxes > User Variables", rename the "UserVar_5" (or another unused one) to "myAmmo" with an Initial Value of "0":

2019-01-08-23-25-13.png


You can display it in your HUD elements...

2019-01-09-00-29-16-NES-MAKER-4-1-0-Version-0x158-Mon-Jeu-Plateforme-MST.png



3/ Make a new "b_create_projectile_usingAmmo.asm" script in the "GameEngineData\Routines\Basic\ModuleScripts\InputScripts" folder, containing:

Code:
	LDA gameHandler
	AND #%00100000	
	BEQ canShoot
	RTS
	
canShoot:
	LDX player1_object
	;;; IF YOU WANT TO USE AN SPECIFIC ANIMATION / ACTION STEP WHEN SHOOTING, comment out the following 3 lines: 
	;;; check if already shooting (assuming the shoot action step is 05)
	;GetCurrentActionType player1_object
	;CMP #$05
	;BNE notAlreadyShooting
	JMP checkIfCanShoot 
	
notAlreadyShooting:
	;;; IF YOU WANT TO USE AN SPECIFIC ANIMATION / ACTION STEP WHEN SHOOTING, comment out the following 1 line: 
	;;; if not already shooting, change it's action step (assuming the shoot action step is 05)
	;ChangeObjectState #$05, #$02
	
	;;; if you want your player stops when he's shooting, comment out the following lines:
	;LDA Object_movement,x
	;AND #%00001111
	;STA Object_movement,x
	;LDA #$00
	;STA Object_h_speed_hi,x
	;STA Object_h_speed_lo,x
	;STA Object_v_speed_hi,x
	;STA Object_v_speed_lo,x
	
;; if was already shooting
checkIfCanShoot:
	;; will check if the weapon is charged (if Ammo > 0) :
	LDA myAmmo
	BNE	continueShooting	;; if not equal to 0, we can shoot
	;;if no Ammo, we're done here.
	RTS
	
;; if enought Ammo we can continue shooting:
continueShooting:
	LDA Object_movement,x
	AND #%00000111
	STA temp2
	TAY

	LDA Object_x_hi,x
	SEC 
	SBC #$08 ;; width of projectile 
	STA temp
	LDA Object_scroll,x
	SBC #$00
	STA temp3

	LDA temp
	;;; offset x for creation
	CLC
	ADC projOffsetTableX,y
	STA temp
	LDA temp3
	ADC #$00
	STA temp3

	LDA Object_y_hi,x
	CLC
	ADC projOffsetTableY,y
	sec
	sbc #$08 ;; height of projectile
	STA temp1	


	CreateObject temp, temp1, #OBJECT_PLAYER_PROJECTILE, #$00, temp3

	;;;; x is now the newly created object's x.
	LDA Object_movement,x
	ORA temp2
	STA Object_movement,x
	LDY temp2
	LDA directionTable,y
	ORA Object_movement,x
	STA Object_movement,x

	;; start Updating Ammo
	DEC myAmmo
	LDA myAmmo
	;;; we also need to set up the routine to update the HUD
	;; for this to work right, health must be a "blank-then-draw" type element.
	STA hudElementTilesToLoad
	LDA #$00
	STA hudElementTilesMax
	UpdateHud HUD_myAmmo
	;; finished Updating Ammo

	;PlaySound #SND_SHOOT
	
doneShooting:
	RTS


4/ Make a new "PowerUp_ChargeAmmo.asm" script in the "GameEngineData\Routines\Basic\ModuleScripts\PowerUpCode" folder, containing:

Code:
;;; Charge the weapon / Ammo
;;; works with HUD variable HUD_myAmmo.

	LDA #PLAYER1_MAX_AMMO
	STA myAmmo

	;;; we also need to set up the routine to update the HUD
	;; for this to work right, health must be a "blank-then-draw" type element.
	STA hudElementTilesToLoad
	LDA #$00
	STA hudElementTilesMax
	UpdateHud HUD_myAmmo
	
	;PlaySound #SND_INCREASE_HEALTH	
skipGettingAmmo:
	RTS


5/ In the "Project Settings > Scripts Settings" assign the "PowerUp_ChargeAmmo.asm" script to the unused "PowerUp_01" (or another one if you prefer)

2019-01-09-00-14-02-Param-tres-du-projet.png



6/ In the "Scripts > Input Scripts", add the "b_create_projectile_usingAmmo.asm" script to your project.
7/ Assign it to your "Press" "B" button.

And voilĂ !
 

dale_coop

Moderator
Staff member
And if you want, in addition to that, you can add an automatic recharge (timer used):

shoot.gif

the myAmmo variable increases itself automatically when you don't use the weapon.

1/ Add a new "User Constant" in the "Project Settings", name it "MAX_AMMO_TIMER" with a value of "100" (but you can try another value you want... it's the time for recharge the ammo... best results: 50~200).

2/ Add a new "User Variable" in the "Project Settings", name it "chargeAmmoCycleTimer" with an initial value of "0".

3/ Make a new script "HandleChargeAmmoCycleTimer.asm" in the "GameEngineData\Routines\Basic\ModuleScripts\MainScripts" folder, with this code:

Code:
;;;; Routines for Ammo automatic charging (Timer) system :
startChargingAmmoCycleTimer:
	LDA #MAX_AMMO_TIMER		;; speed/cycle of the timer (ex: #$05)
	STA chargeAmmoCycleTimer
	RTS

updatechargeAmmoCycleTimer:
	;; check if timer is activated:
	LDA chargeAmmoCycleTimer
	BEQ endUpdatechargeAmmoCycleTimer	;; INACTIVE, no need to recharge	
	
	;; checking if already charged
	LDA myAmmo
	CMP #PLAYER1_MAX_AMMO
	BEQ endUpdatechargeAmmoCycleTimer	;; no need to recharge	
	;; so, we need to recharge
	
	;; check the timer value:
	LDA chargeAmmoCycleTimer
	CMP #$01
	BNE dontUpdatechargeAmmoCycleTimer	;; not yet, we continue timer
	
	;; it's time! What do we do:
	;;JSR ChargeAmmo
	INC myAmmo
	;; update the HUD
	LDA myAmmo	
	STA hudElementTilesToLoad	
	UpdateHud HUD_myAmmo
	
	;; check if we need to restart the timer:
	LDA myAmmo
	CMP #PLAYER1_MAX_AMMO
	BEQ dontUpdatechargeAmmoCycleTimer	;; no need to recharge	
	;; we restart the timer:
	JSR startChargingAmmoCycleTimer	
	JMP endUpdatechargeAmmoCycleTimer

dontUpdatechargeAmmoCycleTimer:
    DEC chargeAmmoCycleTimer
	
endUpdatechargeAmmoCycleTimer:
	RTS


4/ Now, make another new script "HandleGameTimer_usingAmmo.asm" script in the "GameEngineData\Routines\Basic\System" folder, with this code :

Code:
	.include SCR_HANDLE_AMMO_TIMER
HandleGameTimer:
	JSR updatechargeAmmoCycleTimer	;;dale_coop Routines for Ammo automatic charging (Timer) system 
	DEC gameTimer
	BNE dontUpdateGameTimer
	LDA gameTimerLo
	CLC
	ADC #$01
	STA gameTimerLo
	LDA gameTimerHi
	ADC #$00
	STA gameTimerHi
;;;;;;;;;;;;;;;;;;;;;;;;;;; 
	;; DO WHATEVER READS OF THE GAMETIMER YOU MIGHT WANT HERE.
	JSR DoAlarm
;;;;;;;;;;;;;;;;;;;;;;;;;;;
	LDA #DayNightSpeed
	STA gameTimer
dontUpdateGameTimer:
	RTS
	
DoAlarm:
	
	;;; we're going to edge-load monsters one at a time if they are of edge type and are not active.
	LDX #$00
doLoadMonsterOnTimerLoop:

	LDA edgeLoaderInCue
	BEQ noEdgeMonstersInCue
	LDA currentBank
	STA prevBank
	LDY #$1C ;; data bank
	JSR bankswitchY
	JSR CreateTimedEdgeSpawner
	DEC edgeLoaderInCue
	LDY prevBank
	JSR bankswitchY

noEdgeMonstersInCue
	RTS


5/ In your "Project Settings > Script settings", add a new Script Element (click on the "Add" button), named like that :

2019-01-10-00-21-54-Script-Define.png

And assign the script "HandleChargeAmmoCycleTimer.asm" we previously made (located in the "GameEngineData\Routines\Basic\ModuleScripts\MainScripts" folder).


6/ Assign the "HandleGameTimer_usingAmmo.asm" script previously made to the "Handle Game Timer" script element:

2019-01-10-00-31-40-Param-tres-du-projet.png



7/ Modify your "b_create_projectile_usingAmmo.asm" script, around the end of the script, from this:

Code:
	;PlaySound #SND_SHOOT
	
doneShooting:
	RTS

To, this:
Code:
	;PlaySound #SND_SHOOT
	JSR startChargingAmmoCycleTimer	;; starts the recharging system of myAmmo
	
doneShooting:
	RTS


Give it a try... and as usual, this script might be perfect... you are free to modify it, fix it, ... or not use it ;)
 
Wow thank you dale will try this out.

I do have a question how do i drop the item that does the instant charge into the map for testing ?

I am seeing where even though i have no initial ammo i can still shoot is there somewhere in the code that i may have done something wrong ?
 

dale_coop

Moderator
Staff member
Gilbertmaxter said:
I do have a question how do i drop the item that does the instant charge into the map for testing ?

At my 4/ you assign the powerUp script to the PowerUp item.
Then on your screen... just add the powerUp : do a right-click :
2019-01-09-16-56-46-Window.png

(as you can see, I used the PowerUp 01, means the Game Object just under GetSword, in your "Game Objects" hierarchy).

And In your "HUD & Boxes, in the "User Variables" tab, you need to myAmmo (with initial value "0"). And you need to assign the "b_create_projectile_usingAmmo.asm" to your "Press" "B" button.
If something that doesn't work, check again every step of my OM (original post). And tell me if you're still having an issue, and where.
 
Well the issues are as followed.

the hud is still not coming up right here is what it is for my health and ammo.

My health or lives this doesn't work with either one

49388087_10216375439556489_5515464901214076928_n.jpg


My ammo

49619227_10216375439276482_38611124630847488_n.jpg



now the odd thing is when I run the game for testing it doesn't show up correctly for lives. it does for ammo but I can still press B and fire a projectile.

and picking up a ammo power up charger doesn't refresh the hud for some odd reason. And when I die the ammo and health all light up which I know cannot be right.

I have looked at the scripts but cannot see where the error was happening.
 
I did a check and realized I was using the wrong asset that look similar to myAmmo can increase from 0 to 3 using number. however I am not able to get the number to decrease. for the ammo timer is the bigger the number the longer for it to recharge or is it shorter ?

do not know if why it isn't decreasing I have that part in the script.

Also even with the ammo pickup while the ammo does increase but shooting doesn't decrease my counter.


EDIT: I never added the darn input into the project -.-

now I am getting a error on label 93 of it which is startChargingAmmoCycleTimer

Alrighty got that going :)

now just need to work on the HUD Symbols for ammo done , now to get the hearts working lol :)
 

dale_coop

Moderator
Staff member
Ah ah, yeah, dealing with all those objects, and variables... you can quickly mess everything (even for me) :p
 
Yeah it is lol it took me 2 hours to just get the charge system right and have to say i like it more now thank you for your help i need to look now at how you got the three hearts to work.
 

dale_coop

Moderator
Staff member
Gilbertmaxter said:
...i need to look now at how you got the three hearts to work.

For the 3 hearts....
1/ In the "Hud & Boxes > User variables", I set the myHealth to a initial value of "3" (because my Player has a Health value "3" in its Object Details)
2 / In the "Hud & Boxes > User Elements", I set the element 4 to "0-var tiles" with a max value of "3", too.
3/ In the "Project Settings > Script settings", I Assign the "PowerUp_IncreaseHealth.asm" script to the PowerUp element corresponding to Game Object you used for the Health pickup. If you are not sure, share some screenshots I will help you ;)
4/ Place on your screen the game object :)
 
Now this is odd for me its when I suddenly moving the left and right pad made the gun shoot. is there something that may cause it to shoot ?
 

dale_coop

Moderator
Staff member
Gilbertmaxter said:
Now this is odd for me its when I suddenly moving the left and right pad made the gun shoot. is there something that may cause it to shoot ?

LOL, it shouldn't...
Check that your shooting script is correctly assigned in the "input Editor" (on "press" "b" button... ;) and not to "any" or assigned twice, ...)
And check that all your scripts in "Scripts > Input Scripts" finish all with the "RTS" opcode.
 
dale_coop said:
Gilbertmaxter said:
Now this is odd for me its when I suddenly moving the left and right pad made the gun shoot. is there something that may cause it to shoot ?

LOL, it shouldn't...
Check that your shooting script is correctly assigned in the "input Editor" (on "press" "b" button... ;) and not to "any" or assigned twice, ...)
And check that all your scripts in "Scripts > Input Scripts" finish all with the "RTS" opcode.


okay now.. this may be nuts but there may not be a problem with the code at all..

I was using musen and it was showing up all sorts of problems but when I used the emulator packaged with nesmaker. I do not see any errors.

nothing with the gun shooting when I move left or right nothing with the screen going wonky with me shooting the last monster. it was actually normal like what I see you run with dale...

could musen be bad and the emulator built in be better ?
 

dale_coop

Moderator
Staff member
Nope, the builtin emulator is very limited. Mesen and fceux (the one I use) have more functionalities (debugger,...).

You might have some error in the code somewhere that causes the glitches with Mesen.
I will do some tests with Mesen too and try to fix... If I can reproduce and find.
 

dale_coop

Moderator
Staff member
Gilbertmaxter said:
Say dale looking at your melee script. Could i set up the melee script to work when you have no ammo ?

You mean when you have Ammo, pressing B button, it shoots... and when no ammo, pressing B button, it's the weapon..?

Yeah, it should be doable, you could try to add at the beginning of the "b_create_melee_weapon.asm" script, those lines:
Code:
	;; First, will check if the weapon is charged (if Ammo > 0) :
	LDA myAmmo
	BEQ continueCreatingMeleeWeapon	;; if equal to 0, we can create the melee weapon
	;;if still Ammo, the shoot projectile will be used, so we are done here
	RTS
continueCreatingMeleeWeapon:
 
Sweet followed the guide and it seems to work I need to work on the timer as it refresh's the timer to fast, should I increase the timer its set to 100 to make the refresh longer or is it shorter for a longer delay?

also it will still fire the gun and sword when the ammo is at 1 is there another option besides 0 to check for ammo as it does the ammo and then the sword.
 

dale_coop

Moderator
Staff member
You should increase the MAX_AMMO_TIMER constant if you want the timer to be longer. Try 150 or 200...
 

dale_coop

Moderator
Staff member
Gilbertmaxter said:
also it will still fire the gun and sword when the ammo is at 1 is there another option besides 0 to check for ammo as it does the ammo and then the sword.

Yep, here? http://nesmakers.com/viewtopic.php?p=11155#p11155
 
Top Bottom