Hello there. i'm trying to make a fps in dbpro, using scraps of code from many users here in the DBpro comunity. Everything worked like a charm until i've tried to make a sc_raycast collision to make a basic damage code for an enemy. It seems that everywhere i point the gun the enemy still takes dmg. Are there any alternatives for the damage code? Here's the code:
Rem Project: FPS Engine
Rem Created: 11/06/2010 20:01:49
Rem ***** Main Source File *****
set display mode 1280,1024,32
sync on : sync rate 70 : hide mouse
autocam off
makeLevel()
makePlayer()
makeEnemy()
makeAmmoBox()
makeWeapon()
``makebullet()
global EnemyHP# as integer : EnemyHP#=100
global newenemyhp# as integer : newenemyhp#=enemyhp#-10
global Ammo# as integer : Ammo#=1000
global MaxAmmo# as integer : MaxAmmo#=35
global Clips# as integer : Clips#=3
global MaxClips# as integer: MaxClips#=3
global Time# as double float : Time#=0
global time2# as double float : time2#=0
global time3# as double float : time3#=0
global Reload# as integer : Reload#=0
global fire# as integer :fire#=0
GLOBAL HP# AS INTEGER : HP#=100
GLOBAL AP# AS INTEGER : AP#=0
global enemyhit# as integer
global temp# as integer
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
do
movePlayer()
positionCameraToObject(2,0)
makeMinimap(x#,oldy#,z#)
pickAmmo()
pickWeapons()
loadHUD()
fireWeapon()
takeDmg()
sync
loop
function makeLevel()
make object sphere 10,-5000 : ` create skysphere a size in minus will make it inverted
load image "media/urban_sky.jpg",5 : ` sky texture
texture object 10,5 : ` apply sky texture to skysphere
load object "media/level2.x",1 : `load level
load image "media/texture.bmp",2
texture object 1,2
sc_setupComplexObject 1,1,2 : ` sparkys collision stuff
position object 1,100,0,0 : `position level
SC_updateObject 1
position object 10,0,0,0 : ` position skysphere remove if sky moves with player
make camera 2 : ` minicamera
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 makeEnemy()
make object box 4,50,50,50: ` the actual enemy object
position object 4,-150,25,50 : ` enemy start position
sc_setupObject 4,0,1 : ` sparkys collision stuff
`` hide object 2 :` hide the player object
endfunction
function makeAmmoBox()
MAKE OBJECT BOX 5,15,20,50
COLOR OBJECT 5,RGB(000,255,255)
POSITION OBJECT 5,-40,0,0
sc_setupObject 5,0,1
endfunction
function makeWeapon()
make object box 6,15,20,50
color object 6, rgb(150,150,150)
position object 6, -80,10,0
sc_setupObject 6,0,1
endfunction
remstart
function makebullet()
MAKE OBJECT box 7,1,1,1000
sc_setupObject 7,0,1
endfunction
function loadbullet()
MAKE MESH FROM OBJECT 2,3
ADD LIMB 3,1,2
DELETE MESH 2
GLUE OBJECT TO LIMB 7,3,1
``position object 7,object position x(2),fpsviewy#,object position z(2)
disable object zdepth 7
``hide object 7
endfunction
remend
function loadweapon()
MAKE MESH FROM OBJECT 1,2
ADD LIMB 2,1,1
DELETE MESH 1
MAKE OBJECT BOX 3,10,10,100
GLUE OBJECT TO LIMB 3,2,1
position object 3,25,0,50
disable object zdepth 3
endfunction
function loadCrosshair()
CIRCLE SCREEN WIDTH()/2,SCREEN HEIGHT()/2,10
DOT SCREEN WIDTH()/2,SCREEN HEIGHT()/2
endfunction
function fireWeapon()
IF MOUSECLICK()=1 AND KEYSTATE(19)=0
inc time3#, .01
IF Ammo#>0 and time3#>=0.06 THEN DEC Ammo#,1:time3#=0
ENDIF
IF MOUSECLICK()=0 AND KEYSTATE(19)=1 AND Ammo#<MaxAmmo# AND Clips#>0 THEN Reload#=1
IF RELOAD#=1 THEN INC Time#,.01
IF Time#>=1 THEN Ammo#=MaxAmmo#:DEC Clips#,1:Time#=0:Reload#=0
loadCrosshair()
endfunction
function takeDmg()
if mouseclick()=1 and ammo#>0
if SC_rayCast ( 4, object position x(2),object position y(2),object position z(2), object position x(4),object position y(4),object position z(4), 1 ) =1 then enemyhit#=1
else enemyhit#=0
endif
if enemyhit#=1 then inc time2#,.01
if time2#>=0.1 then enemyhp#=enemyhp#-5:enemyhit#=0:time2#=0
``if enemyhp#<=0 then move object up 4, 50
endfunction
function pickAmmo()
IF SC_objectCollision (2,5)=1
INC Clips#,(MaxClips#-Clips#)
INC Ammo#,(MaxAmmo#-Ammo#)
DELETE OBJECT 5
SC_setObjectCollisionOff 5
endif
endfunction
function pickWeapons()
if SC_objectCollision (2,6)=1
loadweapon()
``loadbullet()
delete object 6
SC_setObjectCollisionOff 6
endif
endfunction
function loadHUD()
SET TEXT FONT "VERDANA"
set text size 20
text 0,0,"FPS: "+str$(screen fps())
TEXT 0,SCREEN HEIGHT()-110,"AMMO: "+str$(Ammo#)
TEXT 0,SCREEN HEIGHT()-60,"CLIPS: "+str$(Clips#)
TEXT SCREEN WIDTH()-150,SCREEN HEIGHT()-60,"HP: "+STR$(HP#)
TEXT SCREEN WIDTH()-150,SCREEN HEIGHT()-110,"AP: "+STR$(AP#)
IF OBJECT EXIST(4)=1 THEN CENTER TEXT OBJECT SCREEN X(4),OBJECT SCREEN Y(4)-70,"Enemy Health: "+str$(EnemyHP#)
endfunction
function movePlayer()
remstart
This function is from the sliding demo that comes with
sparkys collision dll
I just added the run and crouch bits
remend
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 sprint
if keystate(29)=1 then runon# = 1 else runon#= 2.5 : `hold ctrl to walk
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# + 3.5 : jumptimer = 20
endif
rem this would be the player's final position without collision
x# = oldx#+vx#
y# = oldy#+vy#
z# = oldz#+vz#
rem first handle gravity - seperated from horizontal movement
rem to achieve a more realistic effect
rem Method: simple - cast a sphere vertically down, if it hits the level then
rem position the object there (sticky collision) then move
rem on to horizontal movement
rem more complex - if we were to only use the simple method the player would be
rem allowed to climb almost vertical slopes. Alternative is to
rem get the normalY direction to work out how flat the gorund
rem below the player is, 0-slope# is flatter, slope#-1 is steeper.
rem if it's flat, use sticky collision, if it's steep slide the
rem player down the slope. Changing slope# determines how steep the
rem player can climb. NOTE: this also effects stairs.
collide = sc_SphereCastGroup(1,oldx#,oldy#,oldz#,oldx#,oldy#+vy#,oldz#,radius#,0)
if collide>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
collide = sc_SphereSlideGroup(1,oldx#,oldy#,oldz#,x#,oldy#,z#,radius#,0)
if collide>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 position the player
position object 2,x#,oldy#,z#
rem position object 10,x#,oldy#,z# : ` position skysphere unrem if sky moves with player
sc_updateObject 2
`` sc_updateobject 7
sc_updateobject 4
SC_updateObject 1
endfunction
function positionCameraToObject(obj,thirdPerson)
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)
if thirdPerson=1
pitch camera down 10
move camera -30
endif
set camera range 1,50010 : ` gets rid of blue screen in distance
endfunction
Function makeMinimap(xstart as float, ystart as float, zstart as float)
set camera range 2, 1,5001 : ` gets rid of blue screen in distance
position camera 2,xstart,ystart+1000,zstart ; ` puts the camera 500 units above the player
remstart
rotate camera 2,object angle x(2),object angle y(2),object angle z(2)
the line above can be used instead of the 1 below to give the player view in the minimap
if you use this line instead then you must use
ystart+radius# in the position camera line above
remend
rotate camera 2,90,object angle y(2),object angle z(2)
set camera view 2,screen width() - 150, screen height() - (screen height() - 10), screen width() - 10, screen height() - (screen height() - 150)
set camera aspect 2,1
endfunction
if memblock exist(1) then delete memblock 1
The part of the code that i'm refering to is this
function takeDmg()
if mouseclick()=1 and ammo#>0
if SC_rayCast ( 4, object position x(2),object position y(2),object position z(2), object position x(4),object position y(4),object position z(4), 1 ) =1 then enemyhit#=1
else enemyhit#=0
endif
if enemyhit#=1 then inc time2#,.01
if time2#>=0.1 then enemyhp#=enemyhp#-5:enemyhit#=0:time2#=0
``if enemyhp#<=0 then move object up 4, 50
endfunction
If anyone can help me it would be greatly apreciated.