Howdy
As an addition to my FPS code here:
http://forum.thegamecreators.com/?m=forum_view&t=154103&b=6
I have made some code for a lift (elevator)
The keys are:
WASD to move
mouse to look around
hold left shift to run
hold c to crouch
space jumps
x operates the lift
Here is the code, there may be a few unused variables as I used the code from the aforementioned thread and just altered it:
Rem Project: Elevator test
Rem Created: 01/09/2009
Rem ***** Main Source File *****
set display mode 1024,768,32
sync on : sync rate 0 : hide mouse
autocam off : set camera range 1,5000
makeLevel()
makeElevator()
makePlayer()
global rtimer as integer
global stimer as integer
global vtimer as integer
rem player movement vector
global vx# as double float
global vy# as double float
global vz# as double float
global x# as double float
global oldy# as double float
global z# as double float
global runon# as double float : runon# = 2.5 : ` used for run instead of walk
global radius# as double float : radius# = 24.5 : ` size of player collision sphere
global fpsviewy# as double float
global gravity# as double float : gravity# = -0.1
global slope# as double float : slope# = 0.5
global ground as integer : ground = 1
global jumptimer as integer : jumptimer = 0
global state as integer : state = 0 : ` elevator up or down flag
global trigger as integer : trigger = 0 : ` elevator trigger zone
do
movePlayer()
positionCameraToObject(2)
if keystate(45)=1 and trigger<>0 then moveElevator() :` x to use Elevator
sync
loop
function moveElevator()
if state = 0 : `elevator at ground floor
loop sound 1
for me = 1 to 2913
move object up 65,1: `move elevator up
SC_updateObject 65 :` update elevator position
movePlayer()
positionCameraToObject(2)
move OBJECT up 66,1: `move elevator trigger zone up
sync
next me
stop sound 1
state=1 : `elevator at top floor
else
loop sound 1
for me = 1 to 2913
move object down 65,1: `move elevator down
SC_updateObject 65 :` update elevator position
movePlayer()
positionCameraToObject(2)
move OBJECT down 66,1: `move elevator trigger zone down
sync
next me
stop sound 1
state = 0
endif
endfunction
function makeElevator()
rem Make Elevator
load sound "elevator/engine.wav",1
load object "elevator/elevator_car.dbo",65
position object 65,0,0,830
sc_setupObject 65,1,0 : ` sparkys collision stuff
rem Make Elevator trigger zone
make object box 66,120,20,90: `trigger zone
position object 66,0,30,830: `position in the Elevator
hide object 66 : ` hide the trigger zone
endfunction
function makeLevel()
load object "elevator/elevator.dbo",1 : `load level
sc_setupComplexObject 1,1,2 : ` sparkys collision stuff
position object 1,0,0,0 : `position level
endfunction
function makePlayer()
make object sphere 2,radius# : ` the actual player object
position object 2,0,5,0 : ` player start position
sc_setupObject 2,0,1 : ` sparkys collision stuff
hide object 2 :` hide the player object
endfunction
function movePlayer()
rem rotate player with mouse
yrotate object 2,object angle y(2)+mousemovex()/3.0
xrotate object 2,object angle x(2)+mousemovey()/3.0
if object angle x(2) >90 then xRotate Object 2,90
if object angle x(2) <-90 then xRotate Object 2,-90
oldx# = object position x(2)
oldy# = object position y(2)
oldz# = object position z(2)
rem apply gravity, and user changes to movement
angy# = object angle y(2)
vx# = 0
vz# = 0
rem if player is jumping or falling then apply 'normal' gravity
rem if not attempt to keep the player stuck to the floor
if vy#=0 and jumptimer=0 then vy# = vy# + 10*gravity# else vy# = vy# + gravity#
if keystate(42)=1 then runon# = 5 else runon#= 2.5 : ` hold leftshift to run
if keystate(46)=1 then radius# = 15.0 else radius#= 24.5 : ` hold c to crouch
if keystate(32)=1 then vx# = vx# + cos(angy#)*runon# : vz# = vz# - sin(angy#)*runon#
if keystate(30)=1 then vx# = vx# - cos(angy#)*runon# : vz# = vz# + sin(angy#)*runon#
if keystate(31)=1 then vx# = vx# - sin(angy#)*runon# : vz# = vz# - cos(angy#)*runon#
if keystate(17)=1 then vx# = vx# + sin(angy#)*runon# : vz# = vz# + cos(angy#)*runon#
rem only jump if on ground, and a certain time after last jump
if ground=1
if spacekey()=1 and jumptimer=0 then vy# = vy# + 4.0 : jumptimer = 20
endif
x# = oldx#+vx#
y# = oldy#+vy#
z# = oldz#+vz#
collidewall = sc_SphereCastGroup(1,oldx#,oldy#,oldz#,oldx#,oldy#+vy#,oldz#,radius#,0)
if collidewall>0
rem how flat is this ground
ny# = sc_getCollisionNormalY()
if abs(ny#)>slope#
rem FLAT, stick
oldy# = sc_getStaticCollisionY()
else
rem STEEP, slide
x# = x# - oldx# : z# = z# - oldz#
oldx# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
oldz# = sc_getCollisionSlideZ()
x# = x# + oldx# : z# = z# + oldz#
endif
rem ny#<0 means the player has hit a ceiling rather than a floor
if ny#>slope#
rem only on ground if standing on flat ground
ground = 1
vy# = 0
else
ground = 0
rem if player has hit a flat ceiling then stop vy# movement
if ny#<-slope# then vy# = gravity#
endif
else
rem nothing below player, not on ground, add vertical speed to player
oldy# = oldy# + vy#
ground = 0
endif
rem jumptimer will decrease only when player is back on ground
rem creates a pause between two successive jumps
if ground = 1 and jumptimer>0 then dec jumptimer
rem handle horizontal movement as sliding
rem player only collides with group 1 (level) objs and moves freely through others
collidewall = sc_SphereSlideGroup(1,oldx#,oldy#,oldz#,x#,oldy#,z#,radius#,0)
if collidewall>0
rem if hit, reposition player, halt movement vector
x# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
z# = sc_getCollisionSlideZ()
vx# = 0
vz# = 0
rem possible code for giving the player a jumping help up stairs...
rem might be useful if slope# is set very high but stairs are still required
`dy# = oldy#-sc_getStaticCollisionY()
`if dy#<slope# and dy#>0 and ground=1 then vy# = 0.5
endif
rem check if in Elevator zone
if object collision (66,2) =1 then trigger=1 else trigger=0
rem position the player
position object 2,x#,oldy#,z#
sc_updateObject 2
endfunction
function positionCameraToObject(obj)
rem This is from sparkys sliding collision demo
fpsviewy#=object position y(2)+radius#: ` camera on top of sphere
position camera object position x(2),fpsviewy#,object position z(2)
rotate camera object angle x(2),object angle y(2),object angle z(2)
rem Position Listener at camera for 3D Sound
Position Listener object position x(2),fpsviewy#,object position z(2)
Rotate Listener object angle x(2),object angle y(2),object angle z(2)
endfunction
if memblock exist(1) then delete memblock 1
The code and all media is included in this thread, download button bottom right.
It may not be the best way of doing things but it works.
Image here:
Nothing to see here, move along please.