Here is a pistol model, put the model and texture into Media\Models\Guns\ folder
and following is my way to change guns and holster them as well keeping bullet counts for every gun the player has.
Change the
Type Entities in the main source file as following:
use key "1" for gun
use key "3" for ak47
use key "q" for holster
use key "r" for reload
use key "e" for sniper modes for each gun
use keys "wasd" for movement
use space key to jump
use ctrl key to crouch
mouse fires
use key "F12" for newton debug
if you have the speech dll, then use "F11" to talk (works on xp and speech installed others)
use key "F10" for glow effect
Type Entities
Name As String
Num As Integer
Img As Integer
Direct As Coordinates
Pos As Coordinates
Ang As Coordinates
Size As Coordinates
Mass As Float
Speed As Float
ShootPower As Float
JumpSpeed As Float
Play_Speed As Float
Draw_S As Float
Draw_E As Float
Idle_S As Float
Idle_E As Float
Reload_S As Float
Reload_E As Float
Fire_S As Float
Fire_E As Float
Bullets As Integer
TotalBullets As Integer
EndType
Change the
Game_Player: in the Game_subs.dba as follows:
Game_Player:
`--------------------------------------
`-------------GAME CARS----------------
`--------------------------------------
set dir GunsModelPath$
obj = _find_free_object()
dim Gun(10) As Entities
`------------------------
`----------AK47----------
`------------------------
obj = _find_free_object()
load object "ak47.md2", obj
img = _find_free_image()
load image "ak47_Base.bmp", img
Gun(1).Num = obj
Gun(1).Img = img
Gun(1).Speed = 2
Gun(1).Play_Speed = 40
Gun(1).Draw_S = 1
Gun(1).Draw_E = 30
Gun(1).Idle_S = 31
Gun(1).Idle_E = 47
Gun(1).Reload_S = 48
Gun(1).Reload_E = 138
Gun(1).Fire_S = 139
Gun(1).Fire_E = 155
Gun(1).Bullets = 30
Gun(1).TotalBullets = 30
Gun(1).Size.x = object size x(Gun(1).Num)
Gun(1).Size.y = object size y(Gun(1).Num)
Gun(1).Size.z = object size z(Gun(1).Num)
texture object Gun(1).Num, Gun(1).Img
set object smoothing Gun(1).Num, 50
set object ambience Gun(1).Num, rgb(5,5,5)
set object ambient Gun(1).Num, 1
yrotate object Gun(1).Num,180
scale object Gun(1).Num, 50, 50, 50
fix object pivot Gun(1).Num
position object Gun(1).Num , -1 , 1 , 2
`lock object on Gun(1).Num
disable object zdepth Gun(1).Num
#CONSTANT G_Draw = 1
#CONSTANT G_Holster = 2
#CONSTANT G_Idle = 3
#CONSTANT G_Reload = 4
#CONSTANT G_Fire = 5
FRAME As Float = 1.0
FRAMEINC As Float = 20.0
ANIMATE As Integer = 1
HOLSTER As Integer = 0
WEAPON As Integer = 1
`------------------------
`----------GUN-----------
`------------------------
obj = _find_free_object()
load object "gun1.md2", obj
img = _find_free_image()
load image "gun1_Base.bmp", img
Gun(2).Num = obj
Gun(2).Img = img
Gun(2).Speed = 2
Gun(2).Play_Speed = 40
Gun(2).Draw_S = 1
Gun(2).Draw_E = 30
Gun(2).Idle_S = 31
Gun(2).Idle_E = 60
Gun(2).Reload_S = 61
Gun(2).Reload_E = 157
Gun(2).Fire_S = 158
Gun(2).Fire_E = 183
Gun(2).Bullets = 12
Gun(2).TotalBullets = 12
Gun(2).Size.x = object size x(Gun(2).Num)
Gun(2).Size.y = object size y(Gun(2).Num)
Gun(2).Size.z = object size z(Gun(2).Num)
texture object Gun(2).Num, Gun(2).Img
set object smoothing Gun(2).Num, 50
set object ambience Gun(2).Num, rgb(5,5,5)
set object ambient Gun(2).Num, 1
yrotate object Gun(2).Num,180
scale object Gun(2).Num, 50, 50, 50
fix object pivot Gun(2).Num
position object Gun(2).Num , -1 , 1 , 2
`lock object on Gun(2).Num
disable object zdepth Gun(2).Num
`------------------------
`---------PLAYER---------
`------------------------
position camera CamSX# , CamSY# , CamSZ#
P_SizeX# = 4.0
P_SizeY# = 8.0
P_SizeZ# = 4.0
P_PosX# = camera position x(0)
P_PosY# = camera position y(0)
P_PosZ# = camera position z(0)
P_AngleX# = camera angle x(0)
P_AngleY# = camera angle y(0)
P_AngleZ# = camera angle z(0)
MakePlayer(P_PosX#,P_PosY#,P_PosZ#,P_SizeX#/2,P_SizeY#/2,P_SizeZ#/2,P_AngleX#,P_AngleY#,P_AngleZ#,25)
G_Player_Obj = obj : N_Player_Obj = Player
Dim Player(10) As Entities
Player(10).Mass = 25.0
Player(10).Speed = 18.0
Player(10).ShootPower = 50000.0
Player(10).JumpSpeed = 75.0
set dir RootPath$
Return
Change the other
Game_Player Motions with the following in the appropriate subs:
`--------------------------------------
`---------GAME ACTIVE MOTION-----------
`--------------------------------------
Game_Player_Gun_Position:
if WEAPON = 1
hide object Gun(2).Num
show object Gun(1).Num
move camera 0,3.5
position object Gun(WEAPON).Num, camera position x(0), camera position y(0), camera position z(0)
rotate object Gun(WEAPON).Num, camera angle x(0), camera angle y(0), camera angle z(0)
move camera 0,-3.5
else
if WEAPON = 2
hide object Gun(1).Num
show object Gun(2).Num
move camera 0,5.5
position object Gun(WEAPON).Num, camera position x(0), camera position y(0), camera position z(0)
rotate object Gun(WEAPON).Num, camera angle x(0), camera angle y(0), camera angle z(0)
move camera 0,-5.5
endif
endif
show sprite spr_CrossHair
Return
`--------------------------------------
`---------GAME ACTIVE MOTION-----------
`--------------------------------------
Game_Player_Motion:
REM FIND CEILING DISTANCE
NDB_BodyGetPosition Player
px# = NDB_GetVector_X() : py# = NDB_GetVector_Y() : pz# = NDB_GetVector_Z()+P_SizeZ#/2
NDB_SetVector 1, px#, py#, pz#
NDB_SetVector 2, px#, py# + P_SizeY#*2 + 1.0, pz#
DistUp# = NDB_NewtonWorldRayCast()
REM FIND GROUND DISTANCE
NDB_BodyGetPosition Player
px# = NDB_GetVector_X() : py# = NDB_GetVector_Y() : pz# = NDB_GetVector_Z()
NDB_SetVector 1, px#, py#, pz#
NDB_SetVector 2, px#, py# - P_SizeY# + 0.5, pz#
DistDown# = NDB_NewtonWorldRayCast()
REM ROTATION
if keystate(18) = 1
FOV = FOV - 1
if WEAPON = 1
if FOV =< 10 then FOV = 10
mousespeed = 5
else
if WEAPON = 2
if FOV =< 40 then FOV = 40
mousespeed = 3
endif
endif
PaX# = wrapvalue((mousemovey()/10.0/MouseSpeed) + PaX#)
PaY# = wrapvalue((mousemovex()/10.0/MouseSpeed) + PaY#)
else
if keystate(18) = 0
FOV = FOV + 1
if FOV => 62 then FOV = 62
mousespeed = 5
PaX# = wrapvalue((mousemovey()/MouseSpeed) + PaX#)
PaY# = wrapvalue((mousemovex()/MouseSpeed) + PaY#)
endif
endif
set camera fov 0,FOV
rem stops mouse from going upside down
if PaX# > 80 and PaX# < 180 then PaX# = 80
if PaX# > 180 and PaX# < 310 then PaX# = 310
rotate camera PaX#, PaY#, 0.0
REM GET POSITIONS
NDB_BodyGetPosition Player
position camera NDB_GetVector_X(), NDB_GetVector_Y()+2, NDB_GetVector_Z()
Player(10).Pos.x = camera position x (0)
Player(10).Pos.y = camera position y (0)
Player(10).Pos.z = camera position z (0)
REM RESET MOTION
MoveX# = 0.0
MoveZ# = 0.0
SOUNDD = 0
REM W_A_S_D
if keystate(17) or keystate(31)
dx# = sin(PaY#)
dz# = cos(PaY#)
inc MoveX#, dx# * (keystate(17)-keystate(31)) :`makes a positive vector with "w", negative with "s"
inc MoveZ#, dz# * (keystate(17)-keystate(31))
endif
if keystate(30) or keystate(32)
dx# = sin(PaY#+90.0)
dz# = cos(PaY#+90.0)
inc MoveX#, dx# * (keystate(32)-keystate(30)) :`makes a positive vector with "d", negative with "a"
inc MoveZ#, dz# * (keystate(32)-keystate(30))
endif
REM NORMALIZE MOTION VECTOR
length# = sqrt( (MoveX#^2)+(MoveZ#^2) )
MoveX# = MoveX# / length#
MoveZ# = MoveZ# / length#
REM SUM FORCES
NDB_NewtonBodyGetVelocity Player
CurrentVel_X# = NDB_GetVector_X()
CurrentVel_Y# = NDB_GetVector_Y()
CurrentVel_Z# = NDB_GetVector_Z()
GoalVel_X# = MoveX# * (Player(10).Speed * ((shiftkey()+1)*2) )
GoalVel_Z# = MoveZ# * (Player(10).Speed * ((shiftkey()+1)*2) )
AccelX# = 0.3 * ((GoalVel_X# - CurrentVel_X#) / N_Time#)
AccelZ# = 0.3 * ((GoalVel_Z# - CurrentVel_Z#) / N_Time#)
if AccelX# > 200.0 then AccelX# = 200.0
if AccelX# < -200.0 then AccelX# = -200.0
if AccelZ# > 200.0 then AccelZ# = 200.0
if AccelZ# < -200.0 then AccelZ# = -200.0
REM APPLY TOTAL FORCES
`set temp vector 1 to force location
NDB_BodyGetPosition Player
`set temp vector 2 to force direction
NDB_SetVector 2, AccelX#, 0.0, AccelZ#
NDB_BodyAddForceGlobal Player
REM JUMPING
AccelY# = 0.0
if spacekey() and SPACEPRESSED = 0
SPACEPRESSED = 1
if DistDown# < 1.0
AccelY# = ((Player(10).JumpSpeed - CurrentVel_Y#) / N_Time#)
NDB_BodyGetPosition Player
NDB_SetVector 2, 0.0, AccelY#, 0.0
NDB_BodyAddForceGlobal Player
endif
endif
if spacekey() = 0 then SPACEPRESSED = 0
Return
`--------------------------------------
`---------GAME ACTIVE MOTION-----------
`--------------------------------------
Game_Player_Shooting:
if mouseclick() = 1 and MOUSE = 0 and HOLSTER = 1 and SHOOT = 1 and Gun(WEAPON).Bullets > 0
MOUSE = 1 : Gun(WEAPON).Bullets = Gun(WEAPON).Bullets - 1
play sound snd_AK47_Fire
FRAME = Gun(WEAPON).Fire_S
ANIMATE = 0
G_Motion = G_Fire
x1# = camera position x() : y1# = camera position y() : z1# = camera position z()
move camera 1.0
x2# = camera position x() : y2# = camera position y() : z2# = camera position z()
move camera -1.0
`we will cast a ray 500 units long.
dx# = x2# - x1#
dy# = y2# - y1#
dz# = z2# - z1#
x2# = x1# + (dx# * 500.0)
y2# = y1# + (dy# * 500.0)
z2# = z1# + (dz# * 500.0)
`cast the ray. put start point in vector 1, end point in vector 2
NDB_SetVector 1, x1#, y1#, z1#
NDB_SetVector 2, x2#, y2#, z2#
dist# = NDB_NewtonWorldrayCast()
if dist# < 1.0
`something hit!
HitBody = NDB_RayCastGetBody()
castdist# = dist# * 500.0
cast_x# = x1# + (dx# * castdist#)
cast_y# = y1# + (dy# * castdist#)
cast_z# = z1# + (dz# * castdist#)
hitmass# = NDB_NewtonBodyGetMassMatrix( HitBody )
if hitmass# > 0.0
`this is a live object, give it a kick!
NDB_SetVector 1, cast_x#, cast_y#, cast_z#
NDB_SetVector 2, dx# * Player(10).ShootPower, dy# * Player(10).ShootPower, dz# * Player(10).ShootPower, 0.0
NDB_BodyAddForceGlobal HitBody
NDB_NewtonWorldUnfreezeBody HitBody
else
`this is the background (mass=0), add a bullethole!
obj = _find_free_object()
make object plain obj, 0.5, 0.5
position object obj, cast_x#-(dx#*0.01), cast_y#-(dy#*0.01), cast_z#-(dz#*0.01)
NDB_RayCastGetNormal
nx# = NDB_GetVector_X() : ny# = NDB_GetVector_Y() : nz# = NDB_GetVector_Z()
x# = cast_x# + nx# : y# = cast_y# + ny# : z# = cast_z# + nz#
point object obj, x#, y#, z#
texture object obj, img_BulletHole
set object transparency obj, 1
endif
endif
endif
if mouseclick() = 0 then MOUSE = 0
return
`--------------------------------------
`---------GAME ACTIVE MOTION-----------
`--------------------------------------
Game_Player_Gun_Motion:
SELECT G_Motion
CASE G_Draw
if ANIMATE = 0
FRAME = FRAME + FRAMEINC * N_Time#
if FRAME => Gun(WEAPON).Draw_E
ANIMATE = 1
SHOOT = 1
FRAME = Gun(WEAPON).Idle_S
endif
REM CLIP PULL SOUND
if FRAME > 15 and FRAME < 20
if sound playing(snd_AK47_Pull) = 0
play sound snd_AK47_Pull
endif
endif
endif
ENDCASE
CASE G_Holster
if ANIMATE = 0
FRAME = FRAME - FRAMEINC * N_Time#
if FRAME =< Gun(WEAPON).Draw_S
ANIMATE = 1
SHOOT = 1
FRAME = Gun(WEAPON).Draw_S
endif
REM CLIP PULL SOUND
if FRAME < 20 and FRAME > 15
if sound playing(snd_AK47_Pull) = 0
play sound snd_AK47_Pull
endif
endif
endif
ENDCASE
CASE G_Reload
if ANIMATE = 0
FRAME = FRAME + FRAMEINC * N_Time#
if FRAME => Gun(WEAPON).Reload_E
ANIMATE = 1
SHOOT = 1
FRAME = Gun(WEAPON).Idle_S
endif
REM CLIP IN OUT SOUNDS
if FRAME > 70 and FRAME < 75
if sound playing(snd_AK47_Out) = 0
play sound snd_AK47_Out
endif
endif
if FRAME > 100 and FRAME < 105
if sound playing(snd_AK47_In) = 0
play sound snd_AK47_In
Gun(WEAPON).Bullets = Gun(WEAPON).TotalBullets
endif
endif
endif
ENDCASE
CASE G_Fire
if ANIMATE = 0
FRAME = FRAME + FRAMEINC * N_Time#
if FRAME => Gun(WEAPON).Fire_E
ANIMATE = 1
FRAME = Gun(WEAPON).Idle_S
endif
endif
ENDCASE
ENDSELECT
set object frame Gun(WEAPON).Num , FRAME
REM DRAW AK47
if scancode() = 4 and ANIMATE = 1 and HOLSTER = 0
WEAPON = 1
FRAME = Gun(WEAPON).Draw_S
HOLSTER = 1
SHOOT = 0
ANIMATE = 0
G_Motion = G_Draw
endif
REM DRAW GUN1
if scancode() = 2 and ANIMATE = 1 and HOLSTER = 0
WEAPON = 2
FRAME = Gun(WEAPON).Draw_S
HOLSTER = 1
SHOOT = 0
ANIMATE = 0
G_Motion = G_Draw
endif
REM HOLSTER GUN
if scancode() = 16 and ANIMATE = 1 and HOLSTER = 1
WEAPON = WEAPON
FRAME = Gun(WEAPON).Draw_E
HOLSTER = 0
SHOOT = 0
ANIMATE = 0
G_Motion = G_Holster
endif
REM RELOAD GUN
if scancode() = 19 and ANIMATE = 1 and HOLSTER = 1 and BULLETS < 30
WEAPON = WEAPON
FRAME = Gun(WEAPON).Reload_S
SHOOT = 0
ANIMATE = 0
G_Motion = G_Reload
endif
if controlkey() > 0 and CROUCH = 0
CROUCH = 1
NDB_NewtonDestroyBody (Player)
P_PosX# = Player(10).Pos.x
P_PosY# = Player(10).Pos.y
P_PosZ# = Player(10).Pos.z
MakePlayer(P_PosX#,P_PosY#,P_PosZ#,P_SizeX#/2,P_SizeY#/3,P_SizeZ#/2,P_AngleX#,P_AngleY#,P_AngleZ#,25)
N_Player_Obj = Player
endif
if controlkey() < 1 and CROUCH > 0 and DistUp# > 0.5
CROUCH = 0
NDB_NewtonDestroyBody (Player)
P_PosX# = Player(10).Pos.x
P_PosY# = Player(10).Pos.y
P_PosZ# = Player(10).Pos.z
MakePlayer(P_PosX#,P_PosY#,P_PosZ#,P_SizeX#/2,P_SizeY#/2,P_SizeZ#/2,P_AngleX#,P_AngleY#,P_AngleZ#,25)
N_Player_Obj = Player
endif
return
Finally change the Game_Info sub as follows to see some info offcourse:
`--------------------------------------
`------------GAME INFORMATION----------
`--------------------------------------
Game_Info_Write:
`------------------
Set Cursor 0,0
ink rgb(0,250,0), 0
`------------------
print "ScanCode: ", scancode()
print ""
print "Strt Time: ", text_hour, ":", text_minute, ":", text_second, ":", text_milisecond
print "End Time: ", end_hour, ":", end_minute, ":", end_second, ":", end_milisecond
print "Newton_Time#: " + str$(N_Time#)
print ""
print "Game FPS: "+ str$(screen fps())
print "Polys: " + str$(STATISTIC(1))
print ""
print "FRAME: ", str$(FRAME)
print ""
print "BULLETS: ", str$(Gun(WEAPON).Bullets)
print ""
`------------------
obj = _find_free_object()
print "Total Objects: ", obj-1
`------------------
img = _find_free_image()
print "Total Images: ", img-1
`------------------
spr = _find_free_sprite()
print "Total Sprite: ", spr-1
`------------------
light = _find_free_light()
print "Total Lights: ", light-1
`------------------
text 0,300,"HitBody:"+str$(HitBody)
text 0,310,"HitMass:"+str$(hitmass#)
text 0,320,"Ragdoll:"+str$(RagDoll)
text 0,330,"DistUp: "+str$(DistUp#)
text 0,340,"DistDown: "+str$(DistDown#)
text 0,350,"WEAPON: "+str$(WEAPON)
Return