Advertisement
vladikcomper

Sonic 1 - Homing Attack Code

Nov 29th, 2012
1,657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ===========================================================================
  2. ; ---------------------------------------------------------------------------
  3. ; Subroutine to perform Homing Attack
  4. ; ---------------------------------------------------------------------------
  5.  
  6. Sonic_HomingAttack:      
  7.         cmpi.b  #2,($FFFFFFEB).w    ; already Homing?
  8.         beq.s   @Homing_Process     ; if yes, branch
  9.         tpress  A+B+C,(SonicControl)
  10.         bne.s   @Homing_Perform
  11.         rts
  12.  
  13. ; ---------------------------------------------------------------------------
  14. @Homing_Process:
  15.         subq.b  #1,$1F(a0)      ; is timer over?
  16.         bne.s   @SetSpeeds      ; if not, branch
  17.         clr.b   ($FFFFFFEB).w
  18.         rts
  19.  
  20.     @SetSpeeds:
  21.         movea.w $3A(a0),a1      ; load object to follow
  22.         move.w  $C(a1),d1
  23.         sub.w   $C(a0),d1       ; d1 -> Ydist
  24.         move.w  8(a1),d2
  25.         sub.w   8(a0),d2        ; d2 -> Xdist
  26.                 jsr     (CalcAngle).l       ; Calcuate Angle
  27.                 jsr     (CalcSine).l        ; Get Sine
  28.                 move.w  #$1000,d3       ; d3 -> Homing Attack Speed
  29.         muls.w  d3,d0
  30.         muls.w  d3,d1
  31.         asr.l   #8,d0
  32.         asr.l   #8,d1
  33.                 move.w  d1,$12(a0)      ; set speed
  34.                 move.w  d0,$10(a0)      ; set speed
  35.         move.w  d0,$14(a0)
  36.         rts
  37.  
  38. ; ---------------------------------------------------------------------------
  39. @Homing_Perform:
  40.         moveq   #-1,d3          ; d3 will contain the direct distance for current d4,d5 (Piphagor yay)
  41.         moveq   #-1,d4          ; d4 will contain the shortest X-dist
  42.         moveq   #-1,d5          ; d5 will contain the shortest Y-dist
  43.         move.w  #$5F,d6
  44.         lea ($FFFFD800).w,a1    ; Load in-level objects RAM
  45.         move.w  #$5F,d6         ; do $60 objects
  46.        
  47.     @ObjectsLoop:                  
  48.         tst.b   1(a1)           ; is object on-screen?
  49.         bpl.s   @NextObject     ; if yes, branch
  50.         tst.b   $20(a1)         ; load touch response
  51.         bne.s   @Homing_TestObject  ; if touch response is not zero, branch
  52.  
  53.     @NextObject:
  54.         lea $40(a1),a1      ; load next object slot
  55.         dbf d6,@ObjectsLoop     ; repeat for all objects in RAM
  56.        
  57.         cmpi.l  #$A0*$A0,d3     ; if Distance ^ 2 > $A0 ^ 2, quit
  58.         bhi.w   Sonic_JumpDash
  59.         move.b  #60,$1F(a0)     ; follow object for 60 frames
  60.         move.b  #2,($FFFFFFEB).w    ; set flag as #2 (Homming Attack)
  61.         move.w  #$BC,d0         ; play JD sound
  62.         jmp (PlaySound_Special).l   ;
  63.  
  64. ; ---------------------------------------------------------------------------
  65. @Homing_TestObject:
  66.         lea @Homing_ObjectList(pc),a2
  67.         move.b  (a1),d0
  68.        
  69.     @CheckID:
  70.         cmp.b   (a2)+,d0        ; compare current obj ID to one in list
  71.         beq.s   @CalcXDist      ; if object is in list, branch
  72.         bhi.s   @CheckID        ; if current obj ID is higer, go on looking
  73.         bra.s   @NextObject     ; the object is not in list, quit
  74.        
  75.     @CalcXDist:
  76.         move.w  8(a1),d1        ; d1 -> ObjX
  77.         sub.w   8(a0),d1        ; d1 -> ObjX - SonX
  78.         bpl.s   @ObjAhead
  79.  
  80.     ; Object is behind Sonic
  81.         btst    #0,$22(a0)      ; is Sonic faced left?
  82.         beq.s   @NextObject     ; if not, we can't home at object
  83.         bra.s   @CalcYDist
  84.  
  85.     @ObjAhead:
  86.         btst    #0,$22(a0)      ; is Sonic faced right?
  87.         bne.s   @NextObject     ; if not, we can't home at object
  88.  
  89.     @CalcYDist:
  90.         move.w  $C(a1),d2       ; d2 -> ObjY
  91.         sub.w   $C(a0),d2       ; d2 -> ObjY - SonY
  92.        
  93.     ; Calculate absolute distance ( d1 * d1 + d2 * d2 )
  94.         move.w  d2,d0           ; d0 -> d2
  95.         swap    d0
  96.         move.w  d1,d0           ; d0 -> d2 d1
  97.         muls.w  d1,d1           ; d1 -> d1 * d1
  98.         muls.w  d2,d2           ; d2 -> d2 * d2
  99.         add.l   d1,d2           ; d2 -> d1 * d1 + d2 * d2
  100.         cmp.l   d3,d2           ; is new distance shorter after all?
  101.         bcc.w   @NextObject     ; if not, branch
  102.  
  103.         move.l  d2,d3           ; set new distance
  104.         move.w  d0,d4           ; set new Xdist
  105.         swap    d0
  106.         move.w  d0,d5           ; set new Ydist
  107.         move.w  a1,$3A(a0)      ; save obj ID
  108.         bra.w   @NextObject
  109.  
  110.  
  111. ; ---------------------------------------------------------------------------
  112. ; Object ids that are available for homming
  113. ; NOTE: They should go in increasing order (1,2,3..)
  114. ; ---------------------------------------------------------------------------
  115.  
  116. @Homing_ObjectList:
  117.         dc.b    $1E, $1F, $22, $26, $2B, $2C, $2D, $40, $42, $43
  118.         dc.b    $50, $55, $60, $78
  119.         dc.b    -1  ; mark end of list
  120.         even
  121.  
  122.  
  123. ; ===========================================================================
  124. ; ---------------------------------------------------------------------------
  125. ; Subroutine to perform Jump Dash
  126. ; ---------------------------------------------------------------------------
  127.  
  128. Sonic_JumpDash:
  129.         tst.b   ($FFFFFFEB).w       ; already Jump Dashing?
  130.         bne.s   @Return         ; if yes, branch
  131.         move.b  #1,($FFFFFFEB).w
  132.         move.w  #$A00,d0        ; set Jump Dash speed
  133.         btst    #6,$22(a0)      ; is Sonic underwater
  134.         beq.s   @ChkShoes
  135.         move.w  #$600,d0        ; set JD speed for underwater
  136.    
  137.     @ChkShoes:
  138.         tst.b   ($FFFFFE2E).w       ; does Sonic have speed shoes?
  139.         beq.s   @ChkOrientation     ; if not, branch
  140.         addi.w  #$200,d0
  141.  
  142.     @ChkOrientation:
  143.         btst    #0,$22(a0)
  144.         beq.s   @SetSpeeds
  145.         neg.w   d0
  146.  
  147.     @SetSpeeds:
  148.         move.w  d0,$10(a0)
  149.         move.w  d0,$14(a0)
  150.         move.w  #0,$12(a0)
  151.         move.w  #$BC,d0         ; play JD sound
  152.         jmp (PlaySound_Special).l   ;
  153.  
  154.     @Return:
  155.         rts
  156.  
  157. ; -------------------------------------------------------------------------
  158. ; Subroutine to stop Sonic, bounce him up and to give him the ability to
  159. ; Jumpdash again when he has performed a Jumpdash
  160. ; -------------------------------------------------------------------------
  161.  
  162. BounceJD:
  163.         cmp.b   #2,($FFFFFFEB).w; was HA flag set?
  164.         bne.s   BounceJD_End    ; if not, branch
  165.         clr.b   ($FFFFFFEB).w   ; if yes, clear HA flag (allow Sonic to HA again)
  166.         clr.w   $10(a0)     ; clear X-velocity (stop sonic)
  167.         move.w  #-$5F0,$12(a0)  ; move sonic upwards
  168.         btst    #6,$22(a0)  ; is sonic underwater?
  169.         beq.s   BounceJD_Shoes  ; if not, branch
  170.         move.w  #-$320,$12(a0)
  171.        
  172. BounceJD_Shoes:
  173.         tst.b   ($FFFFFE2E).w   ; does sonic has speed shoes?
  174.         beq.s   BounceJD_End    ; if not, branch
  175.         move.w  #-$620,$12(a0)  ; use -$620 for Y-velocity (move sonic upwards)
  176.        
  177. BounceJD_End:
  178.         rts         ; return
  179. ; End of function BounceJD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement