rem Project: 3D Side Scroller Demo
rem Created: 25/01/2007 16:02:20
rem Author: TEH_CODERER (Andrew Neale)
rem Set screen resolution
set display mode 800,600,32
rem Hide loading
sync on
sync rate 0
sync
center text 400,300,"LOADING..."
sync
rem Disable autocam
autocam off
rem Hide the cursor
hide mouse
rem Correct the lighting
set normalization on
rem Declare types
type XYZFloat
X as float
Y as float
Z as float
endtype
type Player
Position as XYZFloat
Angle as XYZFloat
Gravity as float
endtype
type FallingPlatform
Position as XYZFloat
Life as integer
MaxLife as integer
Gravity as float
endtype
rem Declare variables
global Player as Player
global dim FallingPlatform(10) as FallingPlatform
rem ID variables
global LEVEL_OBJECT as integer
LEVEL_OBJECT=1
global PLAYER_OBJECT as integer
PLAYER_OBJECT=2
global FIRST_FALLINGPLATFORM_OBJECT as integer
FIRST_FALLINGPLATFORM_OBJECT=10
global LAST_FALLINGPLATFORM_OBJECT as integer
LAST_FALLINGPLATFORM_OBJECT=19
rem Sparky group IDs
global SC_LEVEL_GROUP as integer
SC_LEVEL_GROUP=1
rem Initial values
Player.Position.X=0.0
Player.Position.Y=10.0
Player.Position.Z=0.0
Player.Angle.X=0.0
Player.Angle.Y=0.0
Player.Angle.Z=0.0
Player.Gravity=0.0
rem Load the level object
load object "Media\Level\level.dbo",LEVEL_OBJECT
scale object LEVEL_OBJECT,10,10,10
set object ambient LEVEL_OBJECT,0
set object light LEVEL_OBJECT,0
sc_setupcomplexobject LEVEL_OBJECT,SC_LEVEL_GROUP,2
rem Make player object
make object sphere PLAYER_OBJECT,10
rem Make some falling platforms
MakeFallingPlatform(object size z(LEVEL_OBJECT)*0.1,-40.0,20.0,100)
MakeFallingPlatform(object size z(LEVEL_OBJECT)*0.1,-20.0,20.0,100)
MakeFallingPlatform(object size z(LEVEL_OBJECT)*0.1,0.0,20.0,100)
rem Lock frame rate
sync on
sync rate 60
rem Start the main loop
do
rem Control player
Up=upkey()
Down=downkey()
Right=rightkey()
Left=leftkey()
Player.Gravity=Player.Gravity+0.1
if Player.Gravity>2.0
Player.Gravity=2.0
endif
if Up=1
if OnFloor=1
if sc_raycastgroup(SC_LEVEL_GROUP,Player.Position.X,Player.Position.Y,Player.Position.Z,Player.Position.X,Player.Position.Y+10.0,Player.Position.Z,0)=0
Player.Gravity=-2.0
endif
endif
endif
if Right=1
if Left=0
Player.Angle.Y=curveangle(90.0,Player.Angle.Y,10)
Player.Position.X=Player.Position.X+1.0
endif
else
if Left=1
Player.Angle.Y=curveangle(270.0,Player.Angle.Y,10)
Player.Position.X=Player.Position.X-1.0
endif
endif
Player.Position.Y=Player.Position.Y-Player.Gravity
rem Control falling platforms
for o=FIRST_FALLINGPLATFORM_OBJECT to LAST_FALLINGPLATFORM_OBJECT
if object exist(o)=1
oa=o-FIRST_FALLINGPLATFORM_OBJECT
if FallingPlatform(oa).Life=FallingPlatform(oa).MaxLife
if sc_intersectobject(o,Player.Position.X,Player.Position.Y,Player.Position.Z,Player.Position.X,Player.Position.Y-5.0,Player.Position.Z)>0.0
FallingPlatform(oa).Life=FallingPlatform(oa).Life-1
endif
else
if FallingPlatform(oa).Life>0
a#=FallingPlatform(oa).MaxLife-FallingPlatform(oa).Life
s#=FallingPlatform(oa).MaxLife/10.0
ax#=cos((a#/s#)*(a#+90))*(a#/s#)
az#=sin((a#/s#)*a#)*(a#/s#)
rotate object o,ax#,0.0,az#
FallingPlatform(oa).Life=FallingPlatform(oa).Life-1
else
FallingPlatform(oa).Gravity=FallingPlatform(oa).Gravity+0.2
position object o,object position x(o),object position y(o)-FallingPlatform(oa).Gravity,object position z(o)
FallingPlatform(oa).Life=FallingPlatform(oa).Life-1
if FallingPlatform(oa).Life=-300
position object o,FallingPlatform(oa).Position.X,FallingPlatform(oa).Position.Y,FallingPlatform(oa).Position.Z
rotate object o,0,0,0
FallingPlatform(oa).Life=FallingPlatform(oa).MaxLife
FallingPlatform(oa).Gravity=0.0
endif
endif
sc_updateobject o
endif
endif
next o
rem Update player object
position object PLAYER_OBJECT,Player.Position.X,Player.Position.Y,Player.Position.Z
rotate object PLAYER_OBJECT,Player.Angle.X,Player.Angle.Y,Player.Angle.Z
rem Collision
v=RunGroupCollision(PLAYER_OBJECT,SC_LEVEL_GROUP,0,10.0,10.0,10.0,2)
if v>3
Hit=1
OnFloor=v-4
else
Hit=0
OnFloor=v
endif
if OnFloor=1
Player.Gravity=0.5
endif
if OnFloor>1
Player.Gravity=1.0
endif
rem Update position variables
Player.Position.X=object position x(PLAYER_OBJECT)
Player.Position.Y=object position y(PLAYER_OBJECT)
rem Control camera
position camera Player.Position.X,Player.Position.Y+30,Player.Position.Z-60
point camera Player.Position.X,Player.Position.Y,Player.Position.Z
rem Update the screen
sync
rem End the loop
loop
rem MakeFallingPlatform function
function MakeFallingPlatform(s#,x#,y#,life)
rem Find free platform object
done=0
o=FIRST_FALLINGPLATFORM_OBJECT
repeat
if object exist(o)=0
done=1
else
o=o+1
endif
until done=1
rem Make object
make object box o,s#,0.25*s#,s#
position object o,x#,y#-(0.125*s#),z#
sc_setupobject o,SC_LEVEL_GROUP,2
rem Array values
oa=o-FIRST_FALLINGPLATFORM_OBJECT
FallingPlatform(oa).Life=life
FallingPlatform(oa).MaxLife=life
FallingPlatform(oa).Position.X=x#
FallingPlatform(oa).Position.Y=y#
FallingPlatform(oa).Position.Z=0.0
FallingPlatform(oa).Gravity=0.0
rem End the function
endfunction
rem Group collision function
function RunGroupCollision(ColObj,Group,ExcludeObject,MaxDiameter#,MinDiameter#,Height#,ReturnFlag)
rem State flags
OnFloor=0
Hit=0
rem Store ColObj position
x#=object position x(ColObj)
y#=object position y(ColObj)
z#=object position z(ColObj)
rem Collision
y1=sc_raycastgroup(Group,x#,y#,z#,x#,y#-50,z#,ExcludeObject)
y1#=sc_getcollisiondistance(0)
if y1#>0.0 and y1#<(Height#/2)
y#=y#+((Height#/2)-y1#)
OnFloor=1
Hit=1
endif
y2=sc_raycastgroup(Group,x#,y#,z#,x#,y#+50,z#,ExcludeObject)
y2#=sc_getcollisiondistance(0)
if y2#>0.0 and y2#<(Height#/2)
if OnFloor=0
y#=y#-((Height#/2)-y2#)
OnFloor=2
Hit=1
else
OnFloor=3
Hit=1
endif
endif
for ay=0 to 315 step 45
i=sc_raycastgroup(Group,x#,y#-(Height#/4.0),z#,newxvalue(x#,ay,50),y#-(Height#/4.0),newzvalue(z#,ay,50),ExcludeObject)
i#=sc_getcollisiondistance(0)
if i#>0.0 and i#<(MinDiameter#/2)
x#=newxvalue(x#,ay,i#-(MinDiameter#/2))
z#=newzvalue(z#,ay,i#-(MinDiameter#/2))
Hit=1
endif
next ay
for ay=0 to 315 step 45
i=sc_raycastgroup(Group,x#,y#+(Height#/2.0),z#,newxvalue(x#,ay,50),y#+(Height#/2.0),newzvalue(z#,ay,50),ExcludeObject)
i#=sc_getcollisiondistance(0)
if i#>0.0 and i#<(MinDiameter#/2)
x#=newxvalue(x#,ay,i#-(MinDiameter#/2))
z#=newzvalue(z#,ay,i#-(MinDiameter#/2))
Hit=1
endif
next ay
for ay=0 to 315 step 45
i=sc_raycastgroup(Group,x#,y#,z#,newxvalue(x#,ay,50),y#,newzvalue(z#,ay,50),ExcludeObject)
i#=sc_getcollisiondistance(0)
if i#>0.0 and i#<(MaxDiameter#/2)
x#=newxvalue(x#,ay,i#-(MaxDiameter#/2))
z#=newzvalue(z#,ay,i#-(MaxDiameter#/2))
Hit=1
endif
next ay
rem Update object
position object ColObj,x#,y#,z#
rem Set return value
if ReturnFlag=0
ReturnValue=OnFloor
else
if ReturnFlag=1
ReturnValue=Hit
else
ReturnValue=(Hit*4)+OnFloor
endif
endif
rem End the function
endfunction ReturnValue
rem Single object collision function
function RunObjectCollision(ColObj,LvlObj,MaxDiameter#,MinDiameter#,Height#,ReturnFlag)
rem State flags
OnFloor=0
Hit=0
rem Store ColObj position
x#=object position x(ColObj)
y#=object position y(ColObj)
z#=object position z(ColObj)
rem Collision
y1=sc_raycast(LvlObj,x#,y#,z#,x#,y#-50,z#,0)
y1#=sc_getcollisiondistance(0)
if y1#>0.0 and y1#<(Height#/2)
y#=y#+((Height#/2)-y1#)
OnFloor=1
Hit=1
endif
y2=sc_raycast(LvlObj,x#,y#,z#,x#,y#+50,z#,0)
y2#=sc_getcollisiondistance(0)
if y2#>0.0 and y2#<(Height#/2)
if OnFloor=0
y#=y#-((Height#/2)-y2#)
OnFloor=2
Hit=1
else
OnFloor=3
Hit=1
endif
endif
for ay=0 to 315 step 45
i=sc_raycast(LvlObj,x#,y#-(Height#/4.0),z#,newxvalue(x#,ay,50),y#-(Height#/4.0),newzvalue(z#,ay,50),0)
i#=sc_getcollisiondistance(0)
if i#>0.0 and i#<(MinDiameter#/2)
x#=newxvalue(x#,ay,i#-(MinDiameter#/2))
z#=newzvalue(z#,ay,i#-(MinDiameter#/2))
Hit=1
endif
next ay
for ay=0 to 315 step 45
i=sc_raycast(LvlObj,x#,y#+(Height#/2.0),z#,newxvalue(x#,ay,50),y#+(Height#/2.0),newzvalue(z#,ay,50),0)
i#=sc_getcollisiondistance(0)
if i#>0.0 and i#<(MinDiameter#/2)
x#=newxvalue(x#,ay,i#-(MinDiameter#/2))
z#=newzvalue(z#,ay,i#-(MinDiameter#/2))
Hit=1
endif
next ay
for ay=0 to 315 step 45
i=sc_raycast(LvlObj,x#,y#,z#,newxvalue(x#,ay,50),y#,newzvalue(z#,ay,50),0)
i#=sc_getcollisiondistance(0)
if i#>0.0 and i#<(MaxDiameter#/2)
x#=newxvalue(x#,ay,i#-(MaxDiameter#/2))
z#=newzvalue(z#,ay,i#-(MaxDiameter#/2))
Hit=1
endif
next ay
rem Update object
position object ColObj,x#,y#,z#
rem Set return value
if ReturnFlag=0
ReturnValue=OnFloor
else
if ReturnFlag=1
ReturnValue=Hit
else
ReturnValue=(Hit*4)+OnFloor
endif
endif
rem End the function
endfunction ReturnValue
rem Function to enable Sparky's dll
function EnableSparky()
make memblock 1,1
endfunction
There you go. Use the same project I uploaded earlier but change the code to the above.