
;Falling Komas by Black Falcon, original idea by Jathys

;How to make them fall down:
;set the Koma's Speed 2 to #1##, where # can be any other number you want

lorom
!flag = $7E7806,x
!Speed = $7E7808,x
!flag2 = $7E780A,x

!X = $0F7A,x ;enemy X pos (from room pop)
!Y = $0F7E,x ;enemy Y pos (from room pop)

org $A8E862
	JSR SIGH

org $A8E83A
	JSR SETFLAG

org $A8E8B1
	JSR FALL

org $A8F9C2

;(E91D)

SIGH:
	LDA $0FB6,x		;SMILE SPEED 2
	PHA
	AND #$0100
	STA !Speed
	PLA
	RTS
	
SHOT:
	PHY
	LDX $0E54
	LDA !Speed
	AND #$0100
	BEQ NORMAL
	STZ $12
	STZ $14
	LDA $18A6
	ASL A
	TAY
	LDA $0C18,Y
	AND #$0500
	CMP #$0500
	BNE NORMAL
	LDA $0F7E,x
	SEC
	SBC $0B82
	CLC
	ADC #$000f
	BMI NORMAL
	CMP #$0010
	BCS NORMAL


	LDA $0F7A,x
	SEC
	SBC $0B6E
	BMI left
	LDA #$0003
	STA $12
	STA $14
	BRA STORE
left:
	LDA #$0003
	STA $12
	EOR #$FFFF
	INC A
	STA $14
STORE:
	JSL $A0C6AB
NORMAL:
	PLY
	JSL $A8E91D
	RTL



FALL:
	LDA !Speed
	AND #$0100
	BNE +
	JMP QUIT
+
	LDA $7E7802, X		;extra useable RAM for enemies
	STA $12
	LDA $7E7804, X
	STA $14
	JSL $A0C786			;Vertical motion
	BCC DOWN			;branch if no collision
	

	LDA !flag2
	CMP #$0003
	BCS OVER
	INC a
	STA !flag2
	LDA #$0001
	STA !flag
	BRA QUIT
OVER:
	BRA CHECK
DOWN:

	LDA $7E7804, X
	CMP #$0004			;Cap speed at 4 pixels per frame
	BMI LATER
	LDA #$0000
	STA !flag
	BRA QUIT
LATER:
	LDA $7E7802, X
	CLC
	ADC #$8000
	STA $7E7802, X
	LDA $7E7804, X
	ADC #$0000
	STA $7E7804, X		;Accelerate due to gravity
	LDA #$0000
	STA !flag
	BRA QUIT
CHECK:
	LDA !flag
	CMP #$0001
	BEQ QUIT


	LDA #$0004
	STA $1840
	LDA #$0004
	STA $183E
	LDA #$001B
	JSL $8090A3

	LDA !X
	STA $12		;X position
	LDA !Y
	STA $14		;Y position
	STZ $18
	LDA #$0012
	STA $16		;type
	JSL $B4BC26	;animate dust

SETFLAG:
	LDA #$0001
	STA !flag
QUIT:
	LDA $09A4
	RTS