FMC:Phoniex Alpha Test 2

Miscellaneous Forums/Blitz Showcase/FMC:Phoniex Alpha Test 2

Another look at the new game source, starting to take shape now.

Not sure if I'll be able to release any more publically, thsi isn't just my code so dunno. I don't see no harm in it personally. At least for now.
Strict
'----------------------------=
'
'    Full Metal Conflict 2 : Phoniex
'	
'
'  Coded by Antony Wells & Damien Sturdy
'
' (c)Kinetic Realities 2006
'	
'----------------------------=

'---[ Imports ]---

Import "Aurora.bmx"

Rem
	[ Game ] Class
	
	:Controls general game settings and initialization

End Rem
Type TGame

	'Free Resources
	Function FreeResources()
	
		VisualLevel.Dispose()
		ProxyLevel.Dispose()
		Actors.Dispose()
		PickUps.Dispose()
		For Local tm:oTriMesh = EachIn TriMeshes
			tm.dispose()
		Next
		trimeshes.clear()
			
	End Function
	
	'Initialize the game
	Function Initialize()
		
		VisualLevel = TEntityGroup.Create("Level Visuals")
		ProxyLevel = TEntityGroup.Create("Level Proxies")
		Actors = TEntityGroup.Create("Actors")
		PickUps = TEntityGroup.Create("Pick Ups")
		TriMeshes = CreateList()
		InitializeOde()
		
		
	End Function
	
	'Initialize the ode wrapper and set up spaces and contact gruops.
	Function InitializeOde()
		
		World = OWorld.Create()
		Space = OSpace.Create()
		scenespace = ospace.Create()
		pickspace:Ospace = Space
		playerspace:ospace=ospace.Create()
		Group = OContactGroup.Create()
		dWorldSetQuickStepNumIterations (world.world,16)
		World.SetAutoDisable( True )
		World.SetGravity( OVector3.Create(0,-1.98,0) )
		OOde.SetContactMode( ContactBounce )
		Oode.SetContactBounce( 0.8 )
		Oode.SetContactFriction( 1 )
		todecontroller.Init()
		OHelper.SetActiveSpace( Space )
		dSetInternalSpaceCollideMode(0)


	End Function
	
	'Initliaze the display window/screen.
	Function InitDisplay()
		
		Display = TDisplay.Create()
		Display.OpenScreen()
		
				
	End Function
	
	'Load a level 
	Function LoadLevel(file:String)
		
		TSceneLoader.LoadBest( file )
		Display.ShadowMode( Shadows_TextureMod )
		
		cam = TCamera.Create()
		TGui.Init( Cam )
		cam.position(0,70,70)
		cam.lookat(0,0,0)
		
		Local Trimesh:oTrimesh
		Local collisionScene:TSceneLoader = New TSceneLoader 
		Local colScene:TScene = collisionScene.Loadsafe("Media/"+file)
		For Local v:TEntity =EachIn colscene.entities
			v.castshadows( False )
			v.position(0,-10000,0)
			'v.visible( False )
			'trimesh:oTrimesh=oTrimesh.CreateTriMesh(Space.Space,v.filename)
			'trimeshes.addlast( triMesh )
	
		Next
		
		
	End Function
	
	
	'---[ Game Related Globals ]
	Global Display:TDisplay
	Global cam:TCamera
	Global VisualLevel:TEntityGroup
	Global ProxyLevel:TEntityGroup
	Global Actors:TEntityGroup
	Global PickUps:TEntityGroup
	
	'---[ Ode related globals ]
	Global World:OWorld 
	Global Space:OSpace 
	Global SceneSpace:OSpace 
	Global PickSpace:OSpace 
	Global PlayerSpace:OSpace 
	Global Group:OContactGroup 
	Global TriMeshes:TList
	 
	
	
End Type

Rem 
	
	[ Actor ] Class
	
	Base Abstract class for all actor classes.

End Rem
Type TActor Abstract
	
	Field Visual:TEntity
	Field Body:OBody
	Field Ent:OEntity
	Field isBase
	Field mesh:String
	Field bWidth#,bHeight#,bDepth#
	Field bWeight#
	Field name:String
	Function AddBase(in:TActor )
		bases.addlast( in )
	End Function
	Function FindBase:TActor(name:String)
		For Local base:TActor = EachIn bases
			If base.name.tolower() = name.tolower()
				Return base
			EndIf
		Next
		Return Null
	End Function
	
	Method CreateResources()
	End Method
	
	
	Function Spawn:TActor( from:String,x#,y#,z# )
		Local base:Tactor = tactor.findbase( from )
		If base = Null	
			RuntimeError from+" Does not exist. Unable to spawn."
		End If
		Return base.spawn( x,y,z )
	End Function

	Method Spawn:TActor( x#,y#,z# ) Abstract
	
	Global Bases:TList
	
End Type
TActor.Bases = CreateList()

Rem
	
	[ Ship ] Class Extends [ Actor] Class
	
	Ship class. Used for all hovering vehicles.
	
End Rem
Type TShip Extends TActor Final 
	
	Function CreateBase:TShip(name:String,mesh:String,width#,height#,depth#)
		
		Local out:TShip = New tship
			
			out.bwidth = width
			out.bheight = height
			out.bdepth = bdepth
			out.mesh = mesh
			out.name = name
			out.isbase = True
			tactor.addBase( out )
			
		Return out
				
	End Function
	
	Method Spawn:TActor(x#,y#,z#)
		
		Local out:TShip = New TShip
		out.CreateResources()
		Return out
		
	End Function
	
End Type

Rem

	[ App ] Clas
	
	Controls and starts the application.

End Rem
Type TApp
	'Create Demo Resources
	Function CreateDemoResources()
		TShip.CreateBase( "Hammer","hammer.mesh",3,5,4 )
	End Function
	
	'App entry point.
	Function Go()
		
		TGame.Initialize()	
		TGame.InitDisplay()
		TGame.LoadLevel("scene.OSM")
		CreateDemoResources()	
		Repeat
			
			Self.Update()
			
			Self.Render()
						
		Until TInput.KeyDown(Key_Escape)
		
		Quit()	
							
	End Function
	
	Function Update()
		
		tgame.cam.pitch( - tinput.mouseyspeed() )
		tgame.cam.yaw( - tinput.mousexspeed() )
		If tinput.keydown( KEY_W )
			tgame.cam.move(0,0,2 )
		End If
		If tinput.keydown( KEY_S )
			tgame.cam.move(0,0,-2)
		End If
		CaptureInput()
		
	End Function
	
	Function Render()
		
		Render3D()
		
	End Function
	
	
	Function Quit()
	
		TGame.FreeResources()
	
	End Function
	
	
End Type


Rem

	[ Helper ] Class
	
	Helper funmctions to simplfy commonly used algos.

End Rem

Type THelper
	
	Function linepick(fromspace:ospace,tospace:ospace,x#,y#,z#,xdir#,ydir#,zdir#,length#,group:OContactGroup)
		Local ray=dCreateRay(fromspace.space, length#)
		dGeomRaySet(ray ,x#,y#,z#, xdir#,ydir#,zdir#)
		dspacecollide2(ray,Tospace.space,tgame.World.world , Group.group )
		Local px#=9999999999999,py#=99999999,pz#=99999999
 
		Local hit = False
		If dGeom1CountCollisions(ray) > 0 Then
			For Local Count=1 To dGeom1CountCollisions(ray)
				Local cx#,cy#,cz#
				cx = dGeom1CollisionX(ray,count)
				cy = dGeom1CollisionY(ray,count)
				cz = dGeom1CollisionZ(ray,count)
				Local d1#,d2#
				d1 = Distance( cx,cy,cz,x,y,z )
				d2 = distance( px,py,pz,x,y,z )
				If d1<d2
					px = cx
					py = cy 
					pz = cz
					hit = True
				EndIf
			Next
		EndIf
		If dGeom2CountCollisions(ray) > 0 Then
			For Local Count=1 To dGeom2CountCollisions(ray)
				Local cx#,cy#,cz#
				cx = dGeom2CollisionX(ray,count)
				cy = dGeom2CollisionY(ray,count)
				cz = dGeom2CollisionZ(ray,count)
				Local d1#,d2#
				d1 = Distance( cx,cy,cz,x,y,z )
				d2 = distance( px,py,pz,x,y,z )
				If d1<d2
					px = cx
					py = cy 
					pz = cz
					hit = True
				EndIf
			Next
		EndIf
		pickx = px
		picky = py
		pickz = pz
		dGeomDestroy(Ray)
		Group.empty()
		Return hit
	End Function	

	Global pickx#,picky#,pickz#
	Global picknx#,pickny#,picknz#
	
	Function Distance#(x1#,y1#,z1#,x2#,y2#,z2# )
		Local xd#,yd#,zd#
		xd = x2-x1
		yd = y2-y1
		zd = z2-z1
		Return Sqr( xd*xd+yd*yd+zd*zd )
	End Function
	
End Type


Rem 
	
	[ TriMesh ] Class
	
		Holds Tri Mesh data for each collision proxy.
	
End Rem

 Type oTrimesh
	Field mesh
	Field Vertbank:TBank
	Field tribank:TBank
	Field trimeshdata:Int
	
	'Function CreateTriMesh:Int(trispace:Int,xscale:Float=1.0,yscale:Float=1.0,zscale:Float=1.0)
	Function CreateTriMesh:oTrimesh(trispace:Int,Meshfile$,parent:tentity=Null,x=0,y=0,z=0,doit=1)
	
		Local xscale:Float = 1.0 , yscale:Float = 1.0 , zscale:Float = 1.0
		Local Pivot:tpivot = TPivot.Create()

		If parent<>Null Then
			pivot.position(parent.x() , parent.y() , parent.z() )
			pivot.rotate(parent.getpitch(),parent.getyaw(),parent.getroll())
		EndIf

		If FileType("media"+meshfile$)<>1 Then RuntimeError "Oh no! "+meshfile$
		Local md:tmeshdata = pivot.CreateMeshData(meshfile$)
		If MD.TRIANGLES()>0 Then
			
			Local vertexlistx#[(md.triangles() * 3)+1]
			Local vertexlisty#[(md.triangles() * 3)+1]
			Local vertexlistz#[(md.triangles() * 3)+1]
			Local cnt=0,vrt
			Local ntris = md.triangles()
			Local nverts=ntris*3
						
			For Local j=0 To md.triangles()-1
				For Local v=0 To 2 
					Local vert = md.getTriIndex( j,v ) 
					md.GetVertex( vert ) 
					vertexlistx[cnt] = md.x() 
					vertexlisty[cnt] = md.y() 
					vertexlistz[cnt] = md.z() 
					cnt = cnt + 1
				Next 
			Next



			Local tribank:TBank=CreateBank(NTris*3*4)
			Local Vertbank:TBank=CreateBank(NVerts*4*4)
			Local Offset:Int=0
			Local TriOffset:Int=0
			
			Local tm:oTrimesh = New oTrimesh
			tm.tribank=tribank
			tm.Vertbank=Vertbank
			ListAddFirst(Trimeshlist,tm:oTrimesh)
			
			For Local v=0 To cnt-1
 				PokeFloat(Vertbank,Offset,vertexlistx[v])
 				Offset:+4
 				PokeFloat(Vertbank,Offset,vertexlisty[v])
 				Offset:+4
 				PokeFloat(Vertbank,Offset,vertexlistz[v])
 				Offset:+ 8
 			Next
			ntris = ((cnt) / 3)-1
			nverts=cnt-1
			For Local v = 0 To NTRIS'(cnt - 1) / 3
				DebugLog trioffset+"   "+BankSize(tribank)
 				PokeInt(tribank,TriOffset,vrt)
				TriOffset:+4
 				PokeInt(tribank,TriOffset,vrt+1)
 				TriOffset:+4
 				PokeInt(tribank,TriOffset,vrt+2)
 				TriOffset:+ 4
				vrt=vrt+3
			Next
			
				Local trimeshdata:Int=dGeomTriMeshDataCreate()
				dGeomTriMeshDataBuildSimple(trimeshdata , BankBuf(Vertbank) , NVerts , BankBuf(tribank) , NTris * 3)
				tm.mesh = dCreateTriMesh(trispace , trimeshdata)
				tm.trimeshdata=trimeshdata
			
			Return tm
		EndIf
	End Function
	
	Method dispose()
	
		If Not disposed
		
			dGeomTriMeshDataDestroy(Self.trimeshdata)	
			Self.Vertbank = Null
			Self.tribank = Null
			ListRemove(trimeshlist,Self)
			disposed=True
		
		EndIf
		
	End Method
	
	
	Method Delete()
		dispose()
	End Method
	Field disposed
	
End Type



Rem

	Begin the application

End Rem

TApp.Go()