Limited weapon and charge weapon pickup

Post Reply
User avatar
dale_coop
Posts: 594
Joined: Fri Feb 16, 2018 7:05 am

Limited weapon and charge weapon pickup

Post by dale_coop » Fri Jul 20, 2018 1:56 pm

For my game I wanted to have a weapon (like a laser gun) that need to be charged, and of course, each time you use it, it loses ammo (or energy).
For that I will use the variable "GLOBAL_Player1_Ammo". I share with you how I did it...

First I modified the "InitLoad.asm" script to initialize the variable

Code: Select all

;;; SET UP INITIAL VARIABLE VALUE HERE:
	LDA #$00
	STA GLOBAL_Player1_Ammo
Following the tutorial videos about HUD, I showed the player weapon ammo (with "Var tiles" for the variable "GLOBAL_Player1_Ammo")

Image

Added the code in the "InitLoads.asm" to set up the HUD hooks :

Code: Select all

	LDA #HUD_ELEMENT_4
	STA HUD_updateAmmo
Then, I will add some routines in the "Usersubroutines.asm" script, to decrease the Ammo and to update the HUD:

Code: Select all

decreasePlayerAmmo:	
	LDX GLOBAL_Player1_Ammo
	DEX
	TXA
	STA GLOBAL_Player1_Ammo
	JSR updateHUDPlayerAmmo:	
	RTS
updateHUDPlayerAmmo:	
	STA hudElementTilesToLoad
	LDA #$00
	STA hudElementTilesMax	
	LDA DrawHudBytes
	ORA HUD_updateAmmo
	STA DrawHudBytes
	RTS
And I will modify the "CreateSimpleProjectileWithMotion.asm" script to use and test the variable GLOBAL_Player1_Ammo. If it's different than 0, the script can continue creating the projectile:

Code: Select all

CreateSimpleProjectileWithMotion:
    LDY player1_object
    CPY #$FF ;; if the player object is set to ff, he's dead.
    BNE playerCanCreateProjectile
    RTS
 playerCanCreateProjectile:
	LDA GLOBAL_Player1_Ammo
	BNE continueCreateProjectile
	RTS
continueCreateProjectile:
    ;; get offset
    ;; in a later version, we will use user defined offsets.
    ;; for now, we'll place projectile creation at center of object
    LDA Object_movement,y
    AND #%00000111
    TAX
    LDA weaponOffsetTableX,x
    ;;; now we have the offset
     CLC
     ADC Object_x_hi,y
    STA temp1
    LDA weaponOffsetTableY,x
    CLC
    ADC Object_y_hi,y
    STA temp2
    CreateObject temp1, temp2, #$01, #$00  ;;testVar
    LDA Object_movement,y
    AND #%00000111
    STA temp
    TAY
    LDA DirectionMovementTable,y
    ORA temp
    
    STA Object_movement,x
    
    ;; add object size offset.
    LDA Object_x_hi,x
    SEC
    SBC #$08 ;; half of the width of the intended projectile
    STA Object_x_hi,x
    LDA Object_y_hi,x
    SEC
    SBC #$08 ;; half of the height of the intended projectile
    STA Object_y_hi,x
    
    PlaySound #$00, #$00
    JMP decreasePlayerAmmo
    RTS

Now I create a pickup object to be my "ammo" collectable item. And to be more useful, I will use its "Health" value as my "Ammo recharge" value. Something like this:

Image

So, what I want is when my player takes the ammo pickup, his weapon is fully charged (6 ammo).

Like I wrote in another post, I found a way to have collectable object that doesn't trigger the screen, I will use that technique for my "Ammo collectable" object.
I modified the "HandleObjectCollisions.asm" script, when the player collect the collectable pickup, if its "health" is different than 0, so I charge the Ammo variable and update the HUD, else the script continue as usual:

Code: Select all

otherIsNotAMonsterTypeCollision:
	LDA ObjectFlags,y
	AND #%00100000 ;; is it a 'collectable'?
	BEQ otherIsNotAcollectable
	;;;; IS A pickup / power up
	DeactivateCurrentObject ;; makes the other object go away
							;; since other object is loaded in X
;;=========== WHAT DO YOU WANT TO HAVE HAPPEN WHEN YOU COLLECT THIS ITEM?
;;CHARGE WEAPON:
	LDA Object_health,x  ;; read the "health" of the collectable/pickup
	BEQ continueOtherIsNotAMonsterTypeCollision  ;; if it's NOT a Ammo pickup (=if health is 0)
	STA GLOBAL_Player1_Ammo  ;; it IS ammo, put in the var
	;;PlaySound #$00, #$00
	JMP updateHUDPlayerAmmo 
;;;;;;;;;;;;;;;;;;;;;;;

continueOtherIsNotAMonsterTypeCollision: 
    ;;PlaySound #$01, #$00
    JSR countAllTargets
otherIsNotAcollectable:

Voilà, the "charge weapon" pickup should work, as expected.

ChargedWeapon.gif
ChargedWeapon.gif (45.95 KiB) Viewed 210 times
-----
Sorry about my poor english
All I need: A Damn Fine Cup of Coffee
Post Reply