Advertisement
NovaYoshi

pingas

Oct 31st, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main:
  2.   nop
  3.   di                ;disable interrupts
  4.  
  5.   ld   sp, $dfff
  6.   call wait_vblank
  7.   xor a
  8.   ld [rLCDC], a
  9.  
  10. ; Set palettes and clear the scroll
  11.   ld a, %01101100
  12.   ldh [rBGP], a
  13.   ld a, %00011100
  14.   ldh [rOBP0], a
  15.   ld a, %01101100
  16.   ldh [rOBP1], a
  17.   xor a
  18.   ldh [rSCX], a
  19.   ldh [rSCY], a
  20.  
  21. ; Clear RAM
  22.   ld hl, WRAM_START
  23.   ld bc, 4096
  24.   call memclear
  25.  
  26. ; Copy in DMA routine
  27.   ld hl, oam_dma_routine
  28.   ld de, $ff80
  29.   ld bc, oam_dma_routine_end - oam_dma_routine
  30.   call memcpy
  31.  
  32. ; Upload graphics
  33.   ld hl, graphics
  34.   ld de, VRAM_LOW
  35.   ld bc, 8192
  36.   call memcpy
  37.  
  38. ; Initialize nametable
  39.   ld hl, SCREEN1
  40.   ld bc, 1024
  41.   ld e, " "
  42.   call memset
  43.  
  44. ; Upload graphics
  45.   ld hl, RobotnikText
  46.   ld de, SCREEN1+32*2+3
  47.   ld bc, 14
  48.   call memcpy
  49.   ld hl, LandText
  50.   ld de, SCREEN1+32*3+8
  51.   ld bc, 4
  52.   call memcpy
  53.  
  54.   ld a, LCDCF_ON|LCDCF_OBJ8|LCDCF_OBJON|LCDCF_BGON
  55.   ldh [rLCDC],a
  56.  
  57. ; Initialize variables
  58.   ld a, 30
  59.   ld [PlayerPX], a
  60.   ld [PlayerPYL], a
  61.   ld [PlayerPYH], a
  62.  
  63. forever:
  64.   call wait_vblank
  65.   ld a, $c0
  66.   call $ff80
  67.   call readkeys
  68.  
  69.   ; Clear OAM
  70.   ld hl, WRAM_START
  71.   ld bc, $a0
  72.   call memclear
  73.  
  74.   xor a
  75.   ld [PlayerOnGround], a
  76.  
  77.   ; Add velocity
  78.   lda h, [PlayerPYH]
  79.   lda l, [PlayerPYL]
  80.   lda d, [PlayerVYH]
  81.   lda e, [PlayerVYL]
  82.   add hl, de
  83.   lda [PlayerPYH], h
  84.   lda [PlayerPYL], l
  85.  
  86.   ; Add gravity
  87.   ld h, d
  88.   ld l, e
  89.   ld de, 16
  90.   add hl, de
  91.   lda [PlayerVYH], h
  92.   lda [PlayerVYL], l
  93.  
  94.   ld a, [PlayerPYH]
  95.   cp 144-16
  96.   jr c, .NoGround
  97.   xor a
  98.   ld [PlayerPYL], a
  99.   ld [PlayerVYH], a
  100.   ld [PlayerVYL], a
  101.   ld a, 144-16
  102.   ld [PlayerPYH], a
  103. .NoGround:
  104.  
  105.   ld a, [PlayerPYH]
  106.   cp 144-16
  107.   jr nz, .NoGround2
  108.   ld a, 1
  109.   ld [PlayerOnGround], a
  110. .NoGround2:
  111.  
  112.   ld hl, PlayerPX
  113.   ld a, [KeyDown]
  114.   and KEY_RIGHT
  115.   jr z, .noright
  116.   inc [hl]
  117.   xor a
  118.   ld [PlayerDir], a
  119. .noright:
  120.  
  121.   ld a, [KeyDown]
  122.   and KEY_LEFT
  123.   jr z, .noleft
  124.   dec [hl]
  125.   ld a, 1
  126.   ld [PlayerDir], a
  127. .noleft:
  128.  
  129.   ld a, [KeyNew]
  130.   and KEY_B
  131.   jr z, .nopingas
  132.   ld a, [PingasTimer]
  133.   or a
  134.   jr nz, .nopingas
  135.   ld a, 15
  136.   ld [PingasTimer], a
  137. .nopingas:
  138.  
  139.   ld a, [PlayerOnGround]
  140.   or a
  141.   jr z, .nojump
  142.   ld a, [KeyNew]
  143.   and KEY_A
  144.   jr z, .nojump
  145.   ld a, (-$200)&$ff
  146.   ld [PlayerVYL], a
  147.   ld a, ((-$200)>>8)&$ff
  148.   ld [PlayerVYH], a
  149. .nojump:
  150.  
  151. ; Draw the player
  152.   xor a
  153.   ld [oam_ptr], a
  154.   lda b, [PlayerPX]
  155.   lda c, [PlayerPYH]
  156.   ld hl, StandingFrame
  157.  
  158.   ld a, [KeyDown]
  159.   and KEY_LEFT+KEY_RIGHT
  160.   jr z, .notmoving
  161.   ld a, [retraces]
  162.   and %100
  163.   jr z, .notmoving
  164.   ld hl, WalkingFrame
  165. .notmoving:
  166.  
  167.   ld a, [PlayerOnGround]
  168.   or a
  169.   jr nz, .onground
  170.   ld hl, JumpingFrame
  171. .onground:
  172.  
  173.   ld a, [PingasTimer]
  174.   or a
  175.   jr z, .nopingas2
  176.   dec a
  177.   ld [PingasTimer],a
  178.   ld hl, PingasFrame1
  179.  
  180.   cp 8
  181.   jr nc, .nopingas2
  182.   ld hl, PingasFrame2
  183. .nopingas2:
  184.  
  185.   ld a, [PlayerDir]
  186.   or a
  187.   jr nz, .facingleft
  188.   call DrawMetasprite
  189.   jr .facingright
  190. .facingleft:
  191.   call DrawMetaspriteFlipped
  192. .facingright
  193.  
  194.   jp forever
  195.  
  196. DrawMetasprite:
  197.   ld d, $c0
  198.   lda e, [oam_ptr]
  199.  
  200. .loop:
  201.   push bc
  202.   ldi a, [hl]
  203.   cp 255
  204.   jr z, .done
  205.   add a, c
  206.   ld c, a
  207.   ld [de], a
  208.   inc de
  209.  
  210.   ldi a, [hl]
  211.   add a, b
  212.   ld b, a
  213.   ld [de], a
  214.   inc de
  215.  
  216.   ldi a, [hl]
  217.   ld [de], a
  218.   inc de
  219.  
  220.   xor a
  221.   ld [de], a
  222.   inc de
  223.  
  224.   pop bc
  225.   jp .loop
  226. .done:
  227.  
  228.   pop bc
  229.   ret
  230.  
  231. DrawMetaspriteFlipped:
  232.   ld d, $c0
  233.   lda e, [oam_ptr]
  234.  
  235. .loop:
  236.   push bc
  237.   ldi a, [hl]
  238.   cp 255
  239.   jr z, .done
  240.   add a, c
  241.   ld c, a
  242.   ld [de], a
  243.   inc de
  244.  
  245.   ldi a, [hl]
  246.   cpl
  247.   inc a
  248.   add a, b
  249.   ld b, a
  250.   ld [de], a
  251.   inc de
  252.  
  253.   ldi a, [hl]
  254.   ld [de], a
  255.   inc de
  256.  
  257.   ld a, OAM_XFLIP
  258.   ld [de], a
  259.   inc de
  260.  
  261.   pop bc
  262.   jp .loop
  263. .done:
  264.  
  265.   pop bc
  266.   ret
  267.  
  268.  
  269. vblank:
  270.   push hl
  271.   ld hl, retraces
  272.   inc [hl]
  273.   pop hl
  274.   reti
  275.  
  276. wait_vblank:
  277.   ld a, %00001
  278.   ld [$FFFF],a      ; enable vblank interrupt
  279.   ei
  280.  
  281.   ld   hl, retraces
  282.   ld   a, [hl]
  283. .loop:
  284.   halt
  285.   cp   a, [hl]
  286.   jr   z, .loop
  287.   ret
  288.  
  289. stat:
  290. timer:
  291. serial:
  292. joypad:
  293.   reti
  294.  
  295. memclear:
  296.   xor a
  297.   ldi [hl], a
  298.   dec bc
  299.   ld a,b
  300.   or c
  301.   jr nz, memclear
  302.   ret
  303.  
  304. memset:
  305.   ld [hl], e
  306.   inc hl
  307.   dec bc
  308.   ld a,b
  309.   or c
  310.   jr nz, memset
  311.   ret
  312.  
  313. memcpy:
  314.   ldi a, [hl]
  315.   ld [de], a
  316.   inc de
  317.   dec bc
  318.   ld a,b
  319.   or c
  320.   jr nz, memcpy
  321.   ret
  322.  
  323. oam_dma_routine:
  324.   ldh [rDMA],a
  325.   ld  a,$28
  326. .wait:
  327.   dec a
  328.   jr  nz,.wait
  329.   ret
  330. oam_dma_routine_end:
  331.  
  332. readkeys:
  333.   ld a, [KeyDown]
  334.   ld [KeyLast], a
  335.  
  336.   ld a, KEY_BUTTONS
  337.   ldh [rKEYS], a
  338.   ldh a, [rKEYS]
  339.   ldh a, [rKEYS]
  340.   ldh a, [rKEYS]
  341.   ldh a, [rKEYS]
  342.   ldh a, [rKEYS]
  343.   ldh a, [rKEYS]
  344.   ldh a, [rKEYS]
  345.   ldh a, [rKEYS]
  346.   and $f
  347.   ld b, a
  348.  
  349.   ld a, KEY_DIRECTION
  350.   ldh [rKEYS], a
  351.   ldh a, [rKEYS]
  352.   ldh a, [rKEYS]
  353.   ldh a, [rKEYS]
  354.   ldh a, [rKEYS]
  355.   ldh a, [rKEYS]
  356.   ldh a, [rKEYS]
  357.   ldh a, [rKEYS]
  358.   ldh a, [rKEYS]
  359.   and $f
  360.   swap a
  361.   or b
  362.   cpl
  363.   ld [KeyDown], a
  364.  
  365.   ld a, [KeyLast]
  366.   cpl
  367.   ld b, a
  368.   ld a, [KeyDown]
  369.   and b
  370.   ld [KeyNew], a
  371.   ret
  372.  
  373. StandingFrame:
  374.   db  0, -4, $00,  0, 4, $01
  375.   db  8, -4, $10,  8, 4, $11
  376.   db 16, -4, $20, 16, 4, $21
  377.   db 24, -4, $30, 24, 4, $31
  378.   db 255
  379. WalkingFrame:
  380.   db  0, -4, $00,  0, 4, $01
  381.   db  8, -4, $02,  8, 4, $11
  382.   db 16, -4, $12, 16, 4, $21
  383.   db 24, -4, $22, 24, 4, $23
  384.   db 255
  385. JumpingFrame:
  386.   db  0,-4, $00,  0, 4, $01
  387.   db  8,-4, $02,  8, 4, $11
  388.   db 16,-8, $03, 16, 0, $04
  389.   db 24,-8, $13, 24, 0, $14
  390.   db 16, 8, $05, 24, 8, $15
  391.   db 255
  392. PingasFrame1:
  393.   db  0, -4, $00,  0, 4, $01
  394.   db  8, -4, $10,  8, 4, $11
  395.   db 16, -4, $20, 16, 4, $24
  396.   db 24, -4, $30, 24, 4, $34
  397.   db 16, 12, $25, 24,12, $35
  398.   db 255
  399. PingasFrame2:
  400.   db  0, -4, $00,  0, 4, $01
  401.   db  8, -4, $10,  8, 4, $11
  402.   db 16, -4, $20, 16, 4, $32
  403.   db 24, -4, $30, 24, 4, $33
  404.   db 255
  405.  
  406. RobotnikText:
  407.   db "Super Robotnik"
  408. LandText:
  409.   db "Land"
  410.  
  411.  
  412. graphics:
  413. INCBIN "graphics.chr"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement