Would there be a way to make this gravity code smoother? The gravity is mostly in the functions CheckGravity() and LevelSlidingCollision(). You need the sparky collision dll for it, by the way. I'd appreciate any help. The media is attached.
sync on
sync rate 60
rem Define Types
type character
objectid
name$
age
speed
health
attack
endtype
type friendly
istaken
objectid
name$
age
speed
x#
y#
z#
health
opinion
doesplayerknow
endtype
type enemy
istaken
objectid
x#
y#
z#
name$
speed
health
endtype
type helditem
id
name$
attack
istaken
quantity
endtype
type item
heldid
name$
attack
endtype
rem Define Variables
global NO = 0
global YES = 1
global TRUE = YES
global FALSE = NO
global CHAR = 1
global MOVESPEED# = 0.1
global CHARX#
global CHARY#
global CHARZ#
global player as character
player.objectid = CHAR
global dim npc(5000) as friendly
global dim monster(5000) as enemy
global OLDCHARX#
global OLDCHARY#
global OLDCHARZ#
global JUMPSPEED# = 0.1
global JUMPABLE = TRUE
global jumping
global start_y#
global jump_angle#
global dim items(5000) as item
global dim inventory(29) as helditem
global STATUSTEXTX = 5
global STATUSTEXTY
STATUSTEXTY = screen height() - 20
global GRASSTEXTURE = 1
global STATUS$
global dim friendlygreeting$(6)
global dim mildfriendlygreeting$(6)
global dim somewhatfriendlygreeting$(6)
global dim unfamiliargreeting$(6)
global dim unfriendlygreeting$(6)
global dim dislikegreeting$(6)
global dim hategreeting$(6)
global flag = 0
global flag1 = 0
global LIMB = 1
global LIMBX#
global LIMBY#
global LIMBZ#
global UNDERLIMB = 2
global UNDERLIMBX#
global UNDERLIMBY#
global UNDERLIMBZ#
randomize timer()
rem Other Pre-Loop Setup
hide mouse
rem Load Assets
LoadAssets()
CreateMainCharacter()
UpdateCharacterPosition()
CreateNPC(0, 0, 5, "Arborum", 34, 1, 15, 60, 100, 0)
CreateItem(0, 0, "Bronze Spear", 3)
CreateMonster(10, 0, 0, "Training Dummy", 1, 15, 1000, 0)
GivePlayerItem(0, 1)
SetPlayerSpeed(1)
SetPlayerHealth(20)
SetPlayerAttack(1)
CreateLevel()
SetGreetings()
rem GAME LOOP
rem *************************************************************************************
rem *************************************************************************************
do
rem Update Early Variables
UpdateOldCharacterPosition()
MonsterDeathCheck()
rem Check for Input
GetKeyInput()
GetMouseInput()
rem Position the Camera
PositionCamera()
rem Update Late Variables
UpdateCharacterPosition()
rem Do Collision Checks
NPCSlidingCollision()
LevelSlidingCollision()
CheckGravity()
rem Display the HUD
DisplayHUD()
rem Sync
sync
loop
rem FUNCTIONS
rem *************************************************************************************
rem *************************************************************************************
function CreateMainCharacter()
make object sphere CHAR, 1
make object sphere 9999, 1
make mesh from object LIMB, 9999
delete object 9999
add limb CHAR, LIMB, LIMB
offset limb CHAR, LIMB, 0, 0, 3
hide limb CHAR, LIMB
add limb CHAR, UNDERLIMB, LIMB
offset limb CHAR, UNDERLIMB, 0, -0.5, 0
hide limb CHAR, UNDERLIMB
endfunction
function GetKeyInput()
if keystate(17) = 1
MoveCharacterForward(MOVESPEED# + (player.speed / 100))
endif
if keystate(30) = 1
MoveCharacterLeft(MOVESPEED# + (player.speed / 100))
endif
if keystate(31) = 1
MoveCharacterBack(MOVESPEED# + (player.speed / 100))
endif
if keystate(32) = 1
MoveCharacterRight(MOVESPEED# + (player.speed / 100))
endif
if jumping = 0
start_y# = CHARY# - 2
if spacekey()=1 and JUMPABLE = TRUE
jumping = 1
jump_angle# = 0
start_y# = object position y(CHAR)
endif
endif
if jumping = 1
jump_angle# = jump_angle# + 2
if jump_angle# => 180 then jumping=0
position object CHAR, CHARX#, start_y# + sin(jump_angle#)*4.0, CHARZ#
endif
greeting = rnd(6)
if keystate(29) = 1 and flag = 0
flag = 1
if IsPlayerNearNPC(0) = YES
if npc(0).opinion > 86
SetStatus(friendlygreeting$(greeting))
endif
if npc(0).opinion > 72 and npc(0).opinion <= 86
SetStatus(mildfriendlygreeting$(greeting))
endif
if npc(0).opinion > 58 and npc(0).opinion <= 72
SetStatus(somewhatfriendlygreeting$(greeting))
endif
if npc(0).opinion > 44 and npc(0).opinion <= 58
SetStatus(unfamiliargreeting$(greeting))
endif
if npc(0).opinion > 30 and npc(0).opinion <= 44
SetStatus(unfriendlygreeting$(greeting))
endif
if npc(0).opinion > 16 and npc(0).opinion <= 30
SetStatus(dislikegreeting$(greeting))
endif
if npc(0).opinion <= 16
SetStatus(hategreeting$(greeting))
endif
endif
endif
if keystate(29) = 0 then flag = 0
endfunction
function GetMouseInput()
if mouseclick() = 1 and flag1 = 0
for x = 0 to 5000
if monster(x).health <= 0
KillMonster(x)
exitfunction
endif
if monster(x).istaken = TRUE
if IsPlayerFacingMonster(x)=YES
dec monster(x).health, player.attack
flag1 = 1
endif
endif
next
endif
if mouseclick() = 0 then flag1 = 0
endfunction
function MoveCharacterForward(velocity#)
move object CHAR, velocity#
endfunction
function MoveCharacterLeft(velocity#)
move object left CHAR, velocity#
endfunction
function MoveCharacterRight(velocity#)
move object right CHAR, velocity#
endfunction
function MoveCharacterBack(velocity#)
move object CHAR, -velocity#
endfunction
function PositionCamera()
position camera CHARX#, CHARY#, CHARZ#
rotate camera camera angle x(0)+(mousemovey()*0.1),camera angle y(0)+(mousemovex()*0.1),0
rotate object CHAR, camera angle x(0),camera angle y(0),0
DrawCrosshair()
endfunction
function UpdateCharacterPosition()
CHARX# = object position x(CHAR)
CHARY# = object position y(CHAR)
CHARZ# = object position z(CHAR)
LIMBX# = limb position x(CHAR, LIMB)
LIMBY# = limb position y(CHAR, LIMB)
LIMBZ# = limb position z(CHAR, LIMB)
UNDERLIMBX# = limb position x(CHAR, UNDERLIMB)
UNDERLIMBY# = limb position y(CHAR, UNDERLIMB)
UNDERLIMBZ# = limb position z(CHAR, UNDERLIMB)
endfunction
function UpdateOldCharacterPosition()
OLDCHARX# = object position x(CHAR)
OLDCHARY# = object position y(CHAR)
OLDCHARZ# = object position z(CHAR)
endfunction
function DrawCrosshair()
circle screen width()/2, screen height()/2, 5
endfunction
function CreateNPC(x#, y#, z#, name$, age, speed, health, opinion, objectid, npcid)
if npc(npcid).istaken = FALSE
npc(npcid).istaken = TRUE
npc(npcid).x# = x#
npc(npcid).y# = y#
npc(npcid).z# = z#
npc(npcid).name$ = name$
npc(npcid).age = age
npc(npcid).speed = speed
npc(npcid).health = health
npc(npcid).opinion = opinion
npc(npcid).objectid = objectid
make object sphere objectid, 1
position object objectid, x#, y#, z#
sc_setupobject objectid, 0, 0
else
exitfunction
endif
endfunction
function CreateMonster(x#, y#, z#, name$, speed, health, objectid, monsterid)
if monster(monsterid).istaken = FALSE
monster(monsterid).istaken = TRUE
monster(monsterid).x# = x#
monster(monsterid).y# = y#
monster(monsterid).z# = z#
monster(monsterid).name$ = name$
monster(monsterid).speed = speed
monster(monsterid).health = health
monster(monsterid).objectid = objectid
make object sphere objectid, 1
position object objectid, x#, y#, z#
sc_setupobject objectid, 0, 0
color object objectid, rgb(255, 0, 0)
else
exitfunction
endif
endfunction
function MonsterDeathCheck()
for x = 0 to 5000
if monster(x).istaken = TRUE
if monster(x).health <= 0
if monster(x).objectid <> 0
KillMonster(x)
endif
endif
endif
next
endfunction
function KillMonster(monsterid)
if monster(monsterid).istaken = FALSE
exitfunction
endif
delete object monster(x).objectid
monster(monsterid).istaken = FALSE
monster(monsterid).objectid = 0
monster(monsterid).x# = 0
monster(monsterid).y# = 0
monster(monsterid).z# = 0
monster(monsterid).name$ = ""
monster(monsterid).speed = 0
monster(monsterid).health = 0
endfunction
function CreateItem(itemid, heldid, name$, attack)
items(itemid).heldid = heldid
items(itemid).name$ = name$
items(itemid).attack = attack
endfunction
function SetPlayerSpeed(speed)
player.speed = speed
endfunction
function SetPlayerHealth(health)
player.health = health
endfunction
function SetPlayerAttack(attack)
player.attack = attack
endfunction
function DisplayHUD()
text 5, 5, "FPS: " + str$(screen fps())
text 5, 20, "x: " + str$(CHARX#)
text 5, 35, "y: " + str$(CHARY#)
text 5, 50, "z: " + str$(CHARZ#)
text STATUSTEXTX, STATUSTEXTY, STATUS$
DisplayEnemyStatus()
endfunction
function DisplayEnemyStatus()
for x = 0 to 5000
if monster(x).istaken = TRUE
if IsPlayerNearMonster(x) = TRUE
center text object screen x(monster(x).objectid), object screen y(monster(x).objectid)-150, monster(x).name$ + ": " + str$(monster(x).health)
endif
endif
next
endfunction
function SlidingCollision(x1#, y1#, z1#, x2#, y2#, z2#, radius#, dyn, obj)
c = sc_sphereslide(obj, x1#, y1#, z1#, x2#, y2#, z2#, radius#, 0)
if c > 0
cx# = sc_getcollisionslidex()
cy# = sc_getcollisionslidey()
cz# = sc_getcollisionslidez()
position object dyn, cx#, cy#, cz#
endif
endfunction
function NPCSlidingCollision()
for x = 0 to 5000
if npc(x).istaken = TRUE
SlidingCollision(OLDCHARX#, OLDCHARY#, OLDCHARZ#, CHARX#, CHARY#, CHARZ#, 1.5, CHAR, npc(x).objectid)
endif
if monster(x).istaken = TRUE
SlidingCollision(OLDCHARX#, OLDCHARY#, OLDCHARZ#, CHARX#, CHARY#, CHARZ#, 1.5, CHAR, monster(x).objectid)
endif
next
endfunction
function GivePlayerItem(objectid, quantity)
for x = 0 to 29
if inventory(x).istaken = FALSE
inventory(x).istaken = TRUE
inventory(x).id = items(objectid).heldid
inventory(x).name$ = items(objectid).name$
inventory(x).attack = items(objectid).attack
inventory(x).quantity = quantity
endif
next
endfunction
function CreateLevel()
make object plain 50, 50, 50
xrotate object 50, 90
position object 50, 0, -0.5, 0
texture object 50, GRASSTEXTURE
sc_setupobject 50, 0, 0
fog on
fog distance 100
set ambient light 65
endfunction
function SetGreetings()
friendlygreeting$(0) = "Hey there."
friendlygreeting$(1) = "How's it going?"
friendlygreeting$(2) = "Hey, what's up?"
friendlygreeting$(3) = "What's up?"
friendlygreeting$(4) = "Hey."
friendlygreeting$(5) = "'Ey."
friendlygreeting$(6) = "Nice day, innit?"
mildfriendlygreeting$(0) = "Hello."
mildfriendlygreeting$(1) = "Hey."
mildfriendlygreeting$(2) = "How is it?"
mildfriendlygreeting$(3) = "What is it?"
mildfriendlygreeting$(4) = "Hi."
mildfriendlygreeting$(5) = "Good day."
mildfriendlygreeting$(6) = "'Ello."
somewhatfriendlygreeting$(0) = "Hello."
somewhatfriendlygreeting$(1) = "What is it?"
somewhatfriendlygreeting$(2) = "Do you need something?"
somewhatfriendlygreeting$(3) = "Can I help you?"
somewhatfriendlygreeting$(4) = "Now's not the best time, but I could listen."
somewhatfriendlygreeting$(5) = "Good day."
somewhatfriendlygreeting$(6) = "Good day to you."
unfamiliargreeting$(0) = "Greetings."
unfamiliargreeting$(1) = "What do you need?"
unfamiliargreeting$(2) = "Need anything?"
unfamiliargreeting$(3) = "Why hello there."
unfamiliargreeting$(4) = "Good day to you."
unfamiliargreeting$(5) = "You need something?"
unfamiliargreeting$(6) = "..."
unfriendlygreeting$(0) = "What do you want?"
unfriendlygreeting$(1) = "I got better things to do right now."
unfriendlygreeting$(2) = "What is it this time?"
unfriendlygreeting$(3) = "I got places to be."
unfriendlygreeting$(4) = "Hmm?"
unfriendlygreeting$(5) = "Let's get this over with, shall we?"
unfriendlygreeting$(6) = "Go on..."
dislikegreeting$(0) = "What do you want now?"
dislikegreeting$(1) = "I got places to be, make it quick this time."
dislikegreeting$(2) = "Get out of my sight."
dislikegreeting$(3) = "Why are you still here? Did I miss something?"
dislikegreeting$(4) = "Get on with it."
dislikegreeting$(5) = "You disgust me."
dislikegreeting$(6) = "I pity you."
hategreeting$(0) = "I would kill you if we weren't in public."
hategreeting$(1) = "You disgust me."
hategreeting$(2) = "Get out of my life."
hategreeting$(3) = "I'm tired of you, you know that?"
hategreeting$(4) = "I pity you."
hategreeting$(5) = "Hi, idiot."
hategreeting$(6) = "I have better things to do than listen to you."
endfunction
function IsPlayerNearNPC(npcid)
if XDistanceToObject(CHAR, npc(npcid).objectid) < 3 and XDistanceToObject(CHAR, npc(npcid).objectid) > -3
if YDistanceToObject(CHAR, npc(npcid).objectid) < 3 and YDistanceToObject(CHAR, npc(npcid).objectid) > -3
if ZDistanceToObject(CHAR, npc(npcid).objectid) < 3 and ZDistanceToObject(CHAR, npc(npcid).objectid) > -3
iflag = YES
else
iflag = NO
endif
else
iflag = NO
endif
else
iflag = NO
endif
endfunction iflag
function IsPlayerFacingNPC(npcid)
if intersect object (npc(npcid).objectid, LIMBX#, LIMBY#, LIMBZ#, CHARX#, CHARY#, CHARZ#)>0
iflag = YES
else
iflag = NO
endif
endfunction iflag
function IsPlayerNearMonster(monsterid)
if XDistanceToObject(CHAR, monster(monsterid).objectid) < 3 and XDistanceToObject(CHAR, monster(monsterid).objectid) > -3
if YDistanceToObject(CHAR, monster(monsterid).objectid) < 3 and YDistanceToObject(CHAR, monster(monsterid).objectid) > -3
if ZDistanceToObject(CHAR, monster(monsterid).objectid) < 3 and ZDistanceToObject(CHAR, monster(monsterid).objectid) > -3
iflag = YES
else
iflag = NO
endif
else
iflag = NO
endif
else
iflag = NO
endif
endfunction iflag
function IsPlayerFacingMonster(monsterid)
if intersect object (monster(monsterid).objectid, LIMBX#, LIMBY#, LIMBZ#, CHARX#, CHARY#, CHARZ#)>0
iflag = YES
else
iflag = NO
endif
endfunction iflag
function XDistanceToObject(object1, object2)
if object1 = 0 or object2 = 0 then exitfunction
if object exist (object1) = 0 or object exist (object2) = 0 then exitfunction
distance# = object position x(object1) - object position x(object2)
endfunction distance#
function YDistanceToObject(object1, object2)
if object1 = 0 or object2 = 0 then exitfunction
if object exist (object1) = 0 or object exist (object2) = 0 then exitfunction
distance# = object position y(object1) - object position y(object2)
endfunction distance#
function ZDistanceToObject(object1, object2)
if object1 = 0 or object2 = 0 then exitfunction
if object exist (object1) = 0 or object exist (object2) = 0 then exitfunction
distance# = object position z(object1) - object position z(object2)
endfunction distance#
function CheckGravity()
if intersect object(50, UNDERLIMBX#, UNDERLIMBY#, UNDERLIMBZ#, CHARX#, CHARY#, CHARZ#)=0
position object CHAR, CHARX#, CHARY#-0.1, CHARZ#
endif
endfunction
function LevelSlidingCollision()
SlidingCollision(OLDCHARX#, OLDCHARY#, OLDCHARZ#, CHARX#, CHARY#, CHARZ#, 0.5, CHAR, 50)
endfunction
function SetStatus(newstatus$)
STATUS$ = newstatus$
endfunction
function LoadAssets()
LoadImages()
LoadModels()
endfunction
function LoadImages()
load image "Assets/Textures/grass.bmp", GRASSTEXTURE
endfunction
function LoadModels()
endfunction
delete memblock 1