sync on
sync rate 0
if check display mode(1024,768,32)=1
	set display mode 1024,768,32
endif
randomize timer()
autocam off
hide mouse
 
#Constant plyr 1
#Constant platform 2
speed#=1
turnspeed#=0.7
bulletspeed#=5
cbullet=51
shottime=0
health=100
btime=timer()
 
make object cube plyr,10
position object plyr,0,6,0
color object plyr,0x00ff88
position camera 0,305,0
xrotate camera 90
make object plain platform,2000,2000,1
xrotate object platform,90
make object box 3,50,50,2020
color object 3,0x101010
instance object 4,3
instance object 5,3
instance object 6,3
position object 3,1000,25,0
position object 4,-1000,25,0
position object 5,0,25,1000
position object 6,0,25,-1000
yrotate object 5,90
yrotate object 6,90
for i=7 to 50
	height=rnd(200)+1
	make object box i,rnd(200)+1,height,rnd(200)+1
	repeat
		position object i,rnd(2000)-1000,height/2.0,rnd(2000)-1000
	until object collision(plyr,i)=0
	color object i,0x101010
next i
set text size 16
set text font "Arial"
ink 0,0
 
type enemy
	health as integer
	spawntime as integer
	objectnum as integer
endtype
dim elist(1) as enemy
elist(1).health=10
elist(1).spawntime=timer()
elist(1).objectnum=next_enemy()
make object cube elist(1).objectnum,10
color object elist(1).objectnum,0xff0000
repeat
	position object elist(1).objectnum,rnd(2000)-1000,6,rnd(2000)-1000
until object collision(elist(1).objectnum,0)=0
 
null=make vector3(1)
null=make vector3(2)
 
shots=0
 
do
sync
px#=object position x(plyr)
py#=object position y(plyr)
pz#=object position z(plyr)
move#=(upkey()-downkey())*speed#
turn#=(rightkey()-leftkey())*turnspeed#
move object plyr,move#
yrotate object plyr,object angle y(plyr)+turn#
collide=object collision(plyr,0)
if collide>0 and collide<51 or collide>200
	position object plyr,px#,py#,pz#
else
	px#=object position x(plyr)
	py#=object position y(plyr)
	pz#=object position z(plyr)
endif
position camera px#,py#+300,pz#
yrotate camera object angle y(plyr)
if shiftkey()=1 and timer()-shottime>50
	inc shots
	if object exist(cbullet)=0
		make object cube cbullet,5
		color object cbullet,0
	endif
	position object cbullet,px#,py#,pz#
	rotate object cbullet,object angle x(plyr),object angle y(plyr),object angle z(plyr)
	move object cbullet,6
	shottime=timer()
	inc cbullet
	if cbullet>200
	   cbullet=51
	endif
endif
for i=51 to 200
	if object exist(i)=1
	   move object i,bulletspeed#
	   c=object collision(i,0)
	   if c>0 and c<51
	      delete object i
		endif
	endif
next i
set vector3 1,px#,py#,pz#
text 0,0,"Remaining health: "+str$(health)
text 0,16,"Remaining enemies: "+str$(array count(elist(0)))
for i=1 to array count(elist(0))
	killed=0
	obj=elist(i).objectnum
	ink 0xff0000,0
	line object screen x(plyr),object screen y(plyr),object screen x(obj),object screen y(obj)
	ink 0,0
	ex#=object position x(obj)
	ey#=object position y(obj)
	ez#=object position z(obj)
	set vector3 2,ex#,ey#,ez#
	subtract vector3 2,1,2
	dist#=length vector3(2)
	if dist#>100
		yrotate object obj,object angle y(obj)+rnd(6)-3
		move object obj,0.6
	else
	   point object obj,px#,py#,pz#
	   move object obj,1.2
	endif
	collide=object collision(obj,0)
	if object position x(obj)>975 or object position x(obj)<-975 or object position z(obj)>975 or object position z(obj)<-975
		killed=1
		delete object obj
		array delete element elist(0),i
		collide=0
	endif
	if collide>0
	   if collide<50 or collide>200
	      ax#=object angle x(obj)
	      ay#=object angle y(obj)
	      az#=object angle z(obj)
	      point object obj,ex#,ey#,ez#
	      move object obj,1.5
	      rotate object obj,ax#,ay#,az#
	      if collide=1
	         dec health
			endif
		endif
		if collide<200 and collide>50
			elist(i).health=elist(i).health-1
			if elist(i).health<0
				killed=1
				delete object obj
				array delete element elist(0),i
			endif
		endif
	endif
	if killed=0
	   if timer()-elist(i).spawntime>1000*array count(elist(0))^2*((timer()-btime)/25000) and rnd(100)=0
	      elist(i).spawntime=timer()
	      if array count(elist(0))<20
		      array insert at bottom elist(0)
		      entry=array count(elist(0))
		      elist(entry).objectnum=next_enemy()
				make object cube elist(entry).objectnum,10
				color object elist(entry).objectnum,0xff0000
				autoexit=0
				repeat
					position object elist(entry).objectnum,ex#,ey#,ez#
					yrotate object elist(entry).objectnum,rnd(359)
					repeat
					   inc autoexit
						move object elist(entry).objectnum,1
					until object collision(elist(entry).objectnum,0)<50 or object collision(elist(entry).objectnum,0)>200 or autoexit=50
				until object position x(elist(entry).objectnum)<975 and object position x(elist(entry).objectnum)>-975 and object position z(elist(entry).objectnum)<975 and object position z(elist(entry).objectnum)>-975 or autoexit=50
				elist(entry).spawntime=timer()
				elist(entry).health=10
			endif
	   endif
	endif
	if killed=1
	   dec i
	endif
next i
if health<=0
	cls -1
	text 0,0,"You were killed"
	sync
	wait key
	end
endif
if array count(elist(0))=0
	cls -1
	text 0,0,"You killed all the enemies"
	text 0,25,"Shots fired: "+str$(shots)
	text 0,50,"Time taken:  "+str$(timer()-btime)
	sync
	wait key
	end
endif
 
loop
 
function next_enemy()
	i=201
	repeat
	   inc i
	until object exist(i)=0
endfunction i