Advertisement
Guest User

Main loop

a guest
Dec 13th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. OverworldLoop:
  3.     rst wait_vblank
  4.  
  5.     ; Reset "single-frame" vars
  6.     xor a
  7.     ld [wCurStateFirstFrame], a
  8.  
  9.     ; Change state if needed
  10.     ld a, [wNextState]
  11.     and a
  12.     jr z, .keepCurrentState
  13.     ld a, [wOverworldState]
  14.     ld [wPreviousState], a
  15.     ld a, [wNextState]
  16.     ld [wOverworldState], a
  17.     xor a
  18.     ld [wNextState], a
  19.     inc a ; ld a, 1
  20.     ld [wCurStateFirstFrame], a
  21. .keepCurrentState
  22.  
  23.     ; Begin by retrieving info from the current state
  24.     ld a, BANK(OverworldStatePtrs)
  25.     rst bankswitch
  26.     ld a, [wOverworldState]
  27.     add a, a
  28.     add a, LOW(OverworldStatePtrs)
  29.     ld l, a
  30.     adc a, HIGH(OverworldStatePtrs)
  31.     sub l
  32.     ld h, a
  33.     ld a, [hli]
  34.     ld h, [hl]
  35.     ld l, a
  36.     ; hl points to current state struct
  37.  
  38.     ld a, [hli]
  39.     ld e, a
  40.     ld a, [hli]
  41.     ld d, a
  42.     or e
  43.     jr z, .noFunction
  44.     push hl
  45.     call CallDE
  46.     pop hl
  47. .noFunction
  48.  
  49.     ; BUTTON PROCESSING
  50.  
  51.     ld a, [wNextState]
  52.     and a
  53.     jr nz, .skipButtons ; Skip any button operations if we need to change states
  54.     ld a, [hli] ; Get button mask
  55.     ld c, a
  56.     ldh a, [hHeldButtons]
  57.     and c
  58.     ld c, a
  59.     ldh a, [hPressedButtons]
  60.     or c
  61.     ld c, a
  62.     ld b, 8
  63. .checkButton
  64.     rl c
  65.     ld a, [hli]
  66.     ld e, a
  67.     ld a, [hli]
  68.     jr nc, .skipButton ; Skip button if not selected
  69.     ld d, a
  70.     or e
  71.     jr z, .skipButton
  72.     push hl
  73.     push bc
  74.     call CallDE
  75.     pop bc
  76.     pop hl
  77. .skipButton
  78.     dec b
  79.     jr nz, .checkButton
  80. .skipButtons
  81.  
  82.  
  83. OverworldUpdate:
  84.     ld a, [wDoOverworldUpdates]
  85.     and a
  86.     jp z, OverworldLoop
  87.  
  88.     ; Redraw NPCs
  89.     ld hl, wPlayer
  90.     ld de, wShadowOAM
  91.     ; (SNIP...)
  92.     ld a, d ; ld a, HIGH(wShadowOAM)
  93.     ldh [hOAMBufferHigh], a
  94.  
  95.     ld a, e
  96.     cp $A0
  97.     jr nc, .OAMFull
  98. .clearOAM
  99.     xor a
  100.     ld [de], a
  101.     ld a, e
  102.     add a, 4
  103.     ld e, a
  104.     cp $A0
  105.     jr c, .clearOAM
  106. .OAMFull
  107.  
  108.     ; Check the direction the camera moved, if at all
  109.     ; Ugly, but that's because we have to offset the camera's position, otherwise there are overflows when the camera gets close to the top/left edge
  110.     ld de, wCameraYPos
  111.     ld hl, wCameraPrevYPos
  112.     ; (SNIP...)
  113.     ldh [hCameraXMovementDirection], a
  114.  
  115.     ; Update BG position(s)
  116.     ld a, [wScrollingType]
  117.     add a, a
  118.     add a, LOW(.scrollingFuncs)
  119.     ld l, a
  120.     adc a, HIGH(.scrollingFuncs)
  121.     sub l
  122.     ld h, a
  123.     ld a, [hli]
  124.     ld h, [hl]
  125.     ld l, a
  126.     rst call_hl
  127.     jp OverworldLoop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement