###########################################
#
#  Sw8 Copter
#
#  An adaptation of "Swing Copters"
#  for the Super Chip8.
#  Runs best at 100 cycles/frame.
#
#  Press E to start a game and then
#  press E to switch directions.
#  Avoid obstacles!
#
###########################################

:alias score        v7
:alias collide      v8
:alias player-key   v9
:alias player-x     va
:alias player-y     vb
:alias player-frame vc
:alias player-dir   vd

:const H_SPEED 3
:const BUTTON 6

: sync
	loop
		vf := delay
		if vf != 0 then
	again
	vf := 2
	delay := vf
;

: girders
	i := girder
	v0 := 88
	v1 := 8
	loop
		sprite v0 v1 0
		v0 += 16
		if v0 != 168 then
	again
;

: draw-copter
	i := face-l
	if player-dir == H_SPEED then i := face-r
	collide := 0
	sprite player-x player-y 0
	collide |= vf
	i := blade-0
	if player-frame == 1 then i := blade-1
	v0 := 16
	v0 =- player-y
	sprite player-x v0 0
	collide |= vf
;

: wait-release
	if v0 key then return
	player-key := 0
;

: switch-dir
	v0 := BUTTON
	if player-key != 0 then jump wait-release
	if v0 -key then return
	player-key := 1

	# (~x)+1 = -x
	v0 := 0xFF
	player-dir ^= v0
	player-dir += 1
;

: move-up
	player-y += -1
	v0 := 63
	v0 &= player-y
	if v0 == 0 then score += 1
;

: move-copter
	v0 := 1
	player-frame ^= v0
	move-up
	v0 := random 1
	if v0 == 1 then move-up
	player-x += player-dir
	switch-dir
;

: wait-key
	v0 := 56
	v1 := 18
	i  := tapbubble
	sprite v0 v1 0
	vf := BUTTON
	loop
		if vf -key then
	again
	sprite v0 v1 0
;

: game-over
	vf := 16
	buzzer := vf
	delay  := vf
	loop
		draw-copter
		scroll-left
		draw-copter
		draw-copter
		scroll-right
		draw-copter
		draw-copter
		scroll-right
		draw-copter
		draw-copter
		scroll-left
		draw-copter
		vf := delay
		if vf != 0 then
	again

: show-score
	i := bcd-buffer
	bcd score
	load v2
	i := bighex v1
	v0 := 55
	v1 := 30
	sprite v0 v1 10
	i := bighex v2
	v0 += 9
	sprite v0 v1 10
	vf := BUTTON
	loop
		if vf -key then
	again
	clear

: main
	hires
	player-x   := 56
	player-y   := 48
	player-dir := H_SPEED
	player-key := 0
	score      := 0

	girders
	draw-copter
	wait-key

	loop
		draw-copter
		move-copter
		draw-copter
		if collide != 0 then jump game-over
		sync
	again

: bcd-buffer 0 0 0

: blade-0
 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
 0x01 0x80 0x02 0x40 0x7A 0x5E 0x89 0x91 0xF0 0x0F 0x01 0x80 0x01 0x80 0x00 0x00

: blade-1
 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
 0x01 0x80 0x02 0x40 0x0E 0xF0 0x31 0x0C 0x3E 0xFC 0x01 0x80 0x01 0x80 0x00 0x00

: face-r
 0x07 0xC0 0x18 0x30 0x20 0x08 0x23 0xB8 0x44 0x44 0x48 0x8A 0x48 0x02 0x48 0x02
 0x34 0x44 0x23 0xB8 0x30 0x08 0x4B 0xF4 0x48 0x14 0x30 0x18 0x11 0x20 0x0F 0xC0

: face-l
 0x03 0xE0 0x0C 0x18 0x10 0x04 0x1D 0xC4 0x22 0x22 0x51 0x12 0x40 0x12 0x40 0x12
 0x22 0x2C 0x1D 0xC4 0x10 0x0C 0x2F 0x92 0x28 0x12 0x18 0x0C 0x04 0x88 0x03 0xF0

: girder
 0xFF 0xFF 0x00 0x00 0xC3 0xC3 0x7E 0x7E 0x3C 0x3C 0x3C 0x3C 0x3C 0x3C 0x7E 0x7E
 0xFF 0xFF 0x00 0x00 0xFF 0xFF 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

: tapbubble
 0x3F 0xFC 0x7F 0xFE 0xFF 0xFF 0xF0 0x0F 0xF0 0x0F 0xF3 0xFF 0xF0 0x7F 0xF0 0x7F
 0xF3 0xFF 0xF0 0x0F 0xF0 0x0F 0xFF 0xFF 0x7F 0xFE 0x3F 0xFC 0x00 0xC0 0x00 0x80
