Advertisement
NovaYoshi

adventure player.s

Jun 5th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .proc RunPlayer
  2.   lda #4
  3.   sta PlayerHealth
  4.   jsr UpdateStatus
  5.  
  6.   jsr HandlePlayer
  7.   rts
  8. .endproc
  9.  
  10. NOVA_ACCEL_SPEED = 4
  11. NOVA_DEACCEL_SPEED = 8
  12.  
  13. .proc UpdateStatus
  14. HealthX = 0
  15. HealthCount = 1
  16.   ldy OamPtr
  17.   lda #15
  18.   sta HealthX
  19.   sta OAM_YPOS+(4*0),y
  20.   sta OAM_YPOS+(4*1),y
  21.   sta OAM_XPOS+(4*0),y
  22.   sta OAM_XPOS+(4*2),y
  23.   lda #15+8
  24.   sta OAM_YPOS+(4*2),y
  25.   sta OAM_YPOS+(4*3),y
  26.   sta OAM_XPOS+(4*1),y
  27.   sta OAM_XPOS+(4*3),y
  28.   lda #$4c
  29.   sta OAM_TILE+(4*0),y
  30.   lda #$4d
  31.   sta OAM_TILE+(4*2),y
  32.   lda #$4e
  33.   sta OAM_TILE+(4*1),y
  34.   lda #$4f
  35.   sta OAM_TILE+(4*3),y
  36.   lda #0
  37.   sta OAM_ATTR+(4*0),y
  38.   sta OAM_ATTR+(4*1),y
  39.   sta OAM_ATTR+(4*2),y
  40.   sta OAM_ATTR+(4*3),y
  41.   tya
  42.   add #4*4
  43.   tay
  44.  
  45. ; Draw all hearts needed
  46.   lda PlayerHealth
  47.   lsr
  48.   php
  49.   sta HealthCount
  50. Loop:
  51.   lda HealthCount
  52.   beq NoMoreHearts
  53.   lda #$4a
  54.   jsr MakeHeart
  55.   dec HealthCount
  56.   bne Loop
  57. NoMoreHearts:
  58.  
  59.   plp ; add a half heart if needed
  60.   bcc :+
  61.     lda #$4b
  62.     jsr MakeHeart
  63.   :
  64.  
  65.   sty OamPtr
  66.   rts
  67.  
  68. MakeHeart:
  69.   sta OAM_TILE,y
  70.   lda HealthX
  71.   sta OAM_XPOS,y
  72.   add #8
  73.   sta HealthX
  74.   lda #0
  75.   sta OAM_ATTR,y
  76.   lda #15+16
  77.   sta OAM_YPOS,y
  78.   iny
  79.   iny
  80.   iny
  81.   iny
  82.   rts
  83. .endproc
  84.  
  85. .proc HandlePlayer
  86.   lda #0
  87.   sta PlayerOnGround
  88.  
  89.   lda PlayerVYH
  90.   bmi GravityAddOK
  91.   lda PlayerVYL
  92.   cmp #$60
  93.   bcs SkipGravity
  94. GravityAddOK:
  95.   lda PlayerVYL
  96.   add #4
  97.   sta PlayerVYL
  98.   bcc SkipGravity
  99.     inc PlayerVYH
  100. SkipGravity:
  101.  
  102.   ; apply gravity
  103.   lda PlayerPYL
  104.   add PlayerVYL
  105.   sta PlayerPYL
  106.   lda PlayerPYH
  107.   adc PlayerVYH
  108.   and #$0f
  109.   sta PlayerPYH
  110.  
  111.   ; detect if hitting anything from below
  112.   lda PlayerPYH
  113.   tay
  114.   ldx PlayerDir
  115. ;  lda FeetXOffsetForDir,x   ; left side
  116.   lda #0
  117.   pha
  118.   add PlayerPXL
  119.   lda PlayerPXH
  120.   adc #0
  121.   jsr GetLevelColumnPtr
  122.   sta 0
  123.   pla                      ; right side
  124.   add #$70
  125.   add PlayerPXL
  126.   lda PlayerPXH
  127.   adc #0
  128.   jsr GetLevelColumnPtr
  129.   sta 1
  130.  
  131.   ldx 0
  132.   lda MetatileFlags,x
  133.   and #M_SOLID_ALL
  134.   sta 2
  135.   ldx 1
  136.   lda MetatileFlags,x
  137.   and #M_SOLID_ALL
  138.   ora 2
  139.   beq NotHittingBelow
  140. HittingBelow:
  141.   lda #0
  142.   sta PlayerVYH
  143.   sta PlayerVYL
  144. NotHittingBelow:
  145.  
  146.   ; detect if standing on anything solid
  147.   lda PlayerVYH
  148.   bmi NotStandingOnSolid
  149.   lda #0
  150.   sta PlayerJumping
  151.   lda PlayerPYL
  152.   add #<(24*16)
  153.   lda PlayerPYH
  154.   adc #>(24*16)
  155.   tay
  156.   ldx PlayerDir
  157.   lda #0 ; left side
  158.   pha
  159.   add PlayerPXL
  160.   lda PlayerPXH
  161.   adc #0
  162.   jsr GetLevelColumnPtr
  163.   sta 0
  164.  
  165.   pla                      ; right side
  166.   add #$70
  167.   add PlayerPXL
  168.   lda PlayerPXH
  169.   adc #0
  170.   jsr GetLevelColumnPtr
  171.   sta 1
  172.  
  173.   ldx 0
  174.   lda MetatileFlags,x
  175.   and #M_SOLID_TOP
  176.   sta 2
  177.   ldx 1
  178.   lda MetatileFlags,x
  179.   and #M_SOLID_TOP
  180.   ora 2
  181.   beq NotStandingOnSolid
  182. StandingOnSolid:
  183.   inc PlayerOnGround
  184.  
  185.   lda #0
  186.   sta PlayerVYH
  187.   sta PlayerVYL
  188.   lda #$80
  189.   sta PlayerPYL
  190.  
  191.   lda keylast
  192.   and #KEY_A
  193.   bne :+
  194.   lda keydown
  195.   and #KEY_A
  196.   beq :+
  197.     lda #<(-$50)
  198.     sta PlayerVYL
  199.     lda #>(-$50)
  200.     sta PlayerVYH
  201.     inc PlayerJumping
  202.   :
  203. NotStandingOnSolid:
  204.  
  205.   lda PlayerVYH
  206.   bpl :+
  207.     lda keydown        ; cancel a jump (doesn't appear to work?)
  208.     and #KEY_A
  209.     bne :+
  210.       lda PlayerJumpCancelLock
  211.       bne :+
  212.         lda #0
  213.         sta PlayerVYH
  214.         sta PlayerVYL
  215.   :
  216.  
  217.   ; handle left and right
  218.   lda keydown
  219.   and #KEY_LEFT
  220.   beq NotLeft
  221. ;    lda PlayerDir
  222. ;    bne :+
  223.       lda #1
  224.       sta PlayerDir
  225. ;      lda PlayerPXL
  226. ;      add #8<<4
  227. ;      sta PlayerPXL
  228. ;      bcc :+
  229. ;        inc PlayerPXH
  230. ;    :
  231.  
  232.     lda PlayerVXL
  233.     cmp #-64
  234.     beq NotLeft
  235.  
  236.     lda PlayerVXL
  237.     bne :+
  238.        dec PlayerVXH
  239.   : sub #NOVA_ACCEL_SPEED
  240.     sta PlayerVXL
  241. NotLeft:
  242.  
  243.   lda keydown
  244.   and #KEY_RIGHT
  245.   beq NotRight
  246. ;    lda PlayerDir
  247. ;    beq :+
  248.       lda #0
  249.       sta PlayerDir
  250. ;      lda PlayerPXL
  251. ;      sub #8<<4
  252. ;      sta PlayerPXL
  253. ;      bcs :+
  254. ;        dec PlayerPXH
  255. ;    :
  256.  
  257.     lda PlayerVXL
  258.     cmp #64
  259.     beq NotRight
  260.  
  261.     lda PlayerVXL
  262.     add #NOVA_ACCEL_SPEED
  263.     sta PlayerVXL
  264.     bne NotRight
  265.       inc PlayerVXH
  266. NotRight:
  267.  
  268.   lda keydown
  269.   and #KEY_LEFT | KEY_RIGHT
  270.   bne IsMoving
  271.     lda PlayerVXL
  272.     ora PlayerVXH
  273.     beq IsMoving
  274.       lda PlayerVXH ; if negative, make positive
  275.       and #128
  276.       sta 0
  277.       beq :+
  278.         neg16 PlayerVXL, PlayerVXH
  279.       :
  280.  
  281.       lda PlayerVXL
  282.       sub #NOVA_DEACCEL_SPEED
  283.       sta PlayerVXL
  284.       bcs @NotCarry
  285.         dec PlayerVXH
  286.         bpl @NotCarry
  287.           lda #0
  288.           sta PlayerVXH
  289.           sta PlayerVXL
  290.   @NotCarry:
  291.  
  292.       lda 0   ; if it was negative make negative again
  293.       beq :+
  294.         neg16 PlayerVXL, PlayerVXH
  295.       :
  296. Stopped:
  297. IsMoving:
  298.  
  299.   ; apply speed without caring if it's gonna push us into a wall or not
  300.   lda PlayerPXL
  301.   add PlayerVXL
  302.   sta PlayerPXL
  303.   lda PlayerPXH
  304.   adc PlayerVXH
  305. ;  and #$0f       ; comment out when we put in scrolling
  306.   sta PlayerPXH
  307.  
  308.   ; now see if we pushed into a wall
  309.   lda PlayerPYL
  310.   add #<(23*16)
  311.   lda PlayerPYH
  312.   adc #>(23*16)
  313.   tay
  314.   lda PlayerVXH
  315.   cmp #$80
  316.   lda #0
  317.   rol ; negative bit is now bit 0
  318.   tax
  319.   sta 0
  320.   lda PlayerPXL
  321.   add HSpeedDirectionOffset,x
  322.   lda PlayerPXH
  323.   adc #0
  324.   jsr GetLevelColumnPtr
  325.   tax
  326.   lda MetatileFlags,x
  327.   and #M_SOLID_ALL
  328.   bne YesInWall
  329.   ; try by player's head
  330.   lda PlayerPYL
  331.   add #<(2*16)
  332.   lda PlayerPYH
  333.   adc #>(2*16)
  334.   tay
  335.   lda (LevelBlockPtr),y
  336.   tax
  337.   lda MetatileFlags,x
  338.   and #M_SOLID_ALL
  339.   bne YesInWall
  340.   beq NotInWall
  341. YesInWall:
  342.     lda #0
  343.     sta PlayerVXL
  344.     sta PlayerVXH
  345.     sta PlayerPXL
  346.     lda 0
  347.     bne :+
  348.       lda #$8f
  349.       sta PlayerPXL
  350.       bne NotInWall
  351.     :
  352.     lda 0
  353.     beq :+
  354.       inc PlayerPXH
  355.     :
  356. NotInWall:
  357.  
  358.   rts
  359.  
  360. HSpeedDirectionOffset:
  361.   .byt $8f, 0
  362. .endproc
  363.  
  364. .proc DisplayPlayer
  365. DrawX = 0
  366. DrawY = 1
  367. Attrib = 2
  368.   RealPosToScreenPos PlayerPXL, PlayerPXH, ScrollX, DrawX
  369.   RealPosToScreenPos PlayerPYL, PlayerPYH, ScrollY, DrawY
  370.  
  371.   lda PlayerDir
  372.   bne :+
  373.     lda DrawX
  374.     sub #8
  375.     sta DrawX
  376.   :
  377.  
  378.   lda #$00
  379.   sta PlayerTiles+0
  380.   lda #$01
  381.   sta PlayerTiles+1
  382.   lda #$02
  383.   sta PlayerTiles+2
  384.   lda #$03
  385.   sta PlayerTiles+3
  386.   lda #$04
  387.   sta PlayerTiles+4
  388.   lda #$05
  389.   sta PlayerTiles+5
  390.  
  391.   lda PlayerJumping
  392.   beq :+
  393.     lda #$0c
  394.     sta PlayerTiles+3
  395.     lda #$08
  396.     sta PlayerTiles+4
  397.     lda #$09
  398.     sta PlayerTiles+5
  399.     jmp NoSpecialAnimation
  400.   :
  401.  
  402.   lda PlayerOnGround
  403.   bne :+
  404.     lda #$0d
  405.     sta PlayerTiles+2
  406.     lda #$0e
  407.     sta PlayerTiles+3
  408.     lda #$0a
  409.     sta PlayerTiles+4
  410.     lda #$0b
  411.     sta PlayerTiles+5
  412.     jmp NoSpecialAnimation
  413.   :
  414.  
  415.   ; animate walking
  416.   lda PlayerVXL
  417.   ora PlayerVXH
  418.   beq :+
  419.     lda retraces
  420.     and #%100
  421.     beq :+
  422.       lda #$06
  423.       sta PlayerTiles+4
  424.       lda #$07
  425.       sta PlayerTiles+5
  426.   :
  427. NoSpecialAnimation:
  428.  
  429.   lda #0
  430.   sta Attrib
  431.  
  432.   ; flip horizontally
  433.   lda PlayerDir
  434.   beq :+
  435.     lda #OAM_XFLIP
  436.     sta Attrib
  437.     swapy PlayerTiles+0, PlayerTiles+1
  438.     swapy PlayerTiles+2, PlayerTiles+3
  439.     swapy PlayerTiles+4, PlayerTiles+5
  440.   :
  441.  
  442.   ldx OamPtr
  443.   ldy #0     ; current sprite
  444. PutSprite:
  445.   lda PlayerTiles,y
  446.   sta OAM_TILE,x
  447.  
  448.   lda DrawX
  449.   add XPosList,y
  450.   sta OAM_XPOS,x
  451.  
  452.   lda DrawY
  453.   add YPosList,y
  454.   sta OAM_YPOS,x
  455.  
  456.   lda Attrib
  457.   sta OAM_ATTR,x
  458.   inx
  459.   inx
  460.   inx
  461.   inx
  462.   iny
  463.   cpy #6
  464.   bne PutSprite
  465.   stx OamPtr
  466.   rts
  467.  
  468. XPosList: .byt 0, 8, 0, 8, 0, 8
  469. YPosList: .byt 0, 0, 8, 8, 16, 16
  470. .endproc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement