; 'NES-test example'
;---------------------------------------------------------------------
; Implemented in ATALAN Programming language
; generated by Scratchalan
; [GENERATE_DATE]
;---------------------------------------------------------------------
; NROM rom layout:
; Max codesize : 16Kib
; Max gfxsize  : 8Kib
;---------------------------------------------------------------------

use nes
use nes_sprite
use nes_input
use nes_bg
use nes_mmc1

const [STAGE_NAME]SpritePal:sprPal = file "pal/spritesheet_[STAGE_NAME].pal"


[SPRITE_VAR_DECLARE]
[SPRITES_SET_POS]

out [STAGE_NAME]_sprXPos:array([SPR_ARRAY_SIZE]) of byte
out [STAGE_NAME]_sprYPos:array([SPR_ARRAY_SIZE]) of byte
out [STAGE_NAME]_sprAnim:array([SPR_ARRAY_SIZE]) of byte
out [STAGE_NAME]_sprAttr:array([SPR_ARRAY_SIZE]) of byte

out [STAGE_NAME]_sprite_start:0..255

[BG_VAR_DECLARE]

[STAGE_NAME]_sprHitCheck:proc(<sprNr1:byte <sprNr2:byte <xSize1:byte <ySize1:byte <xSize2:byte <ySize2:byte >result:byte) = 
  tmp1:0..255
  tmp2:0..255
  tmp3:0..255
  tmp4:0..255
  
  if [STAGE_NAME]_sprAttr(sprNr1) bitand 16 = 0
     goto no_hit

  if [STAGE_NAME]_sprAttr(sprNr2) bitand 16 = 0
     goto no_hit

  tmp4 = [STAGE_NAME]_sprYPos(sprNr2) + ySize2 
     
  if tmp4 < [STAGE_NAME]_sprYPos(sprNr1)
     goto no_hit
  
  tmp3 = [STAGE_NAME]_sprYPos(sprNr1) + ySize1
  
  if [STAGE_NAME]_sprYPos(sprNr2) > tmp3
      goto no_hit
  
  tmp1=[STAGE_NAME]_sprXPos(sprNr1) + xSize1
  tmp2=[STAGE_NAME]_sprXPos(sprNr2) + xSize2
  
  if tmp2 > [STAGE_NAME]_sprXPos(sprNr1) and tmp2 < tmp1 or [STAGE_NAME]_sprXPos(sprNr2) < tmp1 and [STAGE_NAME]_sprXPos(sprNr2) > [STAGE_NAME]_sprXPos(sprNr1) 
     result = 1
     goto sprHit    

  no_hit@
  result = 0

  sprHit@

[MESSAGE_HANDLER]

[STAGE_NAME]_init:proc =
 
  [SPRITES_WORKAROUND]
  ; Set Spr CHR Bank
  setMMC1Chr1 [SPR_CHR_BANK_NR]
  ;load sprite pal
  waitvbl
  sprLoadPal [STAGE_NAME]SpritePal
  waitvbl
  bgLoadPal  [STAGE_NAME]SpritePal
  windowIndex = 0 
  ; stage init code
  [STAGE_INIT]
  ; sprites init code
  for i:0..[SPR_ARRAY_SIZE]
      [STAGE_NAME]_sprAttr(i) = 16
  for i:0..[SPR_ARRAY_SIZE]
      [STAGE_NAME]_sprAnim(i) = 0
  [SPRITE_INIT]
  screen_state= 1
  [STAGE_NAME]_sprite_start= 0


[STAGE_NAME]_update:proc=
  if screen_state= 0
    [STAGE_NAME]_init
  else
    ; update code for sprites
    [SPRITE_UPDATE]
    [STAGE_UPDATE]
    [SPRITE_NO_KEY_EVENT_PLAYER1]
    [SPRITE_LEFT_KEY_EVENT_PLAYER1]
    [SPRITE_RIGHT_KEY_EVENT_PLAYER1]
    [SPRITE_UP_KEY_EVENT_PLAYER1]
    [SPRITE_DOWN_KEY_EVENT_PLAYER1]
    [SPRITE_START_KEY_EVENT_PLAYER1]
    [SPRITE_SELECT_KEY_EVENT_PLAYER1]
    [SPRITE_A_KEY_EVENT_PLAYER1]

    [SPRITE_B_KEY_EVENT_PLAYER2]
    [SPRITE_NO_KEY_EVENT_PLAYER2]
    [SPRITE_LEFT_KEY_EVENT_PLAYER2]
    [SPRITE_RIGHT_KEY_EVENT_PLAYER2]
    [SPRITE_UP_KEY_EVENT_PLAYER2]
    [SPRITE_DOWN_KEY_EVENT_PLAYER2]
    [SPRITE_START_KEY_EVENT_PLAYER2]
    [SPRITE_SELECT_KEY_EVENT_PLAYER2]
    [SPRITE_A_KEY_EVENT_PLAYER2]
    [SPRITE_B_KEY_EVENT_PLAYER2]

    [STAGE_NO_KEY_EVENT_PLAYER1]
    [STAGE_ANY_KEY_EVENT_PLAYER1]
    [STAGE_LEFT_KEY_EVENT_PLAYER1]
    [STAGE_RIGHT_KEY_EVENT_PLAYER1]
    [STAGE_UP_KEY_EVENT_PLAYER1]
    [STAGE_DOWN_KEY_EVENT_PLAYER1]
    [STAGE_START_KEY_EVENT_PLAYER1]
    [STAGE_SELECT_KEY_EVENT_PLAYER1]
    [STAGE_A_KEY_EVENT_PLAYER1]
    [STAGE_B_KEY_EVENT_PLAYER1]

    [STAGE_NO_KEY_EVENT_PLAYER2]
    [STAGE_ANY_KEY_EVENT_PLAYER2]
    [STAGE_LEFT_KEY_EVENT_PLAYER2]
    [STAGE_RIGHT_KEY_EVENT_PLAYER2]
    [STAGE_UP_KEY_EVENT_PLAYER2]
    [STAGE_DOWN_KEY_EVENT_PLAYER2]
    [STAGE_START_KEY_EVENT_PLAYER2]
    [STAGE_SELECT_KEY_EVENT_PLAYER2]
    [STAGE_A_KEY_EVENT_PLAYER2]
    [STAGE_B_KEY_EVENT_PLAYER2]

    [SPRITE_DRAW]
