;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; INIT and MAIN JSL targets
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

incsrc subroutinedefs_trasm.asm

DIR = $03
PROPRAM = $04

dcb "INIT"
	JSR SubSetXPos	; this actually jumps to the init routine of the exploding block
	STY $C2,x	; the Y register determines which statue it will be
	;LDA WalkTime,y
	;STA $1540,x
EndInit:
	RTL

SubSetXPos:	; this determines which kind of statue it will be based on its X position
	LDA $E4,x	; sprite X position (low byte)
	LSR
	LSR
	LSR
	LSR		; divide by 16 to get it in terms of tiles
	AND #$03	; we want only 4 possible values
	TAY		; transfer this into Y (to determine the sprite that the exploding block would spawn)
	RTS		; but since this isn't an exploding block, we can just end right here

dcb "MAIN"


	PHB
	PHK
	PLB		
	JSR Run
	PLB
	RTL

RETURN:
	RTS

Run:

	LDA $7FAB10,x
 	AND #$04
	BEQ SKIP
 	JSR GFX_COIN
	RTS
SKIP:	JSR GFX

	LDA $14C8,x	; \ 
	CMP #$08	;  | if status != 8, return
	BNE RETURN	; /
	LDA $9D		; \ if sprites locked, return
	BNE RETURN	; /

	LDA $1602,x
	BEQ KeepRunning

	LDA $1588,x	;sprite blocked status table xxxxUDLR
	AND #$04
	BEQ NoJump
	LDA #$E0
	STA $AA,x

NoJump:

	LDA $B6,x
	SEC
	SBC #$04
	BMI SetZero
	STA $B6,x
	BRA Update
	
SetZero:
	STZ $B6,x
	BRA Update
KeepRunning:

	LDA $1DEF
	CMP #$01
	BEQ GoTime
	JSR SubSetXPos	; this actually jumps to the init routine of the exploding block
	CPY #$03
	BEQ DoneWalk
	LDA #$08
	STA $B6,x
	BRA Update

Donewalk:
	LDA $1DEF
	CMP #$01
	BEQ GoTime
	STZ $B6,x
	BRA Update
	

GoTime:
	LDA $C2,x
	TAY
	LDA RunSpeed,y
	;LDA #$70
	STA $B6,x

Update:
	JSL $81802A	;update position based on speed values
	
	RTS

LevelASM:

	LDA $13C8
	BNE DecRAM
	LDA $16
	AND #$01
	BNE Move
	BRA Done
DecRAM:
	DEC $13C8
	LDA $16
	AND #$01
	BNE Boost
	BRA Done
Boost:
	LDA $7B
	CLC
	ADC #$18
	CLC
	CMP #$7F
	BCC NoSlow
	LDA #$7F
NoSlow:
	STA $7B
Move:
	LDA #$0A
	STA $13C8

Done:
	RTS

RunSpeed:
	dcb $6F,$58,$40,$00
WalkTime:
	dcb $28,$28,$28,$28
;;;;;;;;;;;;;;;;;;;;;;;
;GFX routine
;;;;;;;;;;;;;;;;;;;;;;;

TILEMAP:	dcb $A0,$4E
	dcb $A2,$82

;XDISP:	dcb $00,$00
;	dcb $00,$10

YDISP:	dcb $00,$F0
	dcb $10,$10

GFX:
	JSL SubOffscreenX0
	JSL GetDrawInfo

	LDA $157C,x	;direction...
	STA DIR

	LDA $15F6,x	;properties...
	ORA #$64	;flip the X direction
	STA PROPRAM

	LDX #$00

OAM_Loop:

	LDA $00
	STA $0300,y	;xpos

	LDA $01
	CLC
	ADC YDISP,x
	STA $0301,y	;ypos

	PHX
	LDX $15E9
	LDA $B6,x	;general purpose timer
	PLX
	CMP #$00
	BEQ Normal	;don't animate if not moving horizontal
	CMP #$3A
	BCS RunFast	;animate quickly if running fast
	
	LDA $14		;animate medium if moving, but not quickly
	AND #$08
	BNE Normal
	

Faster:
	INX
	INX
	LDA TILEMAP,x
	STA $0302,y
	LDA $0301,y
	INC
	STA $0301,y
	DEX
	DEX
	BRA Dunn

RunFast:
	LDA $14
	AND #$02
	BNE Faster


Normal:
	LDA TILEMAP,x
	STA $0302,y	;chr

Dunn:
	LDA PROPRAM
	ORA $64
	STA $0303,y	;prop

	INY
	INY
	INY
	INY
	INX

	CPX #$02
	BNE OAM_Loop

	LDX $15E9	;restore sprite index
	LDY #$02	;16x16
	LDA #$01	;2 tiles
	JSL $81B7B3	;reserve

	RTS


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ROUTINES FROM THE LIBRARY ARE PASTED BELOW
; You should never have to modify this code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;





TILEMAP_NUM: db $76,$77,$46,$47,$C2,$C3,$D2,$D3,$D4,$D5,$4D,$67

GFX_COIN:
	LDA #$B0
	STA $0200,y	;xpos COIN
	CLC
	ADC #$10
	STA $0204,y	;xpos NUM1
	CLC
	ADC #$08
	STA $0208,y	;xpos NUM2
	CLC
	ADC #$08
	STA $020C,y	;xpos NUM3

	LDA #$28
	CLC
	ADC #$00
	STA $0201,y	;xpos COIN
	STA $0205,y	;ypos NUM1
	STA $0209,y	;ypos NUM2
	STA $020D,y	;ypos NUM3

	LDA #$DB
	STA $0202,y
	LDA $140C
	STA $7F8888
	CMP #$64
	BCC NOHUND
	SEC
	SBC #$64
	STA $7F8888
	PHX
	LDX #$01
	LDA TILEMAP_NUM,x
	STA $206,y
	PLX
	BRA GOLOOP
NOHUND:	PHX
	LDX #$00
	LDA TILEMAP_NUM,x
	STA $206,y
	PLX
GOLOOP:	PHX
	LDA $7F8888
LOOP:	TAX
	CMP #$0A
	BCC E_LOOP
	SEC
	SBC #$0A
	BRA LOOP
E_LOOP: LDA TILEMAP_NUM,x
	STA $020E,y
	LDX #$00
	LDA $7F8888
LOOP2:	CMP #$0A
	BCC E_LP2
	SEC
	SBC #$0A
	INX
	BRA LOOP2;
E_LP2:	LDA TILEMAP_NUM,x
	STA $020A,y
	PLX

	LDA #$36
	STA $0203,y	;prop
	STA $0207,y	;prop
	STA $020B,y	;prop
	STA $020F,y	;prop

	LDX $15E9	;restore sprite index
	LDY #$00	;8X16
	LDA #$00	;2 tile
	JSL $81B7B3	;reserve
	RTS

