;-------T-------T------------------------T----------------------------------;
;Name:      Bouncing Bobs
;Author:    Paul Manias
;Copyright: DreamWorld Productions (c) 1996-1997.  Freely distributable.
;
;This is a demonstration of bouncing bobs, an extension of the original
;Raining BOBs demo.  It runs in high-resolution + lace with a total of 35
;16x16 bobs at 50 FPS ('020+FAST).

	INCDIR	"INCLUDES:"
	INCLUDE	"games/games_lib.i"
	INCLUDE	"games/games.i"

MAX_IMAGES =	35

	SECTION	"Demo",CODE

;===========================================================================;
;                             INITIALISE DEMO
;===========================================================================;

	STARTGMS

Start:	MOVEM.L	A0-A6/D1-D7,-(SP)
	move.l	GMSBase(pc),a6
	CALL	AllocBlitter
	tst.l	d0
	bne	.Error_Blitter

	lea	PIC_BobTags(pc),a1
	CALL	LoadPic
	tst.l	d0
	beq	.Error_Picture

	move.l	PIC_Bobs(pc),a1
	move.w	PIC_Planes(a1),GSPlanes+2
	move.l	PIC_Palette(a1),GSPalette

	lea	ScreenTags(pc),a0
	CALL	AddScreen
	tst.l	d0
	beq.s	.Error_Screen

	move.l	Screen(pc),a0	;a0 = GameScreen
	moveq	#MAX_IMAGES,d1	;d1 = Maximum amount of images.
	CALL	InitRestore
	tst.l	d0
	beq.s	.Error_Restorelist

	move.l	Screen(pc),a0
	lea	TAGS_Ball(pc),a1	;Initialise the Ball mbob.
	CALL	InitBob
	tst.l	d0
	beq.s	.Error_Ball

	move.l	Screen(pc),a0
	lea	TAGS_Interlaced(pc),a1	;Initialise "HiRes Interlaced" bob.
	CALL	InitBob
	tst.l	d0
	beq.s	.Error_Laced

	move.l	Screen(pc),a0
	CALL	ShowScreen

	CALL	InitJoyPorts

	bsr.s	Main

.ReturnToDOS
	move.l	GMSBase(pc),a6
	move.l	BOB_Interlaced(pc),a1
	CALL	FreeBob
.Error_Laced
	move.l	MBOB_Ball(pc),a1
	CALL	FreeBob
.Error_Ball
	move.l	Screen(pc),d0
	CALL	FreeRestore
.Error_Restorelist
	lea	ScreenTags(pc),a0
	CALL	DeleteScreen
.Error_Screen
	move.l	PIC_Bobs(pc),a1
	CALL	FreePic
.Error_Picture
	CALL	FreeBlitter
.Error_Blitter
	MOVEM.L	(SP)+,A0-A6/D1-D7
	moveq	#ERR_OK,d0
	rts

;===========================================================================;
;                                DEMO CODE
;===========================================================================;

Main:	move.l	GMSBase(pc),a6	;a6 = GMSBase.
	move.l	Screen(pc),a2	;a2 = GameScreen.
	move.l	GS_Bitmap(a2),a0	;a0 = Bitmap.

	move.l	BOB_Interlaced(pc),a1	;a1 = Bob to draw.
	move.w	GS_ScrWidth(a2),d0
	sub.w	BOB_Width(a1),d0
	move.w	d0,BOB_XCoord(a1)

	move.l	GS_MemPtr2(a2),BMP_Data(a0)
	CALL	DrawBob	;>> = Draw the Bob

	move.l	GS_MemPtr1(a2),BMP_Data(a0)
	CALL	DrawBob	;>> = Draw the Bob

	moveq	#$00,d7
	move.l	MBOB_Ball(pc),a1
	move.l	MB_EntryList(a1),a2	;a2 = First entry.
	move.w	MB_AmtEntries(a1),d2	;a2 = Amount of entries.
	subq.w	#1,d2	;d2 = --1 for loop.
	moveq	#$00,d3

.create	eor.w	#1,d3
	moveq	#8,d1	;Set Y speed here.
	CALL	FastRandom
	addq.w	#2,d0
	move.w	d0,BE_YSpeed(a2)
	move.w	#8,d1	;Set X speed here.
	CALL	FastRandom
	addq.w	#1,d0
	move.w	d0,BE_XSpeed(a2)
	tst.w	d3
	beq.s	.posx
	neg.w	BE_XSpeed(a2)

.posx	move.l	Screen(pc),a0
	move.w	GS_ScrHeight(a0),d1	;Starting Y coordinate.
	asr.w	#1,d1
	CALL	FastRandom
	asr.w	#1,d1
	add.w	d1,d0
	move.w	d0,BE_YCoord(a2)

	move.w	GS_ScrWidth(a0),d1	;Starting X coordinate.
	CALL	FastRandom
	move.w	d0,BE_XCoord(a2)

	moveq	#12,d1
	CALL	FastRandom
	move.w	d0,BE_Frame(a2)
	move.b	.Sets(pc,d0.w),BE_Set+1(a2)
	move.w	#1,BE_FChange(a2)
	move.w	d3,BE_Locked(a2)

	lea	NBE_SIZEOF(a2),a2
	dbra	d2,.create
	bra.s	Loop

.Sets	dc.b	0,0,0,0
	dc.b	1,1,1,1
	dc.b	2,2,2,2

;---------------------------------------------------------------------------;

Loop:	addq.w	#1,d7
	move.l	MB_EntryList(a1),a2	;a2 = First entry.
	move.w	MB_AmtEntries(a1),d2
	subq.w	#1,d2
.update	bsr.s	UpdateBob
	lea	NBE_SIZEOF(a2),a2
	dbra	d2,.update

	move.l	Screen(pc),a0
	move.l	GS_Bitmap(a0),a0
	CALL	Restore

	move.l	Screen(pc),a0	;a0 = GameScreen.
	move.l	GS_Bitmap(a0),a0	;a0 = Bitmap.
	move.l	a1,a2	;a2 = Restorelist.
	move.l	MBOB_Ball(pc),a1	;a1 = Bob to draw.
	CALL	DrawBob	;>> = Draw the Bob.

	CALL	WaitVBL	;>> = Wait for VBL.

	move.l	Screen(pc),a0	;a0 = GameScreen.
	CALL	SwapBuffers	;>> = Swap the buffers.

	moveq	#JPORT1,d0	;d0 = JoyPort2
	moveq	#JT_ZBXY,d1	;d1 = Bit switch type.
	CALL	ReadJoyPort	;>> = Go get port status.
	btst	#MB_LMB,d0	;d0 = LMB pressed?
	beq.s	Loop	;>> = No.
	rts

;===========================================================================;
;                               UPDATE A BOB
;===========================================================================;
;Function: Moves the entity according to its internal settings.
;Requires: a1 = Bob structure.
;	   a2 = Entry to update.

GRAVITY =	1

UpdateBob:
	move.l	MB_Screen(a1),a0	;a0 = Bitmap
	move.w	BE_YCoord(a2),d0	;d0 = YCoord
	add.w	BE_YSpeed(a2),d0	;d0 = (YCoord)+YSpeed
	cmp.w	GS_ScrHeight(a0),d0	;d0 = Should this bob bounce?
	blt.s	.NoBounce	;>> = No.
	neg.w	BE_YSpeed(a2)	;a2 = Bounce the bob!
	addq.w	#GRAVITY,BE_YSpeed(a2)	;a2 = Gravity pushes the bob down.
	bra.s	.CheckX
.NoBounce
	move.w	d0,BE_YCoord(a2)	;d0 = Save the change.
	addq.w	#GRAVITY,BE_YSpeed(a2)	;a2 = Gravity pushes the bob down.

.CheckX	move.w	BE_XCoord(a2),d0
	add.w	BE_XSpeed(a2),d0
	cmp.w	GS_ScrWidth(a0),d0
	bcs.s	.NoXBounce
	neg.w	BE_XSpeed(a2)
	bra.s	.Animate
.NoXBounce
	move.w	d0,BE_XCoord(a2)

;---------------------------------------------------------------------------;

.Animate
	tst.w	BE_Locked(a2)
	beq.s	.exit
	move.w	d7,d6
	and.w	#%00000011,d6
	bne.s	.exit
	move.w	BE_FChange(a2),d1
	bgt.s	.Positive

.Negative
	cmp.w	#1,BE_Set(a2)
	bgt.s	.NBlue
	beq.s	.NGreen
.NRed	add.w	d1,BE_Frame(a2)
	tst	BE_Frame(a2)
	bge.s	.exit
	move.w	#1,BE_FChange(a2)
	clr.w	BE_Frame(a2)
	rts
.NGreen	add.w	d1,BE_Frame(a2)
	cmp.w	#4,BE_Frame(a2)
	bge.s	.done
	move.w	#1,BE_FChange(a2)
	move.w	#4,BE_Frame(a2)
	rts
.NBlue	add.w	d1,BE_Frame(a2)
	cmp.w	#8,BE_Frame(a2)
	bge.s	.done
	move.w	#1,BE_FChange(a2)
	move.w	#8,BE_Frame(a2)
.exit	rts

.Positive
	cmp.w	#1,BE_Set(a2)
	bgt.s	.PBlue
	beq.s	.PGreen
.PRed	add.w	d1,BE_Frame(a2)
	cmp.w	#3,BE_Frame(a2)
	ble.s	.done
	move.w	#-1,BE_FChange(a2)
	move.w	#2,BE_Frame(a2)
	rts
.PGreen	add.w	d1,BE_Frame(a2)
	cmp.w	#7,BE_Frame(a2)
	ble.s	.done
	move.w	#-1,BE_FChange(a2)
	move.w	#6,BE_Frame(a2)
	rts
.PBlue	add.w	d1,BE_Frame(a2)
	cmp.w	#11,BE_Frame(a2)
	ble.s	.done
	move.w	#-1,BE_FChange(a2)
	move.w	#10,BE_Frame(a2)
.done	rts

;===========================================================================;
;                                  DATA
;===========================================================================;

  STRUCTURE	NBE,BE_SIZEOF	;Definition for the mutated Entry-
	WORD	BE_XSpeed	;list in the Ball bob.
	WORD	BE_YSpeed
	WORD	BE_Set	;0 = Red, 1 = Green, 2 = Blue.
	WORD	BE_FChange
	WORD	BE_Locked
	LABEL	NBE_SIZEOF

ScreenTags:	dc.l  TAGS_GAMESCREEN
Screen:		dc.l  0
		dc.l  GSA_Rasterlist,Rasterlist
		dc.l  GSA_ScrWidth,640
		dc.l  GSA_ScrHeight,512
		dc.l  GSA_Attrib,DBLBUFFER
		dc.l  GSA_ScrMode,HIRES|LACED
		dc.l  GSA_Planes
GSPlanes:	dc.l  0
		dc.l  GSA_Palette
GSPalette:	dc.l  0
		dc.l  TAGEND

Rasterlist:	COLOURLIST  300,30,00,.colours ;Line, Skip, Colnum, Colours.
		RASTEND
.colours	dc.l  $100000,$200000,$300000,$400000,$500000,$600000,$700000
		dc.l  -1

;---------------------------------------------------------------------------;

PIC_BobTags:	dc.l  TAGS_PICTURE
PIC_Bobs:	dc.l  0
		dc.l  PCA_Options,VIDEOMEM|GETPALETTE
		dc.l  PCA_File,.file
		dc.l  TAGEND
.file		dc.b  "GMS:demos/data/PIC.HRPulse",0
		even

;---------------------------------------------------------------------------;

TAGS_Ball:	dc.l  TAGS_MBOB
MBOB_Ball:	dc.l  0
		dc.l  MBA_AmtEntries,MAX_IMAGES
		dc.l  MBA_GfxCoords,.frames
		dc.l  MBA_Width,16
		dc.l  MBA_Height,16
		dc.l  MBA_EntryList,Images
		dc.l  MBA_Attrib,GENMASKS|CLRNOMASK|CLEAR|CLIP
		dc.l  MBA_PictureTags,PIC_BobTags
		dc.l  MBA_EntrySize,NBE_SIZEOF
		dc.l  TAGEND

.frames		dc.w  00,16*0	;RED
		dc.w  00,16*1
		dc.w  00,16*2
		dc.w  00,16*3
		dc.w  16,16*0	;GREEN
		dc.w  16,16*1
		dc.w  16,16*2
		dc.w  16,16*3
		dc.w  32,16*0	;BLUE
		dc.w  32,16*1
		dc.w  32,16*2
		dc.w  32,16*3
		dc.l  -1

;---------------------------------------------------------------------------;

TAGS_Interlaced	dc.l  TAGS_BOB
BOB_Interlaced:	dc.l  0
		dc.l  BBA_GfxCoords,.frames
		dc.l  BBA_Width,96
		dc.l  BBA_Height,7
		dc.l  BBA_Attrib,GENMASKS
		dc.l  BBA_PictureTags,PIC_BobTags
		dc.l  TAGEND

.frames		dc.w  0,16*4	;X/Y Graphic
		dc.l  -1

;---------------------------------------------------------------------------;

	SECTION	Images,BSS

Images	ds.b	NBE_SIZEOF*MAX_IMAGES	;X/Y/Frame/Speed/Set/FChange/Locked

