Jump to content
  • Advertisement
  • entries
  • comments
  • views

A C64 game - Step 9

Sign in to follow this  


What are enemies if they just sit put and don't move at all? Therefore we now add the sub routine ObjectControl. ObjectControl loops through all objects (even the player) and jumps to the behaviour function depending on the object type. This incurs that the behaviour is tied to the object type. We provide a table with function pointers to every object's behaviour code (including the player).


ObjectControl takes the object type as index into the table and jumps to the target address. For now we have two enemy types, dumb moving up/down or left/right. For moving we reuse the previously created functions we already use for the player, namely ObjectMoveLeft/ObjectMoveRight etc.

Loop over all active objects and jump at their behaviour code. Note that we apply a nasty trick. Since jsr doesn't allow for indirect jumps we manually push the return address on the stack and then call indirect jmp. This allows for the behaviour code to return with rts.

;Enemy Behaviour
!zone ObjectControl
          ldx #0
          ldy SPRITE_ACTIVE,x
          beq .NextObject
          ;enemy is active
          sta ZEROPAGE_POINTER_2
          sta ZEROPAGE_POINTER_2 + 1
          ;set up return address for rts
          lda #>( .NextObject - 1 )
          lda #<( .NextObject - 1 )
          jmp (ZEROPAGE_POINTER_2)
          cpx #8
          bne .ObjectLoop


The main game loop is now changed; removed the call of PlayerControl and added the call to ObjectControl:

;the main game loop
          jsr WaitFrame
          jsr ObjectControl
          jmp GameLoop


The behaviour table is built from the behaviour code addresses. Actually we use two tables for high and low byte, this way we don't have to mess with the index. The < and > operators return the low and high byte of a 16 bit value.

          !byte <PlayerControl
          !byte <BehaviourDumbEnemyLR
          !byte <BehaviourDumbEnemyUD          
          !byte >PlayerControl
          !byte >BehaviourDumbEnemyLR
          !byte >BehaviourDumbEnemyUD


Previous Step 8 Next Step 10

Sign in to follow this  


Recommended Comments

The way these indirect jumps have always been done, is by simply modifying an existing JSR instruction:


lda TABLE_LO, y
sta .JumpPoint+1
lda TABLE_HI, y
sta .JumpPoint+2

.JumpPoint jsr $1234

Share this comment

Link to comment
Ah, neat! I never did assembly on the C64 back then, just started out 2007.

Always good to learn something new ;)

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!