entity pick

Blitz3D Forums/Blitz3D Beginners Area/entity pick

You code is a mess so I've tidied it up a bit. You should really be using indentation to make it more readable. Anyway, this is what you want ( I think ) ..

Graphics3D 800,600,32,1
Dither True
AntiAlias True
BackBuffer()


Type enemy
	Field ID
	Field X
	Field Y
	Field Z
	Field shape 
End Type

Global pistolselected=0
Global Gravity# = -.5 
Global WalkSpeed#=.5 
Global FlyMode=0
Global weaponout=0
Global pistolammo=15
Global CamPivot = CreatePivot( ) 
Global Camera = CreateCamera( )
PositionEntity CamPivot,1010,50,1010 
EntityType CamPivot, 1
EntityRadius CamPivot, 3
EntityType Camera, 1
EntityRadius Camera, 2
Global pistol=CreateCylinder()
EntityParent pistol,camera
PositionEntity pistol,EntityX(Camera)+1.5,EntityY(Camera)-2,EntityZ(Camera)+2 
RotateEntity pistol,100,0,0
HideEntity pistol
Global muzzleglow2=CreateLight(pistol)
LightRange muzzleglow2,.0005 
HideEntity muzzleglow2 
LightColor muzzleglow2,255,255,0

Global city1, terrain, t$

Collisions 1,2,2,3


LoadLevel1()

While Not KeyDown(1)

	Controls()
	
Wend


;=====================================================================================
;=====================================================================================
;=====================================================================================
;=====================================================================================
;=====================================================================================
;=====================================================================================

Function Loadlevel1()

	light=CreateLight(2)
	PositionEntity light,600,100,600
	RotateEntity light,90,0,0 
	LightColor light,100,255,255
	
	terrain=CreateTerrain(32)
	PositionEntity terrain,0,-.02,0
	ScaleEntity terrain,50,20,50
	EntityType terrain,2
	EntityPickMode terrain,2
	
	city1=CreateCube()
	PositionEntity city1,1000,1.5,1000
	EntityType city1,2
	EntityPickMode city1,2
	ScaleEntity city1,50,20,50
	
	SeedRnd 4
	For enemies = 1 To 10
		e.enemy=New enemy
		e\ID = enemies
		e\x=Rnd(1,1000)
		e\y=4
		e\z=Rnd(1,1000)
		e\shape=CreateSphere()
		PositionEntity e\shape,e\x,e\y,e\z
		ScaleEntity e\shape,4,4,4
		EntityType e\shape,3
		EntityRadius e\shape, 4
		EntityPickMode e\shape,1
		NameEntity e\shape, Handle(e)
	Next
	
	CameraRange Camera,.2,1000
	CameraFogMode Camera,1
	CameraFogRange Camera,.2,999
	CameraFogColor Camera,19,164,149
	CameraClsColor Camera,0,255,255
	
End Function

;=====================================================================================
;=====================================================================================
;=====================================================================================

Function controls()
	
	If KeyDown( 42 ) =1 
		WalkSpeed# = 1.5
	Else
		WalkSpeed# = .5
	EndIf
	
	If KeyHit(2)
		ShowEntity pistol
		pistolselected=1
		weaponout=1 
	End If
	
	If FlyMode=0
		If KeyDown(17) =1 Then MoveEntity CamPivot, 0, 0, WalkSpeed# 
		If KeyDown(31) =1 Then MoveEntity CamPivot, 0, 0, 0-WalkSpeed#
		PositionEntity Camera, EntityX#( CamPivot ), EntityY#( CamPivot )+8, EntityZ#( CamPivot )
	EndIf
	
	If KeyDown(30) =1 Then MoveEntity CamPivot, -1, 0, 0
	If KeyDown(32) =1 Then MoveEntity CamPivot, 1, 0, 0 
	
	If EntityCollided(CamPivot,2)
		Gravity#=0
	Else
		Gravity#=-.5 
	EndIf
	
	TranslateEntity CamPivot, 0, Gravity#, 0
	TurnEntity CamPivot, 0, 0 -MouseXSpeed(), 0 
	TurnEntity Camera, MouseYSpeed(), 0, 0
	RotateEntity CamPivot, EntityPitch#( CamPivot ), EntityYaw#( CamPivot ), 0
	MoveMouse GraphicsWidth()/2, GraphicsHeight()/2
	RotateEntity Camera, EntityPitch#( Camera ), EntityYaw#( CamPivot ), 0 
	
	If MouseHit(1) And pistolselected = 1 And pistolammo > 0
		createbullet()
		pistolammo=pistolammo-1
	EndIf

	UpdateWorld()
	RenderWorld()
	
	Color 255,0,255
	Text 0,20,"Picked : "+t$ 
	Text 0,40,"Press 1 to pull out pistol, aim with the dot" 
	
	;dot
	Color 255,0,0
	Rect 378-2,278-2,5,5,0
	
	Flip
	
	HideEntity muzzleglow2 
	
End Function

;=====================================================================================
;=====================================================================================
;=====================================================================================

Function createbullet( )

	ShowEntity muzzleglow2
	
	If weaponout = 1
	
		picked = CameraPick(camera,378,278)
		
		If Picked > 0 

			Select Picked
	
				Case City1
				
					t$ = "City1"
				
				Case Terrain
				
					t$ = "Terrain"
				
				Default
				
					t$ = "Enemy "
	
					blood=CreateSphere()
					ShowEntity blood
					EntityShininess blood,1
					EntityColor blood,200,0,0
					ScaleEntity blood,1,1,1
					PositionEntity blood,PickedX(),PickedY(),PickedZ()
					
					;get picked enemy type
					e.enemy = Object.enemy( EntityName( Picked ) )
					t$ = t$ + e\ID
					
			End Select
			
		Else
		
			t$ = "Nothing"
			
		EndIf
					
	EndIf

End Function